package { import flash.display.DisplayObject; import flash.geom.Matrix3D; import flash.geom.Point; import flash.geom.Vector3D; /** * Abstract base class for 3D models. This holds the untransformed vertices, sides and UVT data, * then can convert the 3D vertices into 2D coordinates to be rendered on the screen. */ public class graphic_flex_image_effects_06_Flex_Mesh3D { private var _vertices:Vector.; private var _container3D:DisplayObject; protected var _vectors:Vector.; protected var _sides:Vector.; protected var _uvtData:Vector.; /** * Constructor. This saves a reference to the parent container of the model for * use in transforming coordinates into the parent coordinate space, as well * as calls the createMesh() method to initialize all of the model properties. * * @param container The container display object in which the model will be rendered. */ public function graphic_flex_image_effects_06_Flex_Mesh3D(container:DisplayObject) { _container3D = container; createMesh(); } /** * Abstract method to be overridden by concrete child classes. * This should initialize all of the vertices, sides and UVT data. */ protected function createMesh():void {} /** * Transforms a 3D vector into a 2D screen coordinate. * * @param vector The 3D vector to convert. * * @return The 2D coordinate in the parent container's coordinate space. */ private function getPoint2D(vector:Vector3D):Point { var point:Point = _container3D.local3DToGlobal(vector); return _container3D.globalToLocal(point); } /** * Applies the specified matrix transform to the vertices in the model. * This is non-destructive, creating a separate list of transformed vertices. * * @param matrix The 3D matrix transform to apply to the model. */ public function applyTransform(matrix:Matrix3D):void { _vertices = new Vector.(); var vertex:Point; var transformedVector:Vector3D; // run through each vertex for each (var vector:Vector3D in _vectors) { // transforms the vector using the matrix transform transformedVector = matrix.deltaTransformVector(vector); // converts the transformed 3D point to a 2D screen coordinate vertex = getPoint2D(transformedVector); // separates point into separate x and y properties, as required by drawTriangles() _vertices.push(vertex.x, vertex.y); } } /** * Returns the 2D coordinates that can be used to draw the 3D model. * This returns the vertices in the form required by drawTriangles(). * * @return The 2D coordinates that can be used to draw the 3D model. */ public function get vertices():Vector. { return _vertices; } /** * Returns the sides data that can be used to draw the 3D model. * This returns the sides in the form required by drawTriangles(). * * @return The sides that can be used to draw the 3D model. */ public function get sides():Vector. { return _sides; } /** * Returns the UVT data that can be used to texture the 3D model. * This returns the data in the form required by drawTriangles(). * * @return The UVT data that can be used to texture the 3D model. */ public function get uvtData():Vector. { return _uvtData; } } }