示例#1
0
        public static List <GameObject> Render(Planet planet, int regionIndex, int chunkIndex)
        {
            var tileRegion = planet.HexSphere.Regions[regionIndex];
            var chunk      = planet.Regions[regionIndex].Chunks[chunkIndex];

            var gameObjects = new List <GameObject>();

            foreach (var(layer, layerIndex) in chunk.Layers.WithIndex())
            {
                foreach (var(blockId, index) in layer.Blocks.WithIndex())
                {
                    var tile  = tileRegion.Tiles[index];
                    var block = BlockMap.GetBlock(blockId);

                    var bottomRadius = planet.BaseRadius +
                                       (chunkIndex * planet.ChunkHeight + layerIndex) * planet.BlockHeight;

                    if (!block.IsRendered)
                    {
                        continue;
                    }

                    var mesh = TileToMeshMapper.Map(tile,
                                                    bottomRadius,
                                                    planet.BlockHeight);

                    var go = new GameObject("Block");
                    go.AddComponent <MeshFilter>();
                    go.AddComponent <MeshRenderer>();
                    go.GetComponent <MeshFilter>().mesh       = mesh;
                    go.GetComponent <MeshRenderer>().material = (block as RenderedBlock)?.Material;

                    if (block.IsSolid)
                    {
                        go.AddComponent <MeshCollider>();
                        go.GetComponent <MeshCollider>().sharedMesh = mesh;
                    }

                    go.transform.position = PointHelpers.ProjectToRadius(tile.Center, bottomRadius).AsVector();

                    gameObjects.Add(go);
                }
            }

            return(gameObjects);
        }
示例#2
0
        public static GameObject Create(Tile tile, float bottomRadius, Material material, float height = 1.0f)
        {
            var block = new GameObject("HexBlock");

            block.AddComponent <MeshFilter>();
            block.AddComponent <MeshRenderer>();
            block.AddComponent <MeshCollider>();

            var mesh = TileToMeshMapper.Map(tile, bottomRadius, height);

            block.GetComponent <MeshFilter>().mesh = mesh;

            block.GetComponent <MeshCollider>().sharedMesh = mesh;

            block.GetComponent <MeshRenderer>().material = material;

            block.transform.position = PointHelpers.ProjectToRadius(tile.Center, bottomRadius).AsVector();
            return(block);
        }
示例#3
0
        public static Mesh Map(Tile tile, float bottomRadius, float height)
        {
            var count  = tile.Boundary.Count;
            var offset = count * 2 - 1;

            var vertices    = new Vector3[offset * 2];
            var tris        = new List <int>();
            var pos         = PointHelpers.ProjectToRadius(tile.Center, bottomRadius).AsVector();
            var vertexIndex = 0;

            for (var i = 0; i < tile.Boundary.Count; i++)
            {
                vertices[vertexIndex]          = PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius).AsVector() - pos;
                vertices[offset + vertexIndex] =
                    PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius + height).AsVector() - pos;
                var next = 1;
                if (i != 1 && i != 2)
                {
                    next++;
                    vertices[vertexIndex + 1] =
                        PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius).AsVector() - pos;
                    vertices[offset + vertexIndex + 1] =
                        PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius + height).AsVector() - pos;
                }
                if (i == 0)
                {
                    next++;
                    vertices[vertexIndex + 2]          = PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius).AsVector() - pos;
                    vertices[offset + vertexIndex + 2] =
                        PointHelpers.ProjectToRadius(tile.Boundary[i], bottomRadius + height).AsVector() - pos;
                }
                if (i == count - 1)
                {
                    next += 2;
                }

                tris.Add(vertexIndex);
                tris.Add((vertexIndex + next) % offset);
                tris.Add(vertexIndex + offset);
                tris.Add((vertexIndex + next) % offset);
                tris.Add((vertexIndex + next) % offset + offset);
                tris.Add(vertexIndex + offset);
                vertexIndex += next;
            }

            tris.Add(1);
            tris.Add(4);
            tris.Add(3);
            tris.Add(offset + 1);
            tris.Add(offset + 3);
            tris.Add(offset + 4);
            tris.Add(4);
            tris.Add(8);
            tris.Add(6);
            tris.Add(offset + 4);
            tris.Add(offset + 6);
            tris.Add(offset + 8);
            tris.Add(8);
            tris.Add(4);
            tris.Add(1);
            tris.Add(offset + 8);
            tris.Add(offset + 1);
            tris.Add(offset + 4);

            if (tile.Boundary.Count > 5)
            {
                tris.Add(8);
                tris.Add(1);
                tris.Add(10);
                tris.Add(offset + 8);
                tris.Add(offset + 10);
                tris.Add(offset + 1);
            }

            Vector2[] uvs = null;
            if (count == 5)
            {
                uvs = new Vector2[]
                {
                    new Vector2(1, 0.3078f),
                    new Vector2(0.8617f, 0.1176f),
                    new Vector2(0, 0.3078f),
                    new Vector2(0.8f, 0.3078f),
                    new Vector2(0.6f, 0.3078f),
                    new Vector2(0.4f, 0.3078f),
                    new Vector2(0.5382f, 0.1176f),
                    new Vector2(0.2f, 0.3078f),
                    new Vector2(0.6999f, 0),
                    new Vector2(1, 0.4778f),
                    new Vector2(0.8617f, 0.6680f),
                    new Vector2(0, 0.4778f),
                    new Vector2(0.8f, 0.4778f),
                    new Vector2(0.6f, 0.4778f),
                    new Vector2(0.4f, 0.4778f),
                    new Vector2(0.5382f, 0.6680f),
                    new Vector2(0.2f, 0.4778f),
                    new Vector2(0.6999f, 0.7856f),
                };
            }
            else if (count == 6)
            {
                uvs = new Vector2[]
                {
                    new Vector2(1, 0.2887f),
                    new Vector2(0.9166f, 0.1444f),
                    new Vector2(0, 0.2887f),
                    new Vector2(0.8332f, 0.2887f),
                    new Vector2(0.6666f, 0.2887f),
                    new Vector2(0.5f, 0.2887f),
                    new Vector2(0.5833f, 0.1444f),
                    new Vector2(0.3333f, 0.2887f),
                    new Vector2(0.6666f, 0),
                    new Vector2(0.1667f, 0.2887f),
                    new Vector2(0.8332f, 0),
                    new Vector2(1, 0.4553f),
                    new Vector2(0.9166f, 0.5996f),
                    new Vector2(0, 0.4553f),
                    new Vector2(0.8332f, 0.4553f),
                    new Vector2(0.6666f, 0.4553f),
                    new Vector2(0.5f, 0.4553f),
                    new Vector2(0.5833f, 0.5996f),
                    new Vector2(0.3333f, 0.4453f),
                    new Vector2(0.6666f, 0.7439f),
                    new Vector2(0.1667f, 0.4553f),
                    new Vector2(0.8332f, 0.7436f),
                };
            }

            var mesh = new Mesh()
            {
                vertices  = vertices,
                triangles = tris.ToArray(),
                uv        = uvs
            };

            mesh.RecalculateNormals();
            return(mesh);
        }