Object3D.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. * @author elephantatwork / www.elephantatwork.ch
  7. */
  8. THREE.Object3D = function () {
  9. Object.defineProperty( this, 'id', { value: THREE.Object3DIdCount ++ } );
  10. this.uuid = THREE.Math.generateUUID();
  11. this.name = '';
  12. this.type = 'Object3D';
  13. this.parent = null;
  14. this.children = [];
  15. this.up = THREE.Object3D.DefaultUp.clone();
  16. var position = new THREE.Vector3();
  17. var rotation = new THREE.Euler();
  18. var quaternion = new THREE.Quaternion();
  19. var scale = new THREE.Vector3( 1, 1, 1 );
  20. var onRotationChange = function () {
  21. quaternion.setFromEuler( rotation, false );
  22. };
  23. var onQuaternionChange = function () {
  24. rotation.setFromQuaternion( quaternion, undefined, false );
  25. };
  26. rotation.onChange( onRotationChange );
  27. quaternion.onChange( onQuaternionChange );
  28. Object.defineProperties( this, {
  29. position: {
  30. enumerable: true,
  31. value: position
  32. },
  33. rotation: {
  34. enumerable: true,
  35. value: rotation
  36. },
  37. quaternion: {
  38. enumerable: true,
  39. value: quaternion
  40. },
  41. scale: {
  42. enumerable: true,
  43. value: scale
  44. },
  45. modelViewMatrix: {
  46. value: new THREE.Matrix4()
  47. },
  48. normalMatrix: {
  49. value: new THREE.Matrix3()
  50. }
  51. } );
  52. this.rotationAutoUpdate = true;
  53. this.matrix = new THREE.Matrix4();
  54. this.matrixWorld = new THREE.Matrix4();
  55. this.matrixAutoUpdate = THREE.Object3D.DefaultMatrixAutoUpdate;
  56. this.matrixWorldNeedsUpdate = false;
  57. this.visible = true;
  58. this.castShadow = false;
  59. this.receiveShadow = false;
  60. this.frustumCulled = true;
  61. this.renderOrder = 0;
  62. this.userData = {};
  63. };
  64. THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 1, 0 );
  65. THREE.Object3D.DefaultMatrixAutoUpdate = true;
  66. THREE.Object3D.prototype = {
  67. constructor: THREE.Object3D,
  68. get eulerOrder () {
  69. console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
  70. return this.rotation.order;
  71. },
  72. set eulerOrder ( value ) {
  73. console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
  74. this.rotation.order = value;
  75. },
  76. get useQuaternion () {
  77. console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
  78. },
  79. set useQuaternion ( value ) {
  80. console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
  81. },
  82. set renderDepth ( value ) {
  83. console.warn( 'THREE.Object3D: .renderDepth has been removed. Use .renderOrder, instead.' );
  84. },
  85. applyMatrix: function ( matrix ) {
  86. this.matrix.multiplyMatrices( matrix, this.matrix );
  87. this.matrix.decompose( this.position, this.quaternion, this.scale );
  88. },
  89. setRotationFromAxisAngle: function ( axis, angle ) {
  90. // assumes axis is normalized
  91. this.quaternion.setFromAxisAngle( axis, angle );
  92. },
  93. setRotationFromEuler: function ( euler ) {
  94. this.quaternion.setFromEuler( euler, true );
  95. },
  96. setRotationFromMatrix: function ( m ) {
  97. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  98. this.quaternion.setFromRotationMatrix( m );
  99. },
  100. setRotationFromQuaternion: function ( q ) {
  101. // assumes q is normalized
  102. this.quaternion.copy( q );
  103. },
  104. rotateOnAxis: function () {
  105. // rotate object on axis in object space
  106. // axis is assumed to be normalized
  107. var q1 = new THREE.Quaternion();
  108. return function ( axis, angle ) {
  109. q1.setFromAxisAngle( axis, angle );
  110. this.quaternion.multiply( q1 );
  111. return this;
  112. };
  113. }(),
  114. rotateX: function () {
  115. var v1 = new THREE.Vector3( 1, 0, 0 );
  116. return function ( angle ) {
  117. return this.rotateOnAxis( v1, angle );
  118. };
  119. }(),
  120. rotateY: function () {
  121. var v1 = new THREE.Vector3( 0, 1, 0 );
  122. return function ( angle ) {
  123. return this.rotateOnAxis( v1, angle );
  124. };
  125. }(),
  126. rotateZ: function () {
  127. var v1 = new THREE.Vector3( 0, 0, 1 );
  128. return function ( angle ) {
  129. return this.rotateOnAxis( v1, angle );
  130. };
  131. }(),
  132. translateOnAxis: function () {
  133. // translate object by distance along axis in object space
  134. // axis is assumed to be normalized
  135. var v1 = new THREE.Vector3();
  136. return function ( axis, distance ) {
  137. v1.copy( axis ).applyQuaternion( this.quaternion );
  138. this.position.add( v1.multiplyScalar( distance ) );
  139. return this;
  140. };
  141. }(),
  142. translate: function ( distance, axis ) {
  143. console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
  144. return this.translateOnAxis( axis, distance );
  145. },
  146. translateX: function () {
  147. var v1 = new THREE.Vector3( 1, 0, 0 );
  148. return function ( distance ) {
  149. return this.translateOnAxis( v1, distance );
  150. };
  151. }(),
  152. translateY: function () {
  153. var v1 = new THREE.Vector3( 0, 1, 0 );
  154. return function ( distance ) {
  155. return this.translateOnAxis( v1, distance );
  156. };
  157. }(),
  158. translateZ: function () {
  159. var v1 = new THREE.Vector3( 0, 0, 1 );
  160. return function ( distance ) {
  161. return this.translateOnAxis( v1, distance );
  162. };
  163. }(),
  164. localToWorld: function ( vector ) {
  165. return vector.applyMatrix4( this.matrixWorld );
  166. },
  167. worldToLocal: function () {
  168. var m1 = new THREE.Matrix4();
  169. return function ( vector ) {
  170. return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
  171. };
  172. }(),
  173. lookAt: function () {
  174. // This routine does not support objects with rotated and/or translated parent(s)
  175. var m1 = new THREE.Matrix4();
  176. return function ( vector ) {
  177. m1.lookAt( vector, this.position, this.up );
  178. this.quaternion.setFromRotationMatrix( m1 );
  179. };
  180. }(),
  181. add: function ( object ) {
  182. if ( arguments.length > 1 ) {
  183. for ( var i = 0; i < arguments.length; i ++ ) {
  184. this.add( arguments[ i ] );
  185. }
  186. return this;
  187. }
  188. if ( object === this ) {
  189. console.error( "THREE.Object3D.add: object can't be added as a child of itself.", object );
  190. return this;
  191. }
  192. if ( object instanceof THREE.Object3D ) {
  193. if ( object.parent !== null ) {
  194. object.parent.remove( object );
  195. }
  196. object.parent = this;
  197. object.dispatchEvent( { type: 'added' } );
  198. this.children.push( object );
  199. } else {
  200. console.error( "THREE.Object3D.add: object not an instance of THREE.Object3D.", object );
  201. }
  202. return this;
  203. },
  204. remove: function ( object ) {
  205. if ( arguments.length > 1 ) {
  206. for ( var i = 0; i < arguments.length; i ++ ) {
  207. this.remove( arguments[ i ] );
  208. }
  209. }
  210. var index = this.children.indexOf( object );
  211. if ( index !== - 1 ) {
  212. object.parent = null;
  213. object.dispatchEvent( { type: 'removed' } );
  214. this.children.splice( index, 1 );
  215. }
  216. },
  217. getChildByName: function ( name ) {
  218. console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
  219. return this.getObjectByName( name );
  220. },
  221. getObjectById: function ( id ) {
  222. return this.getObjectByProperty( 'id', id );
  223. },
  224. getObjectByName: function ( name ) {
  225. return this.getObjectByProperty( 'name', name );
  226. },
  227. getObjectByProperty: function ( name, value ) {
  228. if ( this[ name ] === value ) return this;
  229. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  230. var child = this.children[ i ];
  231. var object = child.getObjectByProperty( name, value );
  232. if ( object !== undefined ) {
  233. return object;
  234. }
  235. }
  236. return undefined;
  237. },
  238. getWorldPosition: function ( optionalTarget ) {
  239. var result = optionalTarget || new THREE.Vector3();
  240. this.updateMatrixWorld( true );
  241. return result.setFromMatrixPosition( this.matrixWorld );
  242. },
  243. getWorldQuaternion: function () {
  244. var position = new THREE.Vector3();
  245. var scale = new THREE.Vector3();
  246. return function ( optionalTarget ) {
  247. var result = optionalTarget || new THREE.Quaternion();
  248. this.updateMatrixWorld( true );
  249. this.matrixWorld.decompose( position, result, scale );
  250. return result;
  251. };
  252. }(),
  253. getWorldRotation: function () {
  254. var quaternion = new THREE.Quaternion();
  255. return function ( optionalTarget ) {
  256. var result = optionalTarget || new THREE.Euler();
  257. this.getWorldQuaternion( quaternion );
  258. return result.setFromQuaternion( quaternion, this.rotation.order, false );
  259. };
  260. }(),
  261. getWorldScale: function () {
  262. var position = new THREE.Vector3();
  263. var quaternion = new THREE.Quaternion();
  264. return function ( optionalTarget ) {
  265. var result = optionalTarget || new THREE.Vector3();
  266. this.updateMatrixWorld( true );
  267. this.matrixWorld.decompose( position, quaternion, result );
  268. return result;
  269. };
  270. }(),
  271. getWorldDirection: function () {
  272. var quaternion = new THREE.Quaternion();
  273. return function ( optionalTarget ) {
  274. var result = optionalTarget || new THREE.Vector3();
  275. this.getWorldQuaternion( quaternion );
  276. return result.set( 0, 0, 1 ).applyQuaternion( quaternion );
  277. };
  278. }(),
  279. raycast: function () {},
  280. traverse: function ( callback ) {
  281. callback( this );
  282. var children = this.children;
  283. for ( var i = 0, l = children.length; i < l; i ++ ) {
  284. children[ i ].traverse( callback );
  285. }
  286. },
  287. traverseVisible: function ( callback ) {
  288. if ( this.visible === false ) return;
  289. callback( this );
  290. var children = this.children;
  291. for ( var i = 0, l = children.length; i < l; i ++ ) {
  292. children[ i ].traverseVisible( callback );
  293. }
  294. },
  295. traverseAncestors: function ( callback ) {
  296. var parent = this.parent;
  297. if ( parent !== null ) {
  298. callback( parent );
  299. parent.traverseAncestors( callback );
  300. }
  301. },
  302. updateMatrix: function () {
  303. this.matrix.compose( this.position, this.quaternion, this.scale );
  304. this.matrixWorldNeedsUpdate = true;
  305. },
  306. updateMatrixWorld: function ( force ) {
  307. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  308. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  309. if ( this.parent === null ) {
  310. this.matrixWorld.copy( this.matrix );
  311. } else {
  312. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  313. }
  314. this.matrixWorldNeedsUpdate = false;
  315. force = true;
  316. }
  317. // update children
  318. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  319. this.children[ i ].updateMatrixWorld( force );
  320. }
  321. },
  322. toJSON: function ( meta ) {
  323. var isRootObject = ( meta === undefined );
  324. var data = {};
  325. // meta is a hash used to collect geometries, materials.
  326. // not providing it implies that this is the root object
  327. // being serialized.
  328. if ( isRootObject ) {
  329. // initialize meta obj
  330. meta = {
  331. geometries: {},
  332. materials: {},
  333. textures: {},
  334. images: {}
  335. };
  336. data.metadata = {
  337. version: 4.4,
  338. type: 'Object',
  339. generator: 'Object3D.toJSON'
  340. };
  341. }
  342. // standard Object3D serialization
  343. data.uuid = this.uuid;
  344. data.type = this.type;
  345. if ( this.name !== '' ) data.name = this.name;
  346. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  347. if ( this.visible !== true ) data.visible = this.visible;
  348. data.matrix = this.matrix.toArray();
  349. if ( this.children.length > 0 ) {
  350. data.children = [];
  351. for ( var i = 0; i < this.children.length; i ++ ) {
  352. data.children.push( this.children[ i ].toJSON( meta ).object );
  353. }
  354. }
  355. var output = {};
  356. if ( isRootObject ) {
  357. var geometries = extractFromCache( meta.geometries );
  358. var materials = extractFromCache( meta.materials );
  359. var textures = extractFromCache( meta.textures );
  360. var images = extractFromCache( meta.images );
  361. if ( geometries.length > 0 ) output.geometries = geometries;
  362. if ( materials.length > 0 ) output.materials = materials;
  363. if ( textures.length > 0 ) output.textures = textures;
  364. if ( images.length > 0 ) output.images = images;
  365. }
  366. output.object = data;
  367. return output;
  368. // extract data from the cache hash
  369. // remove metadata on each item
  370. // and return as array
  371. function extractFromCache ( cache ) {
  372. var values = [];
  373. for ( var key in cache ) {
  374. var data = cache[ key ];
  375. delete data.metadata;
  376. values.push( data );
  377. }
  378. return values;
  379. }
  380. },
  381. clone: function ( recursive ) {
  382. return new this.constructor().copy( this, recursive );
  383. },
  384. copy: function ( source, recursive ) {
  385. if ( recursive === undefined ) recursive = true;
  386. this.name = source.name;
  387. this.up.copy( source.up );
  388. this.position.copy( source.position );
  389. this.quaternion.copy( source.quaternion );
  390. this.scale.copy( source.scale );
  391. this.rotationAutoUpdate = source.rotationAutoUpdate;
  392. this.matrix.copy( source.matrix );
  393. this.matrixWorld.copy( source.matrixWorld );
  394. this.matrixAutoUpdate = source.matrixAutoUpdate;
  395. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  396. this.visible = source.visible;
  397. this.castShadow = source.castShadow;
  398. this.receiveShadow = source.receiveShadow;
  399. this.frustumCulled = source.frustumCulled;
  400. this.renderOrder = source.renderOrder;
  401. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  402. if ( recursive === true ) {
  403. for ( var i = 0; i < source.children.length; i ++ ) {
  404. var child = source.children[ i ];
  405. this.add( child.clone() );
  406. }
  407. }
  408. return this;
  409. }
  410. };
  411. THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype );
  412. THREE.Object3DIdCount = 0;