Created
July 17, 2016 01:00
-
-
Save jamiltron/8430c167d7ebc53482a467d79ea464d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class TileMapMeshBuilder { | |
private static int vertsByTile = 4; | |
private Transform parentTransform; | |
private TileMap tileMap; | |
private Material tileMaterial; | |
float w = 32f; | |
float h = 32f; | |
float sheetW = 128f; | |
float sheetH = 128f; | |
float grassx = 6f; | |
float grassy = 90f; | |
float waterx = 48f; | |
float watery = 90f; | |
float hillx = 6f; | |
float hilly = 48f; | |
float treex = 6f; | |
float treey = 6f; | |
public TileMapMeshBuilder(TileMap tileMap, Material tileMaterial, Transform parentTransform) { | |
this.tileMap = tileMap; | |
this.tileMaterial = tileMaterial; | |
this.parentTransform = parentTransform; | |
} | |
public void BuildMesh(MeshCollider collider, MeshFilter filter, MeshRenderer renderer) { | |
int numVertices = tileMap.Width * tileMap.Height * vertsByTile; | |
Vector3[] vertices = new Vector3[numVertices]; | |
Vector3[] normals = new Vector3[numVertices]; | |
Vector2[] uvs = new Vector2[numVertices]; | |
int[] triangles = new int[numVertices + 2 * numVertices]; | |
int currentVertex = 0; | |
int currentTriangle = 0; | |
int z = 0; | |
for (int y = 0; y < tileMap.Height; y++) { | |
for (int x = 0; x < tileMap.Width; x++) { | |
int newY = (tileMap.Height - 1) - y; | |
vertices[currentVertex] = new Vector3(x, newY, z); | |
vertices[currentVertex + 1] = new Vector3(x + 1, newY, z); | |
vertices[currentVertex + 2] = new Vector3(x, newY + 1, z); | |
vertices[currentVertex + 3] = new Vector3(x + 1, newY + 1, z); | |
normals[currentVertex] = Vector3.up; | |
normals[currentVertex + 1] = Vector3.up; | |
normals[currentVertex + 2] = Vector3.up; | |
normals[currentVertex + 3] = Vector3.up; | |
ApplyTexture(tileMap[x, y].type, currentVertex, uvs); | |
BuildTwoClockwiseTriangles(currentTriangle, currentVertex, triangles); | |
currentVertex += 4; | |
currentTriangle += 6; | |
} | |
} | |
Mesh mesh = new Mesh(); | |
mesh.vertices = vertices; | |
mesh.triangles = triangles; | |
mesh.normals = normals; | |
mesh.uv = uvs; | |
filter.mesh = mesh; | |
collider.sharedMesh = mesh; | |
renderer.sharedMaterial = tileMaterial; | |
parentTransform.localPosition = new Vector3(-tileMap.Width * 0.5f, -tileMap.Height * 0.5f, 0f); | |
Debug.Log("Mesh built!"); | |
} | |
private void BuildTwoClockwiseTriangles(int currentTriangle, int currentVertex, int[] triangles) { | |
triangles[currentTriangle] = currentVertex; | |
triangles[currentTriangle + 1] = currentVertex + 2; | |
triangles[currentTriangle + 2] = currentVertex + 1; | |
triangles[currentTriangle + 3] = currentVertex + 2; | |
triangles[currentTriangle + 4] = currentVertex + 3; | |
triangles[currentTriangle + 5] = currentVertex + 1; | |
} | |
private void ApplyTexture(TileType type, int currentVertex, Vector2[] uvs) { | |
float x0, x1; | |
float y0, y1; | |
if (type == TileType.Grass) { | |
x0 = grassx; | |
y0 = grassy; | |
} else if (type == TileType.Hill) { | |
x0 = hillx; | |
y0 = hilly; | |
} else if (type == TileType.Tree) { | |
x0 = treex; | |
y0 = treey; | |
} else if (type == TileType.Water) { | |
x0 = waterx; | |
y0 = watery; | |
} else { | |
throw new UnityException("Invalid tile type: " + type); | |
} | |
x1 = x0 + w; | |
y1 = y0 + h; | |
uvs[currentVertex] = new Vector2(x0 / sheetW, y0 / sheetH); | |
uvs[currentVertex + 1] = new Vector2(x1 / sheetW, y0 / sheetH); | |
uvs[currentVertex + 2] = new Vector2(x0 / sheetW, y1 / sheetH); | |
uvs[currentVertex + 3] = new Vector2(x1 / sheetW, y1 / sheetH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment