topical media & game development
#graphic-flex-image-effects-06-Flex-DrawTrianglesTest.ax
#graphic-flex-image-effects-06-Flex-DrawTrianglesTest.ax
[swf]
[flash]
flex
package {
import flash.display.TriangleCulling;
import flash.events.Event;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
[SWF(width=600, height=400, backgroundColor=0x000000)]
Demonstrates the use of drawTriangles(), Matrix3D and Vector3D to render a 3D model.
This draws a textured pyramid to the screen and rotates it each frame.
public class @ax-graphic-flex-image-effects-06-Flex-DrawTrianglesTest extends graphic_flex_image_effects_06_Flex_AbstractImageLoader {
private var _model:graphic_flex_image_effects_06_Flex_Mesh3D;
private var _transform:Matrix3D;
Constructor. Centers sprite, creates model and calls super contructor to load image.
public function @ax-graphic-flex-image-effects-06-Flex-DrawTrianglesTest() {
x = stage.stageWidth/2;
y = stage.stageHeight/2;
_model = new graphic_flex_image_effects_06_Flex_PyramidMesh(this);
// creates initial 3D transform
_transform = new Matrix3D();
super("graphic-flex-image-effects-06-assets-bricks.png");
}
Method that is called once image loads in super class. This renders the model,
then sets up a listener for the ENTER_FRAME event to handle the animation.
override protected function runPostImageLoad():void {
render();
addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame);
}
Draws the 3D model to the screen using drawTriangles().
private function render():void {
// applies current transform to the model
_model.applyTransform(_transform);
graphics.clear();
graphics.beginBitmapFill(_loadedBitmap.bitmapData);
graphics.drawTriangles(
_model.vertices,
_model.sides,
_model.uvtData,
TriangleCulling.NEGATIVE
);
graphics.endFill();
}
Handler for the ENTER_FRAME event. This rotates the transform,
then rerenders the model.
parameter: event Event dispatched by this sprite.
private function onSpriteEnterFrame(event:Event):void {
_transform.appendRotation(2, Vector3D.Y_AXIS);
render();
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.