Globe.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import buildModuleUrl from "../Core/buildModuleUrl.js";
  3. import Cartesian3 from "../Core/Cartesian3.js";
  4. import Cartographic from "../Core/Cartographic.js";
  5. import Color from "../Core/Color.js";
  6. import defaultValue from "../Core/defaultValue.js";
  7. import defined from "../Core/defined.js";
  8. import destroyObject from "../Core/destroyObject.js";
  9. import DeveloperError from "../Core/DeveloperError.js";
  10. import Ellipsoid from "../Core/Ellipsoid.js";
  11. import EllipsoidTerrainProvider from "../Core/EllipsoidTerrainProvider.js";
  12. import Event from "../Core/Event.js";
  13. import IntersectionTests from "../Core/IntersectionTests.js";
  14. import NearFarScalar from "../Core/NearFarScalar.js";
  15. import Ray from "../Core/Ray.js";
  16. import Rectangle from "../Core/Rectangle.js";
  17. import Resource from "../Core/Resource.js";
  18. import ShaderSource from "../Renderer/ShaderSource.js";
  19. import Texture from "../Renderer/Texture.js";
  20. import GlobeFS from "../Shaders/GlobeFS.js";
  21. import GlobeVS from "../Shaders/GlobeVS.js";
  22. import GroundAtmosphere from "../Shaders/GroundAtmosphere.js";
  23. import when from "../ThirdParty/when.js";
  24. import GlobeSurfaceShaderSet from "./GlobeSurfaceShaderSet.js";
  25. import GlobeSurfaceTileProvider from "./GlobeSurfaceTileProvider.js";
  26. import GlobeTranslucency from "./GlobeTranslucency.js";
  27. import ImageryLayerCollection from "./ImageryLayerCollection.js";
  28. import QuadtreePrimitive from "./QuadtreePrimitive.js";
  29. import SceneMode from "./SceneMode.js";
  30. import ShadowMode from "./ShadowMode.js";
  31. /**
  32. * The globe rendered in the scene, including its terrain ({@link Globe#terrainProvider})
  33. * and imagery layers ({@link Globe#imageryLayers}). Access the globe using {@link Scene#globe}.
  34. *
  35. * @alias Globe
  36. * @constructor
  37. *
  38. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] Determines the size and shape of the
  39. * globe.
  40. */
  41. function Globe(ellipsoid) {
  42. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  43. var terrainProvider = new EllipsoidTerrainProvider({
  44. ellipsoid: ellipsoid,
  45. });
  46. var imageryLayerCollection = new ImageryLayerCollection();
  47. this._ellipsoid = ellipsoid;
  48. this._imageryLayerCollection = imageryLayerCollection;
  49. this._surfaceShaderSet = new GlobeSurfaceShaderSet();
  50. this._material = undefined;
  51. this._surface = new QuadtreePrimitive({
  52. tileProvider: new GlobeSurfaceTileProvider({
  53. terrainProvider: terrainProvider,
  54. imageryLayers: imageryLayerCollection,
  55. surfaceShaderSet: this._surfaceShaderSet,
  56. }),
  57. });
  58. this._terrainProvider = terrainProvider;
  59. this._terrainProviderChanged = new Event();
  60. this._undergroundColor = Color.clone(Color.BLACK);
  61. this._undergroundColorAlphaByDistance = new NearFarScalar(
  62. ellipsoid.maximumRadius / 1000.0,
  63. 0.0,
  64. ellipsoid.maximumRadius / 5.0,
  65. 1.0
  66. );
  67. this._translucency = new GlobeTranslucency();
  68. makeShadersDirty(this);
  69. /**
  70. * Determines if the globe will be shown.
  71. *
  72. * @type {Boolean}
  73. * @default true
  74. */
  75. this.show = true;
  76. this._oceanNormalMapResourceDirty = true;
  77. this._oceanNormalMapResource = new Resource({
  78. url: buildModuleUrl("Assets/Textures/waterNormalsSmall.jpg"),
  79. });
  80. /**
  81. * The maximum screen-space error used to drive level-of-detail refinement. Higher
  82. * values will provide better performance but lower visual quality.
  83. *
  84. * @type {Number}
  85. * @default 2
  86. */
  87. this.maximumScreenSpaceError = 2;
  88. /**
  89. * The size of the terrain tile cache, expressed as a number of tiles. Any additional
  90. * tiles beyond this number will be freed, as long as they aren't needed for rendering
  91. * this frame. A larger number will consume more memory but will show detail faster
  92. * when, for example, zooming out and then back in.
  93. *
  94. * @type {Number}
  95. * @default 100
  96. */
  97. this.tileCacheSize = 100;
  98. /**
  99. * Gets or sets the number of loading descendant tiles that is considered "too many".
  100. * If a tile has too many loading descendants, that tile will be loaded and rendered before any of
  101. * its descendants are loaded and rendered. This means more feedback for the user that something
  102. * is happening at the cost of a longer overall load time. Setting this to 0 will cause each
  103. * tile level to be loaded successively, significantly increasing load time. Setting it to a large
  104. * number (e.g. 1000) will minimize the number of tiles that are loaded but tend to make
  105. * detail appear all at once after a long wait.
  106. * @type {Number}
  107. * @default 20
  108. */
  109. this.loadingDescendantLimit = 20;
  110. /**
  111. * Gets or sets a value indicating whether the ancestors of rendered tiles should be preloaded.
  112. * Setting this to true optimizes the zoom-out experience and provides more detail in
  113. * newly-exposed areas when panning. The down side is that it requires loading more tiles.
  114. * @type {Boolean}
  115. * @default true
  116. */
  117. this.preloadAncestors = true;
  118. /**
  119. * Gets or sets a value indicating whether the siblings of rendered tiles should be preloaded.
  120. * Setting this to true causes tiles with the same parent as a rendered tile to be loaded, even
  121. * if they are culled. Setting this to true may provide a better panning experience at the
  122. * cost of loading more tiles.
  123. * @type {Boolean}
  124. * @default false
  125. */
  126. this.preloadSiblings = false;
  127. /**
  128. * The color to use to highlight terrain fill tiles. If undefined, fill tiles are not
  129. * highlighted at all. The alpha value is used to alpha blend with the tile's
  130. * actual color. Because terrain fill tiles do not represent the actual terrain surface,
  131. * it may be useful in some applications to indicate visually that they are not to be trusted.
  132. * @type {Color}
  133. * @default undefined
  134. */
  135. this.fillHighlightColor = undefined;
  136. /**
  137. * Enable lighting the globe with the scene's light source.
  138. *
  139. * @type {Boolean}
  140. * @default false
  141. */
  142. this.enableLighting = false;
  143. /**
  144. * Enable dynamic lighting effects on atmosphere and fog. This only takes effect
  145. * when <code>enableLighting</code> is <code>true</code>.
  146. *
  147. * @type {Boolean}
  148. * @default true
  149. */
  150. this.dynamicAtmosphereLighting = true;
  151. /**
  152. * Whether dynamic atmosphere lighting uses the sun direction instead of the scene's
  153. * light direction. This only takes effect when <code>enableLighting</code> and
  154. * <code>dynamicAtmosphereLighting</code> are <code>true</code>.
  155. *
  156. * @type {Boolean}
  157. * @default false
  158. */
  159. this.dynamicAtmosphereLightingFromSun = false;
  160. /**
  161. * Enable the ground atmosphere, which is drawn over the globe when viewed from a distance between <code>lightingFadeInDistance</code> and <code>lightingFadeOutDistance</code>.
  162. *
  163. * @demo {@link https://sandcastle.cesium.com/index.html?src=Ground%20Atmosphere.html|Ground atmosphere demo in Sandcastle}
  164. *
  165. * @type {Boolean}
  166. * @default true
  167. */
  168. this.showGroundAtmosphere = true;
  169. /**
  170. * The distance where everything becomes lit. This only takes effect
  171. * when <code>enableLighting</code> or <code>showGroundAtmosphere</code> is <code>true</code>.
  172. *
  173. * @type {Number}
  174. * @default 10000000.0
  175. */
  176. this.lightingFadeOutDistance = 1.0e7;
  177. /**
  178. * The distance where lighting resumes. This only takes effect
  179. * when <code>enableLighting</code> or <code>showGroundAtmosphere</code> is <code>true</code>.
  180. *
  181. * @type {Number}
  182. * @default 20000000.0
  183. */
  184. this.lightingFadeInDistance = 2.0e7;
  185. /**
  186. * The distance where the darkness of night from the ground atmosphere fades out to a lit ground atmosphere.
  187. * This only takes effect when <code>showGroundAtmosphere</code>, <code>enableLighting</code>, and
  188. * <code>dynamicAtmosphereLighting</code> are <code>true</code>.
  189. *
  190. * @type {Number}
  191. * @default 10000000.0
  192. */
  193. this.nightFadeOutDistance = 1.0e7;
  194. /**
  195. * The distance where the darkness of night from the ground atmosphere fades in to an unlit ground atmosphere.
  196. * This only takes effect when <code>showGroundAtmosphere</code>, <code>enableLighting</code>, and
  197. * <code>dynamicAtmosphereLighting</code> are <code>true</code>.
  198. *
  199. * @type {Number}
  200. * @default 50000000.0
  201. */
  202. this.nightFadeInDistance = 5.0e7;
  203. /**
  204. * True if an animated wave effect should be shown in areas of the globe
  205. * covered by water; otherwise, false. This property is ignored if the
  206. * <code>terrainProvider</code> does not provide a water mask.
  207. *
  208. * @type {Boolean}
  209. * @default true
  210. */
  211. this.showWaterEffect = true;
  212. /**
  213. * True if primitives such as billboards, polylines, labels, etc. should be depth-tested
  214. * against the terrain surface, or false if such primitives should always be drawn on top
  215. * of terrain unless they're on the opposite side of the globe. The disadvantage of depth
  216. * testing primitives against terrain is that slight numerical noise or terrain level-of-detail
  217. * switched can sometimes make a primitive that should be on the surface disappear underneath it.
  218. *
  219. * @type {Boolean}
  220. * @default false
  221. *
  222. */
  223. this.depthTestAgainstTerrain = false;
  224. /**
  225. * Determines whether the globe casts or receives shadows from light sources. Setting the globe
  226. * to cast shadows may impact performance since the terrain is rendered again from the light's perspective.
  227. * Currently only terrain that is in view casts shadows. By default the globe does not cast shadows.
  228. *
  229. * @type {ShadowMode}
  230. * @default ShadowMode.RECEIVE_ONLY
  231. */
  232. this.shadows = ShadowMode.RECEIVE_ONLY;
  233. /**
  234. * The hue shift to apply to the atmosphere. Defaults to 0.0 (no shift).
  235. * A hue shift of 1.0 indicates a complete rotation of the hues available.
  236. * @type {Number}
  237. * @default 0.0
  238. */
  239. this.atmosphereHueShift = 0.0;
  240. /**
  241. * The saturation shift to apply to the atmosphere. Defaults to 0.0 (no shift).
  242. * A saturation shift of -1.0 is monochrome.
  243. * @type {Number}
  244. * @default 0.0
  245. */
  246. this.atmosphereSaturationShift = 0.0;
  247. /**
  248. * The brightness shift to apply to the atmosphere. Defaults to 0.0 (no shift).
  249. * A brightness shift of -1.0 is complete darkness, which will let space show through.
  250. * @type {Number}
  251. * @default 0.0
  252. */
  253. this.atmosphereBrightnessShift = 0.0;
  254. /**
  255. * Whether to show terrain skirts. Terrain skirts are geometry extending downwards from a tile's edges used to hide seams between neighboring tiles.
  256. * Skirts are always hidden when the camera is underground or translucency is enabled.
  257. *
  258. * @type {Boolean}
  259. * @default true
  260. */
  261. this.showSkirts = true;
  262. /**
  263. * Whether to cull back-facing terrain. Back faces are not culled when the camera is underground or translucency is enabled.
  264. *
  265. * @type {Boolean}
  266. * @default true
  267. */
  268. this.backFaceCulling = true;
  269. this._oceanNormalMap = undefined;
  270. this._zoomedOutOceanSpecularIntensity = undefined;
  271. }
  272. Object.defineProperties(Globe.prototype, {
  273. /**
  274. * Gets an ellipsoid describing the shape of this globe.
  275. * @memberof Globe.prototype
  276. * @type {Ellipsoid}
  277. */
  278. ellipsoid: {
  279. get: function () {
  280. return this._ellipsoid;
  281. },
  282. },
  283. /**
  284. * Gets the collection of image layers that will be rendered on this globe.
  285. * @memberof Globe.prototype
  286. * @type {ImageryLayerCollection}
  287. */
  288. imageryLayers: {
  289. get: function () {
  290. return this._imageryLayerCollection;
  291. },
  292. },
  293. /**
  294. * Gets an event that's raised when an imagery layer is added, shown, hidden, moved, or removed.
  295. *
  296. * @memberof Globe.prototype
  297. * @type {Event}
  298. * @readonly
  299. */
  300. imageryLayersUpdatedEvent: {
  301. get: function () {
  302. return this._surface.tileProvider.imageryLayersUpdatedEvent;
  303. },
  304. },
  305. /**
  306. * Returns <code>true</code> when the tile load queue is empty, <code>false</code> otherwise. When the load queue is empty,
  307. * all terrain and imagery for the current view have been loaded.
  308. * @memberof Globe.prototype
  309. * @type {Boolean}
  310. * @readonly
  311. */
  312. tilesLoaded: {
  313. get: function () {
  314. if (!defined(this._surface)) {
  315. return true;
  316. }
  317. return (
  318. this._surface.tileProvider.ready &&
  319. this._surface._tileLoadQueueHigh.length === 0 &&
  320. this._surface._tileLoadQueueMedium.length === 0 &&
  321. this._surface._tileLoadQueueLow.length === 0
  322. );
  323. },
  324. },
  325. /**
  326. * Gets or sets the color of the globe when no imagery is available.
  327. * @memberof Globe.prototype
  328. * @type {Color}
  329. */
  330. baseColor: {
  331. get: function () {
  332. return this._surface.tileProvider.baseColor;
  333. },
  334. set: function (value) {
  335. this._surface.tileProvider.baseColor = value;
  336. },
  337. },
  338. /**
  339. * A property specifying a {@link ClippingPlaneCollection} used to selectively disable rendering on the outside of each plane.
  340. *
  341. * @memberof Globe.prototype
  342. * @type {ClippingPlaneCollection}
  343. */
  344. clippingPlanes: {
  345. get: function () {
  346. return this._surface.tileProvider.clippingPlanes;
  347. },
  348. set: function (value) {
  349. this._surface.tileProvider.clippingPlanes = value;
  350. },
  351. },
  352. /**
  353. * A property specifying a {@link Rectangle} used to limit globe rendering to a cartographic area.
  354. * Defaults to the maximum extent of cartographic coordinates.
  355. *
  356. * @memberof Globe.prototype
  357. * @type {Rectangle}
  358. * @default {@link Rectangle.MAX_VALUE}
  359. */
  360. cartographicLimitRectangle: {
  361. get: function () {
  362. return this._surface.tileProvider.cartographicLimitRectangle;
  363. },
  364. set: function (value) {
  365. if (!defined(value)) {
  366. value = Rectangle.clone(Rectangle.MAX_VALUE);
  367. }
  368. this._surface.tileProvider.cartographicLimitRectangle = value;
  369. },
  370. },
  371. /**
  372. * The normal map to use for rendering waves in the ocean. Setting this property will
  373. * only have an effect if the configured terrain provider includes a water mask.
  374. * @memberof Globe.prototype
  375. * @type {String}
  376. * @default buildModuleUrl('Assets/Textures/waterNormalsSmall.jpg')
  377. */
  378. oceanNormalMapUrl: {
  379. get: function () {
  380. return this._oceanNormalMapResource.url;
  381. },
  382. set: function (value) {
  383. this._oceanNormalMapResource.url = value;
  384. this._oceanNormalMapResourceDirty = true;
  385. },
  386. },
  387. /**
  388. * The terrain provider providing surface geometry for this globe.
  389. * @type {TerrainProvider}
  390. *
  391. * @memberof Globe.prototype
  392. * @type {TerrainProvider}
  393. *
  394. */
  395. terrainProvider: {
  396. get: function () {
  397. return this._terrainProvider;
  398. },
  399. set: function (value) {
  400. if (value !== this._terrainProvider) {
  401. this._terrainProvider = value;
  402. this._terrainProviderChanged.raiseEvent(value);
  403. if (defined(this._material)) {
  404. makeShadersDirty(this);
  405. }
  406. }
  407. },
  408. },
  409. /**
  410. * Gets an event that's raised when the terrain provider is changed
  411. *
  412. * @memberof Globe.prototype
  413. * @type {Event}
  414. * @readonly
  415. */
  416. terrainProviderChanged: {
  417. get: function () {
  418. return this._terrainProviderChanged;
  419. },
  420. },
  421. /**
  422. * Gets an event that's raised when the length of the tile load queue has changed since the last render frame. When the load queue is empty,
  423. * all terrain and imagery for the current view have been loaded. The event passes the new length of the tile load queue.
  424. *
  425. * @memberof Globe.prototype
  426. * @type {Event}
  427. */
  428. tileLoadProgressEvent: {
  429. get: function () {
  430. return this._surface.tileLoadProgressEvent;
  431. },
  432. },
  433. /**
  434. * Gets or sets the material appearance of the Globe. This can be one of several built-in {@link Material} objects or a custom material, scripted with
  435. * {@link https://github.com/CesiumGS/cesium/wiki/Fabric|Fabric}.
  436. * @memberof Globe.prototype
  437. * @type {Material}
  438. */
  439. material: {
  440. get: function () {
  441. return this._material;
  442. },
  443. set: function (material) {
  444. if (this._material !== material) {
  445. this._material = material;
  446. makeShadersDirty(this);
  447. }
  448. },
  449. },
  450. /**
  451. * The color to render the back side of the globe when the camera is underground or the globe is translucent,
  452. * blended with the globe color based on the camera's distance.
  453. * <br /><br />
  454. * To disable underground coloring, set <code>undergroundColor</code> to <code>undefined</code>.
  455. *
  456. * @memberof Globe.prototype
  457. * @type {Color}
  458. * @default {@link Color.BLACK}
  459. *
  460. * @see Globe#undergroundColorAlphaByDistance
  461. */
  462. undergroundColor: {
  463. get: function () {
  464. return this._undergroundColor;
  465. },
  466. set: function (value) {
  467. this._undergroundColor = Color.clone(value, this._undergroundColor);
  468. },
  469. },
  470. /**
  471. * Gets or sets the near and far distance for blending {@link Globe#undergroundColor} with the globe color.
  472. * The alpha will interpolate between the {@link NearFarScalar#nearValue} and
  473. * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
  474. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  475. * Outside of these ranges the alpha remains clamped to the nearest bound. If undefined,
  476. * the underground color will not be blended with the globe color.
  477. * <br /> <br />
  478. * When the camera is above the ellipsoid the distance is computed from the nearest
  479. * point on the ellipsoid instead of the camera's position.
  480. *
  481. * @memberof Globe.prototype
  482. * @type {NearFarScalar}
  483. *
  484. * @see Globe#undergroundColor
  485. *
  486. */
  487. undergroundColorAlphaByDistance: {
  488. get: function () {
  489. return this._undergroundColorAlphaByDistance;
  490. },
  491. set: function (value) {
  492. //>>includeStart('debug', pragmas.debug);
  493. if (defined(value) && value.far < value.near) {
  494. throw new DeveloperError(
  495. "far distance must be greater than near distance."
  496. );
  497. }
  498. //>>includeEnd('debug');
  499. this._undergroundColorAlphaByDistance = NearFarScalar.clone(
  500. value,
  501. this._undergroundColorAlphaByDistance
  502. );
  503. },
  504. },
  505. /**
  506. * Properties for controlling globe translucency.
  507. *
  508. * @memberof Globe.prototype
  509. * @type {GlobeTranslucency}
  510. */
  511. translucency: {
  512. get: function () {
  513. return this._translucency;
  514. },
  515. },
  516. });
  517. function makeShadersDirty(globe) {
  518. var defines = [];
  519. var requireNormals =
  520. defined(globe._material) &&
  521. (globe._material.shaderSource.match(/slope/) ||
  522. globe._material.shaderSource.match("normalEC"));
  523. var fragmentSources = [GroundAtmosphere];
  524. if (
  525. defined(globe._material) &&
  526. (!requireNormals || globe._terrainProvider.requestVertexNormals)
  527. ) {
  528. fragmentSources.push(globe._material.shaderSource);
  529. defines.push("APPLY_MATERIAL");
  530. globe._surface._tileProvider.materialUniformMap = globe._material._uniforms;
  531. } else {
  532. globe._surface._tileProvider.materialUniformMap = undefined;
  533. }
  534. fragmentSources.push(GlobeFS);
  535. globe._surfaceShaderSet.baseVertexShaderSource = new ShaderSource({
  536. sources: [GroundAtmosphere, GlobeVS],
  537. defines: defines,
  538. });
  539. globe._surfaceShaderSet.baseFragmentShaderSource = new ShaderSource({
  540. sources: fragmentSources,
  541. defines: defines,
  542. });
  543. globe._surfaceShaderSet.material = globe._material;
  544. }
  545. function createComparePickTileFunction(rayOrigin) {
  546. return function (a, b) {
  547. var aDist = BoundingSphere.distanceSquaredTo(
  548. a.pickBoundingSphere,
  549. rayOrigin
  550. );
  551. var bDist = BoundingSphere.distanceSquaredTo(
  552. b.pickBoundingSphere,
  553. rayOrigin
  554. );
  555. return aDist - bDist;
  556. };
  557. }
  558. var scratchArray = [];
  559. var scratchSphereIntersectionResult = {
  560. start: 0.0,
  561. stop: 0.0,
  562. };
  563. /**
  564. * Find an intersection between a ray and the globe surface that was rendered. The ray must be given in world coordinates.
  565. *
  566. * @param {Ray} ray The ray to test for intersection.
  567. * @param {Scene} scene The scene.
  568. * @param {Boolean} [cullBackFaces=true] Set to true to not pick back faces.
  569. * @param {Cartesian3} [result] The object onto which to store the result.
  570. * @returns {Cartesian3|undefined} The intersection or <code>undefined</code> if none was found. The returned position is in projected coordinates for 2D and Columbus View.
  571. *
  572. * @private
  573. */
  574. Globe.prototype.pickWorldCoordinates = function (
  575. ray,
  576. scene,
  577. cullBackFaces,
  578. result
  579. ) {
  580. //>>includeStart('debug', pragmas.debug);
  581. if (!defined(ray)) {
  582. throw new DeveloperError("ray is required");
  583. }
  584. if (!defined(scene)) {
  585. throw new DeveloperError("scene is required");
  586. }
  587. //>>includeEnd('debug');
  588. cullBackFaces = defaultValue(cullBackFaces, true);
  589. var mode = scene.mode;
  590. var projection = scene.mapProjection;
  591. var sphereIntersections = scratchArray;
  592. sphereIntersections.length = 0;
  593. var tilesToRender = this._surface._tilesToRender;
  594. var length = tilesToRender.length;
  595. var tile;
  596. var i;
  597. for (i = 0; i < length; ++i) {
  598. tile = tilesToRender[i];
  599. var surfaceTile = tile.data;
  600. if (!defined(surfaceTile)) {
  601. continue;
  602. }
  603. var boundingVolume = surfaceTile.pickBoundingSphere;
  604. if (mode !== SceneMode.SCENE3D) {
  605. surfaceTile.pickBoundingSphere = boundingVolume = BoundingSphere.fromRectangleWithHeights2D(
  606. tile.rectangle,
  607. projection,
  608. surfaceTile.tileBoundingRegion.minimumHeight,
  609. surfaceTile.tileBoundingRegion.maximumHeight,
  610. boundingVolume
  611. );
  612. Cartesian3.fromElements(
  613. boundingVolume.center.z,
  614. boundingVolume.center.x,
  615. boundingVolume.center.y,
  616. boundingVolume.center
  617. );
  618. } else if (defined(surfaceTile.renderedMesh)) {
  619. BoundingSphere.clone(
  620. surfaceTile.renderedMesh.boundingSphere3D,
  621. boundingVolume
  622. );
  623. } else {
  624. // So wait how did we render this thing then? It shouldn't be possible to get here.
  625. continue;
  626. }
  627. var boundingSphereIntersection = IntersectionTests.raySphere(
  628. ray,
  629. boundingVolume,
  630. scratchSphereIntersectionResult
  631. );
  632. if (defined(boundingSphereIntersection)) {
  633. sphereIntersections.push(surfaceTile);
  634. }
  635. }
  636. sphereIntersections.sort(createComparePickTileFunction(ray.origin));
  637. var intersection;
  638. length = sphereIntersections.length;
  639. for (i = 0; i < length; ++i) {
  640. intersection = sphereIntersections[i].pick(
  641. ray,
  642. scene.mode,
  643. scene.mapProjection,
  644. cullBackFaces,
  645. result
  646. );
  647. if (defined(intersection)) {
  648. break;
  649. }
  650. }
  651. return intersection;
  652. };
  653. var cartoScratch = new Cartographic();
  654. /**
  655. * Find an intersection between a ray and the globe surface that was rendered. The ray must be given in world coordinates.
  656. *
  657. * @param {Ray} ray The ray to test for intersection.
  658. * @param {Scene} scene The scene.
  659. * @param {Cartesian3} [result] The object onto which to store the result.
  660. * @returns {Cartesian3|undefined} The intersection or <code>undefined</code> if none was found.
  661. *
  662. * @example
  663. * // find intersection of ray through a pixel and the globe
  664. * var ray = viewer.camera.getPickRay(windowCoordinates);
  665. * var intersection = globe.pick(ray, scene);
  666. */
  667. Globe.prototype.pick = function (ray, scene, result) {
  668. result = this.pickWorldCoordinates(ray, scene, true, result);
  669. if (defined(result) && scene.mode !== SceneMode.SCENE3D) {
  670. result = Cartesian3.fromElements(result.y, result.z, result.x, result);
  671. var carto = scene.mapProjection.unproject(result, cartoScratch);
  672. result = scene.globe.ellipsoid.cartographicToCartesian(carto, result);
  673. }
  674. return result;
  675. };
  676. var scratchGetHeightCartesian = new Cartesian3();
  677. var scratchGetHeightIntersection = new Cartesian3();
  678. var scratchGetHeightCartographic = new Cartographic();
  679. var scratchGetHeightRay = new Ray();
  680. function tileIfContainsCartographic(tile, cartographic) {
  681. return defined(tile) && Rectangle.contains(tile.rectangle, cartographic)
  682. ? tile
  683. : undefined;
  684. }
  685. /**
  686. * Get the height of the surface at a given cartographic.
  687. *
  688. * @param {Cartographic} cartographic The cartographic for which to find the height.
  689. * @returns {Number|undefined} The height of the cartographic or undefined if it could not be found.
  690. */
  691. Globe.prototype.getHeight = function (cartographic) {
  692. //>>includeStart('debug', pragmas.debug);
  693. if (!defined(cartographic)) {
  694. throw new DeveloperError("cartographic is required");
  695. }
  696. //>>includeEnd('debug');
  697. var levelZeroTiles = this._surface._levelZeroTiles;
  698. if (!defined(levelZeroTiles)) {
  699. return;
  700. }
  701. var tile;
  702. var i;
  703. var length = levelZeroTiles.length;
  704. for (i = 0; i < length; ++i) {
  705. tile = levelZeroTiles[i];
  706. if (Rectangle.contains(tile.rectangle, cartographic)) {
  707. break;
  708. }
  709. }
  710. if (i >= length) {
  711. return undefined;
  712. }
  713. var tileWithMesh = tile;
  714. while (defined(tile)) {
  715. tile =
  716. tileIfContainsCartographic(tile._southwestChild, cartographic) ||
  717. tileIfContainsCartographic(tile._southeastChild, cartographic) ||
  718. tileIfContainsCartographic(tile._northwestChild, cartographic) ||
  719. tile._northeastChild;
  720. if (
  721. defined(tile) &&
  722. defined(tile.data) &&
  723. defined(tile.data.renderedMesh)
  724. ) {
  725. tileWithMesh = tile;
  726. }
  727. }
  728. tile = tileWithMesh;
  729. // This tile was either rendered or culled.
  730. // It is sometimes useful to get a height from a culled tile,
  731. // e.g. when we're getting a height in order to place a billboard
  732. // on terrain, and the camera is looking at that same billboard.
  733. // The culled tile must have a valid mesh, though.
  734. if (
  735. !defined(tile) ||
  736. !defined(tile.data) ||
  737. !defined(tile.data.renderedMesh)
  738. ) {
  739. // Tile was not rendered (culled).
  740. return undefined;
  741. }
  742. var ellipsoid = this._surface._tileProvider.tilingScheme.ellipsoid;
  743. //cartesian has to be on the ellipsoid surface for `ellipsoid.geodeticSurfaceNormal`
  744. var cartesian = Cartesian3.fromRadians(
  745. cartographic.longitude,
  746. cartographic.latitude,
  747. 0.0,
  748. ellipsoid,
  749. scratchGetHeightCartesian
  750. );
  751. var ray = scratchGetHeightRay;
  752. var surfaceNormal = ellipsoid.geodeticSurfaceNormal(cartesian, ray.direction);
  753. // Try to find the intersection point between the surface normal and z-axis.
  754. // minimum height (-11500.0) for the terrain set, need to get this information from the terrain provider
  755. var rayOrigin = ellipsoid.getSurfaceNormalIntersectionWithZAxis(
  756. cartesian,
  757. 11500.0,
  758. ray.origin
  759. );
  760. // Theoretically, not with Earth datums, the intersection point can be outside the ellipsoid
  761. if (!defined(rayOrigin)) {
  762. // intersection point is outside the ellipsoid, try other value
  763. // minimum height (-11500.0) for the terrain set, need to get this information from the terrain provider
  764. var minimumHeight;
  765. if (defined(tile.data.tileBoundingRegion)) {
  766. minimumHeight = tile.data.tileBoundingRegion.minimumHeight;
  767. }
  768. var magnitude = Math.min(defaultValue(minimumHeight, 0.0), -11500.0);
  769. // multiply by the *positive* value of the magnitude
  770. var vectorToMinimumPoint = Cartesian3.multiplyByScalar(
  771. surfaceNormal,
  772. Math.abs(magnitude) + 1,
  773. scratchGetHeightIntersection
  774. );
  775. Cartesian3.subtract(cartesian, vectorToMinimumPoint, ray.origin);
  776. }
  777. var intersection = tile.data.pick(
  778. ray,
  779. undefined,
  780. undefined,
  781. false,
  782. scratchGetHeightIntersection
  783. );
  784. if (!defined(intersection)) {
  785. return undefined;
  786. }
  787. return ellipsoid.cartesianToCartographic(
  788. intersection,
  789. scratchGetHeightCartographic
  790. ).height;
  791. };
  792. /**
  793. * @private
  794. */
  795. Globe.prototype.update = function (frameState) {
  796. if (!this.show) {
  797. return;
  798. }
  799. if (frameState.passes.render) {
  800. this._surface.update(frameState);
  801. }
  802. };
  803. /**
  804. * @private
  805. */
  806. Globe.prototype.beginFrame = function (frameState) {
  807. var surface = this._surface;
  808. var tileProvider = surface.tileProvider;
  809. var terrainProvider = this.terrainProvider;
  810. var hasWaterMask =
  811. this.showWaterEffect &&
  812. terrainProvider.ready &&
  813. terrainProvider.hasWaterMask;
  814. if (hasWaterMask && this._oceanNormalMapResourceDirty) {
  815. // url changed, load new normal map asynchronously
  816. this._oceanNormalMapResourceDirty = false;
  817. var oceanNormalMapResource = this._oceanNormalMapResource;
  818. var oceanNormalMapUrl = oceanNormalMapResource.url;
  819. if (defined(oceanNormalMapUrl)) {
  820. var that = this;
  821. when(oceanNormalMapResource.fetchImage(), function (image) {
  822. if (oceanNormalMapUrl !== that._oceanNormalMapResource.url) {
  823. // url changed while we were loading
  824. return;
  825. }
  826. that._oceanNormalMap =
  827. that._oceanNormalMap && that._oceanNormalMap.destroy();
  828. that._oceanNormalMap = new Texture({
  829. context: frameState.context,
  830. source: image,
  831. });
  832. });
  833. } else {
  834. this._oceanNormalMap =
  835. this._oceanNormalMap && this._oceanNormalMap.destroy();
  836. }
  837. }
  838. var pass = frameState.passes;
  839. var mode = frameState.mode;
  840. if (pass.render) {
  841. if (this.showGroundAtmosphere) {
  842. this._zoomedOutOceanSpecularIntensity = 0.4;
  843. } else {
  844. this._zoomedOutOceanSpecularIntensity = 0.5;
  845. }
  846. surface.maximumScreenSpaceError = this.maximumScreenSpaceError;
  847. surface.tileCacheSize = this.tileCacheSize;
  848. surface.loadingDescendantLimit = this.loadingDescendantLimit;
  849. surface.preloadAncestors = this.preloadAncestors;
  850. surface.preloadSiblings = this.preloadSiblings;
  851. tileProvider.terrainProvider = this.terrainProvider;
  852. tileProvider.lightingFadeOutDistance = this.lightingFadeOutDistance;
  853. tileProvider.lightingFadeInDistance = this.lightingFadeInDistance;
  854. tileProvider.nightFadeOutDistance = this.nightFadeOutDistance;
  855. tileProvider.nightFadeInDistance = this.nightFadeInDistance;
  856. tileProvider.zoomedOutOceanSpecularIntensity =
  857. mode === SceneMode.SCENE3D ? this._zoomedOutOceanSpecularIntensity : 0.0;
  858. tileProvider.hasWaterMask = hasWaterMask;
  859. tileProvider.oceanNormalMap = this._oceanNormalMap;
  860. tileProvider.enableLighting = this.enableLighting;
  861. tileProvider.dynamicAtmosphereLighting = this.dynamicAtmosphereLighting;
  862. tileProvider.dynamicAtmosphereLightingFromSun = this.dynamicAtmosphereLightingFromSun;
  863. tileProvider.showGroundAtmosphere = this.showGroundAtmosphere;
  864. tileProvider.shadows = this.shadows;
  865. tileProvider.hueShift = this.atmosphereHueShift;
  866. tileProvider.saturationShift = this.atmosphereSaturationShift;
  867. tileProvider.brightnessShift = this.atmosphereBrightnessShift;
  868. tileProvider.fillHighlightColor = this.fillHighlightColor;
  869. tileProvider.showSkirts = this.showSkirts;
  870. tileProvider.backFaceCulling = this.backFaceCulling;
  871. tileProvider.undergroundColor = this._undergroundColor;
  872. tileProvider.undergroundColorAlphaByDistance = this._undergroundColorAlphaByDistance;
  873. surface.beginFrame(frameState);
  874. }
  875. };
  876. /**
  877. * @private
  878. */
  879. Globe.prototype.render = function (frameState) {
  880. if (!this.show) {
  881. return;
  882. }
  883. if (defined(this._material)) {
  884. this._material.update(frameState.context);
  885. }
  886. this._surface.render(frameState);
  887. };
  888. /**
  889. * @private
  890. */
  891. Globe.prototype.endFrame = function (frameState) {
  892. if (!this.show) {
  893. return;
  894. }
  895. if (frameState.passes.render) {
  896. this._surface.endFrame(frameState);
  897. }
  898. };
  899. /**
  900. * Returns true if this object was destroyed; otherwise, false.
  901. * <br /><br />
  902. * If this object was destroyed, it should not be used; calling any function other than
  903. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  904. *
  905. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  906. *
  907. * @see Globe#destroy
  908. */
  909. Globe.prototype.isDestroyed = function () {
  910. return false;
  911. };
  912. /**
  913. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  914. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  915. * <br /><br />
  916. * Once an object is destroyed, it should not be used; calling any function other than
  917. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  918. * assign the return value (<code>undefined</code>) to the object as done in the example.
  919. *
  920. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  921. *
  922. *
  923. * @example
  924. * globe = globe && globe.destroy();
  925. *
  926. * @see Globe#isDestroyed
  927. */
  928. Globe.prototype.destroy = function () {
  929. this._surfaceShaderSet =
  930. this._surfaceShaderSet && this._surfaceShaderSet.destroy();
  931. this._surface = this._surface && this._surface.destroy();
  932. this._oceanNormalMap = this._oceanNormalMap && this._oceanNormalMap.destroy();
  933. return destroyObject(this);
  934. };
  935. export default Globe;