Esempio n. 1
0
        private void SafeRelease(Trees tree)
        {
            //Debug.LogFormat("Trees: Removed " + tree.ID + " From List!");

            tree.TreeBuffer.SafeRelease();

            Destroy(tree.ComputeShader);

            tree.SurfaceVertices.SafeRelease();
            tree.SurfaceIndices.SafeRelease();
            tree.SurfaceUVs.SafeRelease();
        }
Esempio n. 2
0
        private void UpdateShaderValues(Trees tree)
        {
            tree.ComputeShader.SetInt(ComputeShaderID.terrainResolution, tree.SplatMap.height);
            tree.ComputeShader.SetMatrix(ComputeShaderID.objToWorld, tree.GameObject.transform.localToWorldMatrix);
            tree.ComputeShader.SetFloat(ComputeShaderID.surfaceGridStep, Density);

            tree.MeshTreeGeneratorKernel.SetTexture(ComputeShaderID.splatMap, tree.SplatMap);
            tree.MeshTreeGeneratorKernel.SetTexture(ComputeShaderID.nodeTexture, tree.NodeTexture);
            tree.MeshTreeGeneratorKernel.SetTexture(ComputeShaderID.placementMap, tree.PlacementMap);

            tree.CullKernal.SetBuffer(ComputeShaderID.cullOutBuffer, _megaBuffer);
            tree.CullKernal.SetBuffer(ComputeShaderID.closeBuffer, _closeMegaBuffer);
        }
Esempio n. 3
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        // --- GrassComponent ---
        private void Initialize(Trees tree)
        {
            //Init shaders
            tree.ComputeShader = Instantiate(ComputeShader);

            // Init kernels
            tree.MeshTreeGeneratorKernel = new ComputeKernel(_meshTreeGeneratorKernelName, tree.ComputeShader);
            tree.IndirectTreeKernel      = new ComputeKernel(_indirectTreeKernelName, tree.ComputeShader);
            tree.CullKernal = new ComputeKernel(_cullKernelName, tree.ComputeShader);

            tree.SurfaceSize      = new Vector3(tree.SurfaceMesh.bounds.size.x, tree.SurfaceMesh.bounds.size.y, tree.SurfaceMesh.bounds.size.z);
            tree.SurfaceTriangles = tree.SurfaceMesh.triangles.Length;

            var maxTreeMesh = (int)Mathf.Ceil((tree.SurfaceSize.x * tree.SurfaceSize.z) / Density);

            tree.MaxTree = maxTreeMesh > 0 ? maxTreeMesh : 1;

            tree.TreeBuffer = new ComputeBuffer(tree.MaxTree, sizeof(float) * 4, ComputeBufferType.Append);

            var surfaceVertices = tree.SurfaceMesh.vertices;
            var surfaceIndices  = tree.SurfaceMesh.GetIndices(0);
            var surfaceUVs      = tree.SurfaceMesh.uv;

            //Destroy(tree.SurfaceMesh);

            tree.SurfaceVertices = new ComputeBuffer(surfaceVertices.Length, sizeof(float) * 3, ComputeBufferType.Default);
            tree.SurfaceIndices  = new ComputeBuffer(surfaceIndices.Length, sizeof(int), ComputeBufferType.Default);
            tree.SurfaceUVs      = new ComputeBuffer(surfaceUVs.Length, sizeof(float) * 2, ComputeBufferType.Default);

            tree.ComputeShader.SetInt(ComputeShaderID.cullCount, tree.MaxTree);
            tree.ComputeShader.SetInt(ComputeShaderID.indexCount, surfaceIndices.Length);

            // fill surface vertices
            tree.SurfaceVertices.SetData(surfaceVertices);
            tree.MeshTreeGeneratorKernel.SetBuffer(ComputeShaderID.surfaceVertices, tree.SurfaceVertices);
            tree.SurfaceIndices.SetData(surfaceIndices);
            tree.MeshTreeGeneratorKernel.SetBuffer(ComputeShaderID.surfaceIndices, tree.SurfaceIndices);
            tree.SurfaceUVs.SetData(surfaceUVs);
            tree.MeshTreeGeneratorKernel.SetBuffer(ComputeShaderID.surfaceUVs, tree.SurfaceUVs);

            //tree.ComputeShader.SetInt(ComputeShaderID.terrainResolution, tree.SplatMap.height);
            //tree.ComputeShader.SetMatrix(ComputeShaderID.objToWorld, tree.GameObject.transform.localToWorldMatrix);
            //tree.ComputeShader.SetFloat(ComputeShaderID.surfaceGridStep, Density);

            //tree.MeshTreeGeneratorKernel.SetTexture(ComputeShaderID.splatMap, tree.SplatMap);
            //tree.MeshTreeGeneratorKernel.SetTexture(ComputeShaderID.nodeTexture, tree.NodeTexture);
            //tree.MeshTreeGeneratorKernel.SetTexture(ComputeShaderID.placementMap, tree.PlacementMap);
        }
Esempio n. 4
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public void AddTree(GameObject go)
        {
            var _mesh    = go.GetComponent <MeshFilter>().mesh;
            var _texture = go.GetComponent <MeshRenderer>().material.mainTexture;

            Trees tree = new Trees
            {
                ID           = Guid.NewGuid(),
                GameObject   = go,
                SurfaceMesh  = _mesh,
                SplatMap     = DefaultSplatMap,
                NodeTexture  = (Texture2D)_texture,
                PlacementMap = PlacementMap,
            };

            Initialize(tree);

            //add grass to list
            _drawTree.Add(tree);
        }
Esempio n. 5
0
 private void UpdatePlanePos(Trees tree)
 {
     tree.ComputeShader.SetVectorArray(ComputeShaderID.frustumPlanes, _frustumPlanes);
 }
Esempio n. 6
0
 public void RemoveTree(Trees tree)
 {
     _drawTree.Remove(tree);
     SafeRelease(tree);
 }