Moon.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import buildModuleUrl from '../Core/buildModuleUrl.js';
  2. import Cartesian3 from '../Core/Cartesian3.js';
  3. import defaultValue from '../Core/defaultValue.js';
  4. import defined from '../Core/defined.js';
  5. import defineProperties from '../Core/defineProperties.js';
  6. import destroyObject from '../Core/destroyObject.js';
  7. import Ellipsoid from '../Core/Ellipsoid.js';
  8. import IauOrientationAxes from '../Core/IauOrientationAxes.js';
  9. import Matrix3 from '../Core/Matrix3.js';
  10. import Matrix4 from '../Core/Matrix4.js';
  11. import Simon1994PlanetaryPositions from '../Core/Simon1994PlanetaryPositions.js';
  12. import Transforms from '../Core/Transforms.js';
  13. import EllipsoidPrimitive from './EllipsoidPrimitive.js';
  14. import Material from './Material.js';
  15. /**
  16. * Draws the Moon in 3D.
  17. * @alias Moon
  18. * @constructor
  19. *
  20. * @param {Object} [options] Object with the following properties:
  21. * @param {Boolean} [options.show=true] Determines whether the moon will be rendered.
  22. * @param {String} [options.textureUrl=buildModuleUrl('Assets/Textures/moonSmall.jpg')] The moon texture.
  23. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.MOON] The moon ellipsoid.
  24. * @param {Boolean} [options.onlySunLighting=true] Use the sun as the only light source.
  25. *
  26. *
  27. * @example
  28. * scene.moon = new Cesium.Moon();
  29. *
  30. * @see Scene#moon
  31. */
  32. function Moon(options) {
  33. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  34. var url = options.textureUrl;
  35. if (!defined(url)) {
  36. url = buildModuleUrl('Assets/Textures/moonSmall.jpg');
  37. }
  38. /**
  39. * Determines if the moon will be shown.
  40. *
  41. * @type {Boolean}
  42. * @default true
  43. */
  44. this.show = defaultValue(options.show, true);
  45. /**
  46. * The moon texture.
  47. * @type {String}
  48. * @default buildModuleUrl('Assets/Textures/moonSmall.jpg')
  49. */
  50. this.textureUrl = url;
  51. this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.MOON);
  52. /**
  53. * Use the sun as the only light source.
  54. * @type {Boolean}
  55. * @default true
  56. */
  57. this.onlySunLighting = defaultValue(options.onlySunLighting, true);
  58. this._ellipsoidPrimitive = new EllipsoidPrimitive({
  59. radii : this.ellipsoid.radii,
  60. material : Material.fromType(Material.ImageType),
  61. depthTestEnabled : false,
  62. _owner : this
  63. });
  64. this._ellipsoidPrimitive.material.translucent = false;
  65. this._axes = new IauOrientationAxes();
  66. }
  67. defineProperties(Moon.prototype, {
  68. /**
  69. * Get the ellipsoid that defines the shape of the moon.
  70. *
  71. * @memberof Moon.prototype
  72. *
  73. * @type {Ellipsoid}
  74. * @readonly
  75. *
  76. * @default {@link Ellipsoid.MOON}
  77. */
  78. ellipsoid : {
  79. get : function() {
  80. return this._ellipsoid;
  81. }
  82. }
  83. });
  84. var icrfToFixed = new Matrix3();
  85. var rotationScratch = new Matrix3();
  86. var translationScratch = new Cartesian3();
  87. var scratchCommandList = [];
  88. /**
  89. * @private
  90. */
  91. Moon.prototype.update = function(frameState) {
  92. if (!this.show) {
  93. return;
  94. }
  95. var ellipsoidPrimitive = this._ellipsoidPrimitive;
  96. ellipsoidPrimitive.material.uniforms.image = this.textureUrl;
  97. ellipsoidPrimitive.onlySunLighting = this.onlySunLighting;
  98. var date = frameState.time;
  99. if (!defined(Transforms.computeIcrfToFixedMatrix(date, icrfToFixed))) {
  100. Transforms.computeTemeToPseudoFixedMatrix(date, icrfToFixed);
  101. }
  102. var rotation = this._axes.evaluate(date, rotationScratch);
  103. Matrix3.transpose(rotation, rotation);
  104. Matrix3.multiply(icrfToFixed, rotation, rotation);
  105. var translation = Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame(date, translationScratch);
  106. Matrix3.multiplyByVector(icrfToFixed, translation, translation);
  107. Matrix4.fromRotationTranslation(rotation, translation, ellipsoidPrimitive.modelMatrix);
  108. var savedCommandList = frameState.commandList;
  109. frameState.commandList = scratchCommandList;
  110. scratchCommandList.length = 0;
  111. ellipsoidPrimitive.update(frameState);
  112. frameState.commandList = savedCommandList;
  113. return (scratchCommandList.length === 1) ? scratchCommandList[0] : undefined;
  114. };
  115. /**
  116. * Returns true if this object was destroyed; otherwise, false.
  117. * <br /><br />
  118. * If this object was destroyed, it should not be used; calling any function other than
  119. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  120. *
  121. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  122. *
  123. * @see Moon#destroy
  124. */
  125. Moon.prototype.isDestroyed = function() {
  126. return false;
  127. };
  128. /**
  129. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  130. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  131. * <br /><br />
  132. * Once an object is destroyed, it should not be used; calling any function other than
  133. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  134. * assign the return value (<code>undefined</code>) to the object as done in the example.
  135. *
  136. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  137. *
  138. *
  139. * @example
  140. * moon = moon && moon.destroy();
  141. *
  142. * @see Moon#isDestroyed
  143. */
  144. Moon.prototype.destroy = function() {
  145. this._ellipsoidPrimitive = this._ellipsoidPrimitive && this._ellipsoidPrimitive.destroy();
  146. return destroyObject(this);
  147. };
  148. export default Moon;