public void UpdateMesh( Mesh _mesh )
{
int gridCount = 0;
if ( updateFlags != UpdateFlags.None ) {
for ( int i = 0; i < tileMap_.grids.Length; ++i ) {
int id = tileMap_.grids[i];
if ( id != -1 ) {
++gridCount;
}
}
}
// ========================================================
// Update Vertex
// ========================================================
if ( (updateFlags & UpdateFlags.Vertex) != 0 ) {
int vertexCount = gridCount * 4;
Vector3[] vertices = new Vector3[vertexCount];
// init
float width = tileMap_.col * tileMap_.tileWidth;
float height = tileMap_.row * tileMap_.tileHeight;
float halfWidth = width * 0.5f;
float halfHeight = height * 0.5f;
float offsetX = 0.0f;
float offsetY = 0.0f;
//
switch ( anchor ) {
case Anchor.TopLeft : offsetX = -halfWidth; offsetY = -halfHeight; break;
case Anchor.TopCenter : offsetX = 0.0f; offsetY = -halfHeight; break;
case Anchor.TopRight : offsetX = halfWidth; offsetY = -halfHeight; break;
case Anchor.MidLeft : offsetX = -halfWidth; offsetY = 0.0f; break;
case Anchor.MidCenter : offsetX = 0.0f; offsetY = 0.0f; break;
case Anchor.MidRight : offsetX = halfWidth; offsetY = 0.0f; break;
case Anchor.BotLeft : offsetX = -halfWidth; offsetY = halfHeight; break;
case Anchor.BotCenter : offsetX = 0.0f; offsetY = halfHeight; break;
case Anchor.BotRight : offsetX = halfWidth; offsetY = halfHeight; break;
default : offsetX = 0.0f; offsetY = 0.0f; break;
}
//
int ii = 0;
for ( int rr = 0; rr < tileMap_.row; ++rr ) {
for ( int cc = 0; cc < tileMap_.col; ++cc ) {
//
int i = rr * tileMap_.col + cc;
if ( tileMap_.grids[i] == -1 )
continue;
//
float curX = cc * tileMap_.tileWidth;
float curY = rr * tileMap_.tileHeight;
// build vertices & normals
for ( int r = 0; r < 2; ++r ) {
for ( int c = 0; c < 2; ++c ) {
int j = r * 2 + c;
float x = curX - halfWidth + c * tileMap_.tileSheet.tileWidth - tileMap_.tileOffsetX;
float y = -curY + halfHeight - r * tileMap_.tileSheet.tileHeight + (tileMap_.tileSheet.tileHeight - tileMap_.tileHeight) + tileMap_.tileOffsetY; // last thing adjust mesh start from left-bottom
// build vertices and normals
vertices[4*ii+j] = new Vector3( x - offsetX, y + offsetY, 0.0f );
// DELME {
// // build vertices and normals
// switch ( plane ) {
// case Plane.XY:
// vertices[4*ii+j] = new Vector3( x - offsetX, y + offsetY, 0.0f );
// break;
// case Plane.XZ:
// vertices[4*ii+j] = new Vector3( x - offsetX, 0.0f, y + offsetY );
// break;
// case Plane.ZY:
// vertices[4*ii+j] = new Vector3( 0.0f, y + offsetY, x - offsetX );
// break;
// }
// } DELME end
}
}
++ii;
}
}
//
_mesh.vertices = vertices;
}
// ========================================================
// Update UV
// ========================================================
if ( (updateFlags & UpdateFlags.UV) != 0 ) {
int vertexCount = gridCount * 4;
Vector2[] uvs = new Vector2[vertexCount];
int ii = 0;
for ( int i = 0; i < tileMap_.grids.Length; ++i ) {
if ( tileMap_.grids[i] == -1 )
continue;
// get uv
int sheetID = tileMap_.grids[i];
Rect uv = tileMap_.tileSheet.GetTileUV (sheetID);
float xStart = uv.x;
float yStart = uv.y;
float xEnd = uv.xMax;
float yEnd = uv.yMax;
//
uvs[4*ii+0] = new Vector2 ( xStart, yEnd );
uvs[4*ii+1] = new Vector2 ( xEnd, yEnd );
uvs[4*ii+2] = new Vector2 ( xStart, yStart );
uvs[4*ii+3] = new Vector2 ( xEnd, yStart );
++ii;
}
_mesh.uv = uvs;
}
// ========================================================
// Update Color
// ========================================================
if ( (updateFlags & UpdateFlags.Color) != 0 ) {
//
int vertexCount = gridCount * 4;
Color[] colors = new Color[vertexCount];
for ( int i = 0; i < vertexCount; ++i ) {
colors[i] = color_;
}
_mesh.colors = colors;
}
// ========================================================
// Update Index
// ========================================================
if ( (updateFlags & UpdateFlags.Index) != 0 ) {
//
int indexCount = gridCount * 6;
int[] indices = new int[indexCount];
//
for ( int i = 0; i < gridCount; ++i ) {
int vert_id = 4 * i;
int idx_id = 6 * i;
// build indices
indices[idx_id + 0] = vert_id + 0;
indices[idx_id + 1] = vert_id + 1;
indices[idx_id + 2] = vert_id + 2;
indices[idx_id + 3] = vert_id + 2;
indices[idx_id + 4] = vert_id + 1;
indices[idx_id + 5] = vert_id + 3;
}
//
_mesh.triangles = indices;
}
// // TODO: update bounding rect ( for seeing )
// NOTE: though we set updateFlags to None at exPlane::LateUpdate,
// the Editor still need this or it will caused editor keep dirty
updateFlags = UpdateFlags.None;
}