Beispiel #1
0
        private void SafeRelease(Trees tree)
        {
            tree.TreeBuffer.SafeRelease();

            Destroy(tree.ComputeShader);

            tree.SurfaceVertices.SafeRelease();
            tree.SurfaceIndices.SafeRelease();
            tree.SurfaceUVs.SafeRelease();
        }
Beispiel #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);
        }
Beispiel #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);

            var maxTreeMesh = (int)Mathf.Ceil((tree.SurfaceMesh.bounds.size.x * tree.SurfaceMesh.bounds.size.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;

            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);
        }
Beispiel #4
0
        //public void StartCoroutine()
        //{
        //    StartCoroutine(Initlize());
        //}
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        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);
        }
Beispiel #5
0
 private void UpdatePlanePos(Trees tree)
 {
     tree.ComputeShader.SetVectorArray(ComputeShaderID.frustumPlanes, _frustumPlanes);
 }
Beispiel #6
0
 public void RemoveTree(Trees tree)
 {
     _drawTree.Remove(tree);
     SafeRelease(tree);
 }