Object3D.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. get renderDepth () {
  76. console.warn( 'THREE.Object3D: .renderDepth has been renamed to .renderOrder.' );
  77. return this.renderOrder;
  78. },
  79. set renderDepth ( value ) {
  80. console.warn( 'THREE.Object3D: .renderDepth has been renamed to .renderOrder.' );
  81. this.renderOrder = value;
  82. },
  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 THREE.Quaternion();
  106. return function ( axis, angle ) {
  107. q1.setFromAxisAngle( axis, angle );
  108. this.quaternion.multiply( q1 );
  109. return this;
  110. }
  111. }(),
  112. rotateX: function () {
  113. var v1 = new THREE.Vector3( 1, 0, 0 );
  114. return function ( angle ) {
  115. return this.rotateOnAxis( v1, angle );
  116. };
  117. }(),
  118. rotateY: function () {
  119. var v1 = new THREE.Vector3( 0, 1, 0 );
  120. return function ( angle ) {
  121. return this.rotateOnAxis( v1, angle );
  122. };
  123. }(),
  124. rotateZ: function () {
  125. var v1 = new THREE.Vector3( 0, 0, 1 );
  126. return function ( 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 THREE.Vector3();
  134. return function ( axis, distance ) {
  135. v1.copy( axis ).applyQuaternion( this.quaternion );
  136. this.position.add( v1.multiplyScalar( distance ) );
  137. return this;
  138. }
  139. }(),
  140. translate: function ( distance, axis ) {
  141. console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
  142. return this.translateOnAxis( axis, distance );
  143. },
  144. translateX: function () {
  145. var v1 = new THREE.Vector3( 1, 0, 0 );
  146. return function ( distance ) {
  147. return this.translateOnAxis( v1, distance );
  148. };
  149. }(),
  150. translateY: function () {
  151. var v1 = new THREE.Vector3( 0, 1, 0 );
  152. return function ( distance ) {
  153. return this.translateOnAxis( v1, distance );
  154. };
  155. }(),
  156. translateZ: function () {
  157. var v1 = new THREE.Vector3( 0, 0, 1 );
  158. return function ( distance ) {
  159. return this.translateOnAxis( v1, distance );
  160. };
  161. }(),
  162. localToWorld: function ( vector ) {
  163. return vector.applyMatrix4( this.matrixWorld );
  164. },
  165. worldToLocal: function () {
  166. var m1 = new THREE.Matrix4();
  167. return function ( vector ) {
  168. return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
  169. };
  170. }(),
  171. lookAt: function () {
  172. // This routine does not support objects with rotated and/or translated parent(s)
  173. var m1 = new THREE.Matrix4();
  174. return function ( vector ) {
  175. m1.lookAt( vector, this.position, this.up );
  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 instanceof THREE.Object3D ) {
  191. if ( object.parent !== undefined ) {
  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 = undefined;
  211. object.dispatchEvent( { type: 'removed' } );
  212. this.children.splice( index, 1 );
  213. }
  214. },
  215. getChildByName: function ( name ) {
  216. console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
  217. return this.getObjectByName( name );
  218. },
  219. getObjectById: function ( id ) {
  220. return this.getObjectByProperty( 'id', id );
  221. },
  222. getObjectByName: function ( name ) {
  223. return this.getObjectByProperty( 'name', name );
  224. },
  225. getObjectByProperty: function ( name, value ) {
  226. if ( this[ name ] === value ) return this;
  227. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  228. var child = this.children[ i ];
  229. var object = child.getObjectByProperty( name, value );
  230. if ( object !== undefined ) {
  231. return object;
  232. }
  233. }
  234. return undefined;
  235. },
  236. getWorldPosition: function ( optionalTarget ) {
  237. var result = optionalTarget || new THREE.Vector3();
  238. this.updateMatrixWorld( true );
  239. return result.setFromMatrixPosition( this.matrixWorld );
  240. },
  241. getWorldQuaternion: function () {
  242. var position = new THREE.Vector3();
  243. var scale = new THREE.Vector3();
  244. return function ( optionalTarget ) {
  245. var result = optionalTarget || new THREE.Quaternion();
  246. this.updateMatrixWorld( true );
  247. this.matrixWorld.decompose( position, result, scale );
  248. return result;
  249. }
  250. }(),
  251. getWorldRotation: function () {
  252. var quaternion = new THREE.Quaternion();
  253. return function ( optionalTarget ) {
  254. var result = optionalTarget || new THREE.Euler();
  255. this.getWorldQuaternion( quaternion );
  256. return result.setFromQuaternion( quaternion, this.rotation.order, false );
  257. }
  258. }(),
  259. getWorldScale: function () {
  260. var position = new THREE.Vector3();
  261. var quaternion = new THREE.Quaternion();
  262. return function ( optionalTarget ) {
  263. var result = optionalTarget || new THREE.Vector3();
  264. this.updateMatrixWorld( true );
  265. this.matrixWorld.decompose( position, quaternion, result );
  266. return result;
  267. }
  268. }(),
  269. getWorldDirection: function () {
  270. var quaternion = new THREE.Quaternion();
  271. return function ( optionalTarget ) {
  272. var result = optionalTarget || new THREE.Vector3();
  273. this.getWorldQuaternion( quaternion );
  274. return result.set( 0, 0, 1 ).applyQuaternion( quaternion );
  275. }
  276. }(),
  277. raycast: function () {},
  278. traverse: function ( callback ) {
  279. callback( this );
  280. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  281. this.children[ i ].traverse( callback );
  282. }
  283. },
  284. traverseVisible: function ( callback ) {
  285. if ( this.visible === false ) return;
  286. callback( this );
  287. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  288. this.children[ i ].traverseVisible( callback );
  289. }
  290. },
  291. traverseAncestors: function ( callback ) {
  292. if ( this.parent ) {
  293. callback( this.parent );
  294. this.parent.traverseAncestors( callback );
  295. }
  296. },
  297. updateMatrix: function () {
  298. this.matrix.compose( this.position, this.quaternion, this.scale );
  299. this.matrixWorldNeedsUpdate = true;
  300. },
  301. updateMatrixWorld: function ( force ) {
  302. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  303. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  304. if ( this.parent === undefined ) {
  305. this.matrixWorld.copy( this.matrix );
  306. } else {
  307. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  308. }
  309. this.matrixWorldNeedsUpdate = false;
  310. force = true;
  311. }
  312. // update children
  313. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  314. this.children[ i ].updateMatrixWorld( force );
  315. }
  316. },
  317. toJSON: function ( meta ) {
  318. var isRootObject = ( meta === undefined );
  319. var data = {};
  320. // meta is a hash used to collect geometries, materials.
  321. // not providing it implies that this is the root object
  322. // being serialized.
  323. if ( isRootObject ) {
  324. // initialize meta obj
  325. meta = {
  326. geometries: {},
  327. materials: {},
  328. textures: {},
  329. images: {}
  330. };
  331. data.metadata = {
  332. version: 4.4,
  333. type: 'Object',
  334. generator: 'Object3D.toJSON'
  335. };
  336. }
  337. // standard Object3D serialization
  338. data.uuid = this.uuid;
  339. data.type = this.type;
  340. if ( this.name !== '' ) data.name = this.name;
  341. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  342. if ( this.visible !== true ) data.visible = this.visible;
  343. data.matrix = this.matrix.toArray();
  344. if ( this.children.length > 0 ) {
  345. data.children = [];
  346. for ( var i = 0; i < this.children.length; i ++ ) {
  347. data.children.push( this.children[ i ].toJSON( meta ).object );
  348. }
  349. }
  350. var output = {};
  351. if ( isRootObject ) {
  352. var geometries = extractFromCache( meta.geometries );
  353. var materials = extractFromCache( meta.materials );
  354. var textures = extractFromCache( meta.textures );
  355. var images = extractFromCache( meta.images );
  356. if ( geometries.length > 0 ) output.geometries = geometries;
  357. if ( materials.length > 0 ) output.materials = materials;
  358. if ( textures.length > 0 ) output.textures = textures;
  359. if ( images.length > 0 ) output.images = images;
  360. }
  361. output.object = data;
  362. return output;
  363. // extract data from the cache hash
  364. // remove metadata on each item
  365. // and return as array
  366. function extractFromCache ( cache ) {
  367. var values = [];
  368. for ( var key in cache ) {
  369. var data = cache[ key ];
  370. delete data.metadata;
  371. values.push( data );
  372. }
  373. return values;
  374. }
  375. },
  376. clone: function ( object, recursive ) {
  377. if ( object === undefined ) object = new THREE.Object3D();
  378. if ( recursive === undefined ) recursive = true;
  379. object.name = this.name;
  380. object.up.copy( this.up );
  381. object.position.copy( this.position );
  382. object.quaternion.copy( this.quaternion );
  383. object.scale.copy( this.scale );
  384. object.rotationAutoUpdate = this.rotationAutoUpdate;
  385. object.matrix.copy( this.matrix );
  386. object.matrixWorld.copy( this.matrixWorld );
  387. object.matrixAutoUpdate = this.matrixAutoUpdate;
  388. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  389. object.visible = this.visible;
  390. object.castShadow = this.castShadow;
  391. object.receiveShadow = this.receiveShadow;
  392. object.frustumCulled = this.frustumCulled;
  393. object.renderOrder = this.renderOrder;
  394. object.userData = JSON.parse( JSON.stringify( this.userData ) );
  395. if ( recursive === true ) {
  396. for ( var i = 0; i < this.children.length; i ++ ) {
  397. var child = this.children[ i ];
  398. object.add( child.clone() );
  399. }
  400. }
  401. return object;
  402. }
  403. };
  404. THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype );
  405. THREE.Object3DIdCount = 0;
粤ICP备19079148号