- Home /
Can't generate 3D meshes via script
So, i created a really simple script (suck at programming) to generate a 3D mesh of an hexagon, and when i run it, it looks like the mesh gets flattened out, so instead of getting a 3D mesh, i get a 2D mesh of the hexagon. The fun part is that my script actually works, it generate a 3D mesh (At leats, that's what my mesh filter say; And my vertices are in the right position, i've checked hundreds of times), but for some reason it gets flattened on the y plane (Vertically). It looks like i can't extend my mesh's triagles further from the vector z of my trasform position. My project is set to 2D, but i tried to make a new 3D project to see if that was the problem. It wasn't. What did i get wrong? Here's some images, probably you'll understand better.
Here's what i get:
Here's what the mesh should look like(i've got 20 triangles and 12 vertices, the right number: Maybe the normals are a bit messed up):
Here's the script:
private void generateMesh(Material standard)
{
MeshRenderer hexRend = gameObject.AddComponent<MeshRenderer>();
MeshFilter hexFilt = gameObject.AddComponent<MeshFilter>();
Mesh hexMesh = hexFilt.mesh;
MeshCollider hexCol = gameObject.AddComponent<MeshCollider>();
hexCol.convex = true;
Rigidbody meshRig = gameObject.AddComponent<Rigidbody>();
meshRig.isKinematic = true;
hexRend.material = standardMaterial;
hexMesh.Clear(); hexMesh.ClearBlendShapes();
float scale = transform.localScale.x;
float xxx = scale + (scale / 1.4f);
Vector3[] corners = new Vector3[12];
// lato alto dell'esagono
corners[1] = new Vector3(scale, 0);
corners[2] = new Vector3(scale + (scale / 2), xxx / 2);
corners[3] = new Vector3(scale, xxx);
corners[4] = new Vector3(0, xxx);
corners[5] = new Vector3(-(scale / 2), xxx / 2);
for (int x = 6; x < corners.Length; x++)
{
corners[x] = corners[x - 6];
corners[x] += new Vector3(0, 0, scale);
}
hexMesh.vertices = corners;
hexMesh.triangles = new int[]
{
0, 4, 5,
0, 3, 4,
0, 1, 3,
1, 2, 3,
0, 6, 1,
1, 6, 7,
1, 2, 7,
2, 7, 8,
2, 3, 8,
3, 8, 9,
3, 9, 10,
3, 4, 10,
4, 10, 11,
4, 5, 11,
0, 5, 6,
5, 6, 11,
6, 10, 11,
6, 9, 10,
6, 7, 9,
7, 8, 9
};
hexMesh.RecalculateNormals();
}
I've tried to re-write the script completely so the hexagon get generated on the z plane, instead of the y plane, but it get flattened out on the z plane (So horizontally, instead of vertically; My hexagon got generated, but 2D on the z plane): I've tried to create a new unity project, to see if maybe i messed something up: I've tried to rotate the vertices, to they face the camera: I've tried to attach the script to a primitive 3D object, to see if maybe was the trasform of a new empty object (just because the project is set to 2D) to flatten the mesh: But nothing worked. I don't know what to do anymore. Please... Help. Thanks in advance.
btw i'm not an english native.
Answer by FortisVenaliter · Sep 19, 2017 at 11:13 PM
The z-scale on your transform is zero.
Well spotted in the screenshot :)
There's another thing strange. Why would one use the x scale to calculate the vertex positions? That would mean when you scale your object you actually get the square of the scale. For example if you have a scale of "2" you use a local position of "2" which is then scaled by the same factor of "2" so the vertex ends up at the worldspace position of 4 in relation fo the object origin.
However if the scale would be 4 you place the local vertex at 4 which is then scaled by 4 so you end up at 16. If you plan to use the object scale to define the size, just define your localspace vertices with constant values, just like the default cube mesh which uses +- 0.5 on all axis.
I use the x scale because i use another script to generate a grid, wich create a finite number of new game object and attach this script (that generate the mesh) to them. Just because i didn't wanted to create another public integer variable. It's a silly solution, i know. I am a noob at program$$anonymous$$g, i could have done so many things better, but i'm trying to learn alone without tutorials, so that's why. Didn't notice what i was doing, thank you, i appreciated the comment.
Oh my god... I swear, i've spent 2 days trying to figure this out, and the problem was just that little value... How stupid. Thank you, i think it would have take other two days to find out by myself.
Your answer
Follow this Question
Related Questions
Mesh-Cutting metal Jaws-of-life 0 Answers
Seing through triangles in runtime edited mesh 0 Answers
3d collider mesh to 2d collider mesh 0 Answers
3D Text Mesh - Display Glitch 0 Answers
TextMesh/3DText modify shape 0 Answers