Unity中 噪音算法


           主要是利用Unity Mathf.PerlinNoise   函数(柏林噪声)的不同寻常的功能。

https://docs.unity3d.com/ScriptReference/Mathf.PerlinNoise.html   其中一个实例代码是生成  柏林噪声图片。     第二个实例是动画效果(看似y的随机运动,对随机的动画来讲很好啊!)。

这样设置 得到:


 

1Mathf.PerlinNoise  函数的返回值是 0~1

2、一样的输入,输出也必将是一样的:  比如下面的测试,输出一直是 0.4652731

    void Update()

    {

        float height = heightScale *Mathf.PerlinNoise(5f/*Time.time * xScale*/, 0.0F);

3、他的代码思路就是  100 *100个方块,然后根据 x, z 设置y坐标值和颜色(y有关)

float xSample = (cube.transform.localPosition.x + _seedX) / _relief;

float zSample = (cube.transform.localPosition.z + _seedZ) / _relief;

float noise = Mathf.PerlinNoise(xSample, zSample);

y = _maxHeight * noise;

为了增加随机性, x,z 的系数做随机:

_seedX = Random.value * 100f;

_seedZ = Random.value * 100f;

 

比如使用噪声生成 类似我的世界的地图等

代码相对简单,直接在Awake 函数中执行生成了 地图!


改变   尺寸 (宽度和深度)


改变   最大高度



             如果不启用柏林噪声,而是使用随机的效果:

 

平整度:

 

还有是否添加碰撞体:

 

_relief 跌宕起伏,决定了 采样的间隔大小。值越大跨度越小。

 

地图大小,就是控制父对象的scale

 

[csharp] view plain copy
  1. // RandomMapMaker.cs  
  2. using UnityEngine;  
  3. using System.Collections;  
  4. using System.Collections.Generic;  
  5.   
  6. public class RandomMapMaker : MonoBehaviour {  
  7.   
  8. private float _seedX, _seedZ;  
  9.   
  10. [SerializeField]  
  11. private float _width = 50;  
  12. [SerializeField]  
  13. private float _depth = 50;  
  14.   
  15. [SerializeField]  
  16. private bool _needToCollider = false;  
  17.   
  18. [SerializeField]  
  19. private float _maxHeight = 10;  
  20.   
  21. [SerializeField]  
  22. private bool _isPerlinNoiseMap = true;  
  23.   
  24. [SerializeField]  
  25. private float _relief = 15f;  
  26.   
  27. [SerializeField]  
  28. private bool _isSmoothness = false;  
  29.   
  30. [SerializeField]  
  31. private float _mapSize = 1f;  
  32. //=================================================================================  
  33. //初期化  
  34. //=================================================================================  
  35. private void Awake () {  
  36.   
  37. transform.localScale = new Vector3(_mapSize, _mapSize, _mapSize);  
  38.   
  39. _seedX = Random.value * 100f;  
  40. _seedZ = Random.value * 100f;  
  41.   
  42. for (int x = 0; x < _width; x++) {  
  43. for (int z = 0; z < _depth; z++) {  
  44.   
  45. GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);  
  46. cube.transform.localPosition = new Vector3 (x, 0, z);  
  47. cube.transform.SetParent (transform);  
  48. if(!_needToCollider){  
  49. Destroy(cube.GetComponent<BoxCollider> ());  
  50. }  
  51.   
  52. SetY (cube);  
  53. }  
  54. }  
  55. }  
  56.   
  57. private void OnValidate (){  
  58.   
  59. if(!Application.isPlaying){  
  60. return;  
  61. }  
  62.   
  63. transform.localScale = new Vector3(_mapSize, _mapSize, _mapSize);  
  64.   
  65. foreach (Transform child in transform) {  
  66. SetY (child.gameObject);  
  67. }  
  68. }  
  69.   
  70. private void SetY(GameObject cube){  
  71. float y = 0;  
  72.   
  73. if(_isPerlinNoiseMap){  
  74. float xSample = (cube.transform.localPosition.x + _seedX) / _relief;  
  75. float zSample = (cube.transform.localPosition.z + _seedZ) / _relief;  
  76. float noise = Mathf.PerlinNoise(xSample, zSample);  
  77. y = _maxHeight * noise;  
  78. }  
  79.   
  80. else{  
  81. y = Random.Range (0, _maxHeight);  
  82. }  
  83.   
  84. if(!_isSmoothness){  
  85. y = Mathf.Round (y);  
  86. }  
  87.   
  88. cube.transform.localPosition = new Vector3 (cube.transform.localPosition.x, y, cube.transform.localPosition.z);  
  89.   
  90. Color color = Color.black;  
  91. if(y > _maxHeight * 0.3f){  
  92. ColorUtility.TryParseHtmlString("#019540FF"out color);  
  93. }  
  94. else if(y > _maxHeight * 0.2f){  
  95. ColorUtility.TryParseHtmlString("#2432ADFF"out color);  
  96. }  
  97. else if(y > _maxHeight * 0.1f){  
  98. ColorUtility.TryParseHtmlString("#D4500EFF"out color);  
  99. }  
  100. cube.GetComponent<MeshRenderer> ().material.color = color;  
  101. }  
  102. }  





参考:

百度百科:  http://baike.baidu.com/item/%E6%9F%8F%E6%9E%97%E5%99%AA%E5%A3%B0

http://postd.cc/understanding-perlin-noise/

http://evilonedeath.blog.fc2.com/blog-entry-127.html

http://befool.co.jp/blog/ayumegu/unity-study-Noise/

http://kan-kikuchi.hatenablog.com/entry/PerlinNoise

猜你喜欢

转载自blog.csdn.net/qq_38112703/article/details/79959184