Object3D.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 * as MathUtils from '../math/MathUtils.js';
  9. let _object3DId = 0;
  10. const _v1 = /*@__PURE__*/ new Vector3();
  11. const _q1 = /*@__PURE__*/ new Quaternion();
  12. const _m1 = /*@__PURE__*/ new Matrix4();
  13. const _target = /*@__PURE__*/ new Vector3();
  14. const _position = /*@__PURE__*/ new Vector3();
  15. const _scale = /*@__PURE__*/ new Vector3();
  16. const _quaternion = /*@__PURE__*/ new Quaternion();
  17. const _xAxis = /*@__PURE__*/ new Vector3( 1, 0, 0 );
  18. const _yAxis = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  19. const _zAxis = /*@__PURE__*/ new Vector3( 0, 0, 1 );
  20. const _addedEvent = { type: 'added' };
  21. const _removedEvent = { type: 'removed' };
  22. class Object3D extends EventDispatcher {
  23. constructor() {
  24. super();
  25. this.isObject3D = true;
  26. Object.defineProperty( this, 'id', { value: _object3DId ++ } );
  27. this.uuid = MathUtils.generateUUID();
  28. this.name = '';
  29. this.type = 'Object3D';
  30. this.parent = null;
  31. this.children = [];
  32. this.up = Object3D.DefaultUp.clone();
  33. const position = new Vector3();
  34. const rotation = new Euler();
  35. const quaternion = new Quaternion();
  36. const scale = new Vector3( 1, 1, 1 );
  37. function onRotationChange() {
  38. quaternion.setFromEuler( rotation, false );
  39. }
  40. function onQuaternionChange() {
  41. rotation.setFromQuaternion( quaternion, undefined, false );
  42. }
  43. rotation._onChange( onRotationChange );
  44. quaternion._onChange( onQuaternionChange );
  45. Object.defineProperties( this, {
  46. position: {
  47. configurable: true,
  48. enumerable: true,
  49. value: position
  50. },
  51. rotation: {
  52. configurable: true,
  53. enumerable: true,
  54. value: rotation
  55. },
  56. quaternion: {
  57. configurable: true,
  58. enumerable: true,
  59. value: quaternion
  60. },
  61. scale: {
  62. configurable: true,
  63. enumerable: true,
  64. value: scale
  65. },
  66. modelViewMatrix: {
  67. value: new Matrix4()
  68. },
  69. normalMatrix: {
  70. value: new Matrix3()
  71. }
  72. } );
  73. this.matrix = new Matrix4();
  74. this.matrixWorld = new Matrix4();
  75. this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate;
  76. this.matrixWorldNeedsUpdate = false;
  77. this.matrixWorldAutoUpdate = Object3D.DefaultMatrixWorldAutoUpdate; // checked by the renderer
  78. this.layers = new Layers();
  79. this.visible = true;
  80. this.castShadow = false;
  81. this.receiveShadow = false;
  82. this.frustumCulled = true;
  83. this.renderOrder = 0;
  84. this.animations = [];
  85. this.userData = {};
  86. }
  87. onBeforeRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  88. onAfterRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  89. applyMatrix4( matrix ) {
  90. if ( this.matrixAutoUpdate ) this.updateMatrix();
  91. this.matrix.premultiply( matrix );
  92. this.matrix.decompose( this.position, this.quaternion, this.scale );
  93. }
  94. applyQuaternion( q ) {
  95. this.quaternion.premultiply( q );
  96. return this;
  97. }
  98. setRotationFromAxisAngle( axis, angle ) {
  99. // assumes axis is normalized
  100. this.quaternion.setFromAxisAngle( axis, angle );
  101. }
  102. setRotationFromEuler( euler ) {
  103. this.quaternion.setFromEuler( euler, true );
  104. }
  105. setRotationFromMatrix( m ) {
  106. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  107. this.quaternion.setFromRotationMatrix( m );
  108. }
  109. setRotationFromQuaternion( q ) {
  110. // assumes q is normalized
  111. this.quaternion.copy( q );
  112. }
  113. rotateOnAxis( axis, angle ) {
  114. // rotate object on axis in object space
  115. // axis is assumed to be normalized
  116. _q1.setFromAxisAngle( axis, angle );
  117. this.quaternion.multiply( _q1 );
  118. return this;
  119. }
  120. rotateOnWorldAxis( axis, angle ) {
  121. // rotate object on axis in world space
  122. // axis is assumed to be normalized
  123. // method assumes no rotated parent
  124. _q1.setFromAxisAngle( axis, angle );
  125. this.quaternion.premultiply( _q1 );
  126. return this;
  127. }
  128. rotateX( angle ) {
  129. return this.rotateOnAxis( _xAxis, angle );
  130. }
  131. rotateY( angle ) {
  132. return this.rotateOnAxis( _yAxis, angle );
  133. }
  134. rotateZ( angle ) {
  135. return this.rotateOnAxis( _zAxis, angle );
  136. }
  137. translateOnAxis( axis, distance ) {
  138. // translate object by distance along axis in object space
  139. // axis is assumed to be normalized
  140. _v1.copy( axis ).applyQuaternion( this.quaternion );
  141. this.position.add( _v1.multiplyScalar( distance ) );
  142. return this;
  143. }
  144. translateX( distance ) {
  145. return this.translateOnAxis( _xAxis, distance );
  146. }
  147. translateY( distance ) {
  148. return this.translateOnAxis( _yAxis, distance );
  149. }
  150. translateZ( distance ) {
  151. return this.translateOnAxis( _zAxis, distance );
  152. }
  153. localToWorld( vector ) {
  154. return vector.applyMatrix4( this.matrixWorld );
  155. }
  156. worldToLocal( vector ) {
  157. return vector.applyMatrix4( _m1.copy( this.matrixWorld ).invert() );
  158. }
  159. lookAt( x, y, z ) {
  160. // This method does not support objects having non-uniformly-scaled parent(s)
  161. if ( x.isVector3 ) {
  162. _target.copy( x );
  163. } else {
  164. _target.set( x, y, z );
  165. }
  166. const parent = this.parent;
  167. this.updateWorldMatrix( true, false );
  168. _position.setFromMatrixPosition( this.matrixWorld );
  169. if ( this.isCamera || this.isLight ) {
  170. _m1.lookAt( _position, _target, this.up );
  171. } else {
  172. _m1.lookAt( _target, _position, this.up );
  173. }
  174. this.quaternion.setFromRotationMatrix( _m1 );
  175. if ( parent ) {
  176. _m1.extractRotation( parent.matrixWorld );
  177. _q1.setFromRotationMatrix( _m1 );
  178. this.quaternion.premultiply( _q1.invert() );
  179. }
  180. }
  181. add( object ) {
  182. if ( arguments.length > 1 ) {
  183. for ( let i = 0; i < arguments.length; i ++ ) {
  184. this.add( arguments[ i ] );
  185. }
  186. return this;
  187. }
  188. if ( object === this ) {
  189. console.error( 'THREE.Object3D.add: object can\'t be added as a child of itself.', object );
  190. return this;
  191. }
  192. if ( object && object.isObject3D ) {
  193. if ( object.parent !== null ) {
  194. object.parent.remove( object );
  195. }
  196. object.parent = this;
  197. this.children.push( object );
  198. object.dispatchEvent( _addedEvent );
  199. } else {
  200. console.error( 'THREE.Object3D.add: object not an instance of THREE.Object3D.', object );
  201. }
  202. return this;
  203. }
  204. remove( object ) {
  205. if ( arguments.length > 1 ) {
  206. for ( let i = 0; i < arguments.length; i ++ ) {
  207. this.remove( arguments[ i ] );
  208. }
  209. return this;
  210. }
  211. const index = this.children.indexOf( object );
  212. if ( index !== - 1 ) {
  213. object.parent = null;
  214. this.children.splice( index, 1 );
  215. object.dispatchEvent( _removedEvent );
  216. }
  217. return this;
  218. }
  219. removeFromParent() {
  220. const parent = this.parent;
  221. if ( parent !== null ) {
  222. parent.remove( this );
  223. }
  224. return this;
  225. }
  226. clear() {
  227. for ( let i = 0; i < this.children.length; i ++ ) {
  228. const object = this.children[ i ];
  229. object.parent = null;
  230. object.dispatchEvent( _removedEvent );
  231. }
  232. this.children.length = 0;
  233. return this;
  234. }
  235. attach( object ) {
  236. // adds object as a child of this, while maintaining the object's world transform
  237. // Note: This method does not support scene graphs having non-uniformly-scaled nodes(s)
  238. this.updateWorldMatrix( true, false );
  239. _m1.copy( this.matrixWorld ).invert();
  240. if ( object.parent !== null ) {
  241. object.parent.updateWorldMatrix( true, false );
  242. _m1.multiply( object.parent.matrixWorld );
  243. }
  244. object.applyMatrix4( _m1 );
  245. this.add( object );
  246. object.updateWorldMatrix( false, true );
  247. return this;
  248. }
  249. getObjectById( id ) {
  250. return this.getObjectByProperty( 'id', id );
  251. }
  252. getObjectByName( name ) {
  253. return this.getObjectByProperty( 'name', name );
  254. }
  255. getObjectByProperty( name, value ) {
  256. if ( this[ name ] === value ) return this;
  257. for ( let i = 0, l = this.children.length; i < l; i ++ ) {
  258. const child = this.children[ i ];
  259. const object = child.getObjectByProperty( name, value );
  260. if ( object !== undefined ) {
  261. return object;
  262. }
  263. }
  264. return undefined;
  265. }
  266. getWorldPosition( target ) {
  267. this.updateWorldMatrix( true, false );
  268. return target.setFromMatrixPosition( this.matrixWorld );
  269. }
  270. getWorldQuaternion( target ) {
  271. this.updateWorldMatrix( true, false );
  272. this.matrixWorld.decompose( _position, target, _scale );
  273. return target;
  274. }
  275. getWorldScale( target ) {
  276. this.updateWorldMatrix( true, false );
  277. this.matrixWorld.decompose( _position, _quaternion, target );
  278. return target;
  279. }
  280. getWorldDirection( target ) {
  281. this.updateWorldMatrix( true, false );
  282. const e = this.matrixWorld.elements;
  283. return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
  284. }
  285. raycast( /* raycaster, intersects */ ) {}
  286. traverse( callback ) {
  287. callback( this );
  288. const children = this.children;
  289. for ( let i = 0, l = children.length; i < l; i ++ ) {
  290. children[ i ].traverse( callback );
  291. }
  292. }
  293. traverseVisible( callback ) {
  294. if ( this.visible === false ) return;
  295. callback( this );
  296. const children = this.children;
  297. for ( let i = 0, l = children.length; i < l; i ++ ) {
  298. children[ i ].traverseVisible( callback );
  299. }
  300. }
  301. traverseAncestors( callback ) {
  302. const parent = this.parent;
  303. if ( parent !== null ) {
  304. callback( parent );
  305. parent.traverseAncestors( callback );
  306. }
  307. }
  308. updateMatrix() {
  309. this.matrix.compose( this.position, this.quaternion, this.scale );
  310. this.matrixWorldNeedsUpdate = true;
  311. }
  312. updateMatrixWorld( force ) {
  313. if ( this.matrixAutoUpdate ) this.updateMatrix();
  314. if ( this.matrixWorldNeedsUpdate || force ) {
  315. if ( this.parent === null ) {
  316. this.matrixWorld.copy( this.matrix );
  317. } else {
  318. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  319. }
  320. this.matrixWorldNeedsUpdate = false;
  321. force = true;
  322. }
  323. // update children
  324. const children = this.children;
  325. for ( let i = 0, l = children.length; i < l; i ++ ) {
  326. const child = children[ i ];
  327. if ( child.matrixWorldAutoUpdate === true || force === true ) {
  328. child.updateMatrixWorld( force );
  329. }
  330. }
  331. }
  332. updateWorldMatrix( updateParents, updateChildren ) {
  333. const parent = this.parent;
  334. if ( updateParents === true && parent !== null && parent.matrixWorldAutoUpdate === true ) {
  335. parent.updateWorldMatrix( true, false );
  336. }
  337. if ( this.matrixAutoUpdate ) this.updateMatrix();
  338. if ( this.parent === null ) {
  339. this.matrixWorld.copy( this.matrix );
  340. } else {
  341. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  342. }
  343. // update children
  344. if ( updateChildren === true ) {
  345. const children = this.children;
  346. for ( let i = 0, l = children.length; i < l; i ++ ) {
  347. const child = children[ i ];
  348. if ( child.matrixWorldAutoUpdate === true ) {
  349. child.updateWorldMatrix( false, true );
  350. }
  351. }
  352. }
  353. }
  354. toJSON( meta ) {
  355. // meta is a string when called from JSON.stringify
  356. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  357. const output = {};
  358. // meta is a hash used to collect geometries, materials.
  359. // not providing it implies that this is the root object
  360. // being serialized.
  361. if ( isRootObject ) {
  362. // initialize meta obj
  363. meta = {
  364. geometries: {},
  365. materials: {},
  366. textures: {},
  367. images: {},
  368. shapes: {},
  369. skeletons: {},
  370. animations: {},
  371. nodes: {}
  372. };
  373. output.metadata = {
  374. version: 4.5,
  375. type: 'Object',
  376. generator: 'Object3D.toJSON'
  377. };
  378. }
  379. // standard Object3D serialization
  380. const object = {};
  381. object.uuid = this.uuid;
  382. object.type = this.type;
  383. if ( this.name !== '' ) object.name = this.name;
  384. if ( this.castShadow === true ) object.castShadow = true;
  385. if ( this.receiveShadow === true ) object.receiveShadow = true;
  386. if ( this.visible === false ) object.visible = false;
  387. if ( this.frustumCulled === false ) object.frustumCulled = false;
  388. if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder;
  389. if ( JSON.stringify( this.userData ) !== '{}' ) object.userData = this.userData;
  390. object.layers = this.layers.mask;
  391. object.matrix = this.matrix.toArray();
  392. if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
  393. // object specific properties
  394. if ( this.isInstancedMesh ) {
  395. object.type = 'InstancedMesh';
  396. object.count = this.count;
  397. object.instanceMatrix = this.instanceMatrix.toJSON();
  398. if ( this.instanceColor !== null ) object.instanceColor = this.instanceColor.toJSON();
  399. }
  400. //
  401. function serialize( library, element ) {
  402. if ( library[ element.uuid ] === undefined ) {
  403. library[ element.uuid ] = element.toJSON( meta );
  404. }
  405. return element.uuid;
  406. }
  407. if ( this.isScene ) {
  408. if ( this.background ) {
  409. if ( this.background.isColor ) {
  410. object.background = this.background.toJSON();
  411. } else if ( this.background.isTexture ) {
  412. object.background = this.background.toJSON( meta ).uuid;
  413. }
  414. }
  415. if ( this.environment && this.environment.isTexture && this.environment.isRenderTargetTexture !== true ) {
  416. object.environment = this.environment.toJSON( meta ).uuid;
  417. }
  418. } else if ( this.isMesh || this.isLine || this.isPoints ) {
  419. object.geometry = serialize( meta.geometries, this.geometry );
  420. const parameters = this.geometry.parameters;
  421. if ( parameters !== undefined && parameters.shapes !== undefined ) {
  422. const shapes = parameters.shapes;
  423. if ( Array.isArray( shapes ) ) {
  424. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  425. const shape = shapes[ i ];
  426. serialize( meta.shapes, shape );
  427. }
  428. } else {
  429. serialize( meta.shapes, shapes );
  430. }
  431. }
  432. }
  433. if ( this.isSkinnedMesh ) {
  434. object.bindMode = this.bindMode;
  435. object.bindMatrix = this.bindMatrix.toArray();
  436. if ( this.skeleton !== undefined ) {
  437. serialize( meta.skeletons, this.skeleton );
  438. object.skeleton = this.skeleton.uuid;
  439. }
  440. }
  441. if ( this.material !== undefined ) {
  442. if ( Array.isArray( this.material ) ) {
  443. const uuids = [];
  444. for ( let i = 0, l = this.material.length; i < l; i ++ ) {
  445. uuids.push( serialize( meta.materials, this.material[ i ] ) );
  446. }
  447. object.material = uuids;
  448. } else {
  449. object.material = serialize( meta.materials, this.material );
  450. }
  451. }
  452. //
  453. if ( this.children.length > 0 ) {
  454. object.children = [];
  455. for ( let i = 0; i < this.children.length; i ++ ) {
  456. object.children.push( this.children[ i ].toJSON( meta ).object );
  457. }
  458. }
  459. //
  460. if ( this.animations.length > 0 ) {
  461. object.animations = [];
  462. for ( let i = 0; i < this.animations.length; i ++ ) {
  463. const animation = this.animations[ i ];
  464. object.animations.push( serialize( meta.animations, animation ) );
  465. }
  466. }
  467. if ( isRootObject ) {
  468. const geometries = extractFromCache( meta.geometries );
  469. const materials = extractFromCache( meta.materials );
  470. const textures = extractFromCache( meta.textures );
  471. const images = extractFromCache( meta.images );
  472. const shapes = extractFromCache( meta.shapes );
  473. const skeletons = extractFromCache( meta.skeletons );
  474. const animations = extractFromCache( meta.animations );
  475. const nodes = extractFromCache( meta.nodes );
  476. if ( geometries.length > 0 ) output.geometries = geometries;
  477. if ( materials.length > 0 ) output.materials = materials;
  478. if ( textures.length > 0 ) output.textures = textures;
  479. if ( images.length > 0 ) output.images = images;
  480. if ( shapes.length > 0 ) output.shapes = shapes;
  481. if ( skeletons.length > 0 ) output.skeletons = skeletons;
  482. if ( animations.length > 0 ) output.animations = animations;
  483. if ( nodes.length > 0 ) output.nodes = nodes;
  484. }
  485. output.object = object;
  486. return output;
  487. // extract data from the cache hash
  488. // remove metadata on each item
  489. // and return as array
  490. function extractFromCache( cache ) {
  491. const values = [];
  492. for ( const key in cache ) {
  493. const data = cache[ key ];
  494. delete data.metadata;
  495. values.push( data );
  496. }
  497. return values;
  498. }
  499. }
  500. clone( recursive ) {
  501. return new this.constructor().copy( this, recursive );
  502. }
  503. copy( source, recursive = true ) {
  504. this.name = source.name;
  505. this.up.copy( source.up );
  506. this.position.copy( source.position );
  507. this.rotation.order = source.rotation.order;
  508. this.quaternion.copy( source.quaternion );
  509. this.scale.copy( source.scale );
  510. this.matrix.copy( source.matrix );
  511. this.matrixWorld.copy( source.matrixWorld );
  512. this.matrixAutoUpdate = source.matrixAutoUpdate;
  513. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  514. this.matrixWorldAutoUpdate = source.matrixWorldAutoUpdate;
  515. this.layers.mask = source.layers.mask;
  516. this.visible = source.visible;
  517. this.castShadow = source.castShadow;
  518. this.receiveShadow = source.receiveShadow;
  519. this.frustumCulled = source.frustumCulled;
  520. this.renderOrder = source.renderOrder;
  521. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  522. if ( recursive === true ) {
  523. for ( let i = 0; i < source.children.length; i ++ ) {
  524. const child = source.children[ i ];
  525. this.add( child.clone() );
  526. }
  527. }
  528. return this;
  529. }
  530. }
  531. Object3D.DefaultUp = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  532. Object3D.DefaultMatrixAutoUpdate = true;
  533. Object3D.DefaultMatrixWorldAutoUpdate = true;
  534. export { Object3D };
粤ICP备19079148号