Advertisement
Guest User

Untitled

a guest
Dec 11th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. //MarchingCubesGPU.cs
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class MarchingCubesGPU : MonoBehaviour
  8. {
  9. public bool smoothTerrain;
  10. public bool flatShaded;
  11.  
  12. public MeshFilter meshFilter;
  13. public MeshCollider meshCollider;
  14.  
  15. public float terrainSurface = 0.5f;
  16. public int width = 32;
  17. public int height = 8;
  18. public int depth = 31;
  19. float[,,] terrainMap;
  20.  
  21. public float noiseAmp = 0.06f;
  22. public bool visualiseNoise = true;
  23.  
  24. Vector3[] vertices;
  25. int[] triangles;
  26.  
  27. public ComputeShader shader;
  28.  
  29. void Start()
  30. {
  31.  
  32. }
  33.  
  34. void Update()
  35. {
  36. terrainMap = new float[width + 1, height + 1, depth + 1];
  37. PopulateTerrainMap();
  38. CreateMeshData();
  39. }
  40.  
  41. public static float Noise3D(float x, float y, float z, float frequency, float amplitude, float persistence, int octave, int seed)
  42. {
  43. float noise = 0.0f;
  44.  
  45. for (int i = 0; i < octave; ++i)
  46. {
  47. // Get all permutations of noise for each individual axis
  48. float noiseXY = Mathf.PerlinNoise(x * frequency + seed, y * frequency + seed) * amplitude;
  49. float noiseXZ = Mathf.PerlinNoise(x * frequency + seed, z * frequency + seed) * amplitude;
  50. float noiseYZ = Mathf.PerlinNoise(y * frequency + seed, z * frequency + seed) * amplitude;
  51.  
  52. // Reverse of the permutations of noise for each individual axis
  53. float noiseYX = Mathf.PerlinNoise(y * frequency + seed, x * frequency + seed) * amplitude;
  54. float noiseZX = Mathf.PerlinNoise(z * frequency + seed, x * frequency + seed) * amplitude;
  55. float noiseZY = Mathf.PerlinNoise(z * frequency + seed, y * frequency + seed) * amplitude;
  56.  
  57. // Use the average of the noise functions
  58. noise += (noiseXY + noiseXZ + noiseYZ + noiseYX + noiseZX + noiseZY) / 6.0f;
  59.  
  60. amplitude *= persistence;
  61. frequency *= 2.0f;
  62. }
  63.  
  64. // Use the average of all octaves
  65. return noise / octave;
  66. }
  67.  
  68. void PopulateTerrainMap()
  69. {
  70. // The data points for terrain are stored at the corners of our "cubes", so the terrainMap needs to be 1 larger
  71. // than the width/height of our mesh.
  72. for (int x = 0; x < width + 1; x++)
  73. {
  74. for (int y = 0; y < height + 1; y++)
  75. {
  76. for (int z = 0; z < depth + 1; z++)
  77. {
  78. float thisHeight = Noise3D(x, y, z, noiseAmp, 1.0f, 0.5f, 1, 0);
  79.  
  80. // Set the value of this point in the terrainMap.
  81. terrainMap[x, y, z] = thisHeight;
  82. }
  83. }
  84. }
  85. }
  86.  
  87. void CreateMeshData()
  88. {
  89. ClearMeshData();
  90.  
  91. shader.SetFloat("terrainSurface", terrainSurface);
  92. shader.SetFloats("dimensions", new float[3] { width, height, depth });
  93.  
  94. ComputeBuffer vertexBuffer = new ComputeBuffer(vertices.Length, sizeof(float) * 3);
  95. vertexBuffer.SetData(vertices);
  96. shader.SetBuffer(0, "vertices", vertexBuffer);
  97.  
  98. //ComputeBuffer triangleBuffer = new ComputeBuffer(triangles.Length, sizeof(int));
  99. //triangleBuffer.SetData(triangles);
  100. //shader.SetBuffer(0, "triangles", triangleBuffer);
  101.  
  102. ComputeBuffer terrainMapBuffer = new ComputeBuffer(terrainMap.Length, sizeof(float));
  103. terrainMapBuffer.SetData(terrainMap);
  104. shader.SetBuffer(0, "terrainMap", terrainMapBuffer);
  105.  
  106. shader.Dispatch(0, width / 1, height / 1, depth / 1);
  107.  
  108. vertexBuffer.GetData(vertices);
  109. //triangleBuffer.GetData(triangles);
  110. terrainMapBuffer.GetData(terrainMap);
  111.  
  112. vertexBuffer.Release();
  113. //triangleBuffer.Release();
  114. terrainMapBuffer.Release();
  115.  
  116. MakeTriangles();
  117. BuildMesh();
  118. }
  119.  
  120. void MakeTriangles()
  121. {
  122. List<int> tempTrianges = new List<int>();
  123. for (int i = 0; i < vertices.Length; i++)
  124. {
  125. if (vertices[i] != Vector3.zero)
  126. {
  127. tempTrianges.Add(i);
  128. }
  129. }
  130. triangles = tempTrianges.ToArray();
  131. }
  132.  
  133. void ClearMeshData()
  134. {
  135. vertices = new Vector3[width * height * depth * 15];
  136. //triangles = new int[width * height * depth * 15];
  137. }
  138.  
  139. void BuildMesh()
  140. {
  141. Mesh mesh = new Mesh();
  142. mesh.vertices = vertices;
  143. mesh.triangles = triangles;
  144. mesh.RecalculateNormals();
  145. meshFilter.mesh = mesh;
  146. meshCollider.sharedMesh = mesh;
  147. }
  148.  
  149. void OnDrawGizmosSelected()
  150. {
  151. if (!visualiseNoise || !Application.isPlaying)
  152. {
  153. return;
  154. }
  155.  
  156. for (int x = 0; x < width + 1; x++)
  157. {
  158. for (int y = 0; y < height + 1; y++)
  159. {
  160. for (int z = 0; z < depth + 1; z++)
  161. {
  162. if (terrainMap[x, y, z] > terrainSurface)
  163. {
  164. Gizmos.color = new Color(terrainMap[x, y, z], terrainMap[x, y, z], terrainMap[x, y, z], 1.0f);
  165. Gizmos.DrawSphere(new Vector3(x, y, z), 0.2f);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement