Object3D.js 13 KB

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