Object3D.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author mikael emtinger / http://gomo.se/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. */
  7. THREE.Object3D = function () {
  8. this.id = THREE.Object3DIdCount ++;
  9. this.uuid = THREE.Math.generateUUID();
  10. this.name = '';
  11. this.parent = undefined;
  12. this.children = [];
  13. this.up = THREE.Object3D.DefaultUp.clone();
  14. var scope = this;
  15. var position = new THREE.Vector3();
  16. var rotation = new THREE.Euler();
  17. var quaternion = new THREE.Quaternion();
  18. var scale = new THREE.Vector3( 1, 1, 1 );
  19. rotation.onChange( function () {
  20. quaternion.setFromEuler( rotation, false );
  21. } );
  22. quaternion.onChange( function () {
  23. rotation.setFromQuaternion( quaternion, undefined, false );
  24. } );
  25. Object.defineProperties( this, {
  26. position: {
  27. enumerable: true,
  28. value: position
  29. },
  30. rotation: {
  31. enumerable: true,
  32. value: rotation
  33. },
  34. quaternion: {
  35. enumerable: true,
  36. value: quaternion
  37. },
  38. scale: {
  39. enumerable: true,
  40. value: scale
  41. },
  42. } );
  43. this.renderDepth = null;
  44. this.rotationAutoUpdate = true;
  45. this.matrix = new THREE.Matrix4();
  46. this.matrixWorld = new THREE.Matrix4();
  47. this.matrixAutoUpdate = true;
  48. this.matrixWorldNeedsUpdate = false;
  49. this.visible = true;
  50. this.castShadow = false;
  51. this.receiveShadow = false;
  52. this.frustumCulled = true;
  53. this.userData = {};
  54. };
  55. THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 1, 0 );
  56. THREE.Object3D.prototype = {
  57. constructor: THREE.Object3D,
  58. get eulerOrder () {
  59. console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
  60. return this.rotation.order;
  61. },
  62. set eulerOrder ( value ) {
  63. console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
  64. this.rotation.order = value;
  65. },
  66. get useQuaternion () {
  67. console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
  68. },
  69. set useQuaternion ( value ) {
  70. console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
  71. },
  72. applyMatrix: function ( matrix ) {
  73. this.matrix.multiplyMatrices( matrix, this.matrix );
  74. this.matrix.decompose( this.position, this.quaternion, this.scale );
  75. },
  76. setRotationFromAxisAngle: function ( axis, angle ) {
  77. // assumes axis is normalized
  78. this.quaternion.setFromAxisAngle( axis, angle );
  79. },
  80. setRotationFromEuler: function ( euler ) {
  81. this.quaternion.setFromEuler( euler, true );
  82. },
  83. setRotationFromMatrix: function ( m ) {
  84. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  85. this.quaternion.setFromRotationMatrix( m );
  86. },
  87. setRotationFromQuaternion: function ( q ) {
  88. // assumes q is normalized
  89. this.quaternion.copy( q );
  90. },
  91. rotateOnAxis: function () {
  92. // rotate object on axis in object space
  93. // axis is assumed to be normalized
  94. var q1 = new THREE.Quaternion();
  95. return function ( axis, angle ) {
  96. q1.setFromAxisAngle( axis, angle );
  97. this.quaternion.multiply( q1 );
  98. return this;
  99. }
  100. }(),
  101. rotateX: function () {
  102. var v1 = new THREE.Vector3( 1, 0, 0 );
  103. return function ( angle ) {
  104. return this.rotateOnAxis( v1, angle );
  105. };
  106. }(),
  107. rotateY: function () {
  108. var v1 = new THREE.Vector3( 0, 1, 0 );
  109. return function ( angle ) {
  110. return this.rotateOnAxis( v1, angle );
  111. };
  112. }(),
  113. rotateZ: function () {
  114. var v1 = new THREE.Vector3( 0, 0, 1 );
  115. return function ( angle ) {
  116. return this.rotateOnAxis( v1, angle );
  117. };
  118. }(),
  119. translateOnAxis: function () {
  120. // translate object by distance along axis in object space
  121. // axis is assumed to be normalized
  122. var v1 = new THREE.Vector3();
  123. return function ( axis, distance ) {
  124. v1.copy( axis ).applyQuaternion( this.quaternion );
  125. this.position.add( v1.multiplyScalar( distance ) );
  126. return this;
  127. }
  128. }(),
  129. translate: function ( distance, axis ) {
  130. console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
  131. return this.translateOnAxis( axis, distance );
  132. },
  133. translateX: function () {
  134. var v1 = new THREE.Vector3( 1, 0, 0 );
  135. return function ( distance ) {
  136. return this.translateOnAxis( v1, distance );
  137. };
  138. }(),
  139. translateY: function () {
  140. var v1 = new THREE.Vector3( 0, 1, 0 );
  141. return function ( distance ) {
  142. return this.translateOnAxis( v1, distance );
  143. };
  144. }(),
  145. translateZ: function () {
  146. var v1 = new THREE.Vector3( 0, 0, 1 );
  147. return function ( distance ) {
  148. return this.translateOnAxis( v1, distance );
  149. };
  150. }(),
  151. localToWorld: function ( vector ) {
  152. return vector.applyMatrix4( this.matrixWorld );
  153. },
  154. worldToLocal: function () {
  155. var m1 = new THREE.Matrix4();
  156. return function ( vector ) {
  157. return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
  158. };
  159. }(),
  160. lookAt: function () {
  161. // This routine does not support objects with rotated and/or translated parent(s)
  162. var m1 = new THREE.Matrix4();
  163. return function ( vector ) {
  164. m1.lookAt( vector, this.position, this.up );
  165. this.quaternion.setFromRotationMatrix( m1 );
  166. };
  167. }(),
  168. add: function ( object ) {
  169. if ( object === this ) {
  170. console.warn( "THREE.Object3D.add: An object can't be added as a child of itself." );
  171. return;
  172. }
  173. if ( object instanceof THREE.Object3D ) {
  174. if ( object.parent !== undefined ) {
  175. object.parent.remove( object );
  176. }
  177. object.parent = this;
  178. object.dispatchEvent( { type: 'added' } );
  179. this.children.push( object );
  180. // add to scene
  181. var scene = this;
  182. while ( scene.parent !== undefined ) {
  183. scene = scene.parent;
  184. }
  185. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  186. scene.__addObject( object );
  187. }
  188. }
  189. },
  190. remove: function ( object ) {
  191. var index = this.children.indexOf( object );
  192. if ( index !== - 1 ) {
  193. object.parent = undefined;
  194. object.dispatchEvent( { type: 'removed' } );
  195. this.children.splice( index, 1 );
  196. // remove from scene
  197. var scene = this;
  198. while ( scene.parent !== undefined ) {
  199. scene = scene.parent;
  200. }
  201. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  202. scene.__removeObject( object );
  203. }
  204. }
  205. },
  206. raycast: function () {},
  207. traverse: function ( callback ) {
  208. callback( this );
  209. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  210. this.children[ i ].traverse( callback );
  211. }
  212. },
  213. getObjectById: function ( id, recursive ) {
  214. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  215. var child = this.children[ i ];
  216. if ( child.id === id ) {
  217. return child;
  218. }
  219. if ( recursive === true ) {
  220. child = child.getObjectById( id, recursive );
  221. if ( child !== undefined ) {
  222. return child;
  223. }
  224. }
  225. }
  226. return undefined;
  227. },
  228. getObjectByName: function ( name, recursive ) {
  229. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  230. var child = this.children[ i ];
  231. if ( child.name === name ) {
  232. return child;
  233. }
  234. if ( recursive === true ) {
  235. child = child.getObjectByName( name, recursive );
  236. if ( child !== undefined ) {
  237. return child;
  238. }
  239. }
  240. }
  241. return undefined;
  242. },
  243. getChildByName: function ( name, recursive ) {
  244. console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
  245. return this.getObjectByName( name, recursive );
  246. },
  247. updateMatrix: function () {
  248. this.matrix.compose( this.position, this.quaternion, this.scale );
  249. this.matrixWorldNeedsUpdate = true;
  250. },
  251. updateMatrixWorld: function ( force ) {
  252. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  253. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  254. if ( this.parent === undefined ) {
  255. this.matrixWorld.copy( this.matrix );
  256. } else {
  257. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  258. }
  259. this.matrixWorldNeedsUpdate = false;
  260. force = true;
  261. }
  262. // update children
  263. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  264. this.children[ i ].updateMatrixWorld( force );
  265. }
  266. },
  267. clone: function ( object, recursive ) {
  268. if ( object === undefined ) object = new THREE.Object3D();
  269. if ( recursive === undefined ) recursive = true;
  270. object.name = this.name;
  271. object.up.copy( this.up );
  272. object.position.copy( this.position );
  273. object.quaternion.copy( this.quaternion );
  274. object.scale.copy( this.scale );
  275. object.renderDepth = this.renderDepth;
  276. object.rotationAutoUpdate = this.rotationAutoUpdate;
  277. object.matrix.copy( this.matrix );
  278. object.matrixWorld.copy( this.matrixWorld );
  279. object.matrixAutoUpdate = this.matrixAutoUpdate;
  280. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  281. object.visible = this.visible;
  282. object.castShadow = this.castShadow;
  283. object.receiveShadow = this.receiveShadow;
  284. object.frustumCulled = this.frustumCulled;
  285. object.userData = JSON.parse( JSON.stringify( this.userData ) );
  286. if ( recursive === true ) {
  287. for ( var i = 0; i < this.children.length; i ++ ) {
  288. var child = this.children[ i ];
  289. object.add( child.clone() );
  290. }
  291. }
  292. return object;
  293. }
  294. };
  295. THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype );
  296. THREE.Object3DIdCount = 0;