Creating terrain with the SkinExtrusion class

Unlike the preceding extrusion classes, the SkinExtrusion class does not extend a profile by creating additional dimensions. Instead, it takes an array of individual points and uses them to define the vertices of a 3D object that can be added to the scene. On the surface there does not seem to be a great deal of benefit to using the SkinExtrusion class over manually creating a mesh, like we did in Chapter 2, Creating and Displaying Primitives. In practice however, the points used by the SkinExtrusion class are generated by another class called Elevation. The Elevation class, in turn, takes an image, called a height map, the individual pixel colors of which are used to plot the heights of a collection of points over a plane.

You can see how this works in the following image. The black and white plane is textured with the same height map used to plot the height of the red points above it. You will notice that the points occupy a higher position over the brighter areas of the height map.

Creating terrain with the SkinExtrusion class

In the SkinExtrusionDemo application, we will use the height map (on the left side of the following image) and texture (on the right side) to create a 3D object that looks like an outdoor terrain.

Creating terrain with the SkinExtrusion class

Tip

You can use the free T2 application from http://www.toymaker.info/html/texgen.html to create realistic textures from a height map.

package  
{
  import away3d.core.utils.Cast;
  import away3d.extrusions.Elevation;
  import away3d.extrusions.SkinExtrude;
  import away3d.materials.BitmapMaterial;
  import flash.geom.Vector3D;

  public class SkinExtrusionDemo extends Away3DTemplate
  {

The height map and the texture image files are embedded for easy access.

    [Embed(source="heightmap.jpg")]
    protected var Heightmap:Class;

    [Embed(source="terrain.jpg")]
   protected var Terrain:Class;

    protected var extrude:SkinExtrude;

    public function SkinExtrusionDemo() 
    {
      super();
    }

    protected override function initScene():void
    {
      super.initScene();

      camera.position = new Vector3D(400, 200, 400);
      camera.lookAt(new Vector3D(0, 0, 0));

      var terrainMaterial:BitmapMaterial = 
        new BitmapMaterial(Cast.bitmap(Terrain));

Before we can use the SkinExtrusion class, we first need to create an instance of the Elevation class.

      var elevation:Elevation = new Elevation();

We then use the generate() function to create the multidimensional array of Vector3D objects that will be used by the SkinExtrusion class. The second through to the sixth parameters passed to the generate() function are the same as the default values defined by the function.

The first parameter specifies the BitmapData object that represents the height map.

The second parameter defines the color channel that will be read to determine the height of the resulting points. We supply the string "r" to indicate that we are reading the red colour of the pixels.

Tip

Since our height map is a gray scale image, where the red, green, and blue color values are all the same for any given pixel, it would make no difference which color channel the generate() function reads from.

The third and fourth parameters define how many samples will be read from the height map along the width and height of the height map, respectively. Higher values here result in a more detailed 3D object.

The fifth and sixth parameters define the scale of the plane over which the points will be positioned. The default is 1, which means the plane will have the same dimensions as the height map.

For the seventh parameter, we have not used the default parameter value. It is used to scale the height of the terrain, whose points by default have a maximum height of 255 units. The default value for the scale is 1, but since we have supplied a scale value of 0.25, our terrain 3D object will have a maximum height of one quarter of the default.

      var verticies:Array = 
        elevation.generate(
          Cast.bitmap(Heightmap), 
          "r", 
          16, 
          16, 
          1, 
          1, 
          0.25
        );

We now use this array to generate a new SkinExtrude 3D object.

The coverall init object parameter is set to true to ensure that any material applied to it covers the entire resulting 3D object. Like the LinearExtrusuion class, the recenter parameter will adjust the positions of the vertices that make up the 3D object so the origin of the mesh is at its center. This recentering process also takes the height of the 3D object into account, meaning that when recenter is true, the highest point of the 3D object will be above the local origin of the 3D object, and the lowest point will be below it. You can see the local origin of the recentered SkinExtrude 3D object by the position of the black dot in the following image:

Creating terrain with the SkinExtrusion class

If recenter was false, as it is by default, the origin of the mesh would be at one of the corners, and all of its vertices would be above the origin. In the following image, the black dot shows the local origin of a SkinExtrude 3D object that has not been recentered.

Creating terrain with the SkinExtrusion class
      extrude = new SkinExtrude(verticies, 
        {
          coverall: true, 
          material: terrainMaterial, 
          recenter: true,
          bothsides: true
        }
      );

The points returned by the generate() function lie over a plane whose edges are parallel to the X / Y axes. When these points are passed to the SkinExtrude class, you will in effect create a 3D object that looks like a bumpy vertical wall. By rotating the 3D object by 90 degrees around the X axis, we will orient it so it looks like it represents the ground.

      extrude.rotationX = 90;

Setting the recenter property to true will reposition the 3D object. Here we simply set its position back to the origin of the scene.

      extrude.x = extrude.y = extrude.z = 0;

      scene.addChild(extrude);
    }
  }
}

The following screenshot shows the result of this application:

Creating terrain with the SkinExtrusion class
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.173.199