Object3D.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. var vector = new Vector3();
  171. return function lookAt( x, y, z ) {
  172. if ( x.isVector3 ) {
  173. vector.copy( x );
  174. } else {
  175. vector.set( x, y, z );
  176. }
  177. if ( this.isCamera ) {
  178. m1.lookAt( this.position, vector, this.up );
  179. } else {
  180. m1.lookAt( vector, this.position, this.up );
  181. }
  182. this.quaternion.setFromRotationMatrix( m1 );
  183. };
  184. }(),
  185. add: function ( object ) {
  186. if ( arguments.length > 1 ) {
  187. for ( var i = 0; i < arguments.length; i ++ ) {
  188. this.add( arguments[ i ] );
  189. }
  190. return this;
  191. }
  192. if ( object === this ) {
  193. console.error( "THREE.Object3D.add: object can't be added as a child of itself.", object );
  194. return this;
  195. }
  196. if ( ( object && object.isObject3D ) ) {
  197. if ( object.parent !== null ) {
  198. object.parent.remove( object );
  199. }
  200. object.parent = this;
  201. object.dispatchEvent( { type: 'added' } );
  202. this.children.push( object );
  203. } else {
  204. console.error( "THREE.Object3D.add: object not an instance of THREE.Object3D.", object );
  205. }
  206. return this;
  207. },
  208. remove: function ( object ) {
  209. if ( arguments.length > 1 ) {
  210. for ( var i = 0; i < arguments.length; i ++ ) {
  211. this.remove( arguments[ i ] );
  212. }
  213. return this;
  214. }
  215. var index = this.children.indexOf( object );
  216. if ( index !== - 1 ) {
  217. object.parent = null;
  218. object.dispatchEvent( { type: 'removed' } );
  219. this.children.splice( index, 1 );
  220. }
  221. return this;
  222. },
  223. getObjectById: function ( id ) {
  224. return this.getObjectByProperty( 'id', id );
  225. },
  226. getObjectByName: function ( name ) {
  227. return this.getObjectByProperty( 'name', name );
  228. },
  229. getObjectByProperty: function ( name, value ) {
  230. if ( this[ name ] === value ) return this;
  231. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  232. var child = this.children[ i ];
  233. var object = child.getObjectByProperty( name, value );
  234. if ( object !== undefined ) {
  235. return object;
  236. }
  237. }
  238. return undefined;
  239. },
  240. getWorldPosition: function ( optionalTarget ) {
  241. var result = optionalTarget || new Vector3();
  242. this.updateMatrixWorld( true );
  243. return result.setFromMatrixPosition( this.matrixWorld );
  244. },
  245. getWorldQuaternion: function () {
  246. var position = new Vector3();
  247. var scale = new Vector3();
  248. return function getWorldQuaternion( optionalTarget ) {
  249. var result = optionalTarget || new Quaternion();
  250. this.updateMatrixWorld( true );
  251. this.matrixWorld.decompose( position, result, scale );
  252. return result;
  253. };
  254. }(),
  255. getWorldRotation: function () {
  256. var quaternion = new Quaternion();
  257. return function getWorldRotation( optionalTarget ) {
  258. var result = optionalTarget || new Euler();
  259. this.getWorldQuaternion( quaternion );
  260. return result.setFromQuaternion( quaternion, this.rotation.order, false );
  261. };
  262. }(),
  263. getWorldScale: function () {
  264. var position = new Vector3();
  265. var quaternion = new Quaternion();
  266. return function getWorldScale( optionalTarget ) {
  267. var result = optionalTarget || new Vector3();
  268. this.updateMatrixWorld( true );
  269. this.matrixWorld.decompose( position, quaternion, result );
  270. return result;
  271. };
  272. }(),
  273. getWorldDirection: function () {
  274. var quaternion = new Quaternion();
  275. return function getWorldDirection( optionalTarget ) {
  276. var result = optionalTarget || new Vector3();
  277. this.getWorldQuaternion( quaternion );
  278. return result.set( 0, 0, 1 ).applyQuaternion( quaternion );
  279. };
  280. }(),
  281. raycast: function () {},
  282. traverse: function ( callback ) {
  283. callback( this );
  284. var children = this.children;
  285. for ( var i = 0, l = children.length; i < l; i ++ ) {
  286. children[ i ].traverse( callback );
  287. }
  288. },
  289. traverseVisible: function ( callback ) {
  290. if ( this.visible === false ) return;
  291. callback( this );
  292. var children = this.children;
  293. for ( var i = 0, l = children.length; i < l; i ++ ) {
  294. children[ i ].traverseVisible( callback );
  295. }
  296. },
  297. traverseAncestors: function ( callback ) {
  298. var parent = this.parent;
  299. if ( parent !== null ) {
  300. callback( parent );
  301. parent.traverseAncestors( callback );
  302. }
  303. },
  304. updateMatrix: function () {
  305. this.matrix.compose( this.position, this.quaternion, this.scale );
  306. this.matrixWorldNeedsUpdate = true;
  307. },
  308. updateMatrixWorld: function ( force ) {
  309. if ( this.matrixAutoUpdate ) this.updateMatrix();
  310. if ( this.matrixWorldNeedsUpdate || force ) {
  311. if ( this.parent === null ) {
  312. this.matrixWorld.copy( this.matrix );
  313. } else {
  314. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  315. }
  316. this.matrixWorldNeedsUpdate = false;
  317. force = true;
  318. }
  319. // update children
  320. var children = this.children;
  321. for ( var i = 0, l = children.length; i < l; i ++ ) {
  322. children[ i ].updateMatrixWorld( force );
  323. }
  324. },
  325. toJSON: function ( meta ) {
  326. // meta is a string when called from JSON.stringify
  327. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  328. var output = {};
  329. // meta is a hash used to collect geometries, materials.
  330. // not providing it implies that this is the root object
  331. // being serialized.
  332. if ( isRootObject ) {
  333. // initialize meta obj
  334. meta = {
  335. geometries: {},
  336. materials: {},
  337. textures: {},
  338. images: {}
  339. };
  340. output.metadata = {
  341. version: 4.5,
  342. type: 'Object',
  343. generator: 'Object3D.toJSON'
  344. };
  345. }
  346. // standard Object3D serialization
  347. var object = {};
  348. object.uuid = this.uuid;
  349. object.type = this.type;
  350. if ( this.name !== '' ) object.name = this.name;
  351. if ( this.castShadow === true ) object.castShadow = true;
  352. if ( this.receiveShadow === true ) object.receiveShadow = true;
  353. if ( this.visible === false ) object.visible = false;
  354. if ( JSON.stringify( this.userData ) !== '{}' ) object.userData = this.userData;
  355. object.matrix = this.matrix.toArray();
  356. //
  357. function serialize( library, element ) {
  358. if ( library[ element.uuid ] === undefined ) {
  359. library[ element.uuid ] = element.toJSON( meta );
  360. }
  361. return element.uuid;
  362. }
  363. if ( this.geometry !== undefined ) {
  364. object.geometry = serialize( meta.geometries, this.geometry );
  365. }
  366. if ( this.material !== undefined ) {
  367. if ( Array.isArray( this.material ) ) {
  368. var uuids = [];
  369. for ( var i = 0, l = this.material.length; i < l; i ++ ) {
  370. uuids.push( serialize( meta.materials, this.material[ i ] ) );
  371. }
  372. object.material = uuids;
  373. } else {
  374. object.material = serialize( meta.materials, this.material );
  375. }
  376. }
  377. //
  378. if ( this.children.length > 0 ) {
  379. object.children = [];
  380. for ( var i = 0; i < this.children.length; i ++ ) {
  381. object.children.push( this.children[ i ].toJSON( meta ).object );
  382. }
  383. }
  384. if ( isRootObject ) {
  385. var geometries = extractFromCache( meta.geometries );
  386. var materials = extractFromCache( meta.materials );
  387. var textures = extractFromCache( meta.textures );
  388. var images = extractFromCache( meta.images );
  389. if ( geometries.length > 0 ) output.geometries = geometries;
  390. if ( materials.length > 0 ) output.materials = materials;
  391. if ( textures.length > 0 ) output.textures = textures;
  392. if ( images.length > 0 ) output.images = images;
  393. }
  394. output.object = object;
  395. return output;
  396. // extract data from the cache hash
  397. // remove metadata on each item
  398. // and return as array
  399. function extractFromCache( cache ) {
  400. var values = [];
  401. for ( var key in cache ) {
  402. var data = cache[ key ];
  403. delete data.metadata;
  404. values.push( data );
  405. }
  406. return values;
  407. }
  408. },
  409. clone: function ( recursive ) {
  410. return new this.constructor().copy( this, recursive );
  411. },
  412. copy: function ( source, recursive ) {
  413. if ( recursive === undefined ) recursive = true;
  414. this.name = source.name;
  415. this.up.copy( source.up );
  416. this.position.copy( source.position );
  417. this.quaternion.copy( source.quaternion );
  418. this.scale.copy( source.scale );
  419. this.matrix.copy( source.matrix );
  420. this.matrixWorld.copy( source.matrixWorld );
  421. this.matrixAutoUpdate = source.matrixAutoUpdate;
  422. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  423. this.layers.mask = source.layers.mask;
  424. this.visible = source.visible;
  425. this.castShadow = source.castShadow;
  426. this.receiveShadow = source.receiveShadow;
  427. this.frustumCulled = source.frustumCulled;
  428. this.renderOrder = source.renderOrder;
  429. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  430. if ( recursive === true ) {
  431. for ( var i = 0; i < source.children.length; i ++ ) {
  432. var child = source.children[ i ];
  433. this.add( child.clone() );
  434. }
  435. }
  436. return this;
  437. }
  438. } );
  439. export { Object3D };
粤ICP备19079148号