GlobeTranslucency.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import Check from "../Core/Check.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import NearFarScalar from "../Core/NearFarScalar.js";
  5. import Rectangle from "../Core/Rectangle.js";
  6. /**
  7. * Properties for controlling globe translucency.
  8. *
  9. * @alias GlobeTranslucency
  10. * @constructor
  11. */
  12. function GlobeTranslucency() {
  13. this._enabled = false;
  14. this._frontFaceAlpha = 1.0;
  15. this._frontFaceAlphaByDistance = undefined;
  16. this._backFaceAlpha = 1.0;
  17. this._backFaceAlphaByDistance = undefined;
  18. this._rectangle = Rectangle.clone(Rectangle.MAX_VALUE);
  19. }
  20. Object.defineProperties(GlobeTranslucency.prototype, {
  21. /**
  22. * When true, the globe is rendered as a translucent surface.
  23. * <br /><br />
  24. * The alpha is computed by blending {@link Globe#material}, {@link Globe#imageryLayers},
  25. * and {@link Globe#baseColor}, all of which may contain translucency, and then multiplying by
  26. * {@link GlobeTranslucency#frontFaceAlpha} and {@link GlobeTranslucency#frontFaceAlphaByDistance} for front faces and
  27. * {@link GlobeTranslucency#backFaceAlpha} and {@link GlobeTranslucency#backFaceAlphaByDistance} for back faces.
  28. * When the camera is underground back faces and front faces are swapped, i.e. back-facing geometry
  29. * is considered front facing.
  30. * <br /><br />
  31. * Translucency is disabled by default.
  32. *
  33. * @memberof GlobeTranslucency.prototype
  34. *
  35. * @type {boolean}
  36. * @default false
  37. *
  38. * @see GlobeTranslucency#frontFaceAlpha
  39. * @see GlobeTranslucency#frontFaceAlphaByDistance
  40. * @see GlobeTranslucency#backFaceAlpha
  41. * @see GlobeTranslucency#backFaceAlphaByDistance
  42. */
  43. enabled: {
  44. get: function () {
  45. return this._enabled;
  46. },
  47. set: function (value) {
  48. //>>includeStart('debug', pragmas.debug);
  49. Check.typeOf.bool("enabled", value);
  50. //>>includeEnd('debug');
  51. this._enabled = value;
  52. },
  53. },
  54. /**
  55. * A constant translucency to apply to front faces of the globe.
  56. * <br /><br />
  57. * {@link GlobeTranslucency#enabled} must be set to true for this option to take effect.
  58. *
  59. * @memberof GlobeTranslucency.prototype
  60. *
  61. * @type {number}
  62. * @default 1.0
  63. *
  64. * @see GlobeTranslucency#enabled
  65. * @see GlobeTranslucency#frontFaceAlphaByDistance
  66. *
  67. * @example
  68. * // Set front face translucency to 0.5.
  69. * globe.translucency.frontFaceAlpha = 0.5;
  70. * globe.translucency.enabled = true;
  71. */
  72. frontFaceAlpha: {
  73. get: function () {
  74. return this._frontFaceAlpha;
  75. },
  76. set: function (value) {
  77. //>>includeStart('debug', pragmas.debug);
  78. Check.typeOf.number.greaterThanOrEquals("frontFaceAlpha", value, 0.0);
  79. Check.typeOf.number.lessThanOrEquals("frontFaceAlpha", value, 1.0);
  80. //>>includeEnd('debug');
  81. this._frontFaceAlpha = value;
  82. },
  83. },
  84. /**
  85. * Gets or sets near and far translucency properties of front faces of the globe based on the distance to the camera.
  86. * The translucency will interpolate between the {@link NearFarScalar#nearValue} and
  87. * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
  88. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  89. * Outside of these ranges the translucency remains clamped to the nearest bound. If undefined,
  90. * frontFaceAlphaByDistance will be disabled.
  91. * <br /><br />
  92. * {@link GlobeTranslucency#enabled} must be set to true for this option to take effect.
  93. *
  94. * @memberof GlobeTranslucency.prototype
  95. *
  96. * @type {NearFarScalar}
  97. * @default undefined
  98. *
  99. * @see GlobeTranslucency#enabled
  100. * @see GlobeTranslucency#frontFaceAlpha
  101. *
  102. * @example
  103. * // Example 1.
  104. * // Set front face translucency to 0.5 when the
  105. * // camera is 1500 meters from the surface and 1.0
  106. * // as the camera distance approaches 8.0e6 meters.
  107. * globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(1.5e2, 0.5, 8.0e6, 1.0);
  108. * globe.translucency.enabled = true;
  109. *
  110. * @example
  111. * // Example 2.
  112. * // Disable front face translucency by distance
  113. * globe.translucency.frontFaceAlphaByDistance = undefined;
  114. */
  115. frontFaceAlphaByDistance: {
  116. get: function () {
  117. return this._frontFaceAlphaByDistance;
  118. },
  119. set: function (value) {
  120. //>>includeStart('debug', pragmas.debug);
  121. if (defined(value) && value.far < value.near) {
  122. throw new DeveloperError(
  123. "far distance must be greater than near distance."
  124. );
  125. }
  126. //>>includeEnd('debug');
  127. this._frontFaceAlphaByDistance = NearFarScalar.clone(
  128. value,
  129. this._frontFaceAlphaByDistance
  130. );
  131. },
  132. },
  133. /**
  134. * A constant translucency to apply to back faces of the globe.
  135. * <br /><br />
  136. * {@link GlobeTranslucency#enabled} must be set to true for this option to take effect.
  137. *
  138. * @memberof GlobeTranslucency.prototype
  139. *
  140. * @type {number}
  141. * @default 1.0
  142. *
  143. * @see GlobeTranslucency#enabled
  144. * @see GlobeTranslucency#backFaceAlphaByDistance
  145. *
  146. * @example
  147. * // Set back face translucency to 0.5.
  148. * globe.translucency.backFaceAlpha = 0.5;
  149. * globe.translucency.enabled = true;
  150. */
  151. backFaceAlpha: {
  152. get: function () {
  153. return this._backFaceAlpha;
  154. },
  155. set: function (value) {
  156. //>>includeStart('debug', pragmas.debug);
  157. Check.typeOf.number.greaterThanOrEquals("backFaceAlpha", value, 0.0);
  158. Check.typeOf.number.lessThanOrEquals("backFaceAlpha", value, 1.0);
  159. //>>includeEnd('debug');
  160. this._backFaceAlpha = value;
  161. },
  162. },
  163. /**
  164. * Gets or sets near and far translucency properties of back faces of the globe based on the distance to the camera.
  165. * The translucency will interpolate between the {@link NearFarScalar#nearValue} and
  166. * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
  167. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  168. * Outside of these ranges the translucency remains clamped to the nearest bound. If undefined,
  169. * backFaceAlphaByDistance will be disabled.
  170. * <br /><br />
  171. * {@link GlobeTranslucency#enabled} must be set to true for this option to take effect.
  172. *
  173. * @memberof GlobeTranslucency.prototype
  174. *
  175. * @type {NearFarScalar}
  176. * @default undefined
  177. *
  178. * @see GlobeTranslucency#enabled
  179. * @see GlobeTranslucency#backFaceAlpha
  180. *
  181. * @example
  182. * // Example 1.
  183. * // Set back face translucency to 0.5 when the
  184. * // camera is 1500 meters from the surface and 1.0
  185. * // as the camera distance approaches 8.0e6 meters.
  186. * globe.translucency.backFaceAlphaByDistance = new Cesium.NearFarScalar(1.5e2, 0.5, 8.0e6, 1.0);
  187. * globe.translucency.enabled = true;
  188. *
  189. * @example
  190. * // Example 2.
  191. * // Disable back face translucency by distance
  192. * globe.translucency.backFaceAlphaByDistance = undefined;
  193. */
  194. backFaceAlphaByDistance: {
  195. get: function () {
  196. return this._backFaceAlphaByDistance;
  197. },
  198. set: function (value) {
  199. //>>includeStart('debug', pragmas.debug);
  200. if (defined(value) && value.far < value.near) {
  201. throw new DeveloperError(
  202. "far distance must be greater than near distance."
  203. );
  204. }
  205. //>>includeEnd('debug');
  206. this._backFaceAlphaByDistance = NearFarScalar.clone(
  207. value,
  208. this._backFaceAlphaByDistance
  209. );
  210. },
  211. },
  212. /**
  213. * A property specifying a {@link Rectangle} used to limit translucency to a cartographic area.
  214. * Defaults to the maximum extent of cartographic coordinates.
  215. *
  216. * @memberof GlobeTranslucency.prototype
  217. *
  218. * @type {Rectangle}
  219. * @default {@link Rectangle.MAX_VALUE}
  220. */
  221. rectangle: {
  222. get: function () {
  223. return this._rectangle;
  224. },
  225. set: function (value) {
  226. if (!defined(value)) {
  227. value = Rectangle.clone(Rectangle.MAX_VALUE);
  228. }
  229. Rectangle.clone(value, this._rectangle);
  230. },
  231. },
  232. });
  233. export default GlobeTranslucency;