Object3D.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. configurable: true,
  40. enumerable: true,
  41. value: position
  42. },
  43. rotation: {
  44. configurable: true,
  45. enumerable: true,
  46. value: rotation
  47. },
  48. quaternion: {
  49. configurable: true,
  50. enumerable: true,
  51. value: quaternion
  52. },
  53. scale: {
  54. configurable: true,
  55. enumerable: true,
  56. value: scale
  57. },
  58. modelViewMatrix: {
  59. value: new Matrix4()
  60. },
  61. normalMatrix: {
  62. value: new Matrix3()
  63. }
  64. } );
  65. this.matrix = new Matrix4();
  66. this.matrixWorld = new Matrix4();
  67. this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate;
  68. this.matrixWorldNeedsUpdate = false;
  69. this.layers = new Layers();
  70. this.visible = true;
  71. this.castShadow = false;
  72. this.receiveShadow = false;
  73. this.frustumCulled = true;
  74. this.renderOrder = 0;
  75. this.userData = {};
  76. this.onBeforeRender = function () {};
  77. this.onAfterRender = function () {};
  78. }
  79. Object3D.DefaultUp = new Vector3( 0, 1, 0 );
  80. Object3D.DefaultMatrixAutoUpdate = true;
  81. Object.assign( Object3D.prototype, EventDispatcher.prototype, {
  82. isObject3D: true,
  83. applyMatrix: function ( matrix ) {
  84. this.matrix.multiplyMatrices( matrix, this.matrix );
  85. this.matrix.decompose( this.position, this.quaternion, this.scale );
  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 routine 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. }
  208. var index = this.children.indexOf( object );
  209. if ( index !== - 1 ) {
  210. object.parent = null;
  211. object.dispatchEvent( { type: 'removed' } );
  212. this.children.splice( index, 1 );
  213. }
  214. },
  215. getObjectById: function ( id ) {
  216. return this.getObjectByProperty( 'id', id );
  217. },
  218. getObjectByName: function ( name ) {
  219. return this.getObjectByProperty( 'name', name );
  220. },
  221. getObjectByProperty: function ( name, value ) {
  222. if ( this[ name ] === value ) return this;
  223. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  224. var child = this.children[ i ];
  225. var object = child.getObjectByProperty( name, value );
  226. if ( object !== undefined ) {
  227. return object;
  228. }
  229. }
  230. return undefined;
  231. },
  232. getWorldPosition: function ( optionalTarget ) {
  233. var result = optionalTarget || new Vector3();
  234. this.updateMatrixWorld( true );
  235. return result.setFromMatrixPosition( this.matrixWorld );
  236. },
  237. getWorldQuaternion: function () {
  238. var position = new Vector3();
  239. var scale = new Vector3();
  240. return function getWorldQuaternion( optionalTarget ) {
  241. var result = optionalTarget || new Quaternion();
  242. this.updateMatrixWorld( true );
  243. this.matrixWorld.decompose( position, result, scale );
  244. return result;
  245. };
  246. }(),
  247. getWorldRotation: function () {
  248. var quaternion = new Quaternion();
  249. return function getWorldRotation( optionalTarget ) {
  250. var result = optionalTarget || new Euler();
  251. this.getWorldQuaternion( quaternion );
  252. return result.setFromQuaternion( quaternion, this.rotation.order, false );
  253. };
  254. }(),
  255. getWorldScale: function () {
  256. var position = new Vector3();
  257. var quaternion = new Quaternion();
  258. return function getWorldScale( optionalTarget ) {
  259. var result = optionalTarget || new Vector3();
  260. this.updateMatrixWorld( true );
  261. this.matrixWorld.decompose( position, quaternion, result );
  262. return result;
  263. };
  264. }(),
  265. getWorldDirection: function () {
  266. var quaternion = new Quaternion();
  267. return function getWorldDirection( optionalTarget ) {
  268. var result = optionalTarget || new Vector3();
  269. this.getWorldQuaternion( quaternion );
  270. return result.set( 0, 0, 1 ).applyQuaternion( quaternion );
  271. };
  272. }(),
  273. raycast: function () {},
  274. traverse: function ( callback ) {
  275. callback( this );
  276. var children = this.children;
  277. for ( var i = 0, l = children.length; i < l; i ++ ) {
  278. children[ i ].traverse( callback );
  279. }
  280. },
  281. traverseVisible: function ( callback ) {
  282. if ( this.visible === false ) return;
  283. callback( this );
  284. var children = this.children;
  285. for ( var i = 0, l = children.length; i < l; i ++ ) {
  286. children[ i ].traverseVisible( callback );
  287. }
  288. },
  289. traverseAncestors: function ( callback ) {
  290. var parent = this.parent;
  291. if ( parent !== null ) {
  292. callback( parent );
  293. parent.traverseAncestors( callback );
  294. }
  295. },
  296. updateMatrix: function () {
  297. this.matrix.compose( this.position, this.quaternion, this.scale );
  298. this.matrixWorldNeedsUpdate = true;
  299. },
  300. updateMatrixWorld: function ( force ) {
  301. if ( this.matrixAutoUpdate ) this.updateMatrix();
  302. if ( this.matrixWorldNeedsUpdate || force ) {
  303. if ( this.parent === null ) {
  304. this.matrixWorld.copy( this.matrix );
  305. } else {
  306. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  307. }
  308. this.matrixWorldNeedsUpdate = false;
  309. force = true;
  310. }
  311. // update children
  312. var children = this.children;
  313. for ( var i = 0, l = children.length; i < l; i ++ ) {
  314. children[ i ].updateMatrixWorld( force );
  315. }
  316. },
  317. toJSON: function ( meta ) {
  318. // meta is '' when called from JSON.stringify
  319. var isRootObject = ( meta === undefined || meta === '' );
  320. var output = {};
  321. // meta is a hash used to collect geometries, materials.
  322. // not providing it implies that this is the root object
  323. // being serialized.
  324. if ( isRootObject ) {
  325. // initialize meta obj
  326. meta = {
  327. geometries: {},
  328. materials: {},
  329. textures: {},
  330. images: {}
  331. };
  332. output.metadata = {
  333. version: 4.5,
  334. type: 'Object',
  335. generator: 'Object3D.toJSON'
  336. };
  337. }
  338. // standard Object3D serialization
  339. var object = {};
  340. object.uuid = this.uuid;
  341. object.type = this.type;
  342. if ( this.name !== '' ) object.name = this.name;
  343. if ( JSON.stringify( this.userData ) !== '{}' ) object.userData = this.userData;
  344. if ( this.castShadow === true ) object.castShadow = true;
  345. if ( this.receiveShadow === true ) object.receiveShadow = true;
  346. if ( this.visible === false ) object.visible = false;
  347. object.matrix = this.matrix.toArray();
  348. //
  349. function serialize( library, element ) {
  350. if ( library[ element.uuid ] === undefined ) {
  351. library[ element.uuid ] = element.toJSON( meta );
  352. }
  353. return element.uuid;
  354. }
  355. if ( this.geometry !== undefined ) {
  356. object.geometry = serialize( meta.geometries, this.geometry );
  357. }
  358. if ( this.material !== undefined ) {
  359. if ( Array.isArray( this.material ) ) {
  360. var uuids = [];
  361. for ( var i = 0, l = this.material.length; i < l; i ++ ) {
  362. uuids.push( serialize( meta.materials, this.material[ i ] ) );
  363. }
  364. object.material = uuids;
  365. } else {
  366. object.material = serialize( meta.materials, this.material );
  367. }
  368. }
  369. //
  370. if ( this.children.length > 0 ) {
  371. object.children = [];
  372. for ( var i = 0; i < this.children.length; i ++ ) {
  373. object.children.push( this.children[ i ].toJSON( meta ).object );
  374. }
  375. }
  376. if ( isRootObject ) {
  377. var geometries = extractFromCache( meta.geometries );
  378. var materials = extractFromCache( meta.materials );
  379. var textures = extractFromCache( meta.textures );
  380. var images = extractFromCache( meta.images );
  381. if ( geometries.length > 0 ) output.geometries = geometries;
  382. if ( materials.length > 0 ) output.materials = materials;
  383. if ( textures.length > 0 ) output.textures = textures;
  384. if ( images.length > 0 ) output.images = images;
  385. }
  386. output.object = object;
  387. return output;
  388. // extract data from the cache hash
  389. // remove metadata on each item
  390. // and return as array
  391. function extractFromCache( cache ) {
  392. var values = [];
  393. for ( var key in cache ) {
  394. var data = cache[ key ];
  395. delete data.metadata;
  396. values.push( data );
  397. }
  398. return values;
  399. }
  400. },
  401. clone: function ( recursive ) {
  402. return new this.constructor().copy( this, recursive );
  403. },
  404. copy: function ( source, recursive ) {
  405. if ( recursive === undefined ) recursive = true;
  406. this.name = source.name;
  407. this.up.copy( source.up );
  408. this.position.copy( source.position );
  409. this.quaternion.copy( source.quaternion );
  410. this.scale.copy( source.scale );
  411. this.matrix.copy( source.matrix );
  412. this.matrixWorld.copy( source.matrixWorld );
  413. this.matrixAutoUpdate = source.matrixAutoUpdate;
  414. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  415. this.layers.mask = source.layers.mask;
  416. this.visible = source.visible;
  417. this.castShadow = source.castShadow;
  418. this.receiveShadow = source.receiveShadow;
  419. this.frustumCulled = source.frustumCulled;
  420. this.renderOrder = source.renderOrder;
  421. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  422. if ( recursive === true ) {
  423. for ( var i = 0; i < source.children.length; i ++ ) {
  424. var child = source.children[ i ];
  425. this.add( child.clone() );
  426. }
  427. }
  428. return this;
  429. }
  430. } );
  431. export { Object3D };
粤ICP备19079148号