Object3D.js 14 KB

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