Matrix-transformations.html 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <script src="../../list.js"></script>
  6. <script src="../../page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="../../page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p>
  12. Three.js uses *matrices* to encode 3D transformations---translations (position), rotations, and scaling. Every instance of [page:Object3D] has a [page:Object3D.matrix matrix] which stores that object's position, rotation, and scale. This page describes how to update an object's transformation.
  13. </p>
  14. <h2>Convenience properties and *matrixAutoUpdate*</h2>
  15. There are two ways to update an object's transformation:
  16. <ol>
  17. <li>
  18. Modify the object's *position*, *quaternion*, and *scale* properties, then ask Three.js to recompute the object's matrix from these properties:
  19. <code>
  20. object.position = start_position;
  21. object.quaternion = quaternion;
  22. object.updateMatrix();
  23. </code>
  24. Calling the *updateMatrix* method forces the object's matrix to be recomputed from *position*, *quaternion*, and *scale*. You can also set
  25. <code>
  26. object.matrixAutoUpdate = true;
  27. </code>
  28. in lieu of calling *updateMatrix*. This will force the matrix to be recomputed every frame; for static objects, you should therefore set
  29. <code>
  30. object.matrixAutoUpdate = false;
  31. </code>
  32. </li>
  33. <li>
  34. Modify the object's matrix directly. The [page:Matrix4] class has various methods for modifying the matrix:
  35. <code>
  36. object.matrix.setRotationFromQuaternion(quaternion);
  37. object.matrix.setPosition(start_position);
  38. object.matrixAutoUpdate = false;
  39. </code>
  40. Note that *matrixAutoUpdate* <emph>must</emph> be set to *false* in this case, and you should make sure <emph>not</emph> to call *updateMatrix*. Calling *updateMatrix* will clobber the manual changes made to the matrix, recalculating the matrix from *position*, *scale*, and so on.
  41. </li>
  42. </ol>
  43. <h2>Object and world matrices</h2>
  44. <p>
  45. An object's [page:Object3D.matrix matrix] stores the object's transformation <emph>relative</emph> to the object's [page:Object3D.parent parent]; to get the object's transformation in <emph>world</emph> coordinates, you must access the object's [page:Object3D.matrixWorld].
  46. </p>
  47. <p>
  48. When either the parent or the child object's transformation changes, you can request that the child object's [page:Object3D.matrixWorld matrixWorld] be updated by calling [page:Object3D.updateMatrixWorld updateMatrixWorld]().
  49. </p>
  50. <h2>Rotation and Quaternion</h2>
  51. <p>
  52. Three.js provides two ways of representing 3D rotations: [page:Euler Euler angles] and [page:Quaternion Quaternions], as well as methods for converting between the two. Euler angles are subject to a problem called "gimbal lock," where certain configurations can lose a degree of freedom (preventing the object from being rotated about one axis). For this reason, object rotations are <emph>always</emph> stored in the object's [page:Object3D.quaternion quaternion].
  53. </p>
  54. <p>
  55. Previous versions of the library included a *useQuaternion* property which, when set to false, would cause the object's [page:Object3D.matrix matrix] to be calculated from an Euler angle. This practice is deprecated---instead, you should use the [page:Object3D.setRotationFromEuler setRotationFromEuler] method, which will update the quaternion.
  56. </p>
  57. </body>
  58. </html>