Object3D.js 13 KB

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