Object3D.js 13 KB

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