By default, a text 3D object has no depth (although it is visible from both sides). One of the extrusion classes (which are covered in more detail in Chapter 11, Extrusions and Modifiers) called TextExtrusion
can be used to create an additional 3D object that uses the shape of a text 3D object and extends it into a third dimension. When combined, the TextExtrusion
and TextField3D
objects can be used to create the appearance of a solid block of text. The FontExtrusionDemo
class in the following code snippet gives an example of this process:
package { import away3d.containers.ObjectContainer3D; import away3d.extrusions.TextExtrusion; import away3d.primitives.TextField3D; import flash.events.Event; import wumedia.vector.VectorText; public class FontExtrusionDemo extends Away3DTemplate { [Embed(source="Fonts.swf", mimeType="application/octet-stream")] protected var Fonts:Class;
The TextField3D
3D object and the extrusion
3D object are both added as children of a ObjectContainer3D
object, referenced by the container
property.
protected var container:ObjectContainer3D;
The text
property will reference the TextField3D
object used to display the 3D text.
protected var text:TextField3D;
The extrusion
property will reference the TextExtrusion
object used to give the 3D text some depth.
protected var extrusion:TextExtrusion; public function FontExtrusionDemo() { super(); } protected override function initEngine():void { super.initEngine(); this.camera.z = 0; VectorText.extractFont(new Fonts()); } protected override function initScene():void { super.initScene(); text = new TextField3D("Vera Sans", { text: "Away3D Essentials", align: VectorText.CENTER } );
The TextExtrusion
constructor takes a reference to the TextField3D
object (or any other Mesh
object). It also accepts an init
object, which we have used to specify the depth of the 3D text, and to make both sides of the extruded mesh visible.
extrusion = new TextExtrusion(text, { depth: 10, bothsides:true } );
The ObjectContainer3D
object is created, supplying the TextField3D
and TextExtrusion
3D objects that were created above as children. The initial position of the ObjectContainer3D
object is set to 300 units down the positive end of the Z-axis.
container = new ObjectContainer3D(text, extrusion, { z: 300 } );
The container is then added as a child of the scene.
scene.addChild(container); } protected override function onEnterFrame(event:Event):void { super.onEnterFrame(event);
The container is slowly rotated around its Y-axis by modifying the rotationY
property in every frame. In previous examples, we have simply incremented the rotation property, without any regard for when the value became larger than 360 degrees. After all, rotating a 3D object by 180 or 540 degrees has the same overall effect. But in this case, we do want to keep the value of the rotationY
property between 0 and 360 so we can easily test to see if the rotation is within a given range. To do this, we use the mod (%
) operator.
container.rotationY = (container.rotationY + 1) % 360;
Z-sorting issues can rise due to the fact that the TextExtrusion
and TextField3D
objects are so closely aligned. This issue results in parts of the TextField3D
or TextExturude
3D objects showing through where it is obvious that they should be hidden.
To solve this problem, we can use one of the procedures detailed in Chapter 4, Z-Sorting, to force the sorting order of 3D objects. Here we are assigning a positive value to the TextField3D
screenZOffset
property to force it to be drawn behind the TextExturude
object, when the container has been rotated between 90 and 270 degrees around the Y-axis. When the container is rotated like this, the TextField3D
object is at the back of the scene. Otherwise, the TextField3D
is drawn in front by assigning a negative value to the screenZOffset
property.
if (container.rotationY > 90 && container.rotationY < 270) text.screenZOffset = 10; else text.screenZOffset = -10; } } }
The result of the FontExtrusionDemo
application is shown in the following image: