Exemplo n.º 1
0
        public void ComputeGrassFor(IEnvironmentSurface chunk)
        {
            MeshData meshData = chunk.MeshData;
            int      numTris  = meshData.vertices.Length / 3;

            vertexBuffer.SetData(meshData.vertices);

            int numThreadPerAxis = Mathf.Max(1, Mathf.CeilToInt(numTris / THREAD_SIZE_GROUP));

            grassProperties = new ComputeBuffer(GRASS_PER_COMPUTE, MeshInstancedProperties.Size(), ComputeBufferType.Append);
            grassProperties.SetCounterValue(0);
            Material       mat     = new Material(this.mat);
            Maybe <Bounds> mBounds = chunk.MeshBounds;
            Vector3        offset  = mBounds.Value.center;

            grassShader.SetInt(NAME_OF_TRIANGLE_LENGTH, numTris);
            grassShader.SetVector(NAME_OF_OFFSET, offset);
            grassShader.SetFloat(NAME_OF_BOUNDS, grassMesh.bounds.extents.y);
            grassShader.SetBuffer(0, NAME_OF_TRIANGLE_BUFFER, vertexBuffer);
            grassShader.SetBuffer(0, NAME_OF_GRASS_BUFFER, grassProperties);

            grassShader.Dispatch(0, numThreadPerAxis, 1, 1);

            new InstantiatableData(grassMesh, grassProperties, mat, mBounds);
            grassProperties = null;
        }
        public void ComputeGrassFor(Bounds bounds, int numTris, ComputeBuffer triangleData)
        {
            int numThreadPerAxis = Mathf.Max(1, Mathf.CeilToInt(numTris / 32f));

            grassProperties = new ComputeBuffer(GRASS_PER_COMPUTE, MeshInstancedProperties.Size(), ComputeBufferType.Append);
            grassProperties.SetCounterValue(0);
            Material mat    = new Material(this.mat);
            Vector3  offset = bounds.center;

            grassShader.SetInt(NAME_OF_TRIANGLE_LENGTH, numTris);
            grassShader.SetVector(NAME_OF_OFFSET, offset);
            grassShader.SetFloat(NAME_OF_BOUNDS, grassMesh.bounds.extents.y);
            grassShader.SetBuffer(0, NAME_OF_TRIANGLE_BUFFER, triangleData);
            grassShader.SetBuffer(0, NAME_OF_GRASS_BUFFER, grassProperties);

            grassShader.Dispatch(0, numThreadPerAxis, 1, 1);

            new InstanciableData(grassMesh, numTris, grassProperties, mat, bounds);
            grassProperties = null;
        }