Object3D.js 13 KB

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