/src/away3d/core/base/Object3D.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 969 lines · 561 code · 171 blank · 237 comment · 71 complexity · 64a6ed0d38d82951b78f9a48b492447c MD5 · raw file

  1. package away3d.core.base
  2. {
  3. import away3d.*;
  4. import away3d.controllers.*;
  5. import away3d.core.math.*;
  6. import away3d.events.*;
  7. import away3d.library.assets.*;
  8. import flash.geom.*;
  9. use namespace arcane;
  10. /**
  11. * Dispatched when the position of the 3d object changes.
  12. *
  13. * @eventType away3d.events.Object3DEvent
  14. */
  15. [Event(name="positionChanged", type="away3d.events.Object3DEvent")]
  16. /**
  17. * Dispatched when the scale of the 3d object changes.
  18. *
  19. * @eventType away3d.events.Object3DEvent
  20. */
  21. [Event(name="scaleChanged", type="away3d.events.Object3DEvent")]
  22. /**
  23. * Dispatched when the rotation of the 3d object changes.
  24. *
  25. * @eventType away3d.events.Object3DEvent
  26. */
  27. [Event(name="rotationChanged", type="away3d.events.Object3DEvent")]
  28. /**
  29. * Object3D provides a base class for any 3D object that has a (local) transformation.<br/><br/>
  30. *
  31. * Standard Transform:
  32. * <ul>
  33. * <li> The standard order for transformation is [parent transform] * (Translate+Pivot) * (Rotate) * (-Pivot) * (Scale) * [child transform] </li>
  34. * <li> This is the order of matrix multiplications, left-to-right. </li>
  35. * <li> The order of transformation is right-to-left, however!
  36. * (Scale) happens before (-Pivot) happens before (Rotate) happens before (Translate+Pivot)
  37. * with no pivot, the above transform works out to [parent transform] * Translate * Rotate * Scale * [child transform]
  38. * (Scale) happens before (Rotate) happens before (Translate) </li>
  39. * <li> This is based on code in updateTransform and ObjectContainer3D.updateSceneTransform(). </li>
  40. * <li> Matrix3D prepend = operator on rhs - e.g. transform' = transform * rhs; </li>
  41. * <li> Matrix3D append = operator on lhr - e.g. transform' = lhs * transform; </li>
  42. * </ul>
  43. *
  44. * To affect Scale:
  45. * <ul>
  46. * <li> set scaleX/Y/Z directly, or call scale(delta) </li>
  47. * </ul>
  48. *
  49. * To affect Pivot:
  50. * <ul>
  51. * <li> set pivotPoint directly, or call movePivot() </li>
  52. * </ul>
  53. *
  54. * To affect Rotate:
  55. * <ul>
  56. * <li> set rotationX/Y/Z individually (using degrees), set eulers [all 3 angles] (using radians), or call rotateTo()</li>
  57. * <li> call pitch()/yaw()/roll()/rotate() to add an additional rotation *before* the current transform.
  58. * rotationX/Y/Z will be reset based on these operations. </li>
  59. * </ul>
  60. *
  61. * To affect Translate (post-rotate translate):
  62. *
  63. * <ul>
  64. * <li> set x/y/z/position or call moveTo(). </li>
  65. * <li> call translate(), which modifies x/y/z based on a delta vector. </li>
  66. * <li> call moveForward()/moveBackward()/moveLeft()/moveRight()/moveUp()/moveDown()/translateLocal() to add an
  67. * additional translate *before* the current transform. x/y/z will be reset based on these operations. </li>
  68. * </ul>
  69. */
  70. public class Object3D extends NamedAssetBase
  71. {
  72. /** @private */
  73. arcane var _controller:ControllerBase;
  74. private var _smallestNumber:Number = 0.0000000000000000000001;
  75. private var _transformDirty:Boolean = true;
  76. private var _positionDirty:Boolean;
  77. private var _rotationDirty:Boolean;
  78. private var _scaleDirty:Boolean;
  79. // TODO: not used
  80. // private var _positionValuesDirty:Boolean;
  81. // private var _rotationValuesDirty:Boolean;
  82. // private var _scaleValuesDirty:Boolean;
  83. private var _positionChanged:Object3DEvent;
  84. private var _rotationChanged:Object3DEvent;
  85. private var _scaleChanged:Object3DEvent;
  86. private var _rotationX:Number = 0;
  87. private var _rotationY:Number = 0;
  88. private var _rotationZ:Number = 0;
  89. private var _eulers:Vector3D = new Vector3D();
  90. private var _flipY:Matrix3D = new Matrix3D();
  91. private var _listenToPositionChanged:Boolean;
  92. private var _listenToRotationChanged:Boolean;
  93. private var _listenToScaleChanged:Boolean;
  94. protected var _zOffset:int = 0;
  95. private function invalidatePivot():void
  96. {
  97. _pivotZero = (_pivotPoint.x == 0) && (_pivotPoint.y == 0) && (_pivotPoint.z == 0);
  98. invalidateTransform();
  99. }
  100. private function invalidatePosition():void
  101. {
  102. if (_positionDirty)
  103. return;
  104. _positionDirty = true;
  105. invalidateTransform();
  106. if (_listenToPositionChanged)
  107. notifyPositionChanged();
  108. }
  109. private function notifyPositionChanged():void
  110. {
  111. if (!_positionChanged)
  112. _positionChanged = new Object3DEvent(Object3DEvent.POSITION_CHANGED, this);
  113. dispatchEvent(_positionChanged);
  114. }
  115. override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
  116. {
  117. super.addEventListener(type, listener, useCapture, priority, useWeakReference);
  118. switch (type) {
  119. case Object3DEvent.POSITION_CHANGED:
  120. _listenToPositionChanged = true;
  121. break;
  122. case Object3DEvent.ROTATION_CHANGED:
  123. _listenToRotationChanged = true;
  124. break;
  125. case Object3DEvent.SCALE_CHANGED:
  126. _listenToRotationChanged = true;
  127. break;
  128. }
  129. }
  130. override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
  131. {
  132. super.removeEventListener(type, listener, useCapture);
  133. if (hasEventListener(type))
  134. return;
  135. switch (type) {
  136. case Object3DEvent.POSITION_CHANGED:
  137. _listenToPositionChanged = false;
  138. break;
  139. case Object3DEvent.ROTATION_CHANGED:
  140. _listenToRotationChanged = false;
  141. break;
  142. case Object3DEvent.SCALE_CHANGED:
  143. _listenToScaleChanged = false;
  144. break;
  145. }
  146. }
  147. private function invalidateRotation():void
  148. {
  149. if (_rotationDirty)
  150. return;
  151. _rotationDirty = true;
  152. invalidateTransform();
  153. if (_listenToRotationChanged)
  154. notifyRotationChanged();
  155. }
  156. private function notifyRotationChanged():void
  157. {
  158. if (!_rotationChanged)
  159. _rotationChanged = new Object3DEvent(Object3DEvent.ROTATION_CHANGED, this);
  160. dispatchEvent(_rotationChanged);
  161. }
  162. private function invalidateScale():void
  163. {
  164. if (_scaleDirty)
  165. return;
  166. _scaleDirty = true;
  167. invalidateTransform();
  168. if (_listenToScaleChanged)
  169. notifyScaleChanged();
  170. }
  171. private function notifyScaleChanged():void
  172. {
  173. if (!_scaleChanged)
  174. _scaleChanged = new Object3DEvent(Object3DEvent.SCALE_CHANGED, this);
  175. dispatchEvent(_scaleChanged);
  176. }
  177. protected var _transform:Matrix3D = new Matrix3D();
  178. protected var _scaleX:Number = 1;
  179. protected var _scaleY:Number = 1;
  180. protected var _scaleZ:Number = 1;
  181. protected var _x:Number = 0;
  182. protected var _y:Number = 0;
  183. protected var _z:Number = 0;
  184. protected var _pivotPoint:Vector3D = new Vector3D();
  185. protected var _pivotZero:Boolean = true;
  186. protected var _pos:Vector3D = new Vector3D();
  187. protected var _rot:Vector3D = new Vector3D();
  188. protected var _sca:Vector3D = new Vector3D();
  189. protected var _transformComponents:Vector.<Vector3D>;
  190. /**
  191. * An object that can contain any extra data.
  192. */
  193. public var extra:Object;
  194. /**
  195. * Defines the x coordinate of the 3d object relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  196. */
  197. public function get x():Number
  198. {
  199. return _x;
  200. }
  201. public function set x(val:Number):void
  202. {
  203. if (_x == val)
  204. return;
  205. _x = val;
  206. invalidatePosition();
  207. }
  208. /**
  209. * Defines the y coordinate of the 3d object relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  210. */
  211. public function get y():Number
  212. {
  213. return _y;
  214. }
  215. public function set y(val:Number):void
  216. {
  217. if (_y == val)
  218. return;
  219. _y = val;
  220. invalidatePosition();
  221. }
  222. /**
  223. * Defines the z coordinate of the 3d object relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  224. */
  225. public function get z():Number
  226. {
  227. return _z;
  228. }
  229. public function set z(val:Number):void
  230. {
  231. if (_z == val)
  232. return;
  233. _z = val;
  234. invalidatePosition();
  235. }
  236. /**
  237. * Defines the euler angle of rotation of the 3d object around the x-axis, relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  238. */
  239. public function get rotationX():Number
  240. {
  241. return _rotationX*MathConsts.RADIANS_TO_DEGREES;
  242. }
  243. public function set rotationX(val:Number):void
  244. {
  245. if (rotationX == val)
  246. return;
  247. _rotationX = val*MathConsts.DEGREES_TO_RADIANS;
  248. invalidateRotation();
  249. }
  250. /**
  251. * Defines the euler angle of rotation of the 3d object around the y-axis, relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  252. */
  253. public function get rotationY():Number
  254. {
  255. return _rotationY*MathConsts.RADIANS_TO_DEGREES;
  256. }
  257. public function set rotationY(val:Number):void
  258. {
  259. if (rotationY == val)
  260. return;
  261. _rotationY = val*MathConsts.DEGREES_TO_RADIANS;
  262. invalidateRotation();
  263. }
  264. /**
  265. * Defines the euler angle of rotation of the 3d object around the z-axis, relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  266. */
  267. public function get rotationZ():Number
  268. {
  269. return _rotationZ*MathConsts.RADIANS_TO_DEGREES;
  270. }
  271. public function set rotationZ(val:Number):void
  272. {
  273. if (rotationZ == val)
  274. return;
  275. _rotationZ = val*MathConsts.DEGREES_TO_RADIANS;
  276. invalidateRotation();
  277. }
  278. /**
  279. * Defines the scale of the 3d object along the x-axis, relative to local coordinates.
  280. */
  281. public function get scaleX():Number
  282. {
  283. return _scaleX;
  284. }
  285. public function set scaleX(val:Number):void
  286. {
  287. if (_scaleX == val)
  288. return;
  289. _scaleX = val;
  290. invalidateScale();
  291. }
  292. /**
  293. * Defines the scale of the 3d object along the y-axis, relative to local coordinates.
  294. */
  295. public function get scaleY():Number
  296. {
  297. return _scaleY;
  298. }
  299. public function set scaleY(val:Number):void
  300. {
  301. if (_scaleY == val)
  302. return;
  303. _scaleY = val;
  304. invalidateScale();
  305. }
  306. /**
  307. * Defines the scale of the 3d object along the z-axis, relative to local coordinates.
  308. */
  309. public function get scaleZ():Number
  310. {
  311. return _scaleZ;
  312. }
  313. public function set scaleZ(val:Number):void
  314. {
  315. if (_scaleZ == val)
  316. return;
  317. _scaleZ = val;
  318. invalidateScale();
  319. }
  320. /**
  321. * Defines the rotation of the 3d object as a <code>Vector3D</code> object containing euler angles for rotation around x, y and z axis.
  322. */
  323. public function get eulers():Vector3D
  324. {
  325. _eulers.x = _rotationX*MathConsts.RADIANS_TO_DEGREES;
  326. _eulers.y = _rotationY*MathConsts.RADIANS_TO_DEGREES;
  327. _eulers.z = _rotationZ*MathConsts.RADIANS_TO_DEGREES;
  328. return _eulers;
  329. }
  330. public function set eulers(value:Vector3D):void
  331. {
  332. _rotationX = value.x*MathConsts.DEGREES_TO_RADIANS;
  333. _rotationY = value.y*MathConsts.DEGREES_TO_RADIANS;
  334. _rotationZ = value.z*MathConsts.DEGREES_TO_RADIANS;
  335. invalidateRotation();
  336. }
  337. /**
  338. * The transformation of the 3d object, relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  339. */
  340. public function get transform():Matrix3D
  341. {
  342. if (_transformDirty)
  343. updateTransform();
  344. return _transform;
  345. }
  346. public function set transform(val:Matrix3D):void
  347. {
  348. //ridiculous matrix error
  349. var raw:Vector.<Number> = Matrix3DUtils.RAW_DATA_CONTAINER;
  350. val.copyRawDataTo(raw);
  351. if (!raw[uint(0)]) {
  352. raw[uint(0)] = _smallestNumber;
  353. val.copyRawDataFrom(raw);
  354. }
  355. var elements:Vector.<Vector3D> = Matrix3DUtils.decompose(val);
  356. var vec:Vector3D;
  357. vec = elements[0];
  358. if (_x != vec.x || _y != vec.y || _z != vec.z) {
  359. _x = vec.x;
  360. _y = vec.y;
  361. _z = vec.z;
  362. invalidatePosition();
  363. }
  364. vec = elements[1];
  365. if (_rotationX != vec.x || _rotationY != vec.y || _rotationZ != vec.z) {
  366. _rotationX = vec.x;
  367. _rotationY = vec.y;
  368. _rotationZ = vec.z;
  369. invalidateRotation();
  370. }
  371. vec = elements[2];
  372. if (_scaleX != vec.x || _scaleY != vec.y || _scaleZ != vec.z) {
  373. _scaleX = vec.x;
  374. _scaleY = vec.y;
  375. _scaleZ = vec.z;
  376. invalidateScale();
  377. }
  378. }
  379. /**
  380. * Defines the local point around which the object rotates.
  381. */
  382. public function get pivotPoint():Vector3D
  383. {
  384. return _pivotPoint;
  385. }
  386. public function set pivotPoint(pivot:Vector3D):void
  387. {
  388. if(!_pivotPoint) _pivotPoint = new Vector3D();
  389. _pivotPoint.x = pivot.x;
  390. _pivotPoint.y = pivot.y;
  391. _pivotPoint.z = pivot.z;
  392. invalidatePivot();
  393. }
  394. /**
  395. * Defines the position of the 3d object, relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  396. */
  397. public function get position():Vector3D
  398. {
  399. transform.copyColumnTo(3, _pos);
  400. return _pos.clone();
  401. }
  402. public function set position(value:Vector3D):void
  403. {
  404. _x = value.x;
  405. _y = value.y;
  406. _z = value.z;
  407. invalidatePosition();
  408. }
  409. /**
  410. * Defines the position of the 3d object, relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  411. * @param v the destination Vector3D
  412. * @return
  413. */
  414. public function getPosition(v:Vector3D = null):Vector3D {
  415. if(!v) v = new Vector3D();
  416. transform.copyColumnTo(3, v);
  417. return v;
  418. }
  419. /**
  420. *
  421. */
  422. public function get forwardVector():Vector3D
  423. {
  424. return Matrix3DUtils.getForward(transform);
  425. }
  426. /**
  427. *
  428. */
  429. public function get rightVector():Vector3D
  430. {
  431. return Matrix3DUtils.getRight(transform);
  432. }
  433. /**
  434. *
  435. */
  436. public function get upVector():Vector3D
  437. {
  438. return Matrix3DUtils.getUp(transform);
  439. }
  440. /**
  441. *
  442. */
  443. public function get backVector():Vector3D
  444. {
  445. var director:Vector3D = Matrix3DUtils.getForward(transform);
  446. director.negate();
  447. return director;
  448. }
  449. /**
  450. *
  451. */
  452. public function get leftVector():Vector3D
  453. {
  454. var director:Vector3D = Matrix3DUtils.getRight(transform);
  455. director.negate();
  456. return director;
  457. }
  458. /**
  459. *
  460. */
  461. public function get downVector():Vector3D
  462. {
  463. var director:Vector3D = Matrix3DUtils.getUp(transform);
  464. director.negate();
  465. return director;
  466. }
  467. /**
  468. * Creates an Object3D object.
  469. */
  470. public function Object3D()
  471. {
  472. // Cached vector of transformation components used when
  473. // recomposing the transform matrix in updateTransform()
  474. _transformComponents = new Vector.<Vector3D>(3, true);
  475. _transformComponents[0] = _pos;
  476. _transformComponents[1] = _rot;
  477. _transformComponents[2] = _sca;
  478. _transform.identity();
  479. _flipY.appendScale(1, -1, 1);
  480. }
  481. /**
  482. * Appends a uniform scale to the current transformation.
  483. * @param value The amount by which to scale.
  484. */
  485. public function scale(value:Number):void
  486. {
  487. _scaleX *= value;
  488. _scaleY *= value;
  489. _scaleZ *= value;
  490. invalidateScale();
  491. }
  492. /**
  493. * Moves the 3d object forwards along it's local z axis
  494. *
  495. * @param distance The length of the movement
  496. */
  497. public function moveForward(distance:Number):void
  498. {
  499. translateLocal(Vector3D.Z_AXIS, distance);
  500. }
  501. /**
  502. * Moves the 3d object backwards along it's local z axis
  503. *
  504. * @param distance The length of the movement
  505. */
  506. public function moveBackward(distance:Number):void
  507. {
  508. translateLocal(Vector3D.Z_AXIS, -distance);
  509. }
  510. /**
  511. * Moves the 3d object backwards along it's local x axis
  512. *
  513. * @param distance The length of the movement
  514. */
  515. public function moveLeft(distance:Number):void
  516. {
  517. translateLocal(Vector3D.X_AXIS, -distance);
  518. }
  519. /**
  520. * Moves the 3d object forwards along it's local x axis
  521. *
  522. * @param distance The length of the movement
  523. */
  524. public function moveRight(distance:Number):void
  525. {
  526. translateLocal(Vector3D.X_AXIS, distance);
  527. }
  528. /**
  529. * Moves the 3d object forwards along it's local y axis
  530. *
  531. * @param distance The length of the movement
  532. */
  533. public function moveUp(distance:Number):void
  534. {
  535. translateLocal(Vector3D.Y_AXIS, distance);
  536. }
  537. /**
  538. * Moves the 3d object backwards along it's local y axis
  539. *
  540. * @param distance The length of the movement
  541. */
  542. public function moveDown(distance:Number):void
  543. {
  544. translateLocal(Vector3D.Y_AXIS, -distance);
  545. }
  546. /**
  547. * Moves the 3d object directly to a point in space
  548. *
  549. * @param dx The amount of movement along the local x axis.
  550. * @param dy The amount of movement along the local y axis.
  551. * @param dz The amount of movement along the local z axis.
  552. */
  553. public function moveTo(dx:Number, dy:Number, dz:Number):void
  554. {
  555. if (_x == dx && _y == dy && _z == dz)
  556. return;
  557. _x = dx;
  558. _y = dy;
  559. _z = dz;
  560. invalidatePosition();
  561. }
  562. /**
  563. * Moves the local point around which the object rotates.
  564. *
  565. * @param dx The amount of movement along the local x axis.
  566. * @param dy The amount of movement along the local y axis.
  567. * @param dz The amount of movement along the local z axis.
  568. */
  569. public function movePivot(dx:Number, dy:Number, dz:Number):void
  570. {
  571. if(!_pivotPoint) _pivotPoint = new Vector3D();
  572. _pivotPoint.x += dx;
  573. _pivotPoint.y += dy;
  574. _pivotPoint.z += dz;
  575. invalidatePivot();
  576. }
  577. /**
  578. * Moves the 3d object along a vector by a defined length
  579. *
  580. * @param axis The vector defining the axis of movement
  581. * @param distance The length of the movement
  582. */
  583. public function translate(axis:Vector3D, distance:Number):void
  584. {
  585. var x:Number = axis.x, y:Number = axis.y, z:Number = axis.z;
  586. var len:Number = distance/Math.sqrt(x*x + y*y + z*z);
  587. _x += x*len;
  588. _y += y*len;
  589. _z += z*len;
  590. invalidatePosition();
  591. }
  592. /**
  593. * Moves the 3d object along a vector by a defined length
  594. *
  595. * @param axis The vector defining the axis of movement
  596. * @param distance The length of the movement
  597. */
  598. public function translateLocal(axis:Vector3D, distance:Number):void
  599. {
  600. var x:Number = axis.x, y:Number = axis.y, z:Number = axis.z;
  601. var len:Number = distance/Math.sqrt(x*x + y*y + z*z);
  602. transform.prependTranslation(x*len, y*len, z*len);
  603. _transform.copyColumnTo(3, _pos);
  604. _x = _pos.x;
  605. _y = _pos.y;
  606. _z = _pos.z;
  607. invalidatePosition();
  608. }
  609. /**
  610. * Rotates the 3d object around it's local x-axis
  611. *
  612. * @param angle The amount of rotation in degrees
  613. */
  614. public function pitch(angle:Number):void
  615. {
  616. rotate(Vector3D.X_AXIS, angle);
  617. }
  618. /**
  619. * Rotates the 3d object around it's local y-axis
  620. *
  621. * @param angle The amount of rotation in degrees
  622. */
  623. public function yaw(angle:Number):void
  624. {
  625. rotate(Vector3D.Y_AXIS, angle);
  626. }
  627. /**
  628. * Rotates the 3d object around it's local z-axis
  629. *
  630. * @param angle The amount of rotation in degrees
  631. */
  632. public function roll(angle:Number):void
  633. {
  634. rotate(Vector3D.Z_AXIS, angle);
  635. }
  636. public function clone():Object3D
  637. {
  638. var clone:Object3D = new Object3D();
  639. clone.pivotPoint = pivotPoint;
  640. clone.transform = transform;
  641. clone.name = name;
  642. // todo: implement for all subtypes
  643. return clone;
  644. }
  645. /**
  646. * Rotates the 3d object directly to a euler angle
  647. *
  648. * @param ax The angle in degrees of the rotation around the x axis.
  649. * @param ay The angle in degrees of the rotation around the y axis.
  650. * @param az The angle in degrees of the rotation around the z axis.
  651. */
  652. public function rotateTo(ax:Number, ay:Number, az:Number):void
  653. {
  654. _rotationX = ax*MathConsts.DEGREES_TO_RADIANS;
  655. _rotationY = ay*MathConsts.DEGREES_TO_RADIANS;
  656. _rotationZ = az*MathConsts.DEGREES_TO_RADIANS;
  657. invalidateRotation();
  658. }
  659. /**
  660. * Rotates the 3d object around an axis by a defined angle
  661. *
  662. * @param axis The vector defining the axis of rotation
  663. * @param angle The amount of rotation in degrees
  664. */
  665. public function rotate(axis:Vector3D, angle:Number):void
  666. {
  667. var m:Matrix3D = new Matrix3D();
  668. m.prependRotation(angle, axis);
  669. var vec:Vector3D = m.decompose()[1];
  670. _rotationX += vec.x;
  671. _rotationY += vec.y;
  672. _rotationZ += vec.z;
  673. invalidateRotation();
  674. }
  675. private static var tempAxeX:Vector3D;
  676. private static var tempAxeY:Vector3D;
  677. private static var tempAxeZ:Vector3D;
  678. /**
  679. * Rotates the 3d object around to face a point defined relative to the local coordinates of the parent <code>ObjectContainer3D</code>.
  680. *
  681. * @param target The vector defining the point to be looked at
  682. * @param upAxis An optional vector used to define the desired up orientation of the 3d object after rotation has occurred
  683. */
  684. public function lookAt(target:Vector3D, upAxis:Vector3D = null):void
  685. {
  686. if(!tempAxeX) tempAxeX = new Vector3D();
  687. if(!tempAxeY) tempAxeY = new Vector3D();
  688. if(!tempAxeZ) tempAxeZ = new Vector3D();
  689. var xAxis:Vector3D = tempAxeX;
  690. var yAxis:Vector3D = tempAxeY;
  691. var zAxis:Vector3D = tempAxeZ;
  692. var raw:Vector.<Number>;
  693. upAxis ||= Vector3D.Y_AXIS;
  694. if (_transformDirty) {
  695. updateTransform();
  696. }
  697. zAxis.x = target.x - _x;
  698. zAxis.y = target.y - _y;
  699. zAxis.z = target.z - _z;
  700. zAxis.normalize();
  701. xAxis.x = upAxis.y*zAxis.z - upAxis.z*zAxis.y;
  702. xAxis.y = upAxis.z*zAxis.x - upAxis.x*zAxis.z;
  703. xAxis.z = upAxis.x*zAxis.y - upAxis.y*zAxis.x;
  704. xAxis.normalize();
  705. if (xAxis.length < .05) {
  706. xAxis.x = upAxis.y;
  707. xAxis.y = upAxis.x;
  708. xAxis.z = 0;
  709. xAxis.normalize();
  710. }
  711. yAxis.x = zAxis.y*xAxis.z - zAxis.z*xAxis.y;
  712. yAxis.y = zAxis.z*xAxis.x - zAxis.x*xAxis.z;
  713. yAxis.z = zAxis.x*xAxis.y - zAxis.y*xAxis.x;
  714. raw = Matrix3DUtils.RAW_DATA_CONTAINER;
  715. raw[uint(0)] = _scaleX*xAxis.x;
  716. raw[uint(1)] = _scaleX*xAxis.y;
  717. raw[uint(2)] = _scaleX*xAxis.z;
  718. raw[uint(3)] = 0;
  719. raw[uint(4)] = _scaleY*yAxis.x;
  720. raw[uint(5)] = _scaleY*yAxis.y;
  721. raw[uint(6)] = _scaleY*yAxis.z;
  722. raw[uint(7)] = 0;
  723. raw[uint(8)] = _scaleZ*zAxis.x;
  724. raw[uint(9)] = _scaleZ*zAxis.y;
  725. raw[uint(10)] = _scaleZ*zAxis.z;
  726. raw[uint(11)] = 0;
  727. raw[uint(12)] = _x;
  728. raw[uint(13)] = _y;
  729. raw[uint(14)] = _z;
  730. raw[uint(15)] = 1;
  731. _transform.copyRawDataFrom(raw);
  732. transform = transform;
  733. if (zAxis.z < 0) {
  734. rotationY = (180 - rotationY);
  735. rotationX -= 180;
  736. rotationZ -= 180;
  737. }
  738. }
  739. /**
  740. * Cleans up any resources used by the current object.
  741. */
  742. public function dispose():void
  743. {
  744. }
  745. /**
  746. * @inheritDoc
  747. */
  748. public function disposeAsset():void
  749. {
  750. dispose();
  751. }
  752. /**
  753. * Invalidates the transformation matrix, causing it to be updated upon the next request
  754. */
  755. arcane function invalidateTransform():void
  756. {
  757. _transformDirty = true;
  758. }
  759. protected function updateTransform():void
  760. {
  761. _pos.x = _x;
  762. _pos.y = _y;
  763. _pos.z = _z;
  764. _rot.x = _rotationX;
  765. _rot.y = _rotationY;
  766. _rot.z = _rotationZ;
  767. if (!_pivotZero) {
  768. _sca.x = 1;
  769. _sca.y = 1;
  770. _sca.z = 1;
  771. _transform.recompose(_transformComponents);
  772. _transform.appendTranslation(_pivotPoint.x, _pivotPoint.y, _pivotPoint.z);
  773. _transform.prependTranslation(-_pivotPoint.x, -_pivotPoint.y, -_pivotPoint.z);
  774. _transform.prependScale(_scaleX, _scaleY, _scaleZ);
  775. _sca.x = _scaleX;
  776. _sca.y = _scaleY;
  777. _sca.z = _scaleZ;
  778. }else{
  779. _sca.x = _scaleX;
  780. _sca.y = _scaleY;
  781. _sca.z = _scaleZ;
  782. _transform.recompose(_transformComponents);
  783. }
  784. _transformDirty = false;
  785. _positionDirty = false;
  786. _rotationDirty = false;
  787. _scaleDirty = false;
  788. }
  789. public function get zOffset():int
  790. {
  791. return _zOffset;
  792. }
  793. public function set zOffset(value:int):void
  794. {
  795. _zOffset = value;
  796. }
  797. }
  798. }