topical media & game development

talk show tell print

#graphic-flex-image-effects-06-Flex-Mesh3D.ax

#graphic-flex-image-effects-06-Flex-Mesh3D.ax [swf] [flash] flex


  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 @ax-graphic-flex-image-effects-06-Flex-Mesh3D {
  
                  private var _vertices:Vector.<Number>;
                  private var _container3D:DisplayObject;
  
                  protected var _vectors:Vector.<Vector3D>;
                  protected var _sides:Vector.<int>;
                  protected var _uvtData:Vector.<Number>;
  
                  
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.
parameter: container The container display object in which the model will be rendered.

  
                  public function @ax-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.
parameter: vector The 3D vector to convert.
returns: 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.
parameter: matrix The 3D matrix transform to apply to the model.

  
                  public function applyTransform(matrix:Matrix3D):void {
                          _vertices = new Vector.<Number>();
                          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().
returns: The 2D coordinates that can be used to draw the 3D model.

  
                  public function get vertices():Vector.<Number> {
                          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().
returns: The sides that can be used to draw the 3D model.

  
                  public function get sides():Vector.<int> {
                          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().
returns: The UVT data that can be used to texture the 3D model.

  
                  public function get uvtData():Vector.<Number> {
                          return _uvtData;
                  }
  
          }
  
  }


(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.