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

91.39% Statements 85/93
86.36% Branches 19/22
81.48% Functions 22/27
91.3% Lines 84/92

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                                                  1077x 1077x     1077x 1077x     1077x 1077x   1077x 1077x   1077x   1077x 1077x   1077x     1x                         4593x                               9001x                                                                         7471x           1077x 1077x   1077x 1077x         1077x 1077x 1042x 1042x 11098x     35x 671x               1077x 1077x   1077x                           1x   7467x 7467x   7467x         7467x 39x 39x       1x 902x     1x 1060x     1x 9x     1x 945x     1x 1078x     1x 8x     1x       1x 1826x     1x 206x     1x       282x     1x 247x     1x       2x     1x 3x     1x 22x     1x       1x       1x       1x       1x 1138x 1060x 1060x 1060x     78x 900x 900x       900x       900x 900x                               1x 1x                                       1x 1048x 1048x        
import BatchTexture from "../BatchTexture.js";
import Cesium3DTileFeature from "../Cesium3DTileFeature.js";
import Check from "../../Core/Check.js";
import Color from "../../Core/Color.js";
import defined from "../../Core/defined.js";
import destroyObject from "../../Core/destroyObject.js";
import ModelFeature from "./ModelFeature.js";
import StyleCommandsNeeded from "./StyleCommandsNeeded.js";
import ModelType from "./ModelType.js";
 
/**
 * Manages the {@link ModelFeature}s in a {@link Model}.
 * Extracts the properties from a {@link PropertyTable}.
 *
 * @param {object} options An object containing the following options:
 * @param {Model} options.model The model that owns this feature table.
 * @param {PropertyTable} options.propertyTable The property table from the model used to initialize the model.
 *
 * @alias ModelFeatureTable
 * @constructor
 *
 * @private
 * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
 */
function ModelFeatureTable(options) {
  const model = options.model;
  const propertyTable = options.propertyTable;
 
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("propertyTable", propertyTable);
  Check.typeOf.object("model", model);
  //>>includeEnd('debug');
 
  this._propertyTable = propertyTable;
  this._model = model;
 
  this._features = undefined;
  this._featuresLength = 0;
 
  this._batchTexture = undefined;
 
  this._styleCommandsNeededDirty = false;
  this._styleCommandsNeeded = StyleCommandsNeeded.ALL_OPAQUE;
 
  initialize(this);
}
 
Object.defineProperties(ModelFeatureTable.prototype, {
  /**
   * The batch texture created for the features in this table.
   *
   * @memberof ModelFeatureTable.prototype
   *
   * @type {BatchTexture}
   * @readonly
   *
   * @private
   */
  batchTexture: {
    get: function () {
      return this._batchTexture;
    },
  },
 
  /**
   * The number of features in this table.
   *
   * @memberof ModelFeatureTable.prototype
   *
   * @type {number}
   * @readonly
   *
   * @private
   */
  featuresLength: {
    get: function () {
      return this._featuresLength;
    },
  },
 
  /**
   * Size of the batch texture. This does not count the property table size
   * as that is counted separately through StructuralMetadata.
   *
   * @memberof ModelFeatureTable.prototype
   *
   * @type {number}
   * @readonly
   *
   * @private
   */
  batchTextureByteLength: {
    get: function () {
      if (defined(this._batchTexture)) {
        return this._batchTexture.byteLength;
      }
 
      return 0;
    },
  },
 
  /**
   * A flag to indicate whether or not the types of style commands needed by this feature table have changed.
   *
   * @memberof ModelFeatureTable.prototype
   *
   * @type {boolean}
   * @readonly
   *
   * @private
   */
  styleCommandsNeededDirty: {
    get: function () {
      return this._styleCommandsNeededDirty;
    },
  },
});
 
function initialize(modelFeatureTable) {
  const model = modelFeatureTable._model;
  const is3DTiles = ModelType.is3DTiles(model.type);
 
  const featuresLength = modelFeatureTable._propertyTable.count;
  Iif (featuresLength === 0) {
    return;
  }
 
  let i;
  const features = new Array(featuresLength);
  if (is3DTiles) {
    const content = model.content;
    for (i = 0; i < featuresLength; i++) {
      features[i] = new Cesium3DTileFeature(content, i);
    }
  } else {
    for (i = 0; i < featuresLength; i++) {
      features[i] = new ModelFeature({
        model: model,
        featureId: i,
        featureTable: modelFeatureTable,
      });
    }
  }
 
  modelFeatureTable._features = features;
  modelFeatureTable._featuresLength = featuresLength;
 
  modelFeatureTable._batchTexture = new BatchTexture({
    featuresLength: featuresLength,
    owner: modelFeatureTable,
    statistics: is3DTiles ? model.content.tileset.statistics : undefined,
  });
}
 
/**
 * Creates/updates the batch texture.
 *
 * @param {FrameState} frameState The frame state.
 *
 * @private
 */
ModelFeatureTable.prototype.update = function (frameState) {
  // Assume the number of translucent features has not changed.
  this._styleCommandsNeededDirty = false;
  this._batchTexture.update(undefined, frameState);
 
  const currentStyleCommandsNeeded = StyleCommandsNeeded.getStyleCommandsNeeded(
    this._featuresLength,
    this._batchTexture.translucentFeaturesLength,
  );
 
  if (this._styleCommandsNeeded !== currentStyleCommandsNeeded) {
    this._styleCommandsNeededDirty = true;
    this._styleCommandsNeeded = currentStyleCommandsNeeded;
  }
};
 
ModelFeatureTable.prototype.setShow = function (featureId, show) {
  this._batchTexture.setShow(featureId, show);
};
 
ModelFeatureTable.prototype.setAllShow = function (show) {
  this._batchTexture.setAllShow(show);
};
 
ModelFeatureTable.prototype.getShow = function (featureId) {
  return this._batchTexture.getShow(featureId);
};
 
ModelFeatureTable.prototype.setColor = function (featureId, color) {
  this._batchTexture.setColor(featureId, color);
};
 
ModelFeatureTable.prototype.setAllColor = function (color) {
  this._batchTexture.setAllColor(color);
};
 
ModelFeatureTable.prototype.getColor = function (featureId, result) {
  return this._batchTexture.getColor(featureId, result);
};
 
ModelFeatureTable.prototype.getPickColor = function (featureId) {
  return this._batchTexture.getPickColor(featureId);
};
 
ModelFeatureTable.prototype.getFeature = function (featureId) {
  return this._features[featureId];
};
 
ModelFeatureTable.prototype.hasProperty = function (featureId, propertyName) {
  return this._propertyTable.hasProperty(featureId, propertyName);
};
 
ModelFeatureTable.prototype.hasPropertyBySemantic = function (
  featureId,
  propertyName,
) {
  return this._propertyTable.hasPropertyBySemantic(featureId, propertyName);
};
 
ModelFeatureTable.prototype.getProperty = function (featureId, name) {
  return this._propertyTable.getProperty(featureId, name);
};
 
ModelFeatureTable.prototype.getPropertyBySemantic = function (
  featureId,
  semantic,
) {
  return this._propertyTable.getPropertyBySemantic(featureId, semantic);
};
 
ModelFeatureTable.prototype.getPropertyIds = function (results) {
  return this._propertyTable.getPropertyIds(results);
};
 
ModelFeatureTable.prototype.setProperty = function (featureId, name, value) {
  return this._propertyTable.setProperty(featureId, name, value);
};
 
ModelFeatureTable.prototype.isClass = function (featureId, className) {
  return this._propertyTable.isClass(featureId, className);
};
 
ModelFeatureTable.prototype.isExactClass = function (featureId, className) {
  return this._propertyTable.isExactClass(featureId, className);
};
 
ModelFeatureTable.prototype.getExactClassName = function (featureId) {
  return this._propertyTable.getExactClassName(featureId);
};
 
const scratchColor = new Color();
/**
 * @private
 */
ModelFeatureTable.prototype.applyStyle = function (style) {
  if (!defined(style)) {
    this.setAllColor(BatchTexture.DEFAULT_COLOR_VALUE);
    this.setAllShow(BatchTexture.DEFAULT_SHOW_VALUE);
    return;
  }
 
  for (let i = 0; i < this._featuresLength; i++) {
    const feature = this.getFeature(i);
    const color = defined(style.color)
      ? (style.color.evaluateColor(feature, scratchColor) ??
        BatchTexture.DEFAULT_COLOR_VALUE)
      : BatchTexture.DEFAULT_COLOR_VALUE;
    const show = defined(style.show)
      ? (style.show.evaluate(feature) ?? BatchTexture.DEFAULT_SHOW_VALUE)
      : BatchTexture.DEFAULT_SHOW_VALUE;
 
    this.setColor(i, color);
    this.setShow(i, show);
  }
};
 
/**
 * Returns true if this object was destroyed; otherwise, false.
 * <p>
 * If this object was destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
 * </p>
 *
 * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
 *
 * @see ModelFeatureTable#destroy
 * @private
 */
ModelFeatureTable.prototype.isDestroyed = function () {
  return false;
};
 
/**
 * Destroys the WebGL resources held by this object.  Destroying an object allows for deterministic
 * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
 * <p>
 * Once an object is destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.  Therefore,
 * assign the return value (<code>undefined</code>) to the object as done in the example.
 * </p>
 *
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 *
 * @example
 * e = e && e.destroy();
 *
 * @see ModelFeatureTable#isDestroyed
 * @private
 */
ModelFeatureTable.prototype.destroy = function (frameState) {
  this._batchTexture = this._batchTexture && this._batchTexture.destroy();
  destroyObject(this);
};
 
export default ModelFeatureTable;