TileImagery.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*global define*/
  2. define([
  3. '../Core/defined',
  4. './ImageryState'
  5. ], function(
  6. defined,
  7. ImageryState) {
  8. "use strict";
  9. /**
  10. * The assocation between a terrain tile and an imagery tile.
  11. *
  12. * @alias TileImagery
  13. * @private
  14. *
  15. * @param {Imagery} imagery The imagery tile.
  16. * @param {Cartesian4} textureCoordinateRectangle The texture rectangle of the tile that is covered
  17. * by the imagery, where X=west, Y=south, Z=east, W=north.
  18. */
  19. var TileImagery = function(imagery, textureCoordinateRectangle) {
  20. this.readyImagery = undefined;
  21. this.loadingImagery = imagery;
  22. this.textureCoordinateRectangle = textureCoordinateRectangle;
  23. this.textureTranslationAndScale = undefined;
  24. };
  25. /**
  26. * Frees the resources held by this instance.
  27. */
  28. TileImagery.prototype.freeResources = function() {
  29. if (defined(this.readyImagery)) {
  30. this.readyImagery.releaseReference();
  31. }
  32. if (defined(this.loadingImagery)) {
  33. this.loadingImagery.releaseReference();
  34. }
  35. };
  36. /**
  37. * Processes the load state machine for this instance.
  38. *
  39. * @param {Tile} tile The tile to which this instance belongs.
  40. * @param {Context} context The context.
  41. * @returns {Boolean} True if this instance is done loading; otherwise, false.
  42. */
  43. TileImagery.prototype.processStateMachine = function(tile, context) {
  44. var loadingImagery = this.loadingImagery;
  45. var imageryLayer = loadingImagery.imageryLayer;
  46. if (loadingImagery.state === ImageryState.UNLOADED) {
  47. loadingImagery.state = ImageryState.TRANSITIONING;
  48. imageryLayer._requestImagery(loadingImagery);
  49. }
  50. if (loadingImagery.state === ImageryState.RECEIVED) {
  51. loadingImagery.state = ImageryState.TRANSITIONING;
  52. imageryLayer._createTexture(context, loadingImagery);
  53. }
  54. if (loadingImagery.state === ImageryState.TEXTURE_LOADED) {
  55. loadingImagery.state = ImageryState.TRANSITIONING;
  56. imageryLayer._reprojectTexture(context, loadingImagery);
  57. }
  58. if (loadingImagery.state === ImageryState.READY) {
  59. if (defined(this.readyImagery)) {
  60. this.readyImagery.releaseReference();
  61. }
  62. this.readyImagery = this.loadingImagery;
  63. this.loadingImagery = undefined;
  64. this.textureTranslationAndScale = imageryLayer._calculateTextureTranslationAndScale(tile, this);
  65. return true; // done loading
  66. }
  67. // Find some ancestor imagery we can use while this imagery is still loading.
  68. var ancestor = loadingImagery.parent;
  69. var ancestorsAreStillLoading = false;
  70. while (defined(ancestor) && ancestor.state !== ImageryState.READY) {
  71. ancestorsAreStillLoading = ancestorsAreStillLoading || (ancestor.state !== ImageryState.FAILED && ancestor.state !== ImageryState.INVALID);
  72. ancestor = ancestor.parent;
  73. }
  74. if (this.readyImagery !== ancestor) {
  75. if (defined(this.readyImagery)) {
  76. this.readyImagery.releaseReference();
  77. }
  78. this.readyImagery = ancestor;
  79. if (defined(ancestor)) {
  80. ancestor.addReference();
  81. this.textureTranslationAndScale = imageryLayer._calculateTextureTranslationAndScale(tile, this);
  82. }
  83. }
  84. if (!ancestorsAreStillLoading && (loadingImagery.state === ImageryState.FAILED || loadingImagery.state === ImageryState.INVALID)) {
  85. // This imagery tile is failed or invalid, and we have the "best available" substitute. So we're done loading.
  86. return true; // done loading
  87. }
  88. return false; // not done loading
  89. };
  90. return TileImagery;
  91. });