Object3D.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. getChildByName: function ( name, recursive ) {
  208. console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
  209. return this.getObjectByName( name, recursive );
  210. },
  211. getObjectById: function ( id, recursive ) {
  212. if ( this.id === id ) return this;
  213. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  214. var child = this.children[ i ];
  215. var object = child.getObjectById( id, recursive );
  216. if ( object !== undefined ) {
  217. return object;
  218. }
  219. }
  220. return undefined;
  221. },
  222. getObjectByName: function ( name, recursive ) {
  223. if ( this.name === name ) return this;
  224. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  225. var child = this.children[ i ];
  226. var object = child.getObjectByName( name, recursive );
  227. if ( object !== undefined ) {
  228. return object;
  229. }
  230. }
  231. return undefined;
  232. },
  233. getWorldPosition: function ( optionalTarget ) {
  234. var result = optionalTarget || new THREE.Vector3();
  235. this.updateMatrixWorld( true );
  236. return result.setFromMatrixPosition( this.matrixWorld );
  237. },
  238. getWorldQuaternion: function () {
  239. var position = new THREE.Vector3();
  240. var scale = new THREE.Vector3();
  241. return function ( optionalTarget ) {
  242. var result = optionalTarget || new THREE.Quaternion();
  243. this.updateMatrixWorld( true );
  244. this.matrixWorld.decompose( position, result, scale );
  245. return result;
  246. }
  247. }(),
  248. getWorldRotation: function () {
  249. var quaternion = new THREE.Quaternion();
  250. return function ( optionalTarget ) {
  251. var result = optionalTarget || new THREE.Euler();
  252. this.getWorldQuaternion( quaternion );
  253. return result.setFromQuaternion( quaternion, this.rotation.order, false );
  254. }
  255. }(),
  256. getWorldScale: function () {
  257. var position = new THREE.Vector3();
  258. var quaternion = new THREE.Quaternion();
  259. return function ( optionalTarget ) {
  260. var result = optionalTarget || new THREE.Vector3();
  261. this.updateMatrixWorld( true );
  262. this.matrixWorld.decompose( position, quaternion, result );
  263. return result;
  264. }
  265. }(),
  266. getWorldDirection: function () {
  267. var quaternion = new THREE.Quaternion();
  268. return function ( optionalTarget ) {
  269. var result = optionalTarget || new THREE.Vector3();
  270. this.getWorldQuaternion( quaternion );
  271. return result.set( 0, 0, 1 ).applyQuaternion( quaternion );
  272. }
  273. }(),
  274. raycast: function () {},
  275. traverse: function ( callback ) {
  276. callback( this );
  277. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  278. this.children[ i ].traverse( callback );
  279. }
  280. },
  281. traverseVisible: function ( callback ) {
  282. if ( this.visible === false ) return;
  283. callback( this );
  284. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  285. this.children[ i ].traverseVisible( 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. } else if ( object instanceof THREE.SpotLight ) {
  374. data.color = object.color.getHex();
  375. data.intensity = object.intensity;
  376. data.distance = object.distance;
  377. data.angle = object.angle;
  378. data.exponent = object.exponent;
  379. } else if ( object instanceof THREE.HemisphereLight ) {
  380. data.color = object.color.getHex();
  381. data.groundColor = object.groundColor.getHex();
  382. } else if ( object instanceof THREE.Mesh ) {
  383. data.geometry = parseGeometry( object.geometry );
  384. data.material = parseMaterial( object.material );
  385. } else if ( object instanceof THREE.Line ) {
  386. data.geometry = parseGeometry( object.geometry );
  387. data.material = parseMaterial( object.material );
  388. } else if ( object instanceof THREE.Sprite ) {
  389. data.material = parseMaterial( object.material );
  390. }
  391. data.matrix = object.matrix.toArray();
  392. if ( object.children.length > 0 ) {
  393. data.children = [];
  394. for ( var i = 0; i < object.children.length; i ++ ) {
  395. data.children.push( parseObject( object.children[ i ] ) );
  396. }
  397. }
  398. return data;
  399. }
  400. output.object = parseObject( this );
  401. return output;
  402. },
  403. clone: function ( object, recursive ) {
  404. if ( object === undefined ) object = new THREE.Object3D();
  405. if ( recursive === undefined ) recursive = true;
  406. object.name = this.name;
  407. object.up.copy( this.up );
  408. object.position.copy( this.position );
  409. object.quaternion.copy( this.quaternion );
  410. object.scale.copy( this.scale );
  411. object.renderDepth = this.renderDepth;
  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;
粤ICP备19079148号