Exemple #1
0
    public void BuildMesh()
    {
        var shader = GameResources.ComputeShader;
        var k      = shader.FindKernel("Marching");

        gridBuffer.SetData(gridData, 0, 0, gridData.Length);
        trisBuffer.SetCounterValue(0);

        shader.SetInts("m_Size", GameResources.ChunkSize.x, GameResources.ChunkSize.y, GameResources.ChunkSize.z);
        shader.SetFloats("m_Isolevel", planet.noiseDesc.Isolevel);
        shader.SetBuffer(k, "m_GridPointBuffer", gridBuffer);
        shader.SetBuffer(k, "m_TriBuffer", trisBuffer);

        shader.GetKernelThreadGroupSizes(k, out uint kx, out uint ky, out uint kz);

        var thgx = (int)math.ceil(GameResources.ChunkSize.x / (float)kx);
        var thgy = (int)math.ceil(GameResources.ChunkSize.y / (float)ky);
        var thgz = (int)math.ceil(GameResources.ChunkSize.z / (float)kz);

        shader.Dispatch(k, thgx, thgy, thgz);

        ComputeBuffer.CopyCount(trisBuffer, countBuffer, 0);
        countBuffer.GetData(countBufferArray, 0, 0, 1);

        var tCount = countBufferArray[0];

        if (tCount == 0)
        {
            return;
        }

        trisBuffer.GetData(trisBufferTempArray, 0, 0, tCount);

        var vertices  = new NativeArray <Vertex>(tCount * 3, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
        var triangles = new NativeArray <Saitama.ProceduralMesh.Triangle>(tCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

        for (var i = 0; i < tCount; i++)
        {
            var vIndex = i * 3;

            vertices[vIndex] = new Vertex {
                pos = trisBufferTempArray[i][0]
            };
            vertices[vIndex + 1] = new Vertex {
                pos = trisBufferTempArray[i][1]
            };
            vertices[vIndex + 2] = new Vertex {
                pos = trisBufferTempArray[i][2]
            };

            triangles[i] = new Saitama.ProceduralMesh.Triangle(0, 1, 2) + vIndex;
        }

        mesh.Update(triangles, vertices);
        mesh.RecalculateNormals();

        UpdateMeshCollider();
    }
    public void Execute(int i)
    {
        var vIndex = i * 3;

        vertices[vIndex] = new Vertex {
            pos = trisBuffer[i][0]
        };
        vertices[vIndex + 1] = new Vertex {
            pos = trisBuffer[i][1]
        };
        vertices[vIndex + 2] = new Vertex {
            pos = trisBuffer[i][2]
        };

        triangles[i] = new Saitama.ProceduralMesh.Triangle(0, 1, 2) + vIndex;
    }