Object3D.js 13 KB

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