private void BuildMesh() { GameObject obj = new GameObject(CurrentMap.Name, typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider)); obj.transform.parent = ParentMap.transform; int width = CurrentMap.Width; int height = CurrentMap.Height; Mesh mesh = new Mesh(); Dictionary <int, int> tilesBySubmesh = new Dictionary <int, int>(); Dictionary <int, int[]> trianglesBySubmesh = new Dictionary <int, int[]>(); Dictionary <int, int> controlBySubmesh = new Dictionary <int, int>(); Dictionary <int, int> indexBySubmesh = new Dictionary <int, int>(); // Define 0-index based ids for each texture id int index = 0; foreach (int textureId in CurrentMap.Textures.Keys) { indexBySubmesh[textureId] = index++; } // Gets the number of tiles in each submesh foreach (Tile tile in CurrentMap.Tiles) { if (tilesBySubmesh.ContainsKey(tile.TextureIdentifier)) { tilesBySubmesh[tile.TextureIdentifier]++; } else { tilesBySubmesh[tile.TextureIdentifier] = 1; } } // Creates each triangle array based on the number of tiles foreach (KeyValuePair <int, int> submesh in tilesBySubmesh) { trianglesBySubmesh[submesh.Key] = new int[submesh.Value * 3 * 2]; Debug.Log(string.Format("Texture {0}: {1} triangles", submesh.Key, trianglesBySubmesh[submesh.Key].Length)); // Defines each index for triangle creation control controlBySubmesh[submesh.Key] = 0; } // Defines the variables int vertNumber = width * height * 4; Vector3[] vertices = new Vector3[vertNumber]; Vector2[] uvs = new Vector2[vertNumber]; Vector3[] normals = new Vector3[vertNumber]; for (int i = 0; i < CurrentMap.Tiles.Count; i++) { Tile tile = CurrentMap.Tiles[i]; int x = i % CurrentMap.Width; int z = i / CurrentMap.Width; int topLeft = z * 4 * width + x * 2; int topRight = topLeft + 1; int bottomLeft = topLeft + width * 2; int bottomRight = bottomLeft + 1; vertices[topLeft] = new Vector3(x, HeightStage * (tile.Top + tile.Left + tile.TopLeft + tile.Stage), z); vertices[topRight] = new Vector3(x + 1, HeightStage * (tile.Top + tile.Right + tile.TopRight + tile.Stage), z); vertices[bottomLeft] = new Vector3(x, HeightStage * (tile.Bottom + tile.Left + tile.BottomLeft + tile.Stage), z + 1); vertices[bottomRight] = new Vector3(x + 1, HeightStage * (tile.Bottom + tile.Right + tile.BottomRight + tile.Stage), z + 1); TileUV tileUVs = CurrentMap.Textures[tile.TextureIdentifier].UVs[tile.TileUVIdentifier]; uvs[topLeft] = tileUVs.UV1; uvs[topRight] = tileUVs.UV2; uvs[bottomLeft] = tileUVs.UV3; uvs[bottomRight] = tileUVs.UV4; normals[topLeft] = Vector3.up; normals[topRight] = Vector3.up; normals[bottomLeft] = Vector3.up; normals[bottomRight] = Vector3.up; // Gets the right triangle array and control index according to the submesh int[] triangles = trianglesBySubmesh[tile.TextureIdentifier]; int triangleIndex = controlBySubmesh[tile.TextureIdentifier]; triangles[triangleIndex++] = topLeft; triangles[triangleIndex++] = bottomRight; triangles[triangleIndex++] = topRight; triangles[triangleIndex++] = topLeft; triangles[triangleIndex++] = bottomLeft; triangles[triangleIndex++] = bottomRight; controlBySubmesh[tile.TextureIdentifier] = triangleIndex; } // Defines the variables in the mesh mesh.vertices = vertices; mesh.normals = normals; mesh.uv = uvs; // Defines the triangles and materials for each submesh mesh.subMeshCount = CurrentMap.Textures.Count; Material[] materials = new Material[mesh.subMeshCount]; foreach (KeyValuePair <int, int[]> triangles in trianglesBySubmesh) { mesh.SetTriangles(triangles.Value, indexBySubmesh[triangles.Key]); materials[indexBySubmesh[triangles.Key]] = GetTexturedMaterial(CurrentMap.Textures[triangles.Key].Name); } obj.GetComponent <MeshRenderer>().materials = materials; obj.GetComponent <MeshFilter>().mesh = mesh; obj.GetComponent <MeshCollider>().sharedMesh = obj.GetComponent <MeshFilter>().sharedMesh; }
void LoadSelf(string path) { texture = Resources.Load <Texture2D>(path); int tileSize = 64; int sizeX = texture.width / tileSize; int sizeY = texture.height / tileSize; int tilemapSize = sizeX * sizeY; int id = 0; float precisionX = 1f / texture.width; float precisionY = 1f / texture.height; for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { uv[id] = new TileUV(); uv[id].uv0.x = x * tileSize; uv[id].uv0.y = texture.height - y * tileSize - tileSize; uv[id].uv1.x = uv[id].uv0.x + tileSize; uv[id].uv1.y = uv[id].uv0.y; uv[id].uv2.x = uv[id].uv0.x + tileSize; uv[id].uv2.y = uv[id].uv0.y + tileSize; uv[id].uv3.x = uv[id].uv0.x; uv[id].uv3.y = uv[id].uv0.y + tileSize; sprites[id] = Sprite.Create(texture, new Rect(uv[id].uv0.x, uv[id].uv0.y, tileSize, tileSize), new Vector2(0.5f, 0.5f)); uv[id].uv0.x /= texture.width; uv[id].uv0.y /= texture.height; uv[id].uv1.x /= texture.width; uv[id].uv1.y /= texture.height; uv[id].uv2.x /= texture.width; uv[id].uv2.y /= texture.height; uv[id].uv3.x /= texture.width; uv[id].uv3.y /= texture.height; uv[id].uv0.x += precisionX; uv[id].uv0.y += precisionY; uv[id].uv1.x -= precisionX; uv[id].uv1.y += precisionY; uv[id].uv2.x -= precisionX; uv[id].uv2.y -= precisionY; uv[id].uv3.x += precisionX; uv[id].uv3.y -= precisionY; id += 1; } } }