Object3D.js 15 KB

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