Object3D.js 13 KB

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