public void GenerateTerrain(Vector3 worldPosition, int chunkSize) { Stopwatch sw = new Stopwatch(); int dispatchSize = (int)Mathf.Ceil((float)chunkSize / (float)MinDispatchSize); sw.Start(); verticesBuffer.SetCounterValue(0); // 3D noise generation terrain3DNoiseShader.Dispatch(noiseKernel, dispatchSize, dispatchSize, dispatchSize); // Generate normals normalFromNoiseShader.Dispatch(computeNormalKernel, dispatchSize, dispatchSize, dispatchSize); // We don't need the extra size anymore so we restore the original chunk size dispatchSize = (int)Mathf.Ceil((float)(chunkSize - 1) / (float)MinDispatchSize); // Isosurface generation isoSurfaceShader.Dispatch(marchingCubeKernel, dispatchSize * resolutionPerVoxel, dispatchSize * resolutionPerVoxel, dispatchSize * resolutionPerVoxel); ComputeBuffer.CopyCount(verticesBuffer, verticesCountReadbackBuffer, 0); // Vector3[] n = new Vector3[chunkSize * chunkSize * chunkSize]; // normalsBuffer.GetData(n); // chunkNormals[worldPosition] = n; // Get the vertices count back to the cpu int[] verticesCountBuffer = new int[1]; verticesCountReadbackBuffer.GetData(verticesCountBuffer); int verticesCount = verticesCountBuffer[0] * 3; // ComputeBuffer drawChunkBuffer = new ComputeBuffer(4, sizeof(int), ComputeBufferType.IndirectArguments); // // Create an argument buffer for the DrawProceduralIndirect // int[] drawBufferData = {verticesCount, 1, 0, 0}; // drawChunkBuffer.SetData(drawBufferData); // Align the buffer size on the copy kernel dispatch size int copySize = verticesCount + (copyMeshBufferGroupSize.x - (verticesCount % copyMeshBufferGroupSize.x)); // Allocate the final buffers for the mesh // ComputeBuffer meshVertices = new ComputeBuffer(copySize, sizeof(float) * 3); // ComputeBuffer meshNormals = new ComputeBuffer(copySize, sizeof(float) * 3); // Bind these buffers for copy // copyMeshBuffersShader.SetBuffer(copyMeshBufferKernel, KernelIds.meshVertices, meshVertices); // copyMeshBuffersShader.SetBuffer(copyMeshBufferKernel, KernelIds.meshNormals, meshNormals); // And copy them // copyMeshBuffersShader.Dispatch(copyMeshBufferKernel, copySize / copyMeshBufferGroupSize.x, 1, 1); renderer.ClearChunks(); renderer.AddChunkToRender(verticesBuffer, normalsBuffer, worldPosition * chunkSize, verticesCount); sw.Stop(); }