All files / engine/Source/Scene/Model ModelPrimitiveImagery.js

32.09% Statements 78/243
10.9% Branches 6/55
37.03% Functions 10/27
32.46% Lines 75/231

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973                                                                              12x 12x 12x                   12x                 12x                 12x                         12x                         12x             12x                   12x                       12x                             12x                                 12x                                                                                                                                         3x         3x 3x 3x 3x 3x 3x 3x     3x 3x 3x                                                                                                                                                                                                                                                                                                   3x 3x                                                           3x 3x 3x     3x     3x     3x           3x 3x       3x 3x 3x   3x                                                               5x     4x 4x 4x 7x 7x 7x   4x                                                                                         2x     1x 1x 1x 4x 4x 4x   1x                           4x   4x 4x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           6x 5x     4x 4x     4x   4x 4x 4x 4x 4x 4x                                 6x     5x       5x 1x       4x                           7x 7x          
import defined from "../../Core/defined.js";
import DeveloperError from "../../Core/DeveloperError.js";
import Matrix4 from "../../Core/Matrix4.js";
import Check from "../../Core/Check.js";
import destroyObject from "../../Core/destroyObject.js";
 
import ImageryState from "../ImageryState.js";
import ImageryCoverage from "./ImageryCoverage.js";
import ModelImageryMapping from "./ModelImageryMapping.js";
import ModelUtility from "./ModelUtility.js";
import MappedPositions from "./MappedPositions.js";
 
import Buffer from "../../Renderer/Buffer.js";
import BufferUsage from "../../Renderer/BufferUsage.js";
 
/**
 * A class managing the draping of imagery on a single model primitive.
 *
 * The <code>ModelImagery</code> class creates one instance of this
 * class for each primitive that appears in the model.
 *
 * It is responsible for computing
 * - the mapped (cartographic) positions of the primitive
 * - the imagery tiles that are covered by these mapped positions
 * - the texture coordinates (attributes) that correspond to these mapped positions
 *
 * @private
 */
class ModelPrimitiveImagery {
  /**
   * Creates a new instance
   *
   * @param {Model} model The model
   * @param {ModelRuntimeNode} runtimeNode The node that the primitive is attached to
   * @param {ModelRuntimePrimitive} runtimePrimitive The primitive
   * @throws {DeveloperError} If any argument is not defined
   */
  constructor(model, runtimeNode, runtimePrimitive) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("model", model);
    Check.defined("runtimeNode", runtimeNode);
    Check.defined("runtimePrimitive", runtimePrimitive);
    //>>includeEnd('debug');
 
    /**
     * The model that this instance was created for.
     *
     * @type {Model}
     * @readonly
     * @private
     */
    this._model = model;
 
    /**
     * The node that the primitive is attached to
     *
     * @type {ModelRuntimeNode}
     * @readonly
     * @private
     */
    this._runtimeNode = runtimeNode;
 
    /**
     * The primitive that this instance was created for.
     *
     * @type {ModelRuntimePrimitive}
     * @readonly
     * @private
     */
    this._runtimePrimitive = runtimePrimitive;
 
    /**
     * The <code>MappedPositions</code> objects, one for each ellipsoid
     * of one of the imagery layers
     *
     * These objects are just plain structures that summarize the
     * cartographic positions of the primitive for one specific
     * ellipsoid
     *
     * @type {MappedPositions[]|undefined}
     * @private
     */
    this._mappedPositionsPerEllipsoid = undefined;
 
    /**
     * The last <code>model.modelMatrix</code> for which the mapped
     * positions have been computed.
     *
     * This is used for detecting changes in the model matrix that
     * make it necessary to re-compute the mapped positions.
     *
     * @type {Matrix4}
     * @readonly
     * @private
     */
    this._mappedPositionsModelMatrix = new Matrix4();
 
    /**
     * The value that the <code>Cesium3DTileset.imageryLayersModificationCounter</code>
     * had during the last update call. This is used for triggering updates when the
     * imagery layer collection in the tileset changes.
     */
    this._lastImageryLayersModificationCounter = 0;
 
    /**
     * The texture coordinate attributes, one for each projection.
     *
     * This contains one <code>ModelComponents.Attribute</code> for each
     * unique projection that is used in the imagery layers. These
     * texture coordinate attributes are computed based on the mapped
     * positions for the respective ellipsoid of that projection.
     */
    this._imageryTexCoordAttributesPerProjection = undefined;
 
    /**
     * The current imagery layers.
     *
     * This is initialized when the _coveragesPerLayer are computed,
     * and tracked to that the reference counters of the imageries
     * can be decreased when the coverages per layer are deleted.
     *
     * @type {ImageryLayer[]|undefined}
     * @private
     */
    this._currentImageryLayers = undefined;
 
    /**
     * Information about the imagery tiles that are covered by the positions
     * of the primitive.
     *
     * This is computed in the <code>update</code> function, based on the
     * mapped positions of the primitive. After this computation,
     * <code>_coveragesPerLayer[layerIndex]</code> is an array that contains
     * the <code>ImageryCoverage</code> objects that describe the imagery
     * tiles that are covered, including their texture coordinate rectangle.
     *
     * @type {ImageryCoverage[][]|undefined}
     * @private
     */
    this._coveragesPerLayer = undefined;
 
    /**
     * A flag indicating whether all imagery objects that are covered
     * are "ready".
     *
     * This is initially <code>false</code>. During the calls to the
     * <code>update</code> function (which are triggered from the
     * <code>Model.update</code> function, each frame), the
     * <code>_updateImageries</code> function will be called, and
     * process the imagery tiles, until all them them are in a
     * state like <code>ImageryState.READY</code>, at which point
     * this flag is set to <code>true</code>.
     *
     * @type {boolean}
     * @private
     */
    this._allImageriesReady = false;
  }
 
  /**
   * Returns the <code>ImageryCoverage</code> array that has been
   * computed for the given imagery layer.
   *
   * This assumes that the given imagery layer is part of the
   * imagery layer collection of the model, and that this
   * model primitive imagery is "ready", meaning that the
   * coverages have already been computed.
   *
   * Clients may <b>not</b> modify the returned array or any
   * of its objects!
   *
   * @param {ImageryLayer} imageryLayer The imagery layer
   * @returns {ImageryCoverage[]} The coverage information
   */
  coveragesForImageryLayer(imageryLayer) {
    const model = this._model;
    const imageryLayers = model.imageryLayers;
    const index = imageryLayers.indexOf(imageryLayer);
    if (index === -1) {
      throw new DeveloperError("Imagery layer is not part of the model");
    }
    const coveragesPerLayer = this._coveragesPerLayer;
    if (!defined(coveragesPerLayer)) {
      throw new DeveloperError(
        `The coveragesPerLayer have not been computed yet`,
      );
    }
    return coveragesPerLayer[index];
  }
 
  /**
   * Update the state of this instance.
   *
   * This is called as part of <code>ModelImagery.update</code>, which in
   * turn is part of the <code>Model.update</code> that is called in each
   * frame.
   *
   * This will perform the computations that are required to establish
   * the mapping between the imagery and the primitive. It will...
   * <ul>
   *   <li>
   *     Compute the <code>MappedPositions</code> of the primitive,
   *     one instance for each ellipsoid
   *   </li>
   *   <li>
   *     Compute the "coverages per layer", containing the information
   *     about which parts of the respective imagery layer are covered
   *     by the mapped positions
   *   </li>
   *   <li>
   *     Compute the texture coordinate attributes for the imagery, one
   *     for each projection, and store them as the
   *     <code>_imageryTexCoordAttributesPerProjection</code>
   *   </li>
   *   <li>
   *     Update the imageries (i.e. processing their state machine by
   *     calling <code>Imagery.processStateMachine</code>) until they
   *     are in the <code>ImageryState.READY</code> state
   *   </li>
   * </ul>
   *
   * @param {FrameState} frameState The frame state
   */
  update(frameState) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("frameState", frameState);
    //>>includeEnd('debug');
 
    // If the imagery layers have been modified since the last call
    // to this function, then re-build everything
    const model = this._model;
    const content = model.content;
    const tileset = content.tileset;
    const modificationCounter = tileset.imageryLayersModificationCounter;
    Eif (this._lastImageryLayersModificationCounter !== modificationCounter) {
      delete this._mappedPositionsPerEllipsoid;
      this._lastImageryLayersModificationCounter = modificationCounter;
    }
 
    Eif (this._mappedPositionsNeedUpdate) {
      model.resetDrawCommands();
      this._mappedPositionsPerEllipsoid =
        this._computeMappedPositionsPerEllipsoid();
      this._deleteCoveragesPerLayer();
      this._destroyImageryTexCoordAttributes();
    }
 
    if (!defined(this._imageryTexCoordAttributesPerProjection)) {
      this._imageryTexCoordAttributesPerProjection =
        this._computeImageryTexCoordsAttributesPerProjection();
      this._uploadImageryTexCoordAttributes(frameState.context);
    }
 
    if (!defined(this._coveragesPerLayer)) {
      this._computeCoveragesPerLayer();
      this._allImageriesReady = false;
    }
    if (!this._allImageriesReady) {
      this._updateImageries(frameState);
    }
  }
 
  /**
   * Delete the <code>_coveragesPerLayer</code> if they are defined.
   *
   * This will call <code>deleteCoverages</code> for each set of coverages,
   * and eventually delete the <code>_coveragesPerLayer</code>.
   *
   * This will cause the reference counters of the imageries to be
   * decreased.
   */
  _deleteCoveragesPerLayer() {
    const coveragesPerLayer = this._coveragesPerLayer;
    if (!defined(coveragesPerLayer)) {
      return;
    }
    const imageryLayers = this._currentImageryLayers;
    const length = coveragesPerLayer.length;
    for (let i = 0; i < length; i++) {
      const imageryLayer = imageryLayers[i];
      const coverages = coveragesPerLayer[i];
      this._deleteCoverages(imageryLayer, coverages);
    }
    delete this._currentImageryLayers;
    delete this._coveragesPerLayer;
  }
 
  /**
   * Delete the given imagery coverage objects for the given imagery
   * layer, meaning that it will cause the reference counters of the
   * imageries to be decreased.
   *
   * If the imagery layer already has been destroyed, then nothing
   * will be done.
   *
   * @param {ImageryLayer} imageryLayer The imagery layer
   * @param {ImageryCoverage[]} coverages The coverages
   */
  _deleteCoverages(imageryLayer, coverages) {
    if (imageryLayer.isDestroyed()) {
      return;
    }
    const length = coverages.length;
    for (let i = 0; i < length; i++) {
      const coverage = coverages[i];
      const imagery = coverage.imagery;
      imagery.releaseReference();
    }
  }
 
  /**
   * Create the GPU buffers for the typed arrays that are contained
   * in the <code>_imageryTexCoordAttributesPerProjection</code>
   *
   * @param {Context} context The GL context
   */
  _uploadImageryTexCoordAttributes(context) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("context", context);
    //>>includeEnd('debug');
 
    const attributes = this._imageryTexCoordAttributesPerProjection;
    if (!defined(attributes)) {
      return;
    }
    const n = attributes.length;
    for (let i = 0; i < n; i++) {
      const attribute = attributes[i];
 
      // Allocate the GL resources for the new attribute
      const imageryTexCoordBuffer = Buffer.createVertexBuffer({
        context: context,
        typedArray: attribute.typedArray,
        usage: BufferUsage.STATIC_DRAW,
      });
 
      // TODO_DRAPING Review this. Probably, some cleanup
      // has to happen somewhere else after setting this.
      // Check that the call to "destroy" in
      // _destroyImageryTexCoordAttributes is the right
      // thing to do here.
      imageryTexCoordBuffer.vertexArrayDestroyable = false;
 
      attribute.buffer = imageryTexCoordBuffer;
    }
  }
 
  /**
   * Destroy the <code>_imageryTexCoordAttributesPerProjection</code>
   * array.
   *
   * This is called for cleaning up the allocated GPU resources, before
   * they are supposed to be re-computed with
   * <code>_computeImageryTexCoordsAttributesPerProjection</code>
   */
  _destroyImageryTexCoordAttributes() {
    const attributes = this._imageryTexCoordAttributesPerProjection;
    if (!defined(attributes)) {
      return;
    }
    const n = attributes.length;
    for (let i = 0; i < n; i++) {
      const attribute = attributes[i];
      if (defined(attribute)) {
        if (defined(attribute.buffer)) {
          if (!attribute.buffer.isDestroyed()) {
            attribute.buffer.destroy();
          }
        }
        attributes[i] = undefined;
      }
    }
    delete this._imageryTexCoordAttributesPerProjection;
  }
 
  /**
   * Returns whether the <code>MappedPositions</code> have to be
   * re-computed with <code>_computeMappedPositionsPerEllipsoid</code>.
   *
   * This is <code>true</code> when the positions have not yet been
   * computed, or when the <code>modelMatrix</code> of the model
   * changed since the previous call.
   *
   * @returns {boolean} Whether the mapped positions need an update
   * @private
   */
  get _mappedPositionsNeedUpdate() {
    Eif (!defined(this._mappedPositionsPerEllipsoid)) {
      return true;
    }
    const model = this._model;
    const lastModelMatrix = this._mappedPositionsModelMatrix;
    if (!Matrix4.equals(model.modelMatrix, lastModelMatrix)) {
      return true;
    }
    return false;
  }
 
  /**
   * Computes the mapped positions of the primitive, one for each ellipsoid.
   *
   * This computes the <i>unique</i> ellipsoids that appear in the imagery
   * layers of the model, and creates one <code>MappedPositions</code>
   * object for each of them.
   *
   * The respective <code>MappedPositions</code> objects will contain
   * the cartographic positions that are computed from the positions
   * of the primitive. These will serve as the basis for computing the
   * part of the imagery that is covered by the primitive.
   *
   * These mapped positions depend on the current <code>modelMatrix</code>
   * of the model. So they have to be re-computed when the model matrix
   * changes.
   *
   * @returns {MappedPositions[]} The mapped positions
   * @private
   */
  _computeMappedPositionsPerEllipsoid() {
    const model = this._model;
    const runtimeNode = this._runtimeNode;
    const runtimePrimitive = this._runtimePrimitive;
 
    const primitivePositionAttribute =
      ModelPrimitiveImagery._obtainPrimitivePositionAttribute(
        runtimePrimitive.primitive,
      );
    const numPositions = primitivePositionAttribute.count;
 
    const primitivePositionTransform =
      ModelPrimitiveImagery._computePrimitivePositionTransform(
        model,
        runtimeNode,
        undefined,
      );
 
    const mappedPositionsPerEllipsoid = [];
    const ellipsoids = ModelPrimitiveImagery._computeUniqueEllipsoids(
      model.imageryLayers,
    );
 
    const length = ellipsoids.length;
    for (let i = 0; i < length; i++) {
      const ellipsoid = ellipsoids[i];
      const cartographicPositions =
        ModelImageryMapping.createCartographicPositions(
          primitivePositionAttribute,
          primitivePositionTransform,
          ellipsoid,
        );
      const cartographicBoundingRectangle =
        ModelImageryMapping.computeCartographicBoundingRectangle(
          cartographicPositions,
        );
 
      const mappedPositions = new MappedPositions(
        cartographicPositions,
        numPositions,
        cartographicBoundingRectangle,
        ellipsoid,
      );
      mappedPositionsPerEllipsoid.push(mappedPositions);
    }
    Matrix4.clone(model.modelMatrix, this._mappedPositionsModelMatrix);
    return mappedPositionsPerEllipsoid;
  }
 
  /**
   * Computes an array containing the <i>unique</i> ellipsoids that
   * appear in the imagery layers of the given collection.
   *
   * @param {ImageryLayerCollection} imageryLayers
   * @returns {Ellipsoid[]} The ellipsoids
   * @private
   */
  static _computeUniqueEllipsoids(imageryLayers) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("imageryLayers", imageryLayers);
    //>>includeEnd('debug');
 
    const ellipsoidsSet = new Set();
    const length = imageryLayers.length;
    for (let i = 0; i < length; i++) {
      const imageryLayer = imageryLayers.get(i);
      const ellipsoid = ModelPrimitiveImagery._getEllipsoid(imageryLayer);
      ellipsoidsSet.add(ellipsoid);
    }
    return [...ellipsoidsSet];
  }
 
  /**
   * Computes one coordinate attribute for each unique projection
   * that is used in the imagery layers.
   *
   * This is taking the mapped positions, projecting them with
   * the respective projection, and creating a texture coordinate
   * attribute that describes the texture coordinates of these
   * positions, relative to the cartographic bounding rectangle
   * of the mapped positions.
   *
   * @returns {ModelComponents.Attribute[]} The attributes
   */
  _computeImageryTexCoordsAttributesPerProjection() {
    const model = this._model;
    const imageryLayers = model.imageryLayers;
 
    // Compute the arrays containing ALL projections and the array
    // containing the UNIQUE projections from the imagery layers.
    // Texture coordinate attributes only have to be created once
    // for each projection.
    const allProjections =
      ModelPrimitiveImagery._extractProjections(imageryLayers);
    const uniqueProjections = [...new Set(allProjections)];
 
    // Create one texture coordinate attribute for each distinct
    // projection that is used in the imagery layers
    const attributes = this._createImageryTexCoordAttributes(uniqueProjections);
    return attributes;
  }
 
  /**
   * Computes an array containing the projections that are used in
   * the given imagery layers.
   *
   * (Note that this array may contain duplicates)
   *
   * @param {ImageryLayerCollection} imageryLayers The imagery layers
   * @returns {MapProjection[]} The projections
   * @private
   */
  static _extractProjections(imageryLayers) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("imageryLayers", imageryLayers);
    //>>includeEnd('debug');
 
    const projections = [];
    const length = imageryLayers.length;
    for (let i = 0; i < length; i++) {
      const imageryLayer = imageryLayers.get(i);
      const projection = ModelPrimitiveImagery._getProjection(imageryLayer);
      projections.push(projection);
    }
    return projections;
  }
 
  /**
   * Returns the projection of the given imagery layer.
   *
   * This only exists to hide a train wreck
   *
   * @param {ImageryLayer} imageryLayer The imagery layer
   * @returns {MapProjection} The projection
   * @private
   */
  static _getProjection(imageryLayer) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("imageryLayer", imageryLayer);
    //>>includeEnd('debug');
    const projection = imageryLayer.imageryProvider.tilingScheme.projection;
    return projection;
  }
 
  /**
   * Create texture coordinates, one for each projection.
   *
   * This will create a texture coordinate attribute for each of the given projections,
   * using <code>ModelImageryMapping.createTextureCoordinatesAttributeForMappedPositions</code>,
   *
   * (This means that the given projections should indeed be unique,
   * i.e. contain no duplicates)
   *
   * @param {MapProjection[]} uniqueProjections The projections
   * @returns {ModelComponents.Attribute[]} The attributes
   */
  _createImageryTexCoordAttributes(uniqueProjections) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("uniqueProjections", uniqueProjections);
    //>>includeEnd('debug');
 
    const imageryTexCoordAttributePerUniqueProjection = [];
    const length = uniqueProjections.length;
    for (let i = 0; i < length; i++) {
      // Obtain the mapped positions for the ellipsoid that is used
      // in the projection (i.e. the cartographic positions of the
      // primitive, for the respective ellipsoid)
      const projection = uniqueProjections[i];
      const ellipsoid = projection.ellipsoid;
      const mappedPositions = this.mappedPositionsForEllipsoid(ellipsoid);
 
      // Create the actual attribute
      const imageryTexCoordAttribute =
        ModelImageryMapping.createTextureCoordinatesAttributeForMappedPositions(
          mappedPositions,
          projection,
        );
      imageryTexCoordAttributePerUniqueProjection.push(
        imageryTexCoordAttribute,
      );
    }
    return imageryTexCoordAttributePerUniqueProjection;
  }
 
  /**
   * Compute the coverage information for the primitive, based on the
   * imagery layers that are associated with the model.
   *
   * This updates the <code>_coveragesPerLayer[layerIndex]</code>, which
   * is an array that contains the <code>ImageryCoverage</code> objects that
   * describe the imagery tiles that are covered by the primitive, including
   * their texture coordinate rectangle.
   *
   * This has to be called after the mapped positions for the primitive
   * have been computed with <code>_computeMappedPositionsPerEllipsoid</code>.
   *
   * @private
   */
  _computeCoveragesPerLayer() {
    const coveragesPerLayer = [];
    const currentImageryLayers = [];
 
    const model = this._model;
    const imageryLayers = model.imageryLayers;
    const length = imageryLayers.length;
    for (let i = 0; i < length; i++) {
      const imageryLayer = imageryLayers.get(i);
      const coverages = this._computeCoverage(imageryLayer);
      coveragesPerLayer.push(coverages);
      currentImageryLayers.push(imageryLayer);
    }
 
    this._coveragesPerLayer = coveragesPerLayer;
    this._currentImageryLayers = currentImageryLayers;
  }
 
  /**
   * Compute the coverage information for the primitive, based on the
   * given imagery layer.
   *
   * This returns an array that contains the <code>ImageryCoverage</code>
   * objects that describe the imagery tiles that are covered by the
   * primitive, including their texture coordinate rectangle.
   *
   * This has to be called after the mapped positions for the primitive
   * have been computed with <code>_computeMappedPositionsPerEllipsoid</code>.
   *
   * @param {ImageryLayer} imageryLayer The imagery layer
   * @returns {ImageryCoverage[]} The coverage information
   * @private
   */
  _computeCoverage(imageryLayer) {
    const mappedPositions = this.mappedPositionsForImageryLayer(imageryLayer);
    const cartographicBoundingRectangle =
      mappedPositions.cartographicBoundingRectangle;
 
    const imageryLevel = this._computeImageryLevel(
      imageryLayer,
      cartographicBoundingRectangle,
    );
 
    const coverages = ImageryCoverage.createImageryCoverages(
      cartographicBoundingRectangle,
      imageryLayer,
      imageryLevel,
    );
    return coverages;
  }
 
  /**
   * Compute a <code>level</code> for accessing the imagery from the given
   * imagery layer that is suitable for a primitive with the given bounding
   * rectangle.
   *
   * @param {ImageryLayer} imageryLayer The imagery layer
   * @param {Rectangle} cartographicBoundingRectangle The cartographic
   * bounding rectangle, as obtained from the MappedPositions for
   * the given imagery layer
   * @returns {number} The imagery level
   */
  _computeImageryLevel(imageryLayer, cartographicBoundingRectangle) {
    const imageryProvider = imageryLayer.imageryProvider;
    const tilingScheme = imageryProvider.tilingScheme;
    const rectangle = tilingScheme.rectangle;
 
    // The number of tiles covered by the boundingRectangle (b)
    // for a certain level, based on the tiling scheme rectangle (r) is
    // numberOfTilesCovered = b / (r / 2^level)
    // Solving for "level" yields
    // level = log2( numberOfTilesCovered * r / b)
 
    // The goal here is to drape approximately (!) one imagery
    // tile on each primitive. In practice, it may be more
    // (up to 9 in theory)
    const desiredNumberOfTilesCovered = 1;
 
    // Perform the computation of the desired level, based on the
    // number of tiles that should be covered (by whatever is
    // larger, the width or the height)
    let boundingRectangleSize = cartographicBoundingRectangle.width;
    let rectangleSize = rectangle.width;
    if (
      cartographicBoundingRectangle.height > cartographicBoundingRectangle.width
    ) {
      boundingRectangleSize = cartographicBoundingRectangle.height;
      rectangleSize = rectangle.height;
    }
    const desiredLevel = Math.log2(
      (desiredNumberOfTilesCovered * rectangleSize) / boundingRectangleSize,
    );
 
    // Clamp the level to a valid range, and an integer value
    const imageryLevel = ImageryCoverage._clampImageryLevel(
      imageryProvider,
      desiredLevel,
    );
    return imageryLevel;
  }
 
  /**
   * Update all <code>Imagery</code> objects.
   *
   * This is called as part of <code>update</code>, until all required
   * imagery tiles are "ready", as indicated by their <code>state</code>
   * being <code>ImageryState.READY</code>.
   *
   * This is called after it has been determined which imagery tiles are
   * covered by the primitive (i.e. after the <code>_coveragesPerLayer</code>
   * have been computed by calling <code>_computeCoverages</code>).
   *
   * For each covered imagery tile, this will call
   * <code>Imagery.processStateMachine</code> until they are either
   * READY, FAILED, or INVALID.
   *
   * Once they all are in one of these final states, it will set the
   * <code>_allImageriesReady</code> flag to <code>true</code>.
   *
   * @param {FrameState} frameState The frame state, to be passed to
   * <code>imagery.processStateMachine</code>
   * @private
   */
  _updateImageries(frameState) {
    const model = this._model;
    const coveragesPerLayer = this._coveragesPerLayer;
    const length = coveragesPerLayer.length;
    let allImageriesReady = true;
    for (let i = 0; i < length; i++) {
      const coverages = coveragesPerLayer[i];
      const n = coverages.length;
      for (let j = 0; j < n; j++) {
        const coverage = coverages[j];
        const imagery = coverage.imagery;
 
        // In the context of loading the imagery for draping
        // it over the primitive, the imagery counts as "ready"
        // when it is really ready, but also when it failed
        // or was invalid (otherwise, the primitive would
        // never turn "ready"
        const countsAsReady =
          imagery.state === ImageryState.READY ||
          imagery.state === ImageryState.FAILED ||
          imagery.state === ImageryState.INVALID;
        if (!countsAsReady) {
          allImageriesReady = false;
          imagery.processStateMachine(frameState, false, false);
        }
      }
    }
 
    // When the imageries turned ready, reset the draw commands
    // to trigger a rendering with the updated draw commands
    // that include the imagery now.
    if (allImageriesReady) {
      model.resetDrawCommands();
    }
    this._allImageriesReady = allImageriesReady;
  }
 
  /**
   * Returns the <code>MappedPositions</code> object that contains
   * information about the primitive positions that have been computed
   * for the given imagery layer.
   *
   * This assumes that <code>_computeMappedPositionsPerEllipsoid</code> has
   * already been called.
   *
   * @param {ImageryLayer} imageryLayer The imageryLayer
   * @returns {MappedPositions} The mapped positions
   * @throws {DeveloperError} If the mapped positions for the
   * ellipsoid could not be found.
   */
  mappedPositionsForImageryLayer(imageryLayer) {
    const ellipsoid = ModelPrimitiveImagery._getEllipsoid(imageryLayer);
    return this.mappedPositionsForEllipsoid(ellipsoid);
  }
 
  /**
   * Returns the <code>MappedPositions</code> object that contains
   * information about the primitive positions that have been computed
   * from the given ellipsoid.
   *
   * This assumes that <code>_computeMappedPositions</code> has
   * already been called.
   *
   * @param {Ellipsoid} ellipsoid The ellipsoid
   * @returns {MappedPositions} The mapped positions
   * @throws {DeveloperError} If the mapped positions for the
   * given ellipsoid could not be found.
   */
  mappedPositionsForEllipsoid(ellipsoid) {
    const mappedPositionsPerEllipsoid = this._mappedPositionsPerEllipsoid;
    if (!defined(mappedPositionsPerEllipsoid)) {
      throw new DeveloperError(
        `The mappedPositions have not been computed yet`,
      );
    }
 
    const length = mappedPositionsPerEllipsoid.length;
    for (let i = 0; i < length; i++) {
      const mappedPositions = mappedPositionsPerEllipsoid[i];
      if (mappedPositions.ellipsoid === ellipsoid) {
        return mappedPositions;
      }
    }
    throw new DeveloperError(
      `Could not find mapped positions for ellipsoid ${ellipsoid}`,
    );
  }
 
  /**
   * Returns the texture coordinate attributes for the primitive that
   * are used for draping the imagery.
   *
   * This will be available when this object is <code>ready</code>, and
   * will contain one attribute for each unique projection that appears
   * in the imagery layers.
   *
   * @returns {ModelComponents.Attribute[]} The attributes
   */
  imageryTexCoordAttributesPerProjection() {
    const imageryTexCoordAttributesPerProjection =
      this._imageryTexCoordAttributesPerProjection;
    if (!defined(imageryTexCoordAttributesPerProjection)) {
      throw new DeveloperError(
        `The imagery texture coordinate attributes have not been computed yet`,
      );
    }
    return this._imageryTexCoordAttributesPerProjection;
  }
 
  /**
   * Returns whether the draping computations are "ready".
   *
   * This means that the <code>coveragesPerLayer</code> information
   * has been computed, which describes the set of imagery tiles
   * that are covered by the primitive, <b>and</b> that all the
   * covered imagery tiles are in a state that counts as "ready"
   * (i.e. <code>ImageryState.READY</code>, <code>FAILED</code>,
   * or <code>INVALID</code>).
   *
   * @returns {boolean} Whether the draping computations are ready
   */
  get ready() {
    const coveragesPerLayer = this._coveragesPerLayer;
    if (!defined(coveragesPerLayer)) {
      return false;
    }
    return this._allImageriesReady;
  }
 
  /**
   * Returns whether this object was destroyed.
   *
   * If this object was destroyed, calling any function other than
   * <code>isDestroyed</code> will result in a {@link DeveloperError}.
   *
   * @returns {boolean} Whether this object was destroyed
   */
  isDestroyed() {
    return false;
  }
 
  /**
   * Destroys this object and all its resources.
   */
  destroy() {
    if (this.isDestroyed()) {
      return;
    }
    this._deleteCoveragesPerLayer();
    this._destroyImageryTexCoordAttributes();
    return destroyObject(this);
  }
 
  /**
   * Compute the transform that apparently has to be applied to
   * the positions attribute of a primitive, to compute the
   * actual, final positions in ECEF coordinates.
   *
   * This is based on the computation of the bounding
   * sphere that is done at the end of buildDrawCommands
   *
   * @param {Model} model The model
   * @param {ModelComponents.Node} runtimeNode The runtime node
   * that the primitive is attached to
   * @param {Matrix4} [result] The result
   * @returns {Matrix4} The result
   * @private
   */
  static _computePrimitivePositionTransform(model, runtimeNode, result) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("model", model);
    Check.defined("runtimeNode", runtimeNode);
    //>>includeEnd('debug');
 
    Eif (!defined(result)) {
      result = new Matrix4();
    }
 
    const modelSceneGraph = model.sceneGraph;
 
    Matrix4.clone(Matrix4.IDENTITY, result);
    Matrix4.multiply(result, model.modelMatrix, result);
    Matrix4.multiply(result, modelSceneGraph.components.transform, result);
    Matrix4.multiply(result, modelSceneGraph.axisCorrectionMatrix, result);
    Matrix4.multiply(result, runtimeNode.computedTransform, result);
    return result;
  }
 
  /**
   * Returns the <code>"POSITION"</code> attribute from the given primitive.
   *
   * The <code>"POSITION"</code> attribute is required. If it is not
   * defined for the given primitive, then a <code>DeveloperError</code>
   * is thrown.
   *
   * @param {ModelComponents.Primitive} primitive The primitive
   * @returns {ModelComponents.Attribute} The position attribute
   * @throws {DeveloperError} If there is no position attribute
   * @private
   */
  static _obtainPrimitivePositionAttribute(primitive) {
    //>>includeStart('debug', pragmas.debug);
    Check.defined("primitive", primitive);
    //>>includeEnd('debug');
 
    const primitivePositionAttribute = ModelUtility.getAttributeBySemantic(
      primitive,
      "POSITION",
    );
    if (!defined(primitivePositionAttribute)) {
      throw new DeveloperError(
        "The primitive does not have a POSITION attribute",
      );
    }
    return primitivePositionAttribute;
  }
 
  /**
   * Returns the ellipsoid of the given imagery layer.
   *
   * This only exists to hide a train wreck
   *
   * @param {ImageryLayer} imageryLayer The imagery layer
   * @returns {Ellipsoid} The ellipsoid
   * @private
   */
  static _getEllipsoid(imageryLayer) {
    const ellipsoid =
      imageryLayer.imageryProvider.tilingScheme.projection.ellipsoid;
    return ellipsoid;
  }
}
 
export default ModelPrimitiveImagery;