All files / engine/Source/DataSources Cesium3DTilesetVisualizer.js

95.19% Statements 99/104
87.75% Branches 43/49
100% Functions 8/8
95.04% Lines 96/101

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                      1x                       197x 1x   196x         196x         196x 196x 196x 196x 196x 196x                   1x   779x 1x       778x 778x 778x   778x 15x 15x     15x   15x         15x 12x 12x         15x       15x 3x 1x   3x     12x 10x       10x   10x     12x 10x     2x 2x 2x   2x             778x               1x 1x           1x 190x       190x 190x 190x 190x 9x   190x                           1x         17x 1x   16x 1x       15x 15x 7x     8x 8x 7x     1x       1x   1x           1x               248x 248x 248x   248x 35x 35x         248x 28x 28x 22x   6x 6x       248x 2x 2x 2x         17x 17x 10x 8x   10x         10x         10x 10x 9x 8x   8x       8x   2x 2x          
import AssociativeArray from "../Core/AssociativeArray.js";
import BoundingSphere from "../Core/BoundingSphere.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import Matrix4 from "../Core/Matrix4.js";
import Resource from "../Core/Resource.js";
import Cesium3DTileset from "../Scene/Cesium3DTileset.js";
import BoundingSphereState from "./BoundingSphereState.js";
import Property from "./Property.js";
 
const modelMatrixScratch = new Matrix4();
 
/**
 * A {@link Visualizer} which maps {@link Entity#tileset} to a {@link Cesium3DTileset}.
 * @alias Cesium3DTilesetVisualizer
 * @constructor
 *
 * @param {Scene} scene The scene the primitives will be rendered in.
 * @param {EntityCollection} entityCollection The entityCollection to visualize.
 */
function Cesium3DTilesetVisualizer(scene, entityCollection) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(scene)) {
    throw new DeveloperError("scene is required.");
  }
  Iif (!defined(entityCollection)) {
    throw new DeveloperError("entityCollection is required.");
  }
  //>>includeEnd('debug');
 
  entityCollection.collectionChanged.addEventListener(
    Cesium3DTilesetVisualizer.prototype._onCollectionChanged,
    this,
  );
 
  this._scene = scene;
  this._primitives = scene.primitives;
  this._entityCollection = entityCollection;
  this._tilesetHash = {};
  this._entitiesToVisualize = new AssociativeArray();
  this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
}
 
/**
 * Updates models created this visualizer to match their
 * Entity counterpart at the given time.
 *
 * @param {JulianDate} time The time to update to.
 * @returns {boolean} This function always returns true.
 */
Cesium3DTilesetVisualizer.prototype.update = function (time) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(time)) {
    throw new DeveloperError("time is required.");
  }
  //>>includeEnd('debug');
 
  const entities = this._entitiesToVisualize.values;
  const tilesetHash = this._tilesetHash;
  const primitives = this._primitives;
 
  for (let i = 0, len = entities.length; i < len; i++) {
    const entity = entities[i];
    const tilesetGraphics = entity._tileset;
 
    let resource;
    const tilesetData = tilesetHash[entity.id];
    const show =
      entity.isShowing &&
      entity.isAvailable(time) &&
      Property.getValueOrDefault(tilesetGraphics._show, time, true);
 
    let modelMatrix;
    if (show) {
      modelMatrix = entity.computeModelMatrix(time, modelMatrixScratch);
      resource = Resource.createIfNeeded(
        Property.getValueOrUndefined(tilesetGraphics._uri, time),
      );
    }
 
    const tileset = defined(tilesetData)
      ? tilesetData.tilesetPrimitive
      : undefined;
 
    if (!show) {
      if (defined(tileset)) {
        tileset.show = false;
      }
      continue;
    }
 
    if (!defined(tilesetData) || resource.url !== tilesetData.url) {
      Iif (defined(tileset)) {
        primitives.removeAndDestroy(tileset);
      }
 
      delete tilesetHash[entity.id];
 
      createTileset(resource, tilesetHash, entity, primitives);
    }
 
    if (!defined(tileset)) {
      continue;
    }
 
    tileset.show = true;
    Eif (defined(modelMatrix)) {
      tileset.modelMatrix = modelMatrix;
    }
    tileset.maximumScreenSpaceError = Property.getValueOrDefault(
      tilesetGraphics.maximumScreenSpaceError,
      time,
      tileset.maximumScreenSpaceError,
    );
  }
 
  return true;
};
 
/**
 * Returns true if this object was destroyed; otherwise, false.
 *
 * @returns {boolean} True if this object was destroyed; otherwise, false.
 */
Cesium3DTilesetVisualizer.prototype.isDestroyed = function () {
  return false;
};
 
/**
 * Removes and destroys all primitives created by this instance.
 */
Cesium3DTilesetVisualizer.prototype.destroy = function () {
  this._entityCollection.collectionChanged.removeEventListener(
    Cesium3DTilesetVisualizer.prototype._onCollectionChanged,
    this,
  );
  const entities = this._entitiesToVisualize.values;
  const tilesetHash = this._tilesetHash;
  const primitives = this._primitives;
  for (let i = entities.length - 1; i > -1; i--) {
    removeTileset(this, entities[i], tilesetHash, primitives);
  }
  return destroyObject(this);
};
 
/**
 * Computes a bounding sphere which encloses the visualization produced for the specified entity.
 * The bounding sphere is in the fixed frame of the scene's globe.
 *
 * @param {Entity} entity The entity whose bounding sphere to compute.
 * @param {BoundingSphere} result The bounding sphere onto which to store the result.
 * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
 *                       BoundingSphereState.PENDING if the result is still being computed, or
 *                       BoundingSphereState.FAILED if the entity has no visualization in the current scene.
 * @private
 */
Cesium3DTilesetVisualizer.prototype.getBoundingSphere = function (
  entity,
  result,
) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(entity)) {
    throw new DeveloperError("entity is required.");
  }
  if (!defined(result)) {
    throw new DeveloperError("result is required.");
  }
  //>>includeEnd('debug');
 
  const tilesetData = this._tilesetHash[entity.id];
  if (!defined(tilesetData) || tilesetData.loadFail) {
    return BoundingSphereState.FAILED;
  }
 
  const primitive = tilesetData.tilesetPrimitive;
  if (!defined(primitive)) {
    return BoundingSphereState.PENDING;
  }
 
  Iif (!primitive.show) {
    return BoundingSphereState.FAILED;
  }
 
  BoundingSphere.clone(primitive.boundingSphere, result);
 
  return BoundingSphereState.DONE;
};
 
/**
 * @private
 */
Cesium3DTilesetVisualizer.prototype._onCollectionChanged = function (
  entityCollection,
  added,
  removed,
  changed,
) {
  let i;
  let entity;
  const entities = this._entitiesToVisualize;
  const tilesetHash = this._tilesetHash;
  const primitives = this._primitives;
 
  for (i = added.length - 1; i > -1; i--) {
    entity = added[i];
    Iif (defined(entity._tileset)) {
      entities.set(entity.id, entity);
    }
  }
 
  for (i = changed.length - 1; i > -1; i--) {
    entity = changed[i];
    if (defined(entity._tileset)) {
      entities.set(entity.id, entity);
    } else {
      removeTileset(this, entity, tilesetHash, primitives);
      entities.remove(entity.id);
    }
  }
 
  for (i = removed.length - 1; i > -1; i--) {
    entity = removed[i];
    removeTileset(this, entity, tilesetHash, primitives);
    entities.remove(entity.id);
  }
};
 
function removeTileset(visualizer, entity, tilesetHash, primitives) {
  const tilesetData = tilesetHash[entity.id];
  if (defined(tilesetData)) {
    if (defined(tilesetData.tilesetPrimitive)) {
      primitives.removeAndDestroy(tilesetData.tilesetPrimitive);
    }
    delete tilesetHash[entity.id];
  }
}
 
async function createTileset(resource, tilesetHash, entity, primitives) {
  tilesetHash[entity.id] = {
    url: resource.url,
    loadFail: false,
  };
 
  try {
    const tileset = await Cesium3DTileset.fromUrl(resource);
    tileset.id = entity;
    primitives.add(tileset);
 
    Iif (!defined(tilesetHash[entity.id])) {
      return;
    }
 
    tilesetHash[entity.id].tilesetPrimitive = tileset;
  } catch (error) {
    console.error(error);
    tilesetHash[entity.id].loadFail = true;
  }
}
 
export default Cesium3DTilesetVisualizer;