- Home /
Procedural cube code
I've been following this tutorial - http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/
My question is about the mesh.vertices and mesh.triangles arrays. Do I have to type them out in the way I have? It seems like a lot of typing.
Here's the code. Blame UnityAnswers + Monodevelop for the spacing.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (MeshCollider))]
[RequireComponent (typeof (MeshFilter))]
[RequireComponent (typeof (MeshRenderer))]
public class MyCube : MonoBehaviour {
public float cubeX = 1f;
public float cubeY = 1f;
public float cubeZ = 1f;
public float sections = 1f;
public void Rebuild()
{
MeshFilter meshFilter = GetComponent<MeshFilter>();
// FACE 1 VERTS
Vector3 p0 = new Vector3(0, 0, 0);
Vector3 p1 = new Vector3(cubeX, 0, 0);
Vector3 p2 = new Vector3(cubeX, cubeY, 0);
Vector3 p3 = new Vector3(0, cubeY, 0);
// FACE 2 VERTS
Vector3 p4 = new Vector3(cubeX, 0, 0);
Vector3 p5 = new Vector3(cubeX, 0, cubeZ);
Vector3 p6 = new Vector3(cubeX, cubeY, cubeZ);
Vector3 p7 = new Vector3(cubeX, cubeY, 0);
Mesh mesh = meshFilter.sharedMesh;
if (mesh == null){
meshFilter.mesh = new Mesh();
mesh = meshFilter.sharedMesh;
}
mesh.Clear();
// Returns a copy of the vertex positions or assigns a new vertex positions array.
mesh.vertices = new Vector3[]{
p0,p1,p2,
p0,p2,p3,
p2,p1,p3,
p0,p3,p1,
p4,p5,p6,
p4,p6,p5,
p4,p6,p7,
p4,p7,p6
};
// Array containing all triangles in the mesh.
mesh.triangles = new int[]{
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17,
18,19,20,
21,22,23
};
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
}
}
No, I'm pretty sure I can blame you for the spacing. It's not that hard to fix. In fact, I'm doing it for you now.
Just to be clear, you need 24 entries in mesh.vertices for a cube (4 per side, because of UVs and normals), though you only need 8 unique values for the vertices. You also should have 36 entries in mesh.triangles.
@JaredThomson: Uhm, what are you talking about? Have you actually seen the original post? It was unreadable since you had to scroll down 4 or 5 pages. The "bla$$anonymous$$g" wasn't about the code itself. You can even write your code in a single line. It was about posting code here for others. In case you haven't realised: The question you see at the moment has been fixed by syclamoth.
We're doing a lot of moderating on this Q&A site and you just woke a dead question (which actually has been answered in the comments above) for no reason.
@JaredThomson - Nobody $$anonymous$$ds beginners. What I (and many other users) $$anonymous$$d is people that ask for (free) help with their code without having a bit of decency to learn and follow the rules regarding formatting their question, rather than expecting "moderators" (i.e. other users) to fix it for them.
@JaredThomson - I'm aware that this thread has already gone off-topic somewhat, but I wanted only to touch on one interesting point you raised...
You tutor people in making Unity games "who sometimes don't even know how to use a computer.". Unity has a reputation for being very accessible, and creating games is a great way to introduce people to program$$anonymous$$g (especially to engage young people in computer science). However, the fact that Unity is often people's first experience of program$$anonymous$$g, and also that there are a great many "casual" Unity developers is somewhat of both a blessing and a curse. $$anonymous$$any of the more experienced developers have left the community in frustration at the "dumbing-down" of the quality of questions and answers here recently. It's not the "newbies" fault - everybody was a beginner once - it's just that there is a very diverse range of backgrounds and abilities between Unity users, and those more experienced programmers who rely on Unity to make a living often find their requests for very specific technical help (that might be critical in the release of their game, and hence their income) lost in a mass of, what are essentially basic program$$anonymous$$g questions.
I'd argue that one of the most important things you can learn as a rookie programmer these days is: "When you get stuck, how and where should you ask for help?". $$anonymous$$ost internet fora have fairly common standards regarding search before you post, include a clear description reproducing the problem, don't use CAPS or abusive language etc. Then, each forum will have its own standards on top of that. On Unity Answers there is a clear FAQ and even tutorial video linked in the sidebar which explain the standards expected here, and clear code formatting is one of them.
That's all - please do encourage your students to post their Unity questions here next time they get stuck. But make sure they read the FAQ first :)