Culling/Indices: Drawing a Rectangle

Sorry to ask about something so simple but I’ve been buggering around with this for an hour now and I can’t get the right indices or normals to show a textured rectangle.

This will draw the bottom right of the triangle but leaves the top left, I’ve looked behind it and it’s not even the wrong way and being culled it just isn’t being drawn:

var test;
var mesh:Mesh;
var width:float;
var height:float;
var grassTex:UnityEngine.Material;
function Start() {
	mesh = new Mesh();
	
	var verts : Vector3[] = new Vector3[4];
	verts[0] = new Vector3(-width/2.0, 0, -height/2.0);
	verts[1] = new Vector3(-width/2.0, 0,  height/2.0);
	verts[2] = new Vector3(width/2.0,  0, -height/2.0);
	verts[3] = new Vector3(width/2.0,  0,  height/2.0);
	mesh.vertices = verts;
	
	var indices: int[] = new int[6];
	indices[0] = 0;
	indices[1] = 2;
	indices[2] = 3;
	indices[3] = 0;
	indices[4] = 3;
	indices[1] = 1;
	mesh.triangles = indices;
	
	var norms:Vector3[] = new Vector3[4];
	for (var i:int = 0; i < norms.Length; i++)
		norms[i] = Vector3.up;
	mesh.normals = norms;
	
	var uvs:Vector2[] = new Vector2[4]; 
	uvs[0] = new Vector2(0,0);
	uvs[1] = new Vector2(1,0);
	uvs[2] = new Vector2(0,1);
	uvs[3] = new Vector2(1,1);
	mesh.uv = uvs;
		
	GetComponent(MeshFilter).mesh = mesh;
}

function Update () {
	Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, grassTex, 0);
}

That was mashed together from old forum posts, I’ve tried different orders of indices but they haven’t worked.
To me the order should be 0,1,2, 1,3,2 as that would be clockwise I believe? But it doesn’t work.

Could someone help me out? Thanks.

it has got to be the order of the indices. If you see one triangle then you look at the other side and you see the other triangle. To make sure it is the indices you can try a shader that doesn’t cull backfaces i.e. with Cull Off
just guessing, but you can try:

	indices[0] = 1;
	indices[1] = 2;
	indices[2] = 0;
	indices[3] = 1;
	indices[4] = 3;
	indices[1] = 2;

you can find a simple example for a quad like this here

Take a look at the index numbers of the “indices” array when you assign the triangles. As an aside (which would also prevent that from happening) you can declare arrays like this:

var indices = [1, 2, 0, 1, 3, 2];

Same with the UVs, etc.:

var uvs = [Vector2(0,0), Vector2(1,0), Vector2(0,1), Vector2(1,1)];

Actually, if you’re not going to alter the UVs later, then you don’t even need to declare a separate array:

mesh.uv = [Vector2(0,0), Vector2(1,0), Vector2(0,1), Vector2(1,1)];

–Eric

It’s doing this now, I don’t see what’s wrong with the order.
Do the uv’s need to somehow match up to the order at which the triangles are drawn?

Thanks for the help, haven’t slept in 30 hours so it’s getting pretty frustrating.

	indices[0] = 1;
	indices[1] = 2;
	indices[2] = 0;
	indices[3] = 1;
	indices[4] = 3;
	indices[**1**] = 2;  <---------!!!!!

–Eric

I’m actually angry, why would I do that.
Thank you very much!

30 hours without sleep will definitely do that to you, speaking from experience. :wink:

–Eric