예제 #1
0
    public static TerrainChunk CreateChunk(MarchingRegion region, MarchingTerrain terrain, Material material, bool generate = true)
    {
        var chunkGameObject = new GameObject($"chunk({region.X},{region.Y},{region.Z})");
        var chunk           = chunkGameObject.AddComponent <TerrainChunk>();

        chunkGameObject.transform.position = new Vector3(region.X, region.Y, region.Z);

        chunk.x = region.X / region.Width;
        chunk.y = region.Y / region.Height;
        chunk.z = region.Z / region.Length;

        chunk.Terrain = terrain;

        chunk.region   = region;
        chunk.marching = new Marching();

        chunk.meshFilter   = chunk.AddComponentIfNotAdded <MeshFilter>();
        chunk.meshRenderer = chunk.AddComponentIfNotAdded <MeshRenderer>();

        chunk.meshRenderer.material = material;

        if (generate)
        {
            chunk.Generate();
        }

        return(chunk);
    }
예제 #2
0
    public Mesh Generate(int lodLevel, MarchingRegion region, TerrainMap map)
    {
        triangle_Index = 0;
        vertice_Index  = 0;

        float[] cubeData = new float[8];

        for (int x = region.X; x < region.Right; x += lodLevel)
        {
            for (int y = region.Y; y < region.Top; y += lodLevel)
            {
                for (int z = region.Z; z < region.Forward; z += lodLevel)
                {
                    Color color = map.GetColor(x, y, z);

                    GetHue(color, out float u, out float v);

                    int flagIndex = 0;

                    Vector3 point = new Vector3(
                        x % region.Width,
                        y % region.Height,
                        z % region.Length);

                    for (int i = 0; i < 8; i++)
                    {
                        int ix = x + MarchingData.VertexOffset[i].x * lodLevel;
                        int iy = y + MarchingData.VertexOffset[i].y * lodLevel;
                        int iz = z + MarchingData.VertexOffset[i].z * lodLevel;

                        float surface = map.GetSurface(ix, iy, iz);

                        if (surface <= threshould)
                        {
                            flagIndex |= 1 << i;
                        }

                        cubeData[i] = surface;
                    }

                    March(lodLevel, point, u, v, flagIndex, cubeData);
                }
            }
        }

        Mesh mesh = new Mesh();

        mesh.SetVertices(vertices, 0, vertice_Index);
        mesh.SetTriangles(triangles, 0, triangle_Index, 0);
        mesh.SetUVs(0, uvs, 0, vertice_Index);

        mesh.RecalculateBounds();
        mesh.RecalculateNormals();

        return(mesh);
    }