- Home /
Creating a Pentahedron (a 5 sided shape) Mesh procedurally
I try to create a 5 sided shape mesh procedurally in a JS script, then i dragged and dropped it in the Main Camera so i can see it, here's the code:
function Start() {
var tmpobj: GameObject = new GameObject();
tmpobj.AddComponent(MeshFilter);
tmpobj.AddComponent(MeshRenderer);
var verts: Vector3[] = new Vector3[5];
var normals: Vector3[] = new Vector3[5];
var uv: Vector2[] = new Vector2[5];
var tri: int[] = new int[12]; //3 vertices * 4 triangles =12
var mf: MeshFilter = GetComponent(MeshFilter);
//vertices positioning of the points P1,P2,P3,P4,P5
verts[0] = new Vector3(0, 0, 0); //P5
verts[1] = new Vector3(-5, 0, -5); //P1
verts[2] = new Vector3(-5, 0, 5); //P2
verts[3] = new Vector3(5, 0, 5); //P3
verts[4] = new Vector3(5, 0, -5); //P4
for (i = 0; i < normals.Length; i++) {
normals[i] = Vector3.up;
}
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
uv[4] = new Vector2(0, 0);
//triangles (clockwise)
tri[0] = 0;
tri[1] = 1;
tri[2] = 2;
tri[3] = 0;
tri[4] = 2;
tri[5] = 3;
tri[6] = 0;
tri[7] = 3;
tri[8] = 4;
tri[9] = 0;
tri[10] = 4;
tri[11] = 1;
var mesh: Mesh = new Mesh();
mesh.name = "Roof_01";
mesh.vertices = verts;
mesh.triangles = tri;
mesh.uv = uv;
mesh.normals = normals;
mesh.RecalculateNormals();
mf.mesh = mesh;
}
I don't know what i'm doing wrong and i can't see the mesh. The thing is it's my first time using Meshes by code and i'm not familiar with them yet. I get that exception:
"MissingComponentException: There is no 'MeshFilter' attached to the "Main Camera" game object, but a script is trying to access it. You probably need to add a MeshFilter to the game object "Main Camera". Or your script needs to check if the component is attached before using it. Roof.Start () (at Assets/Roof.js:79)"
Any idea why i can't see the Mesh? Thanks in advance.
Answer by Wolfram · Jun 15, 2012 at 08:32 PM
Replace GetComponent(MeshFilter) with tmpobj.GetComponent(MeshFilter), you're trying to find the MeshFilter on your current object.
Or, slightly more efficient, remove that line altogether, and replace the line at the beginning of your script with var mf: $$anonymous$$eshFilter = tmpobj.AddComponent($$anonymous$$eshFilter);
Your answer
Follow this Question
Related Questions
Determine whether or not model is "inside out" 2 Answers
Combining meshes (different materials) together for rotation/translation 1 Answer
Custom Mesh UV Problem 1 Answer
Mesh return vs out Mesh 2 Answers
Strange shading on procedural mesh 0 Answers