Object3D.js 13 KB

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