Object3D.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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.renderDepth = null;
  47. this.rotationAutoUpdate = true;
  48. this.matrix = new THREE.Matrix4();
  49. this.matrixWorld = new THREE.Matrix4();
  50. this.matrixAutoUpdate = true;
  51. this.matrixWorldNeedsUpdate = false;
  52. this.visible = true;
  53. this.castShadow = false;
  54. this.receiveShadow = false;
  55. this.frustumCulled = true;
  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. 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. console.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. console.error( "THREE.Object3D.add:", object, "can't be added as a child of itself." );
  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. console.error( "THREE.Object3D.add:", object, "is not an instance of THREE.Object3D." );
  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. raycast: function () {},
  208. traverse: function ( callback ) {
  209. callback( this );
  210. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  211. this.children[ i ].traverse( callback );
  212. }
  213. },
  214. traverseVisible: function ( callback ) {
  215. if ( this.visible === false ) return;
  216. callback( this );
  217. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  218. this.children[ i ].traverseVisible( callback );
  219. }
  220. },
  221. getObjectById: function ( id, recursive ) {
  222. if ( this.id === id ) return this;
  223. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  224. var child = this.children[ i ];
  225. var object = child.getObjectById( id, recursive );
  226. if ( object !== undefined ) {
  227. return object;
  228. }
  229. }
  230. return undefined;
  231. },
  232. getObjectByName: function ( name, recursive ) {
  233. if ( this.name === name ) return this;
  234. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  235. var child = this.children[ i ];
  236. var object = child.getObjectByName( name, recursive );
  237. if ( object !== undefined ) {
  238. return object;
  239. }
  240. }
  241. return undefined;
  242. },
  243. getChildByName: function ( name, recursive ) {
  244. console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
  245. return this.getObjectByName( name, recursive );
  246. },
  247. updateMatrix: function () {
  248. this.matrix.compose( this.position, this.quaternion, this.scale );
  249. this.matrixWorldNeedsUpdate = true;
  250. },
  251. updateMatrixWorld: function ( force ) {
  252. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  253. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  254. if ( this.parent === undefined ) {
  255. this.matrixWorld.copy( this.matrix );
  256. } else {
  257. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  258. }
  259. this.matrixWorldNeedsUpdate = false;
  260. force = true;
  261. }
  262. // update children
  263. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  264. this.children[ i ].updateMatrixWorld( force );
  265. }
  266. },
  267. toJSON: function () {
  268. var output = {
  269. metadata: {
  270. version: 4.3,
  271. type: 'Object',
  272. generator: 'ObjectExporter'
  273. }
  274. };
  275. //
  276. var geometries = {};
  277. var parseGeometry = function ( geometry ) {
  278. if ( output.geometries === undefined ) {
  279. output.geometries = [];
  280. }
  281. if ( geometries[ geometry.uuid ] === undefined ) {
  282. var json = geometry.toJSON();
  283. delete json.metadata;
  284. geometries[ geometry.uuid ] = json;
  285. output.geometries.push( json );
  286. }
  287. return geometry.uuid;
  288. };
  289. //
  290. var materials = {};
  291. var parseMaterial = function ( material ) {
  292. if ( output.materials === undefined ) {
  293. output.materials = [];
  294. }
  295. if ( materials[ material.uuid ] === undefined ) {
  296. var json = material.toJSON();
  297. delete json.metadata;
  298. materials[ material.uuid ] = json;
  299. output.materials.push( json );
  300. }
  301. return material.uuid;
  302. };
  303. //
  304. var parseObject = function ( object ) {
  305. var data = {};
  306. data.uuid = object.uuid;
  307. data.type = object.type;
  308. if ( object.name !== '' ) data.name = object.name;
  309. if ( JSON.stringify( object.userData ) !== '{}' ) data.userData = object.userData;
  310. if ( object.script !== undefined ) data.script = object.script.source;
  311. if ( object.visible !== true ) data.visible = object.visible;
  312. if ( object instanceof THREE.PerspectiveCamera ) {
  313. data.fov = object.fov;
  314. data.aspect = object.aspect;
  315. data.near = object.near;
  316. data.far = object.far;
  317. } else if ( object instanceof THREE.OrthographicCamera ) {
  318. data.left = object.left;
  319. data.right = object.right;
  320. data.top = object.top;
  321. data.bottom = object.bottom;
  322. data.near = object.near;
  323. data.far = object.far;
  324. } else if ( object instanceof THREE.AmbientLight ) {
  325. data.color = object.color.getHex();
  326. } else if ( object instanceof THREE.DirectionalLight ) {
  327. data.color = object.color.getHex();
  328. data.intensity = object.intensity;
  329. } else if ( object instanceof THREE.PointLight ) {
  330. data.color = object.color.getHex();
  331. data.intensity = object.intensity;
  332. data.distance = object.distance;
  333. } else if ( object instanceof THREE.SpotLight ) {
  334. data.color = object.color.getHex();
  335. data.intensity = object.intensity;
  336. data.distance = object.distance;
  337. data.angle = object.angle;
  338. data.exponent = object.exponent;
  339. } else if ( object instanceof THREE.HemisphereLight ) {
  340. data.color = object.color.getHex();
  341. data.groundColor = object.groundColor.getHex();
  342. } else if ( object instanceof THREE.Mesh ) {
  343. data.geometry = parseGeometry( object.geometry );
  344. data.material = parseMaterial( object.material );
  345. } else if ( object instanceof THREE.Sprite ) {
  346. data.material = parseMaterial( object.material );
  347. }
  348. data.matrix = object.matrix.toArray();
  349. if ( object.children.length > 0 ) {
  350. data.children = [];
  351. for ( var i = 0; i < object.children.length; i ++ ) {
  352. data.children.push( parseObject( object.children[ i ] ) );
  353. }
  354. }
  355. return data;
  356. }
  357. output.object = parseObject( this );
  358. return output;
  359. },
  360. clone: function ( object, recursive ) {
  361. if ( object === undefined ) object = new THREE.Object3D();
  362. if ( recursive === undefined ) recursive = true;
  363. object.name = this.name;
  364. object.up.copy( this.up );
  365. object.position.copy( this.position );
  366. object.quaternion.copy( this.quaternion );
  367. object.scale.copy( this.scale );
  368. object.renderDepth = this.renderDepth;
  369. object.rotationAutoUpdate = this.rotationAutoUpdate;
  370. object.matrix.copy( this.matrix );
  371. object.matrixWorld.copy( this.matrixWorld );
  372. object.matrixAutoUpdate = this.matrixAutoUpdate;
  373. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  374. object.visible = this.visible;
  375. object.castShadow = this.castShadow;
  376. object.receiveShadow = this.receiveShadow;
  377. object.frustumCulled = this.frustumCulled;
  378. object.userData = JSON.parse( JSON.stringify( this.userData ) );
  379. if ( this.script !== undefined ) object.script = this.script.clone();
  380. if ( recursive === true ) {
  381. for ( var i = 0; i < this.children.length; i ++ ) {
  382. var child = this.children[ i ];
  383. object.add( child.clone() );
  384. }
  385. }
  386. return object;
  387. }
  388. };
  389. THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype );
  390. THREE.Object3DIdCount = 0;