Object3D.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. import { Quaternion } from '../math/Quaternion.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { EventDispatcher } from './EventDispatcher.js';
  5. import { Euler } from '../math/Euler.js';
  6. import { Layers } from './Layers.js';
  7. import { Matrix3 } from '../math/Matrix3.js';
  8. import { _Math } from '../math/Math.js';
  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. var object3DId = 0;
  17. function Object3D() {
  18. Object.defineProperty( this, 'id', { value: object3DId ++ } );
  19. this.uuid = _Math.generateUUID();
  20. this.name = '';
  21. this.type = 'Object3D';
  22. this.parent = null;
  23. this.children = [];
  24. this.up = Object3D.DefaultUp.clone();
  25. var position = new Vector3();
  26. var rotation = new Euler();
  27. var quaternion = new Quaternion();
  28. var scale = new Vector3( 1, 1, 1 );
  29. function onRotationChange() {
  30. quaternion.setFromEuler( rotation, false );
  31. }
  32. function onQuaternionChange() {
  33. rotation.setFromQuaternion( quaternion, undefined, false );
  34. }
  35. rotation.onChange( onRotationChange );
  36. quaternion.onChange( onQuaternionChange );
  37. Object.defineProperties( this, {
  38. position: {
  39. configurable: true,
  40. enumerable: true,
  41. value: position
  42. },
  43. rotation: {
  44. configurable: true,
  45. enumerable: true,
  46. value: rotation
  47. },
  48. quaternion: {
  49. configurable: true,
  50. enumerable: true,
  51. value: quaternion
  52. },
  53. scale: {
  54. configurable: true,
  55. enumerable: true,
  56. value: scale
  57. },
  58. modelViewMatrix: {
  59. value: new Matrix4()
  60. },
  61. normalMatrix: {
  62. value: new Matrix3()
  63. }
  64. } );
  65. this.matrix = new Matrix4();
  66. this.matrixWorld = new Matrix4();
  67. this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate;
  68. this.matrixWorldNeedsUpdate = false;
  69. this.layers = new Layers();
  70. this.visible = true;
  71. this.castShadow = false;
  72. this.receiveShadow = false;
  73. this.frustumCulled = true;
  74. this.renderOrder = 0;
  75. this.userData = {};
  76. }
  77. Object3D.DefaultUp = new Vector3( 0, 1, 0 );
  78. Object3D.DefaultMatrixAutoUpdate = true;
  79. Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
  80. constructor: Object3D,
  81. isObject3D: true,
  82. onBeforeRender: function () {},
  83. onAfterRender: function () {},
  84. applyMatrix: function ( matrix ) {
  85. this.matrix.multiplyMatrices( matrix, this.matrix );
  86. this.matrix.decompose( this.position, this.quaternion, this.scale );
  87. },
  88. applyQuaternion: function ( q ) {
  89. this.quaternion.premultiply( q );
  90. return this;
  91. },
  92. setRotationFromAxisAngle: function ( axis, angle ) {
  93. // assumes axis is normalized
  94. this.quaternion.setFromAxisAngle( axis, angle );
  95. },
  96. setRotationFromEuler: function ( euler ) {
  97. this.quaternion.setFromEuler( euler, true );
  98. },
  99. setRotationFromMatrix: function ( m ) {
  100. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  101. this.quaternion.setFromRotationMatrix( m );
  102. },
  103. setRotationFromQuaternion: function ( q ) {
  104. // assumes q is normalized
  105. this.quaternion.copy( q );
  106. },
  107. rotateOnAxis: function () {
  108. // rotate object on axis in object space
  109. // axis is assumed to be normalized
  110. var q1 = new Quaternion();
  111. return function rotateOnAxis( axis, angle ) {
  112. q1.setFromAxisAngle( axis, angle );
  113. this.quaternion.multiply( q1 );
  114. return this;
  115. };
  116. }(),
  117. rotateOnWorldAxis: function () {
  118. // rotate object on axis in world space
  119. // axis is assumed to be normalized
  120. // method assumes no rotated parent
  121. var q1 = new Quaternion();
  122. return function rotateOnWorldAxis( axis, angle ) {
  123. q1.setFromAxisAngle( axis, angle );
  124. this.quaternion.premultiply( q1 );
  125. return this;
  126. };
  127. }(),
  128. rotateX: function () {
  129. var v1 = new Vector3( 1, 0, 0 );
  130. return function rotateX( angle ) {
  131. return this.rotateOnAxis( v1, angle );
  132. };
  133. }(),
  134. rotateY: function () {
  135. var v1 = new Vector3( 0, 1, 0 );
  136. return function rotateY( angle ) {
  137. return this.rotateOnAxis( v1, angle );
  138. };
  139. }(),
  140. rotateZ: function () {
  141. var v1 = new Vector3( 0, 0, 1 );
  142. return function rotateZ( angle ) {
  143. return this.rotateOnAxis( v1, angle );
  144. };
  145. }(),
  146. translateOnAxis: function () {
  147. // translate object by distance along axis in object space
  148. // axis is assumed to be normalized
  149. var v1 = new Vector3();
  150. return function translateOnAxis( axis, distance ) {
  151. v1.copy( axis ).applyQuaternion( this.quaternion );
  152. this.position.add( v1.multiplyScalar( distance ) );
  153. return this;
  154. };
  155. }(),
  156. translateX: function () {
  157. var v1 = new Vector3( 1, 0, 0 );
  158. return function translateX( distance ) {
  159. return this.translateOnAxis( v1, distance );
  160. };
  161. }(),
  162. translateY: function () {
  163. var v1 = new Vector3( 0, 1, 0 );
  164. return function translateY( distance ) {
  165. return this.translateOnAxis( v1, distance );
  166. };
  167. }(),
  168. translateZ: function () {
  169. var v1 = new Vector3( 0, 0, 1 );
  170. return function translateZ( distance ) {
  171. return this.translateOnAxis( v1, distance );
  172. };
  173. }(),
  174. localToWorld: function ( vector ) {
  175. return vector.applyMatrix4( this.matrixWorld );
  176. },
  177. worldToLocal: function () {
  178. var m1 = new Matrix4();
  179. return function worldToLocal( vector ) {
  180. return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
  181. };
  182. }(),
  183. lookAt: function () {
  184. // This method does not support objects having non-uniformly-scaled parent(s)
  185. var q1 = new Quaternion();
  186. var m1 = new Matrix4();
  187. var target = new Vector3();
  188. var position = new Vector3();
  189. return function lookAt( x, y, z ) {
  190. if ( x.isVector3 ) {
  191. target.copy( x );
  192. } else {
  193. target.set( x, y, z );
  194. }
  195. var parent = this.parent;
  196. this.updateWorldMatrix( true, false );
  197. position.setFromMatrixPosition( this.matrixWorld );
  198. if ( this.isCamera ) {
  199. m1.lookAt( position, target, this.up );
  200. } else {
  201. m1.lookAt( target, position, this.up );
  202. }
  203. this.quaternion.setFromRotationMatrix( m1 );
  204. if ( parent ) {
  205. m1.extractRotation( parent.matrixWorld );
  206. q1.setFromRotationMatrix( m1 );
  207. this.quaternion.premultiply( q1.inverse() );
  208. }
  209. };
  210. }(),
  211. add: function ( object ) {
  212. if ( arguments.length > 1 ) {
  213. for ( var i = 0; i < arguments.length; i ++ ) {
  214. this.add( arguments[ i ] );
  215. }
  216. return this;
  217. }
  218. if ( object === this ) {
  219. console.error( "THREE.Object3D.add: object can't be added as a child of itself.", object );
  220. return this;
  221. }
  222. if ( ( object && object.isObject3D ) ) {
  223. if ( object.parent !== null ) {
  224. object.parent.remove( object );
  225. }
  226. object.parent = this;
  227. object.dispatchEvent( { type: 'added' } );
  228. this.children.push( object );
  229. } else {
  230. console.error( "THREE.Object3D.add: object not an instance of THREE.Object3D.", object );
  231. }
  232. return this;
  233. },
  234. remove: function ( object ) {
  235. if ( arguments.length > 1 ) {
  236. for ( var i = 0; i < arguments.length; i ++ ) {
  237. this.remove( arguments[ i ] );
  238. }
  239. return this;
  240. }
  241. var index = this.children.indexOf( object );
  242. if ( index !== - 1 ) {
  243. object.parent = null;
  244. object.dispatchEvent( { type: 'removed' } );
  245. this.children.splice( index, 1 );
  246. }
  247. return this;
  248. },
  249. getObjectById: function ( id ) {
  250. return this.getObjectByProperty( 'id', id );
  251. },
  252. getObjectByName: function ( name ) {
  253. return this.getObjectByProperty( 'name', name );
  254. },
  255. getObjectByProperty: function ( name, value ) {
  256. if ( this[ name ] === value ) return this;
  257. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  258. var child = this.children[ i ];
  259. var object = child.getObjectByProperty( name, value );
  260. if ( object !== undefined ) {
  261. return object;
  262. }
  263. }
  264. return undefined;
  265. },
  266. getWorldPosition: function ( target ) {
  267. if ( target === undefined ) {
  268. console.warn( 'THREE.Object3D: .getWorldPosition() target is now required' );
  269. target = new Vector3();
  270. }
  271. this.updateMatrixWorld( true );
  272. return target.setFromMatrixPosition( this.matrixWorld );
  273. },
  274. getWorldQuaternion: function () {
  275. var position = new Vector3();
  276. var scale = new Vector3();
  277. return function getWorldQuaternion( target ) {
  278. if ( target === undefined ) {
  279. console.warn( 'THREE.Object3D: .getWorldQuaternion() target is now required' );
  280. target = new Quaternion();
  281. }
  282. this.updateMatrixWorld( true );
  283. this.matrixWorld.decompose( position, target, scale );
  284. return target;
  285. };
  286. }(),
  287. getWorldScale: function () {
  288. var position = new Vector3();
  289. var quaternion = new Quaternion();
  290. return function getWorldScale( target ) {
  291. if ( target === undefined ) {
  292. console.warn( 'THREE.Object3D: .getWorldScale() target is now required' );
  293. target = new Vector3();
  294. }
  295. this.updateMatrixWorld( true );
  296. this.matrixWorld.decompose( position, quaternion, target );
  297. return target;
  298. };
  299. }(),
  300. getWorldDirection: function ( target ) {
  301. if ( target === undefined ) {
  302. console.warn( 'THREE.Object3D: .getWorldDirection() target is now required' );
  303. target = new Vector3();
  304. }
  305. this.updateMatrixWorld( true );
  306. var e = this.matrixWorld.elements;
  307. return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
  308. },
  309. raycast: function () {},
  310. traverse: function ( callback ) {
  311. callback( this );
  312. var children = this.children;
  313. for ( var i = 0, l = children.length; i < l; i ++ ) {
  314. children[ i ].traverse( callback );
  315. }
  316. },
  317. traverseVisible: function ( callback ) {
  318. if ( this.visible === false ) return;
  319. callback( this );
  320. var children = this.children;
  321. for ( var i = 0, l = children.length; i < l; i ++ ) {
  322. children[ i ].traverseVisible( callback );
  323. }
  324. },
  325. traverseAncestors: function ( callback ) {
  326. var parent = this.parent;
  327. if ( parent !== null ) {
  328. callback( parent );
  329. parent.traverseAncestors( callback );
  330. }
  331. },
  332. updateMatrix: function () {
  333. this.matrix.compose( this.position, this.quaternion, this.scale );
  334. this.matrixWorldNeedsUpdate = true;
  335. },
  336. updateMatrixWorld: function ( force ) {
  337. if ( this.matrixAutoUpdate ) this.updateMatrix();
  338. if ( this.matrixWorldNeedsUpdate || force ) {
  339. if ( this.parent === null ) {
  340. this.matrixWorld.copy( this.matrix );
  341. } else {
  342. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  343. }
  344. this.matrixWorldNeedsUpdate = false;
  345. force = true;
  346. }
  347. // update children
  348. var children = this.children;
  349. for ( var i = 0, l = children.length; i < l; i ++ ) {
  350. children[ i ].updateMatrixWorld( force );
  351. }
  352. },
  353. updateWorldMatrix: function ( updateParents, updateChildren ) {
  354. var parent = this.parent;
  355. if ( updateParents === true && parent !== null ) {
  356. parent.updateWorldMatrix( true, false );
  357. }
  358. if ( this.matrixAutoUpdate ) this.updateMatrix();
  359. if ( this.parent === null ) {
  360. this.matrixWorld.copy( this.matrix );
  361. } else {
  362. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  363. }
  364. // update children
  365. if ( updateChildren === true ) {
  366. var children = this.children;
  367. for ( var i = 0, l = children.length; i < l; i ++ ) {
  368. children[ i ].updateWorldMatrix( false, true );
  369. }
  370. }
  371. },
  372. toJSON: function ( meta ) {
  373. // meta is a string when called from JSON.stringify
  374. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  375. var output = {};
  376. // meta is a hash used to collect geometries, materials.
  377. // not providing it implies that this is the root object
  378. // being serialized.
  379. if ( isRootObject ) {
  380. // initialize meta obj
  381. meta = {
  382. geometries: {},
  383. materials: {},
  384. textures: {},
  385. images: {},
  386. shapes: {}
  387. };
  388. output.metadata = {
  389. version: 4.5,
  390. type: 'Object',
  391. generator: 'Object3D.toJSON'
  392. };
  393. }
  394. // standard Object3D serialization
  395. var object = {};
  396. object.uuid = this.uuid;
  397. object.type = this.type;
  398. if ( this.name !== '' ) object.name = this.name;
  399. if ( this.castShadow === true ) object.castShadow = true;
  400. if ( this.receiveShadow === true ) object.receiveShadow = true;
  401. if ( this.visible === false ) object.visible = false;
  402. if ( this.frustumCulled === false ) object.frustumCulled = false;
  403. if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder;
  404. if ( JSON.stringify( this.userData ) !== '{}' ) object.userData = this.userData;
  405. object.layers = this.layers.mask;
  406. object.matrix = this.matrix.toArray();
  407. if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
  408. //
  409. function serialize( library, element ) {
  410. if ( library[ element.uuid ] === undefined ) {
  411. library[ element.uuid ] = element.toJSON( meta );
  412. }
  413. return element.uuid;
  414. }
  415. if ( this.isMesh || this.isLine || this.isPoints ) {
  416. object.geometry = serialize( meta.geometries, this.geometry );
  417. var parameters = this.geometry.parameters;
  418. if ( parameters !== undefined && parameters.shapes !== undefined ) {
  419. var shapes = parameters.shapes;
  420. if ( Array.isArray( shapes ) ) {
  421. for ( var i = 0, l = shapes.length; i < l; i ++ ) {
  422. var shape = shapes[ i ];
  423. serialize( meta.shapes, shape );
  424. }
  425. } else {
  426. serialize( meta.shapes, shapes );
  427. }
  428. }
  429. }
  430. if ( this.material !== undefined ) {
  431. if ( Array.isArray( this.material ) ) {
  432. var uuids = [];
  433. for ( var i = 0, l = this.material.length; i < l; i ++ ) {
  434. uuids.push( serialize( meta.materials, this.material[ i ] ) );
  435. }
  436. object.material = uuids;
  437. } else {
  438. object.material = serialize( meta.materials, this.material );
  439. }
  440. }
  441. //
  442. if ( this.children.length > 0 ) {
  443. object.children = [];
  444. for ( var i = 0; i < this.children.length; i ++ ) {
  445. object.children.push( this.children[ i ].toJSON( meta ).object );
  446. }
  447. }
  448. if ( isRootObject ) {
  449. var geometries = extractFromCache( meta.geometries );
  450. var materials = extractFromCache( meta.materials );
  451. var textures = extractFromCache( meta.textures );
  452. var images = extractFromCache( meta.images );
  453. var shapes = extractFromCache( meta.shapes );
  454. if ( geometries.length > 0 ) output.geometries = geometries;
  455. if ( materials.length > 0 ) output.materials = materials;
  456. if ( textures.length > 0 ) output.textures = textures;
  457. if ( images.length > 0 ) output.images = images;
  458. if ( shapes.length > 0 ) output.shapes = shapes;
  459. }
  460. output.object = object;
  461. return output;
  462. // extract data from the cache hash
  463. // remove metadata on each item
  464. // and return as array
  465. function extractFromCache( cache ) {
  466. var values = [];
  467. for ( var key in cache ) {
  468. var data = cache[ key ];
  469. delete data.metadata;
  470. values.push( data );
  471. }
  472. return values;
  473. }
  474. },
  475. clone: function ( recursive ) {
  476. return new this.constructor().copy( this, recursive );
  477. },
  478. copy: function ( source, recursive ) {
  479. if ( recursive === undefined ) recursive = true;
  480. this.name = source.name;
  481. this.up.copy( source.up );
  482. this.position.copy( source.position );
  483. this.quaternion.copy( source.quaternion );
  484. this.scale.copy( source.scale );
  485. this.matrix.copy( source.matrix );
  486. this.matrixWorld.copy( source.matrixWorld );
  487. this.matrixAutoUpdate = source.matrixAutoUpdate;
  488. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  489. this.layers.mask = source.layers.mask;
  490. this.visible = source.visible;
  491. this.castShadow = source.castShadow;
  492. this.receiveShadow = source.receiveShadow;
  493. this.frustumCulled = source.frustumCulled;
  494. this.renderOrder = source.renderOrder;
  495. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  496. if ( recursive === true ) {
  497. for ( var i = 0; i < source.children.length; i ++ ) {
  498. var child = source.children[ i ];
  499. this.add( child.clone() );
  500. }
  501. }
  502. return this;
  503. }
  504. } );
  505. export { Object3D };