Unity Note - Project11 Wave2
        
        
            
        
        
            按照上次的设想:
效果还不错
加了几个,调下参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
   | using System.Collections; using System.Collections.Generic; using UnityEngine;
  public class WaveCreator : MonoBehaviour {     [SerializeField] private int waveLength = 100; // length of wave     [SerializeField] private float segLen = 0.2f; // length of each segment     [SerializeField] private float samplingInterval = 20f;     [SerializeField] private float maxHeight = 10f;
      [SerializeField] private string colorHex = "#45b787";     [SerializeField] private float colorAlpha = 0.8f;
      private Color waveColor;     private float seedX = 0;     private float seedY = 0;          void Start()     {         // generate random seed         seedX = Random.value * 100f;         seedY = Random.value * 100f;
          // set material         GetComponent<MeshRenderer>().material = InitMaterial();     }
      void Update() {         float[] arr = GenerateNoiseArray(waveLength);         DrawWave(arr);     }
      private float[] GenerateNoiseArray(int len){         // generate wave height array         float[] arr = new float[len];         for(int i=0; i<arr.Length; i++){             float x = (i + seedX + Time.time) / samplingInterval;             float y = (i + seedY) / samplingInterval;             // noise between 0.0 and 1.0             arr[i] = Mathf.PerlinNoise(x, y) * maxHeight;         }
          return arr;     }
      private void DrawWave(float[] arr){         int n = arr.Length;
          Vector3[] vertices = new Vector3[2*n];         List<int> triangleList = new List<int>();
          // add vertices         for(int i=0; i<n; i++){             vertices[2*i] = new Vector3(segLen*(i+1), 0, 0);             vertices[2*i + 1] = new Vector3(segLen*(i+1), arr[i], 0);         }
          // add triangles         for(int i=0;i<n-1;i++){             // clockwise order             triangleList.Add(2*i);             triangleList.Add(2*i + 1);             triangleList.Add(2*i + 2);
              triangleList.Add(2*i + 2);             triangleList.Add(2*i + 1);             triangleList.Add(2*i + 3);         }
          int[] triangles = triangleList.ToArray();
          // get mesh         Mesh mesh = GetComponent<MeshFilter>().mesh;
          // clear original mesh         mesh.Clear();
          // set new vertices         mesh.vertices = vertices;
          // set new triangles         mesh.triangles = triangles;
          // calculate mesh         mesh.RecalculateNormals();     }
      private Material InitMaterial(){         Material material = new Material(Shader.Find("Standard"));
          // set rendering mode         SetFade(material);
          // set color         ColorUtility.TryParseHtmlString(colorHex, out waveColor);         waveColor.a = colorAlpha;         material.SetColor("_Color", waveColor);
          return material;     }
      private void SetFade(Material material){         // set rendering mode to fade to show alpha         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);         material.SetInt("_ZWrite", 0);         material.DisableKeyword("_ALPHATEST_ON");         material.EnableKeyword("_ALPHABLEND_ON");         material.DisableKeyword("_ALPHAPREMULTIPLY_ON");         material.renderQueue = 3000;     }
  }
   |