/src/away3d/cameras/lenses/LensBase.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 215 lines · 137 code · 27 blank · 51 comment · 11 complexity · cbb1c67cf56b04b9265afe706066927d MD5 · raw file

  1. package away3d.cameras.lenses
  2. {
  3. import away3d.cameras.Camera3D;
  4. import away3d.core.math.Matrix3DUtils;
  5. import flash.events.EventDispatcher;
  6. import flash.geom.Matrix3D;
  7. import flash.geom.Rectangle;
  8. import flash.geom.Vector3D;
  9. import away3d.arcane;
  10. import away3d.errors.AbstractMethodError;
  11. import away3d.events.LensEvent;
  12. use namespace arcane;
  13. /**
  14. * An abstract base class for all lens classes. Lens objects provides a projection matrix that transforms 3D geometry to normalized homogeneous coordinates.
  15. */
  16. public class LensBase extends EventDispatcher
  17. {
  18. protected var _matrix:Matrix3D;
  19. protected var _scissorRect:Rectangle = new Rectangle();
  20. protected var _viewPort:Rectangle = new Rectangle();
  21. protected var _near:Number = 20;
  22. protected var _far:Number = 3000;
  23. protected var _aspectRatio:Number = 1;
  24. protected var _matrixInvalid:Boolean = true;
  25. protected var _frustumCorners:Vector.<Number> = new Vector.<Number>(8*3, true);
  26. private var _unprojection:Matrix3D;
  27. private var _unprojectionInvalid:Boolean = true;
  28. /**
  29. * Creates a new LensBase object.
  30. */
  31. public function LensBase()
  32. {
  33. _matrix = new Matrix3D();
  34. }
  35. /**
  36. * Retrieves the corner points of the lens frustum.
  37. */
  38. public function get frustumCorners():Vector.<Number>
  39. {
  40. return _frustumCorners;
  41. }
  42. public function set frustumCorners(frustumCorners:Vector.<Number>):void
  43. {
  44. _frustumCorners = frustumCorners;
  45. }
  46. /**
  47. * The projection matrix that transforms 3D geometry to normalized homogeneous coordinates.
  48. */
  49. public function get matrix():Matrix3D
  50. {
  51. if (_matrixInvalid) {
  52. updateMatrix();
  53. _matrixInvalid = false;
  54. }
  55. return _matrix;
  56. }
  57. public function set matrix(value:Matrix3D):void
  58. {
  59. _matrix = value;
  60. invalidateMatrix();
  61. }
  62. /**
  63. * The distance to the near plane of the frustum. Anything behind near plane will not be rendered.
  64. */
  65. public function get near():Number
  66. {
  67. return _near;
  68. }
  69. public function set near(value:Number):void
  70. {
  71. if (value == _near)
  72. return;
  73. _near = value;
  74. invalidateMatrix();
  75. }
  76. /**
  77. * The distance to the far plane of the frustum. Anything beyond the far plane will not be rendered.
  78. */
  79. public function get far():Number
  80. {
  81. return _far;
  82. }
  83. public function set far(value:Number):void
  84. {
  85. if (value == _far)
  86. return;
  87. _far = value;
  88. invalidateMatrix();
  89. }
  90. /**
  91. * Calculates the normalised position in screen space of the given scene position relative to the camera.
  92. *
  93. * @param point3d the position vector of the scene coordinates to be projected.
  94. * @param v The destination Vector3D object
  95. * @return The normalised screen position of the given scene coordinates relative to the camera.
  96. */
  97. public function project(point3d:Vector3D, v:Vector3D = null):Vector3D
  98. {
  99. if(!v) v = new Vector3D();
  100. Matrix3DUtils.transformVector(matrix, point3d, v);
  101. v.x = v.x/v.w;
  102. v.y = -v.y/v.w;
  103. //z is unaffected by transform
  104. v.z = point3d.z;
  105. return v;
  106. }
  107. public function get unprojectionMatrix():Matrix3D
  108. {
  109. if (_unprojectionInvalid) {
  110. _unprojection ||= new Matrix3D();
  111. _unprojection.copyFrom(matrix);
  112. _unprojection.invert();
  113. _unprojectionInvalid = false;
  114. }
  115. return _unprojection;
  116. }
  117. /**
  118. * Calculates the scene position relative to the camera of the given normalized coordinates in screen space.
  119. *
  120. * @param nX The normalised x coordinate in screen space, -1 corresponds to the left edge of the viewport, 1 to the right.
  121. * @param nY The normalised y coordinate in screen space, -1 corresponds to the top edge of the viewport, 1 to the bottom.
  122. * @param sZ The z coordinate in screen space, representing the distance into the screen.
  123. * @param v The destination Vector3D object
  124. * @return The scene position relative to the camera of the given screen coordinates.
  125. */
  126. public function unproject(nX:Number, nY:Number, sZ:Number, v:Vector3D = null):Vector3D
  127. {
  128. throw new AbstractMethodError();
  129. }
  130. /**
  131. * Creates an exact duplicate of the lens
  132. */
  133. public function clone():LensBase
  134. {
  135. throw new AbstractMethodError();
  136. }
  137. /**
  138. * The aspect ratio (width/height) of the view. Set by the renderer.
  139. * @private
  140. */
  141. arcane function get aspectRatio():Number
  142. {
  143. return _aspectRatio;
  144. }
  145. arcane function set aspectRatio(value:Number):void
  146. {
  147. if (_aspectRatio == value || (value*0) != 0)
  148. return;
  149. _aspectRatio = value;
  150. invalidateMatrix();
  151. }
  152. /**
  153. * Invalidates the projection matrix, which will cause it to be updated on the next request.
  154. */
  155. protected function invalidateMatrix():void
  156. {
  157. _matrixInvalid = true;
  158. _unprojectionInvalid = true;
  159. // notify the camera that the lens matrix is changing. this will mark the
  160. // viewProjectionMatrix in the camera as invalid, and force the matrix to
  161. // be re-queried from the lens, and therefore rebuilt.
  162. dispatchEvent(new LensEvent(LensEvent.MATRIX_CHANGED, this));
  163. }
  164. /**
  165. * Updates the matrix
  166. */
  167. protected function updateMatrix():void
  168. {
  169. throw new AbstractMethodError();
  170. }
  171. arcane function updateScissorRect(x:Number, y:Number, width:Number, height:Number):void
  172. {
  173. _scissorRect.x = x;
  174. _scissorRect.y = y;
  175. _scissorRect.width = width;
  176. _scissorRect.height = height;
  177. invalidateMatrix();
  178. }
  179. arcane function updateViewport(x:Number, y:Number, width:Number, height:Number):void
  180. {
  181. _viewPort.x = x;
  182. _viewPort.y = y;
  183. _viewPort.width = width;
  184. _viewPort.height = height;
  185. invalidateMatrix();
  186. }
  187. }
  188. }