- Home /
Generated Mesh Triangles not Being Made/Visible?
I wrote code for generating a mesh at specific coordinates as such:
FixedUpdate(){
//Lots of code that tracks mouse position
if (Input.GetMouseButtonDown (0)) {
createTileMesh (listIndex);
}
}
void createTileMesh(int tileIndex){
Mesh tileMesh = new Mesh ();
MeshFilter tileFilter;
MeshRenderer tileRender;
Vector3 debugTileCentre;
Vector3[] vertices = new Vector3[4];
int[] triangles = new int[6];
Vector3[] normals = new Vector3[4];
vertices = tileList [listIndex].VERTICES;
debugTileCentre = tileList [listIndex].TILECENTRE;
Debug.Log (tileIndex);
Debug.Log (debugTileCentre);
vertices [0].y = vertices [0].y + 1f;
vertices [1].y = vertices [1].y + 1f;
vertices [2].y = vertices [2].y + 1f;
vertices [3].y = vertices [3].y + 1f;
Debug.Log (vertices [0]);
Debug.Log (vertices [1]);
Debug.Log (vertices [2]);
Debug.Log (vertices [3]);
triangles[0] = 0;
triangles[1] = 2;
triangles[2] = 1;
triangles[3] = 1;
triangles[4] = 3;
triangles[5] = 2;
normals [0] = Vector3.up;
normals [1] = Vector3.up;
normals [2] = Vector3.up;
normals [3] = Vector3.up;
tileMesh.vertices = vertices;
tileMesh.normals = normals;
tileMesh.triangles = triangles;
tileMesh = GetComponent<MeshFilter> ().mesh;
tileRender = GetComponent<MeshRenderer> ();
MeshCollider mesh_collider = GetComponent<MeshCollider>();
//tileFilter = GetComponent<MeshFilter>().mesh;
Debug.Log ("Done Mesh");
}
For those of you wondering about the vertices[] array a sample output (I've tested with the debug log thing) would be something like:
//Sample values
vertices[0] = (1.8, 1, -22.3) //Top left corner
vertices[1] = (1.8, 1, -23.8) //Top right corner
vertices[2] = (0.3, 1, -22.3) //Bottom left corner
vertices[3] = (0.3, 1, -23.8) //Bottom right corner
Try as I might, no triangles in game view, or scene view from the bottom or top.
Answer by Cherno · Jul 23, 2015 at 06:43 AM
tileMesh = GetComponent<MeshFilter> ().mesh;
means that the tileMesh mesh, which you declared at the start at your method and to which you have assigned the created vertices, triangles and normals, gets overriden with the MeshFilter's own mesh... So the method's work is basically wasted ;) It has to be the other way around:
GetComponent<MeshFilter> ().mesh = tileMesh;
Right, there are other things that get created and are then replaced by something else, like the Vector3 array.
Also:
Using "Get$$anonymous$$ouseButtonDown" in FixedUpdate is a very bad idea. "XXXDown" events shouldn't be checked in FixedUpdate since FixedUpdate might not run every frame and it could simply miss the keypress. Use Update for such events.
If you fixed that mesh replacement (like Cherno said) you will notice that one of your triangle is facing up and the other is facing down. The first triangle is counter-clockwise and will face down, the other one is clockwise and will face up.
Your labelling "top left / right" seems to be wrong in relation to the actual axes. keep in $$anonymous$$d that -23.8 is smaller than -22.3. It looks like you use the x-axis as top-down axis and the z-axis as left-right axis. When viewed from the top (use use Vector3.up as normal so "+y" would be the front side) you labelled the more negative z position as "right" but it's actually left. You should draw your points on a piece of paper and work out the required indices from there. You need clockwise winding in Unity.
Also keep in $$anonymous$$d that the vertex positions are in local space. So they are offset from the $$anonymous$$eshFilters origin.
Thanks! This worked to fix my issue, I'm not sure why I thought it was a good idea to create a mesh then overwrite it after being made haha...
@Bunny8, thanks for the suggestions! $$anonymous$$y map extends from 0,0 to +x,-z so left is 0,0 and right is 0,-z; but nonetheless you were right about the winding order and triangle facing the wrong direction.
Your answer
Follow this Question
Related Questions
Save only visible part of mesh 1 Answer
Extruding faces of a mold. Need to properly assign vertices to triangles 0 Answers
Null Reference Exception Assigning Vertices to Mesh 0 Answers
Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer
Lighting up all triangles between two points in a mesh? 1 Answer