using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public int pixWidth;
public int pixHeight;
public float xOrg;
public float yOrg;
public float scale = 1.0f;
private Texture2D noiseTex;
private Color[] pix;
private Renderer rend;
public float heightScale = 1.0F;
public float xScale = 1.0F;
void Start ()
{
rend = GetComponent<Renderer>();
noiseTex = new Texture2D(pixWidth, pixHeight);
pix = new Color[noiseTex.width * noiseTex.height];
rend.material.mainTexture = noiseTex;
}
void CalcNoise()
{
int y = 0;
while (y < noiseTex.height)
{
int x = 0;
while( x < noiseTex.width )
{
float xCoord = xOrg + x / noiseTex.width * scale;
float yCoord = yOrg + y / noiseTex.height * scale;
float sample = Mathf.PerlinNoise(xCoord, yCoord);
pix[y * noiseTex.width + x] = new Color(sample, sample, sample);
x++;
}
y++;
}
noiseTex.SetPixels(pix);
noiseTex.Apply();
}
void Update ()
{
CalcNoise();
float height = heightScale * Mathf.PerlinNoise(Time.time * xScale, 0.0F);
Vector3 pos = transform.position;
pos.y = height;
transform.position = pos;
}
}
第一个例子使用噪声重新生成贴图
第二个例子使用噪声产生y轴上的随机运动