- Home /
How can I generate a up facing quad mesh with adjustable res?
Hello! I am making a little oceanic game, and right now, I am working on making a ocean floor. I have gotten quite far, I have a compute shader, that can be inputed some vertices and output a new list of vertices with differing heights. Snipped from compute shader:
 [numthreads(8,1,1)]
 void CSMain (uint3 id : SV_DispatchThreadID)
 {
     // Get current vertex position
     float3 position = vertices[id.x];
 
     // Because vertices are local, just add centre position of parent "SeaFloorChunk"
     position += worldSpaceVertexOffset;
 
     vertices[id.x].y = snoise(position / terrainSmoothness) * heightMagnitude;
 }
 
Right now, I am using this code to generate a plane mesh (vertices and triangles) to input to the compute shader to edit.
 public float _size;
 public int resolution;
 
 void Generate()
     {
         System.DateTime startingTime = System.DateTime.Now;
 
         vertices = new Vector3[(resolution + 1) * (resolution + 1)];
 
         float stepSize = _size / resolution;
 
         int index = 0;
         for(float x = 0; x <= resolution; x ++)
         {
             for(float z = 0; z <= resolution; z ++)
             {
                 Vector3 position = new Vector3(
                     x * stepSize, 
                     1, 
                     z * stepSize
                 );
 
                 vertices[index] = position;
                 index ++;
             }
         }
 
         int t = 0;
         int v = 0;
 
         triangles = new int[resolution * resolution * 6];
 
         for(int y = 0; y < resolution; y++) {
             for(int x = 0; x < resolution; x++) {
                 triangles[t] = v;
                 triangles[t + 1] = v + resolution + 1;
                 triangles[t + 2] = v + 1;
                 
                 triangles[t + 3] = v + 1;
                 triangles[t + 4] = v + resolution + 1;
                 triangles[t + 5] = v + resolution + 2;
 
                 t += 6;
                 v ++;
             }
             v ++;
         }
 
         planeMesh = new BaseMeshData();
         planeMesh.vertices = vertices;
         planeMesh.triangles = triangles;
 
         System.TimeSpan duration = System.DateTime.Now.Subtract(startingTime);
         Debug.Log($"Took {((float) duration.Milliseconds) / 1000} seconds to generate plane.");
     }
With some other code, this is the output:
It is sorta smooth, but you might notice two things. First, the two strips of chunks are separated, when in theory all chunks are separated by a _size of 32. Number Two, you might also notice some strips poking out of the terrain. I don't know what it is, but I think it is some weird bug with the triangles in my Generating a plane function.
I was wondering if someone could help me construct a better method (That works) of generating a base plane, with a given resolution, resolution meaning higher the resolution, higher the amount of vertices on the plane.
Thank you so much!
Your answer
 
 
             Follow this Question
Related Questions
How do i code a Mesh collider ? 1 Answer
Is there a similar Command to lineRenderer.SetVertexCount for mesh?? 1 Answer
How does the Triangles work on Mesh? 1 Answer
How do i access the MeshExtrusion.cs from Unity's PE? 1 Answer
How do i have a Vector3[] position be in another Vector3[] position (That Updates)?? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                