Game Development Reference
Unity Hands On
Meshes and Normals
Step 1. Download Chapter Two/PolyNormals.zip from the Web site,
unzip, and open.
Step 2. In the Scene, create two plane objects and place them
side by side.
Step 3. Add a directional light and rotate it so the light is hitting the
planes at about 30°.
Step 4. Take the CrumbleMesh.js script and attach it to both
Step 5. Select one of the planes in the Hierarchy, and tick the box next
to Recalculate Normals of the Crumble Mesh Script component in the
Step 6. Play. You will be able to see the planes deforming as shown
in Figure 2.19 .
Fig 2.19 Two planes, a directional
light, and a script to deform the
surfaces. The plane on the left has
its normal recalculated.
Step 7. Examine the CrumpleMesh script in the editor. Although the
code itself is complex for a beginner programmer, there are a few
lines of significance to point out. First, the script uses a class called
Perlin() . The complete code for this can be found in the Plugin folder
in the Project. The specifics of this at this time are not important but
it is the cause of the smooth deformation movement of the planes. It
is essentially a fractal algorithm for a random yet smooth movement.
Each vertex of the plane's mesh is captured and translated using a
value from the Perlin function. The line
var mesh : Mesh = GetComponent(MeshFilter).mesh;
is the Unity method for getting the mesh of any object to which
the script is attached. At the very bottom of the script is the option
to recalculate the normal. The script will do this when the tick box