Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created December 30, 2020 04:59
Show Gist options
  • Save todorok1/2383289da83c8d6fe15512424a8dea3e to your computer and use it in GitHub Desktop.
Save todorok1/2383289da83c8d6fe15512424a8dea3e to your computer and use it in GitHub Desktop.
パーリンノイズを使って2次元のテクスチャを生成するサンプルです。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <Summary>
/// パーリンノイズのテクスチャを生成するクラスです。
/// </Summary>
public class GeneratePerlinTexture2D : MonoBehaviour
{
// パーリンノイズのテクスチャを表示するイメージへの参照です。
public RawImage perlinImage;
// 生成したテクスチャです。
Texture2D texture;
public float xOrigin;
public float yOrigin;
public float scale = 0.01f;
void Start()
{
DrawPerli2D();
}
void DrawPerli2D()
{
int width = (int)perlinImage.rectTransform.sizeDelta.x;
int height = (int)perlinImage.rectTransform.sizeDelta.y;
texture = new Texture2D(width, height);
Color[] colorArray = new Color[width * height];
for (float y = 0; y < height; y++)
{
for (float x = 0; x < width; x++)
{
float xValue = xOrigin + x * scale;
float yValue = yOrigin + y * scale;
float perlinValue = Mathf.PerlinNoise(xValue, yValue);
colorArray[(int)x + (int)y * width] = new Color(perlinValue, perlinValue, perlinValue);
}
}
texture.SetPixels(colorArray);
texture.Apply();
perlinImage.texture = texture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment