Example #1
0
 public void Dispose()
 {
     verticesBuffer.Dispose();
     instanceCountBuffer.Dispose();
     resultBuffer.Dispose();
     clusterBuffer.Dispose();
     removebuffer.Dispose();
     heightMapBuffer.Dispose();
     referenceBuffer.Dispose();
     triangleBuffer.Dispose();
     notUsedHeightmapIndices.Dispose();
     current = null;
 }
Example #2
0
        public TerrainDrawStreaming(int maximumLength, int meshSize, ComputeShader transformShader)
        {
            current = this;
            if (meshSize % 2 != 0)
            {
                Debug.LogError("Terrain panel's size should be even number!");
                meshSize++;
            }
            this.meshSize = meshSize;
            //Initialize Mesh and triangles
            int vertexCount = meshSize + 1;

            vertSize      = vertexCount;
            heightMapSize = vertSize * vertSize;
            NativeArray <float2> terrainVertexArray = new NativeArray <float2>(vertexCount * vertexCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            float2 *arrPtr = terrainVertexArray.Ptr();

            for (int x = 0; x < vertexCount; ++x)
            {
                for (int y = 0; y < vertexCount; ++y)
                {
                    arrPtr[y * vertexCount + x] = new float2(x, y) / meshSize - new float2(0.5f, 0.5f);
                }
            }
            verticesBuffer = new ComputeBuffer(terrainVertexArray.Length, sizeof(float2));
            verticesBuffer.SetData(terrainVertexArray);
            heightMapBuffer = new ComputeBuffer(maximumLength * (vertexCount * vertexCount), sizeof(float));
            NativeArray <int> triangles = new NativeArray <int>(6 * meshSize * meshSize, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            int *trianglePtr = triangles.Ptr();

            for (int x = 0, count = 0; x < meshSize; ++x)
            {
                for (int y = 0; y < meshSize; ++y)
                {
                    int4 indices = new int4(vertexCount * y + x, vertexCount * (y + 1) + x, vertexCount * y + (x + 1), vertexCount * (y + 1) + (x + 1));
                    trianglePtr[count]     = indices.x;
                    trianglePtr[count + 1] = indices.y;
                    trianglePtr[count + 2] = indices.z;
                    trianglePtr[count + 3] = indices.y;
                    trianglePtr[count + 4] = indices.w;
                    trianglePtr[count + 5] = indices.z;
                    count += 6;
                }
            }
            triangleBuffer = new ComputeBuffer(triangles.Length, sizeof(int));
            triangleBuffer.SetData(triangles);
            triangles.Dispose();
            terrainVertexArray.Dispose();
            removebuffer = new ComputeBuffer(100, sizeof(int2));
            //Initialize indirect
            clusterBuffer        = new ComputeBuffer(maximumLength, sizeof(TerrainPanel));
            referenceBuffer      = new NativeList <ulong>(maximumLength, Allocator.Persistent);
            this.transformShader = transformShader;
            resultBuffer         = new ComputeBuffer(maximumLength, sizeof(int));
            instanceCountBuffer  = new ComputeBuffer(5, sizeof(int), ComputeBufferType.IndirectArguments);
            NativeArray <int> indirect = new NativeArray <int>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            indirect[0] = triangleBuffer.count;
            instanceCountBuffer.SetData(indirect);
            indirect.Dispose();
            notUsedHeightmapIndices = new NativeList <int>(maximumLength, Allocator.Persistent);
            for (int i = 0; i < maximumLength; ++i)
            {
                notUsedHeightmapIndices.Add(i);
            }
        }