コード例 #1
0
        /// <summary>
        /// Generates the required atlas information for a tile.
        /// </summary>
        /// <param name="def">The building definition containing the atlas.</param>
        /// <returns>The atlas information used for rendering.</returns>
        public static AtlasInfo[] GenerateAtlas(this BuildingDef def)
        {
            var tileAtlas = def.BlockTileAtlas.items;
            int forbidIndex = tileAtlas[0].name.Length - 12, startIndex = forbidIndex - 9,
                n         = tileAtlas.Length;
            var atlasInfo = new AtlasInfo[n];

            for (int k = 0; k < n; k++)
            {
                var    atlasEntry   = tileAtlas[k];
                string name         = atlasEntry.name;
                string requiredStr  = name.Substring(startIndex, 8);
                string forbiddenStr = name.Substring(forbidIndex, 8);
                atlasInfo[k].requiredConnections  = (Bits)Convert.ToInt32(requiredStr, 2);
                atlasInfo[k].forbiddenConnections = (Bits)Convert.ToInt32(forbiddenStr, 2);
                atlasInfo[k].uvBox = atlasEntry.uvBox;
                atlasInfo[k].name  = name;
            }
            return(atlasInfo);
        }
コード例 #2
0
        /// <summary>
        /// Generates the vertices for one tile in the mesh.
        /// </summary>
        /// <param name="atlasInfo">The texture atlas to use for rendering.</param>
        /// <param name="x">The bottom left X coordinate.</param>
        /// <param name="y">The bottom left Y coordinate.</param>
        /// <param name="connected">The sides which are connected.</param>
        /// <param name="color">The tint color to use when rendering.</param>
        public static void AddVertexInfo(this AtlasInfo atlasInfo, int x, int y,
                                         Bits connected, Color color)
        {
            float   size     = Grid.CellSizeInMeters;
            Vector2 botLeft  = new Vector2(x, y);
            Vector2 topRight = new Vector2(x + size, y + size);
            Vector2 uvWX     = new Vector2(atlasInfo.uvBox.x, atlasInfo.uvBox.w);
            Vector2 uvYZ     = new Vector2(atlasInfo.uvBox.z, atlasInfo.uvBox.y);
            var     vertices = MeshUtil.vertices;
            var     uvs      = MeshUtil.uvs;
            var     indices  = MeshUtil.indices;
            var     colors   = MeshUtil.colours;

            if ((connected & Bits.Left) == 0)
            {
                botLeft.x -= WORLD_TRIM_SIZE;
            }
            else
            {
                uvWX.x += TRIM_UV_SIZE.x;
            }
            if ((connected & Bits.Right) == 0)
            {
                topRight.x += WORLD_TRIM_SIZE;
            }
            else
            {
                uvYZ.x -= TRIM_UV_SIZE.x;
            }
            if ((connected & Bits.Up) == 0)
            {
                topRight.y += WORLD_TRIM_SIZE;
            }
            else
            {
                uvYZ.y -= TRIM_UV_SIZE.y;
            }
            if ((connected & Bits.Down) == 0)
            {
                botLeft.y -= WORLD_TRIM_SIZE;
            }
            else
            {
                uvWX.y += TRIM_UV_SIZE.y;
            }
            int v0 = vertices.Count;

            vertices.Add(botLeft);
            vertices.Add(new Vector2(topRight.x, botLeft.y));
            vertices.Add(topRight);
            vertices.Add(new Vector2(botLeft.x, topRight.y));
            uvs.Add(uvWX);
            uvs.Add(new Vector2(uvYZ.x, uvWX.y));
            uvs.Add(uvYZ);
            uvs.Add(new Vector2(uvWX.x, uvYZ.y));
            indices.Add(v0);
            indices.Add(v0 + 1);
            indices.Add(v0 + 2);
            indices.Add(v0);
            indices.Add(v0 + 2);
            indices.Add(v0 + 3);
            colors.Add(color);
            colors.Add(color);
            colors.Add(color);
            colors.Add(color);
        }