Sponsored By

Featured Blog | This community-written post highlights the best of what the game industry has to offer. Read more like it on the Game Developer Blogs.

Manipulating Meshes in Unity3D

The ability to amend the shape of a 3D object vertex by vertex gives us the power to dynamically change objects.

Deniz Opal

September 22, 2012

5 Min Read

Manipulating Meshes in Unity3D

As its name suggests Unity3D is used to create 3D games. Its true to say the 3D items used in the games are created in external modeling tools, however, as Unity3D develops, this is becoming less and less the case. Firstly the terrain editor is an example of an entire landscape, complete with vegetation, built entirely inside the editor. The C# APIs also allow us to manipulate the shape of 3D items in various ways.

In order to import a 3D object into Unity3D we can simply drag and drop it into the Project pane. There are various types that are supported. FBX is particularly recommended so if your modeling software can export to FBX its considered best practice to do so. When you build the game Unity3D will convert the file into an internal and optimized format, so there is no difference in performance, but it helps to keep away from exotic file formats to make the engines job easier.

Back in the old days of game development we would have had to create the 3D objects programmatically, painstakingly calculating the coordinates of each vertex. Today, the modeling tools at hand make it easier for programmers. However, the ability to amend the shape of a 3D object vertex by vertex gives us the power to dynamically change objects, therefore effects such as melting an object, flowing a liquid or vibrating a string can be programmed in a manner that would interact with its environment and be effected by such things as gravity, wind, and the surface under it. 


At the core of a 3D asset these is the mesh. A mesh is a collection of 3D coordinates (vertices) grouped together in  triangle arrays to form the 3D shape. 

If you create a new cube object in a Unity3D project, and select it in the Hierarchy pane and check the Inspector pane, you can see that there is a component called Cube(Mesh Filter) that has a property called Mesh with the value Cube. If you change the value of Cube, to say, Sphere, you’ll see that the object has now changed to a sphere. This is because by changing the mesh we have changed the 3D coordinates that dictate the shape of the object.

Unity3D allows us a number of methods to manipulate these mesh objects dynamically, this is very powerful because it means we don’t have to rely on our modeling tools to create or manipulate 3D shapes. 

Mesh Methods And Properties

The vertices Property
The vertices property is an array of Vector3D items that set each vertex and define the boundaries of the mesh.

The uv Property
The uv property is an array of Vector2D items that sets how the material is applied to the mesh. 

The triangles Property
The triangles property is an array of integers, where each three integers in sequence represent a relationship of three vertices forming a triangle. So a single triangle object between the first, second, and third vertex items in the vertices array would exist in the triangles array as {0,1,2}. 

A simple square shape would exist in the array with another three integers {0,1,2,1,2,3}.

As you can see the triangles array works in groups of three, each group referring to the vertex items in the vertices array.

This property is an array of integers that set the triangles (polys), or, the relationship of the vertices to one another. 

The Clear() Method 
This method will clear the triangle and vertex data from the mesh.

The RecalculateBounds() Method
We have to call RecalculateBounds to make sure that the triangles from the vertex relationships are recalculated correctly after vertex coordinates have been changed.

The RecalculateNormals() Method
The "Normals" of the mesh are the faces of the 3D object that are actually rendered. By calling this method we instruct the engine to recalculate the normals of the mesh.

Code Examples

For the first example we can use a source mesh to change the values of a destination mesh. From the editors GameObject / Create Other menu create a cube object and a sphere object. We can code a simple script that will assign the mesh from one to the other.


Create the above class file and add it to the sphere object, drag and drop the cube object into its goSource property (you can see it in the inspector pane) and press the Play button. You can see that the sphere has changed into a cube. This is because we have used the mesh of the cube as a template to change the mesh of the sphere.
You could remove the script from the sphere, add it to the cube and add the sphere to the property, now if you press play the cube will transform into a sphere.

We can manipulate each vertex individually to achieve other effects. If we randomly add values to the coordinates of vertices we can achieve a jingling effect.


Add the above script to any 3d object (such as the sphere that was created before) and press play, you'll see that the object jingles randomly as each vertex in the mesh is changed.

Read more about:

Featured Blogs

About the Author(s)

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like