Moon.js 5.6 KB

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