Object3D.js 13 KB

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