Drawing a Cube Mesh from Scratch

var cubeVertices = [
	Vector3(0, 0, 0),
	Vector3(10, 0, 0),
	Vector3(0, 10, 0),
	Vector3(10, 10, 0),
	Vector3(0, 0, 10),
	Vector3(10, 0, 10),
	Vector3(0, 10, 10),
	Vector3(10, 10, 10)
];

var cubeTriangles = [
	0, 1, 2,
	0, 2, 3,
	1, 0, 4,
	1, 5, 4,
	1, 5, 3,
	7, 5, 3,
	7, 2, 3,
	7, 6, 2,
	4, 6, 2,
	4, 0, 2,
	4, 5, 7,
	4, 6, 7
];

function Start () {
	var mesh = new Mesh ();
	GetComponent (MeshFilter).mesh = mesh;
	mesh.vertices = cubeVertices;
//	mesh.uv = cubeUV;
	mesh.triangles = cubeTriangles;
}

ok, now tell me what I’m doin’ wrong. :roll:

(More information?)
Okay, so, I’m looking at the API docs on the Mesh class. Some parts are pretty decent, and others a little on the sparse side. I think my problem might have to do with normals (what 3D problem doesn’t?), but I’m not sure how to set those up. It says it’s a Vector3, but then it wants me to use a quaternion? I’m like, wtf are these, anyway? They’re supposed to point… outwards. That’s all I know. :I

Oh, and I got all those numbers by takin’ out a notebook drawin’ a cube and putting 3D coordinates to it, then, well, following the directions I saw in the API docs… Just on treemeat.

Oh, and before someone comes up with something dense, like, ‘what’s wrong with the cube object’, there’s plenty wrong with that. If I want to make a bunch of Blockular things (things made out of discrete cubes, and kept track of internally as voxels), then having a truckload (think of a big number, then double it) of objects in the game is not gonna help matters. However, if I just make the blockular objects out of triangles and some clever scriptin’ functions, then, well, we might have the start of somethin’ Legogogular.

Alright, after some research, I realized that my mesh wasn’t being displayed because the object I was attaching it to didn’t have a renderer. Now that I can actually see my triangle(s), I’m a little confused. At one point in time I saw a black cube, but now all I see is a single triangle. When I inspect my variable in the editor (I should make it private later, I know), I see that it only sees the first triplet (corresponding to the triangle), thus, only displays a single triangle. I’m not sure Unity is being consistent, because it’s almost exactly the same code as before.

Also, I checked back on my notes, and 0,2,3 (second triplet in cubeTriangles) should be 1,2,3.

Hmm, by making the variable private, I think I fixed it. It displays my cube perfectly, it’s beautiful!

But I’m wondering if anyone has run across inconsistency in the editor along these lines before? Any other pitfalls I should be aware of?

It’s not inconsistent: variables in the inspector override whatever you have them set to in the script…otherwise, every time you saved the script, your variables would be reset to what’s in the script, which would be practically unusable. Private variables always use what’s in the script.

–Eric

That makes more sense, and I had that feeling. Now I need to worry about normals, since only some of my faces are showing. I guess I’ll need to do some experimenting, some for() loops, and a little bit of math.

Thanks for confirming my suspicions.

Faces not appearing is about winding order, actually…normals only have to do with lighting.

–Eric

Yeah you may want to double check how you declare your triangles(vertex order) to see why some aren’t displaying.

I have actually created a cube in script from scratch before too, not as easy as it may sound.