Hello! I am attempting to display a 2D animation, held in a 2D array, by animating vertex color of a procedurally generated mesh. I squash the 2D array into a 1D array before passing it to the vertex colors.
The result is very warped – like the animation is ripped in half and then offset to the edges.
It should look like this (very simplified):
[32644-derp_base.jpg*|32644]
But in Unity it looks like this:
[32645-derp_after.jpg*|32645]
Clearly something is being lost in translation between the 2D array of animated cells and the way I am passing that data to the vertex colors.
Everything “works” in terms of the animation playing based on what the abstract 2D cells are doing, but the visual is skewed and ripped in half. I tried just importing a mesh from Maya instead of doing this procedural approach, but the results were even worse – noise basically.
Below is the code I am using.
The mesh generation is from Quill18’s tutorial:
public int size_x = 150;
public int size_z = 150;
public float tileSize = 1.0f;
public int tileResolution;
float MeshColorTest = .01f;
// Use this for initialization
void Start () {
BuildMesh ();
}
public void BuildMesh(){
//Generate Mesh Data
int numTiles = size_x * size_z;
int numTris = numTiles * 2;
int vsize_x = size_x + 1;
int vsize_z = size_z + 1;
int numVerts = vsize_x * vsize_z;
// Generate the mesh data
Vector3[] vertices = new Vector3[ numVerts ];
Vector3[] normals = new Vector3[numVerts];
Vector2[] uv = new Vector2[numVerts];
int[] triangles = new int[ numTris * 3 ];
int x, z;
for(z=0; z < vsize_z; z++) {
for(x=0; x < vsize_x; x++) {
vertices[ z * vsize_x + x ] = new Vector3( x*tileSize, 0, -z*tileSize );
normals[ z * vsize_x + x ] = Vector3.up;
uv[ z * vsize_x + x ] = new Vector2( (float)x / size_x, 1f - (float)z / size_z );
}
}
Debug.Log ("Done Verts!");
for(z=0; z < size_z; z++) {
for(x=0; x < size_x; x++) {
int squareIndex = z * size_x + x;
int triOffset = squareIndex * 6;
triangles[triOffset + 0] = z * vsize_x + x + 0;
triangles[triOffset + 2] = z * vsize_x + x + vsize_x + 0;
triangles[triOffset + 1] = z * vsize_x + x + vsize_x + 1;
triangles[triOffset + 3] = z * vsize_x + x + 0;
triangles[triOffset + 5] = z * vsize_x + x + vsize_x + 1;
triangles[triOffset + 4] = z * vsize_x + x + 1;
}
}
Debug.Log ("Done Triangles!");
//Create a new Mesh and populate with the data
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
//Assign our mesh to our filter/renderer
MeshFilter mesh_filter = GetComponent<MeshFilter>();
//MeshCollider mesh_collider = GetComponent<MeshCollider>();
mesh_filter.mesh = mesh;
}
The 2D animation, held as values from 0-1 in abstract “cells” in a 2D array, is squished into a 1D array with this code:
public void convert2Dto1D(){
double value;
for(int i = 0; i < columns; i++){
for(int j = 0; j < rows; j++){
value = board[i,j].V;
RD_VertexColorsArray[i+columns*j] = value;
}
}
}
Then I update the mesh’s vertex color data with the 1D array’s values:
void UpdateGridGraphics(){
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Color[] colors = new Color[vertices.Length];
int i = 0;
while (i < vertices.Length - 301) {
float value = (float)game_grid.RD_VertexColorsArray*;*
_ colors = new Color(1.0f - value, 1.0f - value, 1.0f - value,1.0f);_
* i++;*
* }*
* mesh.colors = colors;*
* }*
Thanks a lot for taking a look!
*
*