TileImagery.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import defined from '../Core/defined.js';
  2. import ImageryState from './ImageryState.js';
  3. /**
  4. * The assocation between a terrain tile and an imagery tile.
  5. *
  6. * @alias TileImagery
  7. * @private
  8. *
  9. * @param {Imagery} imagery The imagery tile.
  10. * @param {Cartesian4} textureCoordinateRectangle The texture rectangle of the tile that is covered
  11. * by the imagery, where X=west, Y=south, Z=east, W=north.
  12. * @param {Boolean} useWebMercatorT true to use the Web Mercator texture coordinates for this imagery tile.
  13. */
  14. function TileImagery(imagery, textureCoordinateRectangle, useWebMercatorT) {
  15. this.readyImagery = undefined;
  16. this.loadingImagery = imagery;
  17. this.textureCoordinateRectangle = textureCoordinateRectangle;
  18. this.textureTranslationAndScale = undefined;
  19. this.useWebMercatorT = useWebMercatorT;
  20. }
  21. /**
  22. * Frees the resources held by this instance.
  23. */
  24. TileImagery.prototype.freeResources = function() {
  25. if (defined(this.readyImagery)) {
  26. this.readyImagery.releaseReference();
  27. }
  28. if (defined(this.loadingImagery)) {
  29. this.loadingImagery.releaseReference();
  30. }
  31. };
  32. /**
  33. * Processes the load state machine for this instance.
  34. *
  35. * @param {Tile} tile The tile to which this instance belongs.
  36. * @param {FrameState} frameState The frameState.
  37. * @param {Boolean} skipLoading True to skip loading, e.g. new requests, creating textures. This function will
  38. * still synchronously process imagery that's already mostly ready to go, e.g. use textures
  39. * already loaded on ancestor tiles.
  40. * @returns {Boolean} True if this instance is done loading; otherwise, false.
  41. */
  42. TileImagery.prototype.processStateMachine = function(tile, frameState, skipLoading) {
  43. var loadingImagery = this.loadingImagery;
  44. var imageryLayer = loadingImagery.imageryLayer;
  45. loadingImagery.processStateMachine(frameState, !this.useWebMercatorT, skipLoading);
  46. if (loadingImagery.state === ImageryState.READY) {
  47. if (defined(this.readyImagery)) {
  48. this.readyImagery.releaseReference();
  49. }
  50. this.readyImagery = this.loadingImagery;
  51. this.loadingImagery = undefined;
  52. this.textureTranslationAndScale = imageryLayer._calculateTextureTranslationAndScale(tile, this);
  53. return true; // done loading
  54. }
  55. // Find some ancestor imagery we can use while this imagery is still loading.
  56. var ancestor = loadingImagery.parent;
  57. var closestAncestorThatNeedsLoading;
  58. while (defined(ancestor) && (ancestor.state !== ImageryState.READY || (!this.useWebMercatorT && !defined(ancestor.texture)))) {
  59. if (ancestor.state !== ImageryState.FAILED && ancestor.state !== ImageryState.INVALID) {
  60. // ancestor is still loading
  61. closestAncestorThatNeedsLoading = closestAncestorThatNeedsLoading || ancestor;
  62. }
  63. ancestor = ancestor.parent;
  64. }
  65. if (this.readyImagery !== ancestor) {
  66. if (defined(this.readyImagery)) {
  67. this.readyImagery.releaseReference();
  68. }
  69. this.readyImagery = ancestor;
  70. if (defined(ancestor)) {
  71. ancestor.addReference();
  72. this.textureTranslationAndScale = imageryLayer._calculateTextureTranslationAndScale(tile, this);
  73. }
  74. }
  75. if (loadingImagery.state === ImageryState.FAILED || loadingImagery.state === ImageryState.INVALID) {
  76. // The imagery tile is failed or invalid, so we'd like to use an ancestor instead.
  77. if (defined(closestAncestorThatNeedsLoading)) {
  78. // Push the ancestor's load process along a bit. This is necessary because some ancestor imagery
  79. // tiles may not be attached directly to a terrain tile. Such tiles will never load if
  80. // we don't do it here.
  81. closestAncestorThatNeedsLoading.processStateMachine(frameState, !this.useWebMercatorT, skipLoading);
  82. return false; // not done loading
  83. }
  84. // This imagery tile is failed or invalid, and we have the "best available" substitute.
  85. return true; // done loading
  86. }
  87. return false; // not done loading
  88. };
  89. export default TileImagery;