- Home /
Generate and extrude a mesh
Hello, I'm trying to create a script that reads an array of ints and, based on the numbers on it, generates a mesh and extrudes it when the number is not '0'.
For instance, if the array was: "1111110001101011000111111"
or (for a more visual-friendly example):
1 1 1 1 1
1 0 0 0 1
1 0 1 0 1
1 0 0 0 1
1 1 1 1 1
The result I would want is this: http://oi49.tinypic.com/mbmn2h.jpg
I took a look in the procedural example but it didn't helped me much because it extrudes a mesh that is already shaped on the desired form. I'm trying to do a more generic mesh extruder. It simply reads the vector, creates a squared shaped mesh and extrudes it based on the numbers.
-edit- Any ideas on how to aproach this issue?
Thanks in advance.
The big issue is that you have to create vertices, but you extrude faces... In other words, each number here would represent 4 vertices.
Each set of 4 vertices would make a square. If you want it in a perfect square, you can use array.length to get the length and square root it until you get a non-whole number, then go by rows of the previous whole-number. Hope that helped at least a little.
Is there a way to know in which oreder I'm reading the vertices? For instance if I create the first 4: . . . . How can I know that the next two I create will be concted with the rightmost two?
Answer by Fattie · Jun 08, 2012 at 08:11 AM
There is an unbelievably simple way to do this.
It is almost impossible you would go about actually building mesh on the fly to achieve this incredibly simple need.
Here is the method:
1) Make yourself A BOX.
2) If there is a "1" there, use this Unity command: "Instantiate"
I hope this has helped.
I was actually thinking about it. We have a script that makes that to create the level. I could just switch the prefabs for ground and walls to very low-poly ones and intantiate them. It would still be a little heavier then what I was looking for, but it is simple enough and does the job.
I think I will go with that aproach.
Thank you all for the help. All the answers were very helpfull.
Answer by Bunny83 · Jun 07, 2012 at 11:15 PM
It depends on how optimised you want it. If you "extrude" square by square you will have a lot unnecessary triangles in between. It would be much simpler to create the mesh from scratch based on your array.
There is no easy solution for this problem / task. You have to do it yourself. Don't ask others to write scripts for you. If you have a specific problem, ask a question.
If you want to do a simple square by square extrude, just do it like this:
I guess you have this plane mesh you've showed in the left picture:
go through all squares in the mesh (2 triangles)
use the number from your array to determine if you want to extrude the triangle or not
delete the 2 triangles of the square (remove the 6 indices).
Duplicate the 4 vertices and move them upwards.
Create the vertices for the sides (the same positions as the others but with other normal vectors)
create your 10 triangles (5 x 2)
But again, it's propably even simpler to create it from scratch based on your array.
Thanks, Reading again my question I guess it may lead to think that I'm asking for a script, although I'm not. $$anonymous$$y question is to know some aproach to the issue (wich fortunately you gave!). About your answer: to create an triangle is it something simmilar to OpenGL where you create, for instance 3 vertices an it closes a triangle? Or there are some function that creates a face based on the vertices you pass as reference?
I know you said you are about to create a script, but a lot people here just expect to get a complete script they can copy & paste ;)
You should first get familiar with Unity's $$anonymous$$esh class. Unity uses indexed triangles exclusively. That means you have one vertex array and one index array. The vertex array is made up of several arrays in Unity. They are vertices, normals, tangents, uv, uv2 and colors. The index array is the triangles array. You don't need all vertices arrays, only those that are required by the shader you want to use.
The index array contains integer values that are indices into the vertex array. The indices count is always a multiple of 3 because you need always 3 vertices to form a triangle ;)
So if you have a triangles array with these values: 0,1,2, 0,2,3
It will create 2 triangles which share the vertex 0 and 2, so they form a quad if the vertices are aranges in a square.