- Home /
Can you generate a mesh at runtime?
I am a total beginner to unity but a veteran programmer with 10 years of C# and even more OO development. I am thinking about writing a 3D space-based game that involves the creation of an object that is an "engine" consisting of various parts. Once the object is created, I don't need to "render" the inner structure of the object in real time, the movement of a shell, a mesh, to cover the object is all I would need to move the object in 3D space.
I have scanned the documentation looking for something that tells me this is possible, but cannot find anything. Even Unity's video documentation says that all scene objects must be created in a 3D tool and then imported; yet, I see other videos that imply this kind of strategy is possible.
Is it possible for unity to generate a "mesh" within the game itself, or are all 3D objects prefabricated without any kind of dynamic way to create them?
I fully acknowledge that the actual mesh creation (the calculation of the location of all the triangles) would have be be written by me in some fashion, even if what I need is fundamentally possible.
Answer by DaveA · May 20, 2013 at 06:26 AM
You REALLY scanned the docs? Cuz here it is: http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html
Seriously I did. But when you know where to look, it's easy, when you don't ... it's hard to take the first step.
You can also use your title as a search string, just include the keyword Unity : http://www.google.com/search?btnG=1&pws=0&q=Unity+Can+you+generate+a+mesh+at+runtime
Answer by Bunnybomb7670 · May 20, 2013 at 06:05 AM
You can yes, you can procedurally generate a mesh, however It is not recommended for high geometry meshes because your going to have a very painful job of making it. To create a mesh, you do :
var newVertices : Vector3[]; // stores vertices of mesh
var newUV : Vector2[]; // stores UV ( texture coords )
var newTriangles : int[]; // stores triangles
function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
mesh.Clear(); // clear all the mesh data..
// Do some calculations...
mesh.vertices = newVertices; // assign the meshes vertices
mesh.uv = newUV; // assign the meshes UVs
mesh.triangles = newTriangles; // assign the meshes triangles
}
Looking at that code, you can see the basic structure of a mesh. The mesh basically stores an array of upto 65000 vertices and lots of triangles ( not sure of the limit ) aswell as UVs , Vertex Colors and much more.
Vertices accept a vector3 for a coordinate, triangles are integers, each integer is a vertex index, so for example , a Triangle array with the values 1 , 2 , 3 means it would create a triangle between the vertices array index 1 , 2 , 3 . You may need to do some messing around sometimes because depending on the direction which you define your triangles, clockwise or anticlockwise, your going to be flipping the triangles normal direction and possibly making it invisible.
Overall, I can say that if you want to make a high poly detailed mesh, I do recommend that you design it yourself in a 3D modelling program such as blender, It will take less time to do.
I plan on calculating all the points, then use a memento.
Answer by RhoneRanger · May 20, 2013 at 06:25 AM
Look into the Instantiate function, where you load in a mesh at a certain position.
GameObject go = Instantiate(MyMesh,myPosition,Quaternion.identity) as GameObject;
he means procedurally, "Even Unity's video documentation says that all scene objects must be created in a 3D tool and then imported"