Globe.js 31 KB

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