- Home /
Weird triangle indexing problem
I have a GameObject called "Ground", which has an empty Mesh Filter, Mesh Renderer and Mesh Collider attached, plus the following script. It creates a new terrain-like mesh - instead of quads, it is arranged in a hex-like formation. Then it allows you to click on the mesh with a mouse, and it prints the triangle index to the console. All fine - except that the triangle index reported seems to be wrong. It should run from 0 at the lower left, up to 19 at the lower right, then start the next row at 20 and so on. But instead, it's reported as 0 in the lower right, 19 in the lower left, then skip 20-39 and the next row begins with 40.
The really odd thing is, if I create one less row or column of triangles than I should, the triangle index reported is correct (see the note attached to the line below). I can't work out how this is happening?
EDIT: I just tried printing both the contents of tri[] and ground.triangles[] in order, just after the line "ground.triangles = tri"... and they were in a completely different order to each other! How on earth can that happen?
var ground : Mesh; var groundCollider : MeshCollider; var cam : Camera; var width : int = 10; var depth : int = 10; var verts : int; var tris : int; //-------------------------------------------------------------------------------------------------------- function Awake() { ground = GetComponent(MeshFilter).mesh; groundCollider = GetComponent(MeshCollider); cam = GameObject.Find("/Main Camera").GetComponent(Camera); verts = (width+1)*(depth+1); tris = width*depth*2; } //-------------------------------------------------------------------------------------------------------- function Start() { Build(); } //-------------------------------------------------------------------------------------------------------- function Build() {
var vert : Vector3[] = new Vector3[verts];
var uvA : Vector2[] = new Vector2[verts];
var uvB : Vector2[] = new Vector2[verts];
var norm : Vector3[] = new Vector3[verts];
var col : Color[] = new Color[verts];
var tri : int[] = new int[tris*3];
//----------------------------------------------------------------
var n : int;
for (var z : int=0;z<=depth;z++) {
for (var x : int=0;x<=width;x++) {
n = GridToVert(x,z);
if (z%2 == 0) { // z is even
vert[n] = Vector3( x, 0, z );
uvA[n] = Vector2( x, z );
} else {
vert[n] = Vector3( x+0.5, 0, z );
uvA[n] = Vector2( x+0.5, z );
}
}
}
//----------------------------------------------------------------
n=0;
for (z=0;z<depth;z++) { // change to "depth-1" and everything works right
for (x=0;x<width;x++) { // OR change to "width-1" and everything works right
if (z%2 == 0) { // z is even
tri[n+0] = GridToVert(x,z);
tri[n+1] = GridToVert(x,z+1);
tri[n+2] = GridToVert(x+1,z);
tri[n+3] = GridToVert(x+1,z);
tri[n+4] = GridToVert(x,z+1);
tri[n+5] = GridToVert(x+1,z+1);
} else {
tri[n+0] = GridToVert(x,z);
tri[n+1] = GridToVert(x,z+1);
tri[n+2] = GridToVert(x+1,z+1);
tri[n+3] = GridToVert(x,z);
tri[n+4] = GridToVert(x+1,z+1);
tri[n+5] = GridToVert(x+1,z);
}
n=n+6;
}
}
print(n);
//----------------------------------------------------------------
ground.Clear();
ground.vertices = vert;
ground.uv = uvA;
ground.triangles = tri;
ground.RecalculateNormals();
ground.Optimize();
groundCollider.sharedMesh = ground;
} //-------------------------------------------------------------------------------------------------------- function GridToVert( x : int, z : int ) { if (x<0 || x>width+1 || z<0 || z>depth+1) { return float.NaN; } else return (z*(width+1)) + x; } //-------------------------------------------------------------------------------------------------------- function Update() {
if (Input.GetMouseButtonDown(0)) {
// Get ground position of mouse
var ray : Ray = cam.ScreenPointToRay( Input.mousePosition );
var hit : RaycastHit;
if( Physics.Raycast(ray, hit) ) {
var t = hit.triangleIndex;
print(t);
}
}
}
Answer by robinking · Feb 07, 2011 at 10:00 PM
I've discovered the culprit, it's:
ground.Optimize();
Is it a good idea for me to keep this line in, or can I do without it? (iPhone)
I don't use it myself; it never seems to make any difference with the generated meshes I've done.
Your answer
Follow this Question
Related Questions
Trouble recalculating 3D mesh's triangles after deleting verts 2 Answers
How to colorize a face of a Mesh 0 Answers
Duplicate and separate triangles from a mesh 1 Answer
Procedural Cylinder Generation 2 Answers
Create the visual spring in Unity? 3 Answers