Object3D.js 14 KB

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