柏林噪声——Mathf.PerlinNoise()

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

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

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PerlinNoiseTest : MonoBehaviour {
  5.  
  6. public int pixWidth;
  7. public int pixHeight;
  8. public float xOrg;
  9. public float yOrg;
  10. public float scale = 1.0F;
  11. private Texture2D noiseTex;
  12. private Color[] pix;
  13. void Start()
  14. {
  15. noiseTex = new Texture2D(pixWidth, pixHeight);
  16. pix = new Color[noiseTex.width * noiseTex.height];
  17. renderer.material.mainTexture = noiseTex;
  18. }
  19. void CalcNoise()
  20. {
  21. float y = 0.0F;
  22. while (y < noiseTex.height)
  23. {
  24. float x = 0.0F;
  25. while (x < noiseTex.width)
  26. {
  27. float xCoord = xOrg + x / noiseTex.width * scale;
  28. float yCoord = yOrg + y / noiseTex.height * scale;
  29. float sample = Mathf.PerlinNoise(xCoord, yCoord);
  30. pix[(int)(y * noiseTex.width + x)] = new Color(sample, sample, sample);
  31. x++;
  32. }
  33. y++;
  34. }
  35. noiseTex.SetPixels(pix);
  36. noiseTex.Apply();
  37. }
  38.  
  39. void Update()
  40. {
  41. CalcNoise();
  42. }
  43. }

 

22222.png

333333.png

 

运行效果

11111.png

 

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号