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