Moon.js 5.1 KB

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