- Home /
Runtime mesh generation
Hy all,
I'm trying to create a mesh in runtime but I don't understand the Mesh class: How the vertices work for example.
I want to create a script that create's a mesh, and I can give them parameter like generatemesh(heights[], dimensionx, dimensiony) like a terrain. the heights should have different values only in one axis. so for example in every x=1 coord the height will be 5 and in every x=2 coord will be 10.
Answer by Jesse Anders · Jun 07, 2011 at 01:46 AM
The 'vertices' array contains the positions of the vertices in the mesh, and the 'triangles' array indexes into the 'vertices' array, with each set of 3 consecutive indices describing a triangle.
You can arrange the vertices however you want, but for something like this you'd typically arrange the vertices by row and then by column or vice versa. Generating the triangle indices then just involves a loop and a little arithmetic.
For examples of how to create meshes via code, check out the examples in the documentation for the Mesh class (if you haven't already), and the 'procedural mesh' examples on the Unity website.
Thanks for explaining, it's working now! But I got some new issue. I created square from 2 triangle, but the texture only appears on the first triangle. What should I do?
$$anonymous$$ake sure both triangles are wound the same way (I'm assu$$anonymous$$g you're not using a two-sided shader). If that doesn't solve the problem, try selecting the object and then exa$$anonymous$$ing the mesh in the scene view (it should appear in blue wireframe) to make sure there are actually two triangles.
I'm sure that everything is alright, if I remove material the pink non-materialed "texture" is okay. I thought I must do something with that "uv" but that is another thing what I don't understand.
Incorrect texture coordinates could be the problem (although one triangle of a quad being rendered completely invisible is a bit unusual). Typical texture coordinates for a quad would be (0, 0), (1, 0), (0, 1), and (1, 1), with the order depending on the order of the vertex positions.
Answer by insominx · Feb 08, 2013 at 09:54 PM
This video breaks the whole thing down pretty well: Unity Tips and Tricks
Your answer
Follow this Question
Related Questions
Change color of mesh triangle based on Y position in world space 1 Answer
Run Time Plastic Shrink/Wrap , Mesh Manipulation 0 Answers
Split single triangle mesh into 2 Equal Parts 0 Answers
Combining Mesh at Runtime via Script causes temporary FPS drop 1 Answer
Best way to transition one mesh to another with the option to change the target mesh 0 Answers