- Home /
Procedurally generated mesh JS - explain the error please
Hey guys, I decided to try my luck at procedurally generating meshes. I've tried following this guide, but since it's in C# I wanted to translate it into JS. Since the general idea translates I thought I was managing quite well, but there are some compiler errors I keep getting that I cannot explain.
It's probably something extremely simple and stupid I'm doing wrong/overlooking and might very well have to do with me not understanding built in arrays very well (finding unity related documentation on them is proving difficult)
TBH I'm just a noob when it comes to scripting who helps out the even lesser noobs around here ^.^ Also, it's late, I'm tired and I need heeeelp :p I've added text explaining the code and pointing out the errors.
@MenuItem ("GameObject/Create Other/Tetrahedron") //add it to the menu
static function CreateTetrahedron () {
var Tetrahedron : GameObject = new GameObject ("Tetrahedron"); //create an empty gameobject with that name
Tetrahedron.AddComponent(MeshFilter); //add a meshfilter
var p0 : Vector3 = Vector3(0,0,0); //the four points that make a tetrahedron
var p1 : Vector3 = Vector3(1,0,0);
var p2 : Vector3 = Vector3(0.5f,0,Mathf.Sqrt(0.75f));
var p3 : Vector3 = Vector3(0.5f,Mathf.Sqrt(0.75f),Mathf.Sqrt(0.75f)/3);
var newVertices : Vector3[] = new Vector3[] { //the non-shared vertices (line 14)
p0,p1,p2,p0, //Assets/Editor/Tetrahedron.js(14,50): BCE0044: expecting (, found '['.
p2,p3,p2,p1, //Assets/Editor/Tetrahedron.js(15,19): BCE0044: expecting :, found ','.
p3,p0,p3,p1
};
var newTriangles : int[] = new int[]{ //the triangle faces the points make up
0,1,2,
0,2,3,
2,1,3,
0,3,1
};
var newMesh : Mesh = new Mesh (); //create a new mesh, assign the vertices and triangles
newMesh.vertices = newVertices;
newMesh.triangles = newTriangles;
newMesh.RecalculateNormals(); //recalculate normals, bounds and optimize
newMesh.RecalculateBounds();
newMesh.Optimize();
Tetrahedron.GetComponent(MeshFilter).mesh = newMesh; //assign the created mesh as the used mesh
}
Answer by loramaru · May 05, 2011 at 03:42 AM
The problem is that you're using the C# array initialization. Here's what you want for JavaScript.
var newVertices : Vector3[] = [ p0,p1,p2,p0, p2,p3,p2,p1, p3,p0,p3,p1 ];
var newTriangles : int[] = [
0,1,2, 0,2,3, 2,1,3, 0,3,1 ];
Also you had one more error that crops up once those are fixed; so here's a bonus fix. :)
(Tetrahedron.GetComponent(MeshFilter) as MeshFilter).mesh = newMesh;
Haha , proves how blind you become when you stare too long at something. Thanks a lot :-)
Also you mixed up the vertices from the second example with the triangles from the first. Your code will generate a flat triangle. You really want... var newTriangles : int[] = [0,1,2,3,4,5,6,7,8,9,10,11];
Yes! Thank you! I got it to work :D thank you very very very very much!
How would I make the mesh visible, i couldn't figure it out
How would I make the pyramid visible? I've tried adding a renderer but I couldn't get it to work