参照Brackeys视频 https://www.youtube.com/watch?v=bG0uEXV6aHQ
Perlin Noise并不是全部随机,而是相互之间有些关系,allow change to occur gradually,给更多的organic feel有机感而不是computational的感觉。
步骤
创建一个Quad
创建一个Script
using UnityEngine;
public class PerlinNoise: MonoBehaviour
{
public int width=256;
public int height = 256;
public int scale =20;
public float offsetX = 100f;
public float offsetY = 100f;
void Start(){
offsetX = Random.Range(0f,99999f);
offsetY = Random.Range(0f,99999f);
}
void Update(){
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = GenerateTexture();
}
Texture2D GenerateTexture()
{
Texture2D texture = new Texture2D(width, height);
//Generate a perlin noise map for the texture
for(int x = 0; x<width; x++ )
{
for (int y = 0; y< height; y++)
{
Color color = CalculateColor(x,y);
texture.SetPixel(x,y,color);
}
}
texture.Apply();
return texture;
}
Color CalculateColor(int x, int y)
{
float xCoord = (float)x / width* scale+offsetX ;
float yCoord = (float)y/ height* scale+offsetY;
float sample = Mathf.PerlinNoise(x,y);
return new Color(sample,sample,sample);
}
}
要替换一下原先的standard shader使用unlit
- 创建一个scale * perlinnoise的数;可以放大;
- 创建两个x y 的offset去拖动,之后要随机的话就可以修改这个;
- 创建一个Start()去修改
Tips
Perlin Noise 每一个整数循环,所以不可以传入整数;
网友评论