Object3D.js 13 KB

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