예제 #1
0
        public TerrainChunk(TerraTerrain terrain, TerrainChunk parent, Vector3 start, Vector3 right, Vector3 forward, int resolution, int layer)
        {
            this.parent     = parent;
            this.start      = start;
            this.right      = right;
            this.forward    = forward;
            this.resolution = resolution;
            this.layer      = layer;

            go           = new GameObject("TerrainChunkLOD" + layer);
            meshFilter   = go.AddComponent <MeshFilter>();
            meshRenderer = go.AddComponent <MeshRenderer>();

            if (parent == null)
            {
                go.transform.parent = terrain.transform;
            }
            else
            {
                go.transform.parent = parent.go.transform;
            }

            mesh            = new Mesh();
            meshFilter.mesh = mesh;
        }
예제 #2
0
        public void Generate(TerraTerrain terrain)
        {
            if (mesh != null)
            {
                mesh.Clear();
            }
            meshRenderer.material = terrain.Material;

            Vector3[] vertices  = new Vector3[(resolution + 1) * (resolution + 1)];
            Vector3[] normals   = new Vector3[vertices.Length];
            Vector2[] uvs       = new Vector2[vertices.Length];
            int[]     triangles = new int[resolution * resolution * 6];

            for (int z = 0, index = 0; z <= resolution; z++)
            {
                for (int x = 0; x <= resolution; x++, index++)
                {
                    terrain.Graph.CurrentVertex = 2 * start + (2 * x * (right / resolution)) + (2 * z * (forward / resolution));
                    terrain.Graph.CurrentUV     = new Vector2((float)x / resolution, (float)z / resolution);
                    vertices[index]             = (Vector3)terrain.Graph.OutNode.GetValue(null);
                    normals[index] = Vector3.up;
                    uvs[index]     = terrain.Graph.CurrentUV;
                }
            }

            for (int ti = 0, vi = 0, y = 0; y < resolution; y++, vi++)
            {
                for (int x = 0; x < resolution; x++, ti += 6, vi++)
                {
                    triangles[ti]     = vi;
                    triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                    triangles[ti + 4] = triangles[ti + 1] = vi + resolution + 1;
                    triangles[ti + 5] = vi + resolution + 2;
                }
            }

            mesh.vertices  = vertices;
            mesh.normals   = normals;
            mesh.uv        = uvs;
            mesh.triangles = triangles;
            mesh.RecalculateNormals();
            go.transform.localScale = Vector3.one;
        }