柏林噪声——Mathf.PerlinNoise()

作者:追风剑情 发布于:2016-3-24 14:16 分类:Unity3d

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

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

 

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号