Object3D.js 9.4 KB

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