Gamasutra: The Art & Business of Making Gamesspacer
Manipulating Meshes in Unity3D
Printer-Friendly VersionPrinter-Friendly Version
arrowPress Releases
April 19, 2014
PR Newswire
View All





If you enjoy reading this site, you might also want to check out these UBM TechWeb sites:


 
Manipulating Meshes in Unity3D
by Deniz Opal on 09/22/12 01:37:00 pm   Featured Blogs

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

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. 

Description

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.

http://pastebin.com/BHnC8HMS

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.

http://pastebin.com/2BY5F03K

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.


Related Jobs

Activision Publishing
Activision Publishing — Vancouver, British Columbia, Canada
[04.19.14]

Principal Graphics Programmer
Insomniac Games
Insomniac Games — Burbank, California, United States
[04.18.14]

Associate Engine Programmer
Insomniac Games
Insomniac Games — Burbank, California, United States
[04.18.14]

iOS Programmer
Insomniac Games
Insomniac Games — Burbank , California, United States
[04.18.14]

Senior Engine Programmer






Comments


Kevin Gallant
profile image
Morphing animation techniques baked in the FBX is an easy was to change the source mesh to targets. Anyway we can reduce the coding for artists the better.

Deniz Opal
profile image
That is the normal solution and its entirely possible that its a better solution for a task, so yes I agree with you. However, where disk and memory space is an issue, its potentially a great advantage to have the morphing done in a few lines of code and some smart algorithms rather than import an expensive game object and baked animations. I can think of plenty of cases where this would be an advantage, in particular if the morphing must be dynamic, for example, drapping a cloth over various objects, it would be very expensive to pre-animate the cloth for each potential object in the game, a "drapping algorithim" would be the smarter solution.


none
 
Comment: