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

95.91% Statements 94/98
95.38% Branches 62/65
100% Functions 3/3
95.91% Lines 94/98

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                                                                                                  1608x   1608x 1608x 1608x   1608x 1607x 1606x                   1605x                 1605x                 1605x                       1605x                   1605x                 1605x                 1605x                       1605x                               1605x                               1605x                   1605x                       1x 1798x 1798x   1798x 1798x 1798x   1798x 1798x   1798x 1798x   1798x   1798x     1798x 1798x         1798x   1798x   1798x   1798x   1798x         1798x             1798x   1798x   1798x   1798x       1798x   1798x   1798x   1798x     1798x 14x     1798x   1798x 6x     1798x 5x     1798x 3x     1798x 15x     1798x 23x     1798x 9x     1798x 1290x           1290x       1798x 1797x         1798x 1798x 1798x   1798x 1141x 1141x 1141x     1798x 4x     1798x 10x     1798x   1798x 1775x     1798x 1x     1798x             1798x   1798x   1798x             1798x 83x         83x 81x             1717x       1717x 1061x           656x              
import Check from "../../Core/Check.js";
import Frozen from "../../Core/Frozen.js";
import defined from "../../Core/defined.js";
import PrimitiveType from "../../Core/PrimitiveType.js";
import SceneMode from "../SceneMode.js";
import AlphaPipelineStage from "./AlphaPipelineStage.js";
import BatchTexturePipelineStage from "./BatchTexturePipelineStage.js";
import ClassificationPipelineStage from "./ClassificationPipelineStage.js";
import CPUStylingPipelineStage from "./CPUStylingPipelineStage.js";
import CustomShaderMode from "./CustomShaderMode.js";
import CustomShaderPipelineStage from "./CustomShaderPipelineStage.js";
import DequantizationPipelineStage from "./DequantizationPipelineStage.js";
import EdgeDetectionPipelineStage from "./EdgeDetectionPipelineStage.js";
import EdgeVisibilityPipelineStage from "./EdgeVisibilityPipelineStage.js";
import FeatureIdPipelineStage from "./FeatureIdPipelineStage.js";
import GeometryPipelineStage from "./GeometryPipelineStage.js";
import ImageryPipelineStage from "./ImageryPipelineStage.js";
import LightingPipelineStage from "./LightingPipelineStage.js";
import MaterialPipelineStage from "./MaterialPipelineStage.js";
import MetadataPickingPipelineStage from "./MetadataPickingPipelineStage.js";
import MetadataPipelineStage from "./MetadataPipelineStage.js";
import ModelUtility from "./ModelUtility.js";
import MorphTargetsPipelineStage from "./MorphTargetsPipelineStage.js";
import PickingPipelineStage from "./PickingPipelineStage.js";
import PointCloudStylingPipelineStage from "./PointCloudStylingPipelineStage.js";
import PrimitiveOutlinePipelineStage from "./PrimitiveOutlinePipelineStage.js";
import PrimitiveStatisticsPipelineStage from "./PrimitiveStatisticsPipelineStage.js";
import SceneMode2DPipelineStage from "./SceneMode2DPipelineStage.js";
import SelectedFeatureIdPipelineStage from "./SelectedFeatureIdPipelineStage.js";
import SkinningPipelineStage from "./SkinningPipelineStage.js";
import VerticalExaggerationPipelineStage from "./VerticalExaggerationPipelineStage.js";
import WireframePipelineStage from "./WireframePipelineStage.js";
import oneTimeWarning from "../../Core/oneTimeWarning.js";
 
/**
 * In memory representation of a single primitive, that is, a primitive
 * and its corresponding mesh.
 *
 * @param {object} options An object containing the following options:
 * @param {ModelComponents.Primitive} options.primitive The primitive component.
 * @param {ModelComponents.Node} options.node The node that this primitive belongs to.
 * @param {Model} options.model The {@link Model} this primitive belongs to.
 *
 * @alias ModelRuntimePrimitive
 * @constructor
 *
 * @private
 */
function ModelRuntimePrimitive(options) {
  options = options ?? Frozen.EMPTY_OBJECT;
 
  const primitive = options.primitive;
  const node = options.node;
  const model = options.model;
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("options.primitive", primitive);
  Check.typeOf.object("options.node", node);
  Check.typeOf.object("options.model", model);
  //>>includeEnd('debug');
 
  /**
   * The primitive component associated with this primitive.
   *
   * @type {ModelComponents.Primitive}
   *
   * @private
   */
  this.primitive = primitive;
 
  /**
   * A reference to the node this primitive belongs to.
   *
   * @type {ModelComponents.Node}
   *
   * @private
   */
  this.node = node;
 
  /**
   * A reference to the model
   *
   * @type {Model}
   *
   * @private
   */
  this.model = model;
 
  /**
   * Pipeline stages to apply to this primitive. This
   * is an array of classes, each with a static method called
   * <code>process()</code>
   *
   * @type {object[]}
   * @readonly
   *
   * @private
   */
  this.pipelineStages = [];
 
  /**
   * The generated {@link ModelDrawCommand} or {@link ClassificationModelDrawCommand}
   * associated with this primitive.
   *
   * @type {ModelDrawCommand|ClassificationModelDrawCommand}
   *
   * @private
   */
  this.drawCommand = undefined;
 
  /**
   * The bounding sphere of this primitive in object-space.
   *
   * @type {BoundingSphere}
   *
   * @private
   */
  this.boundingSphere = undefined;
 
  /**
   * The bounding sphere of this primitive in 2D world space.
   *
   * @type {BoundingSphere}
   *
   * @private
   */
  this.boundingSphere2D = undefined;
 
  /**
   * A buffer containing the primitive's positions projected to 2D world
   * coordinates. This is generated by SceneMode2DPipelineStage and used for
   * rendering in 2D / CV mode. The memory is managed by Model; this is just
   * a reference.
   *
   * @type {Buffer}
   *
   * @private
   */
  this.positionBuffer2D = undefined;
 
  /**
   * An array containing the lengths of the vertex batches for classification.
   * Vertices with the same feature ID are batched together, and each batch is
   * drawn with a different draw command in order to properly classify other
   * assets.
   * <p>
   * This is generated by ClassificationPipelineStage. The memory is managed by
   * Model; this is just a reference.
   * </p>
   *
   * @type {number[]}
   *
   * @private
   */
  this.batchLengths = undefined;
 
  /**
   * An array containing the offsets of the vertex batches for classification.
   * Vertices with the same feature ID are batched together, and each batch is
   * drawn with a different draw command in order to properly classify other
   * assets.
   * <p>
   * This is generated by ClassificationPipelineStage. The memory is managed by
   * Model; this is just a reference.
   * </p>
   *
   * @type {number[]}
   *
   * @private
   */
  this.batchOffsets = undefined;
 
  /**
   * Update stages to apply to this primitive.
   *
   * @type {object[]}
   * @readonly
   *
   * @private
   */
  this.updateStages = [];
}
 
/**
 * Configure the primitive pipeline stages. If the pipeline needs to be re-run,
 * call this method again to ensure the correct sequence of pipeline stages are
 * used.
 *
 * @param {FrameState} frameState The frame state.
 *
 * @private
 */
ModelRuntimePrimitive.prototype.configurePipeline = function (frameState) {
  const pipelineStages = this.pipelineStages;
  pipelineStages.length = 0;
 
  const primitive = this.primitive;
  const node = this.node;
  const model = this.model;
 
  const customShader = model.customShader;
  const style = model.style;
 
  const useWebgl2 = frameState.context.webgl2;
  const mode = frameState.mode;
  const use2D =
    mode !== SceneMode.SCENE3D && !frameState.scene3DOnly && model._projectTo2D;
  const hasVerticalExaggeration =
    frameState.verticalExaggeration !== 1.0 && model.hasVerticalExaggeration;
 
  const hasMorphTargets =
    defined(primitive.morphTargets) && primitive.morphTargets.length > 0;
  const hasSkinning = defined(node.skin);
 
  // Check whether the model is part of a `Model3DTileContent` that
  // belongs to a tileset that has imagery layers. If this is the
  // case, then the `ImageryPipelineStage` will be required.
  const hasImageryLayers = defined(model.imageryLayers);
 
  const hasCustomShader = defined(customShader);
  const hasCustomFragmentShader =
    hasCustomShader && defined(customShader.fragmentShaderText);
  const materialsEnabled =
    !hasCustomFragmentShader ||
    customShader.mode !== CustomShaderMode.REPLACE_MATERIAL;
  const hasQuantization = ModelUtility.hasQuantizedAttributes(
    primitive.attributes,
  );
 
  const generateWireframeIndices =
    model.debugWireframe &&
    PrimitiveType.isTriangles(primitive.primitiveType) &&
    // Generating index buffers for wireframes is always possible in WebGL2.
    // However, this will only work in WebGL1 if the model was constructed with
    // enableDebugWireframe set to true.
    (model._enableDebugWireframe || useWebgl2);
 
  const pointCloudShading = model.pointCloudShading;
  const hasAttenuation =
    defined(pointCloudShading) && pointCloudShading.attenuation;
  const hasPointCloudBackFaceCulling =
    defined(pointCloudShading) && pointCloudShading.backFaceCulling;
  const hasPointCloudStyle =
    primitive.primitiveType === PrimitiveType.POINTS &&
    (defined(style) || hasAttenuation || hasPointCloudBackFaceCulling);
 
  const hasOutlines =
    model._enableShowOutline && defined(primitive.outlineCoordinates);
 
  const hasEdgeVisibility = defined(primitive.edgeVisibility);
 
  const featureIdFlags = inspectFeatureIds(model, node, primitive);
 
  const hasClassification = defined(model.classificationType);
 
  // Start of pipeline -----------------------------------------------------
  if (use2D) {
    pipelineStages.push(SceneMode2DPipelineStage);
  }
 
  pipelineStages.push(GeometryPipelineStage);
 
  if (generateWireframeIndices) {
    pipelineStages.push(WireframePipelineStage);
  }
 
  if (hasClassification) {
    pipelineStages.push(ClassificationPipelineStage);
  }
 
  if (hasMorphTargets) {
    pipelineStages.push(MorphTargetsPipelineStage);
  }
 
  if (hasSkinning) {
    pipelineStages.push(SkinningPipelineStage);
  }
 
  if (hasPointCloudStyle) {
    pipelineStages.push(PointCloudStylingPipelineStage);
  }
 
  if (hasQuantization) {
    pipelineStages.push(DequantizationPipelineStage);
  }
 
  if (hasImageryLayers) {
    Iif (hasOutlines) {
      oneTimeWarning(
        "outlines-and-draping",
        "Primitive outlines disable imagery draping",
      );
    } else {
      pipelineStages.push(ImageryPipelineStage);
    }
  }
 
  if (materialsEnabled) {
    pipelineStages.push(MaterialPipelineStage);
  }
 
  // These stages are always run to ensure structs
  // are declared to avoid compilation errors.
  pipelineStages.push(FeatureIdPipelineStage);
  pipelineStages.push(MetadataPipelineStage);
  pipelineStages.push(MetadataPickingPipelineStage);
 
  if (featureIdFlags.hasPropertyTable) {
    pipelineStages.push(SelectedFeatureIdPipelineStage);
    pipelineStages.push(BatchTexturePipelineStage);
    pipelineStages.push(CPUStylingPipelineStage);
  }
 
  if (hasVerticalExaggeration) {
    pipelineStages.push(VerticalExaggerationPipelineStage);
  }
 
  if (hasCustomShader) {
    pipelineStages.push(CustomShaderPipelineStage);
  }
 
  pipelineStages.push(LightingPipelineStage);
 
  if (model.allowPicking) {
    pipelineStages.push(PickingPipelineStage);
  }
 
  if (hasOutlines) {
    pipelineStages.push(PrimitiveOutlinePipelineStage);
  }
 
  Iif (hasEdgeVisibility) {
    // Indicate to Scene (after primitive updates) that the edge MRT should be enabled.
    frameState.edgeVisibilityRequested = true;
    pipelineStages.push(EdgeVisibilityPipelineStage);
    pipelineStages.push(EdgeDetectionPipelineStage);
  }
 
  pipelineStages.push(AlphaPipelineStage);
 
  pipelineStages.push(PrimitiveStatisticsPipelineStage);
 
  return;
};
 
function inspectFeatureIds(model, node, primitive) {
  let featureIds;
  // Check instances first, as this is the most specific type of
  // feature ID
  if (defined(node.instances)) {
    featureIds = ModelUtility.getFeatureIdsByLabel(
      node.instances.featureIds,
      model.instanceFeatureIdLabel,
    );
 
    if (defined(featureIds)) {
      return {
        hasFeatureIds: true,
        hasPropertyTable: defined(featureIds.propertyTableId),
      };
    }
  }
 
  featureIds = ModelUtility.getFeatureIdsByLabel(
    primitive.featureIds,
    model.featureIdLabel,
  );
  if (defined(featureIds)) {
    return {
      hasFeatureIds: true,
      hasPropertyTable: defined(featureIds.propertyTableId),
    };
  }
 
  return {
    hasFeatureIds: false,
    hasPropertyTable: false,
  };
}
 
export default ModelRuntimePrimitive;