コード例 #1
0
        /// <summary>
        /// Applies MeshData to this Quad's mesh
        /// </summary>
        private void ApplyToMesh(MeshData md)
        {
            if (!mesh)
            {
                mesh = new Mesh();
            }
            else
            {
                mesh.Clear();
            }

            mesh.vertices  = md.vertices;
            mesh.triangles = Utils.GetTriangles(configuration.ToString());
            mesh.colors32  = md.colors;
            mesh.uv        = md.uv;
            mesh.uv4       = md.uv2;

            mesh.RecalculateBounds();
            mesh.normals = md.normals;
            //mesh.RecalculateNormals();
            initialized = true;

            if (renderedQuad)
            {
                renderedQuad.GetComponent <MeshFilter>().mesh = mesh;
            }
        }
コード例 #2
0
        /// <summary>
        /// Finds this Quad's neighbors, then checks if edge fans are needed. If so, they are applied.
        /// </summary>
        public void GetNeighbors()
        {
            if (neighborIds == null) //Finding the IDs of all neigbors. This is only done once.
            {
                neighborIds = new string[4];
                for (int i = 0; i < 4; i++)
                {
                    neighborIds[i] = QuadNeighbor.GetNeighbor(index, i.ToString());
                }
            }
            neighbors = new Quad[4];
            for (int i = 0; i < neighbors.Length; i++) //Trying to find neighbors by id. If not there, neighbor has a lower subdivision level, last char of id is removed.
            {
                int j = 0;
                while (neighbors[i] == null && j < 4)
                {
                    neighbors[i] = planet.FindQuad(neighborIds[i].Substring(0, neighborIds[i].Length - j));
                    j++;
                }

                /*neighbors[i] = null;
                 * neighbors[i] = planet.FindQuad(neighborIds[i]);
                 *
                 * if (neighbors[i] == null)
                 *  neighbors[i] = planet.FindQuad(neighborIds[i].Substring(0, neighborIds[i].Length - 1));*/
            }

            configuration = bool4.False;

            for (int i = 0; i < neighbors.Length; i++) //Creating configuration based on neighbor levels.
            {
                if (neighbors[i] != null)
                {
                    if (neighbors[i].level == level - 1)
                    {
                        configuration[i] = true;
                    }
                    else
                    {
                        configuration[i] = false;
                    }
                }
                else
                {
                    configuration[i] = false;
                }
            }

            if (configuration != configurationOld) //Loading plane mesh and starting generation on another thread or GPU.
            {
                configurationOld = new bool4(configuration);

                if (!initialized)
                {
                    MeshData md = new MeshData(ConstantTriArrays.extendedPlane, planet.quadMesh.normals, planet.quadMesh.uv);

                    if (planet.mode == Mode.ComputeShader)
                    {
                        int kernelIndex = planet.computeShader.FindKernel("ComputePositions");

                        computeBuffer = new ComputeBuffer(1225, 12);
                        computeBuffer.SetData(ConstantTriArrays.extendedPlane);

                        planet.computeShader.SetFloat("scale", scale);
                        planet.computeShader.SetFloats("trPosition", new float[] { trPosition.x, trPosition.y, trPosition.z });
                        planet.computeShader.SetFloat("radius", planet.radius);
                        planet.computeShader.SetFloats("rotation", new float[] { rotation.x, rotation.y, rotation.z, rotation.w });
                        planet.computeShader.SetFloat("noiseDiv", 1f / planet.heightScale);

                        planet.computeShader.SetBuffer(kernelIndex, "dataBuffer", computeBuffer);

                        planet.computeShader.Dispatch(kernelIndex, 5, 1, 1);

                        gpuReadbackReq = AsyncGPUReadback.Request(computeBuffer);

                        isComputingOnGPU = true;
                    }
                    else
                    {
                        method = SpherifyAndDisplace;
                        cookie = method.BeginInvoke(md, null, null);
                    }
                }
                else
                {
                    if (mesh.vertices.Length > 0)
                    {
                        mesh.triangles = Utils.GetTriangles(configuration.ToString());
                    }
                    if (renderedQuad)
                    {
                        renderedQuad.GetComponent <MeshFilter>().mesh = mesh;
                    }
                }
            }
        }