We have seen how a height map can be used to modify the height of a planar surface with the SkinExtrusion
/ Elevation
classes. The HeightMapModifier
class uses the same principle, but instead of being limited to modifying the height of a planar surface, it can be used to modify the surface height of any 3D object, be it a flat 3D object like a plane, a solid 3D object like a sphere, or a complex 3D object loaded from an external model file. It does this by repositioning the vertices of a 3D object along their normal vector (a normal vector is simply a vector that is perpendicular to a surface).
Take a look at the following height map:
The HeightMapModifierDemo
application will apply this height map to a sphere to create what could be a cratered moon or asteroid.
package { import away3d.core.utils.Cast; import away3d.materials.utils.HeightMapDataChannel; import away3d.modifiers.HeightMapModifier; import away3d.primitives.Plane; import away3d.primitives.Sphere; import flash.geom.Vector3D; public class HeightMapModifierDemo extends Away3DTemplate { [Embed(source="sphere.jpg")] protected var SphereTex:Class; protected var sphere:Sphere; public function HeightMapModifierDemo() { super(); } protected override function initScene():void { super.initScene(); camera.position = new Vector3D(0, 0, 500); camera.lookAt(new Vector3D(0, 0, 0)); sphere = new Sphere( { segmentsW: 32, segmentsH: 32 } ); scene.addChild(sphere);
To apply the height map to our sphere 3D object, we first need to create a new instance of the HeightMapModifier
class. We supply the 3D object that we will be modifying, the height map, the color channel to read from the height map, and the maximum distance to vary the surface of the 3D object by.
There is some inconsistency between the types used to define a color channel. We saw previously that the Elevation
and ElevationReader
classes take a string to define a color channel. The HeightMapModifier
class is a little different, taking instead a unit
. Here we use one of the constant values defined in the HeightMapDataChannel
class to specify that the HeightMapModifier
class should read the red color channel.
var modifier:HeightMapModifier = new HeightMapModifier( sphere, Cast.bitmap(SphereTex), HeightMapDataChannel.RED, 16 );
The execute()
function will then actually modify our sphere 3D object.
modifier.execute(); } } }
The following screenshot shows the end result:
You may notice that the HeightMapModifier
class will use a height map in the opposite manner to the Elevation
class. The brighter areas in a height map used by the HeightMapModifier
class will actually result in a depression when applied to a 3D object, instead of an increased height as is the case with the Elevation
class. The simple way around this is to supply a value of -1
to the scale
parameter in the HeightMapModifier
constructor, like so:
var modifier:HeightMapModifier = new HeightMapModifier( sphere, Cast.bitmap(SphereTex), HeightMapDataChannel.RED, 16, -1 );
This change reverses the sign of the values read from the height map (that is, positive values become negative), and produces the following result: