鸟语天空
柏林噪声——Mathf.PerlinNoise()
post by:追风剑情 2016-3-24 14:16

柏林噪声用来模拟火焰、云彩、生成地形高度图、奇形怪状的岩石、树木和大理石表面等。

using UnityEngine;
using System.Collections;

public class PerlinNoiseTest : MonoBehaviour {

    public int pixWidth;
    public int pixHeight;
    public float xOrg;
    public float yOrg;
    public float scale = 1.0F;
    private Texture2D noiseTex;
    private Color[] pix;
    void Start()
    {
        noiseTex = new Texture2D(pixWidth, pixHeight);
        pix = new Color[noiseTex.width * noiseTex.height];
        renderer.material.mainTexture = noiseTex;
    }
    void CalcNoise()
    {
        float y = 0.0F;
        while (y < noiseTex.height)
        {
            float x = 0.0F;
            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[(int)(y * noiseTex.width + x)] = new Color(sample, sample, sample);
                x++;
            }
            y++;
        }
        noiseTex.SetPixels(pix);
        noiseTex.Apply();
    }

    void Update()
    {
        CalcNoise();
    }
}

 

22222.png

333333.png

 

运行效果

11111.png

 

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容