cesium
Version:
CesiumJS is a JavaScript library for creating 3D globes and 2D maps in a web browser without a plugin.
1,593 lines (1,433 loc) • 83.2 kB
JavaScript
import BoundingSphere from "../../Core/BoundingSphere.js";
import Cartesian3 from "../../Core/Cartesian3.js";
import Cartographic from "../../Core/Cartographic.js";
import Check from "../../Core/Check.js";
import Credit from "../../Core/Credit.js";
import ColorBlendMode from "../ColorBlendMode.js";
import ClippingPlaneCollection from "../ClippingPlaneCollection.js";
import defined from "../../Core/defined.js";
import defaultValue from "../../Core/defaultValue.js";
import DeveloperError from "../../Core/DeveloperError.js";
import DistanceDisplayCondition from "../../Core/DistanceDisplayCondition.js";
import GltfLoader from "../GltfLoader.js";
import HeightReference from "../HeightReference.js";
import ImageBasedLighting from "../ImageBasedLighting.js";
import ModelExperimentalAnimationCollection from "./ModelExperimentalAnimationCollection.js";
import ModelExperimentalSceneGraph from "./ModelExperimentalSceneGraph.js";
import ModelExperimentalStatistics from "./ModelExperimentalStatistics.js";
import ModelExperimentalType from "./ModelExperimentalType.js";
import ModelExperimentalUtility from "./ModelExperimentalUtility.js";
import Pass from "../../Renderer/Pass.js";
import Resource from "../../Core/Resource.js";
import destroyObject from "../../Core/destroyObject.js";
import Matrix3 from "../../Core/Matrix3.js";
import Matrix4 from "../../Core/Matrix4.js";
import ModelFeatureTable from "./ModelFeatureTable.js";
import PointCloudShading from "../PointCloudShading.js";
import B3dmLoader from "./B3dmLoader.js";
import GeoJsonLoader from "./GeoJsonLoader.js";
import I3dmLoader from "./I3dmLoader.js";
import PntsLoader from "./PntsLoader.js";
import Color from "../../Core/Color.js";
import SceneMode from "../SceneMode.js";
import SceneTransforms from "../SceneTransforms.js";
import ShadowMode from "../ShadowMode.js";
import SplitDirection from "../SplitDirection.js";
/**
* A 3D model. This is a new architecture that is more decoupled than the older {@link Model}. This class is still experimental.
* <p>
* Do not call this function directly, instead use the `from` functions to create
* the Model from your source data type.
* </p>
*
* @alias ModelExperimental
* @constructor
*
* @param {Object} options Object with the following properties:
* @param {Resource} options.resource The Resource to the 3D model.
* @param {Boolean} [options.show=true] Whether or not to render the model.
* @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms the model from model to world coordinates.
* @param {Number} [options.scale=1.0] A uniform scale applied to this model.
* @param {Number} [options.minimumPixelSize=0.0] The approximate minimum pixel size of the model regardless of zoom.
* @param {Number} [options.maximumScale] The maximum scale size of a model. An upper limit for minimumPixelSize.
* @param {Object} [options.id] A user-defined object to return when the model is picked with {@link Scene#pick}.
* @param {Boolean} [options.allowPicking=true] When <code>true</code>, each primitive is pickable with {@link Scene#pick}.
* @param {Boolean} [options.clampAnimations=true] Determines if the model's animations should hold a pose over frames where no keyframes are specified.
* @param {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the model casts or receives shadows from light sources.
* @param {Boolean} [options.debugShowBoundingVolume=false] For debugging only. Draws the bounding sphere for each draw command in the model.
* @param {Boolean} [options.enableDebugWireframe=false] For debugging only. This must be set to true for debugWireframe to work in WebGL1. This cannot be set after the model has loaded.
* @param {Boolean} [options.debugWireframe=false] For debugging only. Draws the model in wireframe. Will only work for WebGL1 if enableDebugWireframe is set to true.
* @param {Boolean} [options.cull=true] Whether or not to cull the model using frustum/horizon culling. If the model is part of a 3D Tiles tileset, this property will always be false, since the 3D Tiles culling system is used.
* @param {Boolean} [options.opaquePass=Pass.OPAQUE] The pass to use in the {@link DrawCommand} for the opaque portions of the model.
* @param {CustomShader} [options.customShader] A custom shader. This will add user-defined GLSL code to the vertex and fragment shaders. Using custom shaders with a {@link Cesium3DTileStyle} may lead to undefined behavior.
* @param {Cesium3DTileContent} [options.content] The tile content this model belongs to. This property will be undefined if model is not loaded as part of a tileset.
* @param {HeightReference} [options.heightReference=HeightReference.NONE] Determines how the model is drawn relative to terrain.
* @param {Scene} [options.scene] Must be passed in for models that use the height reference property.
* @param {DistanceDisplayCondition} [options.distanceDisplayCondition] The condition specifying at what distance from the camera that this model will be displayed.
* @param {Color} [options.color] A color that blends with the model's rendered color.
* @param {ColorBlendMode} [options.colorBlendMode=ColorBlendMode.HIGHLIGHT] Defines how the color blends with the model.
* @param {Number} [options.colorBlendAmount=0.5] Value used to determine the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
* @param {Color} [options.silhouetteColor=Color.RED] The silhouette color. If more than 256 models have silhouettes enabled, there is a small chance that overlapping models will have minor artifacts.
* @param {Number} [options.silhouetteSize=0.0] The size of the silhouette in pixels.
* @param {ClippingPlaneCollection} [options.clippingPlanes] The {@link ClippingPlaneCollection} used to selectively disable rendering the model.
* @param {Cartesian3} [options.lightColor] The light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
* @param {ImageBasedLighting} [options.imageBasedLighting] The properties for managing image-based lighting on this model.
* @param {Boolean} [options.backFaceCulling=true] Whether to cull back-facing geometry. When true, back face culling is determined by the material's doubleSided property; when false, back face culling is disabled. Back faces are not culled if the model's color is translucent.
* @param {Credit|String} [options.credit] A credit for the data source, which is displayed on the canvas.
* @param {Boolean} [options.showCreditsOnScreen=false] Whether to display the credits of this model on screen.
* @param {SplitDirection} [options.splitDirection=SplitDirection.NONE] The {@link SplitDirection} split to apply to this model.
* @param {Boolean} [options.projectTo2D=false] Whether to accurately project the model's positions in 2D. If this is true, the model will be projected accurately to 2D, but it will use more memory to do so. If this is false, the model will use less memory and will still render in 2D / CV mode, but its positions may be inaccurate. This disables minimumPixelSize and prevents future modification to the model matrix. This also cannot be set after the model has loaded.
* @param {String|Number} [options.featureIdLabel="featureId_0"] Label of the feature ID set to use for picking and styling. For EXT_mesh_features, this is the feature ID's label property, or "featureId_N" (where N is the index in the featureIds array) when not specified. EXT_feature_metadata did not have a label field, so such feature ID sets are always labeled "featureId_N" where N is the index in the list of all feature Ids, where feature ID attributes are listed before feature ID textures. If featureIdLabel is an integer N, it is converted to the string "featureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
* @param {String|Number} [options.instanceFeatureIdLabel="instanceFeatureId_0"] Label of the instance feature ID set used for picking and styling. If instanceFeatureIdLabel is set to an integer N, it is converted to the string "instanceFeatureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
* @param {Object} [options.pointCloudShading] Options for constructing a {@link PointCloudShading} object to control point attenuation based on geometric error and lighting.
* @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.
*/
export default function ModelExperimental(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("options.loader", options.loader);
Check.typeOf.object("options.resource", options.resource);
//>>includeEnd('debug');
/**
* The loader used to load resources for this model.
* The corresponding constructor parameter is undocumented, since
* ResourceLoader is part of the private API.
*
* @type {ResourceLoader}
* @private
*/
this._loader = options.loader;
this._resource = options.resource;
/**
* Type of this model, to distinguish individual glTF files from 3D Tiles
* internally. The corresponding constructor parameter is undocumented, since
* ModelExperimentalType is part of the private API.
*
* @type {ModelExperimentalType}
* @readonly
*
* @private
*/
this.type = defaultValue(options.type, ModelExperimentalType.GLTF);
/**
* The 4x4 transformation matrix that transforms the model from model to world coordinates.
* When this is the identity matrix, the model is drawn in world coordinates, i.e., Earth's Cartesian WGS84 coordinates.
* Local reference frames can be used by providing a different transformation matrix, like that returned
* by {@link Transforms.eastNorthUpToFixedFrame}.
*
* @type {Matrix4}
* @default {@link Matrix4.IDENTITY}
*
* @example
* const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
* m.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
*/
this.modelMatrix = Matrix4.clone(
defaultValue(options.modelMatrix, Matrix4.IDENTITY)
);
this._modelMatrix = Matrix4.clone(this.modelMatrix);
this._scale = defaultValue(options.scale, 1.0);
this._minimumPixelSize = defaultValue(options.minimumPixelSize, 0.0);
this._maximumScale = options.maximumScale;
/**
* The scale value after being clamped by the maximum scale parameter.
* Used to adjust bounding spheres without repeated calculation.
*
* @type {Number}
* @private
*/
this._clampedScale = defined(this._maximumScale)
? Math.min(this._scale, this._maximumScale)
: this._scale;
this._computedScale = this._clampedScale;
/**
* Whether or not the ModelExperimentalSceneGraph should call updateModelMatrix.
* This will be true if any of the model matrix, scale, minimum pixel size, or maximum scale are dirty.
*
* @type {Number}
* @private
*/
this._updateModelMatrix = false;
/**
* If defined, this matrix is used to transform miscellaneous properties like
* clipping planes and image-based lighting instead of the modelMatrix. This is
* so that when models are part of a tileset, these properties get transformed
* relative to a common reference (such as the root).
*
* @type {Matrix4}
* @private
*/
this.referenceMatrix = undefined;
this._iblReferenceFrameMatrix = Matrix3.clone(Matrix3.IDENTITY); // Derived from reference matrix and the current view matrix
this._resourcesLoaded = false;
this._drawCommandsBuilt = false;
this._ready = false;
this._customShader = options.customShader;
this._content = options.content;
this._texturesLoaded = false;
this._defaultTexture = undefined;
this._activeAnimations = new ModelExperimentalAnimationCollection(this);
this._clampAnimations = defaultValue(options.clampAnimations, true);
this._id = options.id;
this._idDirty = false;
const color = options.color;
this._color = defaultValue(color) ? Color.clone(color) : undefined;
this._colorBlendMode = defaultValue(
options.colorBlendMode,
ColorBlendMode.HIGHLIGHT
);
this._colorBlendAmount = defaultValue(options.colorBlendAmount, 0.5);
const silhouetteColor = defaultValue(options.silhouetteColor, Color.RED);
this._silhouetteColor = Color.clone(silhouetteColor);
this._silhouetteSize = defaultValue(options.silhouetteSize, 0.0);
this._silhouetteDirty = false;
this._cull = defaultValue(options.cull, true);
this._opaquePass = defaultValue(options.opaquePass, Pass.OPAQUE);
this._allowPicking = defaultValue(options.allowPicking, true);
this._show = defaultValue(options.show, true);
this._style = undefined;
let featureIdLabel = defaultValue(options.featureIdLabel, "featureId_0");
if (typeof featureIdLabel === "number") {
featureIdLabel = `featureId_${featureIdLabel}`;
}
this._featureIdLabel = featureIdLabel;
let instanceFeatureIdLabel = defaultValue(
options.instanceFeatureIdLabel,
"instanceFeatureId_0"
);
if (typeof instanceFeatureIdLabel === "number") {
instanceFeatureIdLabel = `instanceFeatureId_${instanceFeatureIdLabel}`;
}
this._instanceFeatureIdLabel = instanceFeatureIdLabel;
this._featureTables = [];
this._featureTableId = undefined;
this._featureTableIdDirty = true;
// Keeps track of resources that need to be destroyed when the draw commands are reset.
this._pipelineResources = [];
// Keeps track of resources that need to be destroyed when the Model is destroyed.
this._modelResources = [];
// Keeps track of the pick IDs for this model. These are stored and destroyed in the
// pipeline resources array; the purpose of this array is to separate them from other
// resources and update their ID objects when necessary.
this._pickIds = [];
// The model's bounding sphere and its initial radius are computed
// in ModelExperimentalSceneGraph.
this._boundingSphere = new BoundingSphere();
this._initialRadius = undefined;
this._heightReference = defaultValue(
options.heightReference,
HeightReference.NONE
);
this._heightDirty = this._heightReference !== HeightReference.NONE;
this._removeUpdateHeightCallback = undefined;
this._clampedModelMatrix = undefined; // For use with height reference
const scene = options.scene;
if (defined(scene) && defined(scene.terrainProviderChanged)) {
this._terrainProviderChangedCallback = scene.terrainProviderChanged.addEventListener(
function () {
this._heightDirty = true;
},
this
);
}
this._scene = scene;
this._distanceDisplayCondition = options.distanceDisplayCondition;
const pointCloudShading = new PointCloudShading(options.pointCloudShading);
this._attenuation = pointCloudShading.attenuation;
this._pointCloudShading = pointCloudShading;
// If the given clipping planes don't have an owner, make this model its owner.
// Otherwise, the clipping planes are passed down from a tileset.
const clippingPlanes = options.clippingPlanes;
if (defined(clippingPlanes) && clippingPlanes.owner === undefined) {
ClippingPlaneCollection.setOwner(clippingPlanes, this, "_clippingPlanes");
} else {
this._clippingPlanes = clippingPlanes;
}
this._clippingPlanesState = 0; // If this value changes, the shaders need to be regenerated.
this._clippingPlanesMatrix = Matrix4.clone(Matrix4.IDENTITY); // Derived from reference matrix and the current view matrix
this._lightColor = Cartesian3.clone(options.lightColor);
this._imageBasedLighting = defined(options.imageBasedLighting)
? options.imageBasedLighting
: new ImageBasedLighting();
this._shouldDestroyImageBasedLighting = !defined(options.imageBasedLighting);
this._backFaceCulling = defaultValue(options.backFaceCulling, true);
this._backFaceCullingDirty = false;
this._shadows = defaultValue(options.shadows, ShadowMode.ENABLED);
this._shadowsDirty = false;
this._debugShowBoundingVolumeDirty = false;
this._debugShowBoundingVolume = defaultValue(
options.debugShowBoundingVolume,
false
);
this._enableDebugWireframe = defaultValue(
options.enableDebugWireframe,
false
);
this._debugWireframe = defaultValue(options.debugWireframe, false);
// Credit specified by the user.
let credit = options.credit;
if (typeof credit === "string") {
credit = new Credit(credit);
}
this._credit = credit;
// Credits to be added from the Resource (if it is an IonResource)
this._resourceCredits = [];
// Credits parsed from the glTF by GltfLoader.
this._gltfCredits = [];
this._showCreditsOnScreen = defaultValue(options.showCreditsOnScreen, false);
this._showCreditsOnScreenDirty = true;
this._splitDirection = defaultValue(
options.splitDirection,
SplitDirection.NONE
);
this._statistics = new ModelExperimentalStatistics();
this._sceneMode = undefined;
this._projectTo2D = defaultValue(options.projectTo2D, false);
this._completeLoad = function (model, frameState) {};
this._texturesLoadedPromise = undefined;
this._readyPromise = initialize(this);
}
function createModelFeatureTables(model, structuralMetadata) {
const featureTables = model._featureTables;
const propertyTables = structuralMetadata.propertyTables;
const length = propertyTables.length;
for (let i = 0; i < length; i++) {
const propertyTable = propertyTables[i];
const modelFeatureTable = new ModelFeatureTable({
model: model,
propertyTable: propertyTable,
});
featureTables.push(modelFeatureTable);
}
return featureTables;
}
function selectFeatureTableId(components, model) {
const featureIdLabel = model._featureIdLabel;
const instanceFeatureIdLabel = model._instanceFeatureIdLabel;
let i, j;
let featureIdAttribute;
let node;
// Scan the nodes till we find one with instances, get the feature table ID
// if the feature ID attribute of the user-selected index is present.
for (i = 0; i < components.nodes.length; i++) {
node = components.nodes[i];
if (defined(node.instances)) {
featureIdAttribute = ModelExperimentalUtility.getFeatureIdsByLabel(
node.instances.featureIds,
instanceFeatureIdLabel
);
if (
defined(featureIdAttribute) &&
defined(featureIdAttribute.propertyTableId)
) {
return featureIdAttribute.propertyTableId;
}
}
}
// Scan the primitives till we find one with textures or attributes, get the feature table ID
// if the feature ID attribute/texture of the user-selected index is present.
for (i = 0; i < components.nodes.length; i++) {
node = components.nodes[i];
for (j = 0; j < node.primitives.length; j++) {
const primitive = node.primitives[j];
const featureIds = ModelExperimentalUtility.getFeatureIdsByLabel(
primitive.featureIds,
featureIdLabel
);
if (defined(featureIds)) {
return featureIds.propertyTableId;
}
}
}
}
// Returns whether the color alpha state has changed between invisible, translucent, or opaque
function isColorAlphaDirty(currentColor, previousColor) {
if (!defined(currentColor) && !defined(previousColor)) {
return false;
}
if (defined(currentColor) !== defined(previousColor)) {
return true;
}
const currentAlpha = currentColor.alpha;
const previousAlpha = previousColor.alpha;
return (
Math.floor(currentAlpha) !== Math.floor(previousAlpha) ||
Math.ceil(currentAlpha) !== Math.ceil(previousAlpha)
);
}
function initialize(model) {
const loader = model._loader;
const resource = model._resource;
loader.load();
const loaderPromise = loader.promise.then(function (loader) {
const components = loader.components;
const structuralMetadata = components.structuralMetadata;
if (
defined(structuralMetadata) &&
structuralMetadata.propertyTableCount > 0
) {
createModelFeatureTables(model, structuralMetadata);
}
const sceneGraph = new ModelExperimentalSceneGraph({
model: model,
modelComponents: components,
});
model._sceneGraph = sceneGraph;
model._gltfCredits = sceneGraph.components.asset.credits;
const resourceCredits = model._resource.credits;
if (defined(resourceCredits)) {
const length = resourceCredits.length;
for (let i = 0; i < length; i++) {
model._resourceCredits.push(resourceCredits[i]);
}
}
model._resourcesLoaded = true;
});
// Transcoded .pnts models do not have textures
const texturesLoadedPromise = defaultValue(
loader.texturesLoadedPromise,
Promise.resolve()
);
model._texturesLoadedPromise = texturesLoadedPromise
.then(function () {
model._texturesLoaded = true;
// Re-run the pipeline so texture memory statistics are re-computed
if (loader._incrementallyLoadTextures) {
model.resetDrawCommands();
}
})
.catch(
ModelExperimentalUtility.getFailedLoadFunction(model, "model", resource)
);
const promise = new Promise(function (resolve, reject) {
model._completeLoad = function (model, frameState) {
// Set the model as ready after the first frame render since the user might set up events subscribed to
// the post render event, and the model may not be ready for those past the first frame.
frameState.afterRender.push(function () {
model._ready = true;
resolve(model);
});
};
});
return loaderPromise
.then(function () {
return promise;
})
.catch(
ModelExperimentalUtility.getFailedLoadFunction(model, "model", resource)
);
}
Object.defineProperties(ModelExperimental.prototype, {
/**
* When <code>true</code>, this model is ready to render, i.e., the external binary, image,
* and shader files were downloaded and the WebGL resources were created. This is set to
* <code>true</code> right before {@link ModelExperimental#readyPromise} is resolved.
*
* @memberof ModelExperimental.prototype
*
* @type {Boolean}
* @readonly
*
* @default false
*/
ready: {
get: function () {
return this._ready;
},
},
/**
* Gets the promise that will be resolved when this model is ready to render, i.e. when the external resources
* have been downloaded and the WebGL resources are created.
* <p>
* This promise is resolved at the end of the frame before the first frame the model is rendered in.
* </p>
*
* @memberof ModelExperimental.prototype
*
* @type {Promise.<ModelExperimental>}
* @readonly
*/
readyPromise: {
get: function () {
return this._readyPromise;
},
},
/**
* A promise that resolves when all textures are loaded.
* When <code>incrementallyLoadTextures</code> is true this may resolve after
* <code>promise</code> resolves.
*
* @memberof ModelExperimental.prototype
*
* @type {Promise<void>}
* @readonly
*
* @private
*/
texturesLoadedPromise: {
get: function () {
return this._texturesLoadedPromise;
},
},
/**
* @private
*/
loader: {
get: function () {
return this._loader;
},
},
/**
* Get the estimated memory usage statistics for this model.
*
* @memberof ModelExperimental.prototype
*
* @type {ModelExperimentalStatistics}
* @readonly
*
* @private
*/
statistics: {
get: function () {
return this._statistics;
},
},
/**
* The currently playing glTF animations.
*
* @memberof ModelExperimental.prototype
*
* @type {ModelExperimentalAnimationCollection}
* @readonly
*/
activeAnimations: {
get: function () {
return this._activeAnimations;
},
},
/**
* Determines if the model's animations should hold a pose over frames where no keyframes are specified.
*
* @memberof ModelExperimental.prototype
* @type {Boolean}
*
* @default true
*/
clampAnimations: {
get: function () {
return this._clampAnimations;
},
set: function (value) {
this._clampAnimations = value;
},
},
/**
* Whether or not to cull the model using frustum/horizon culling. If the model is part of a 3D Tiles tileset, this property
* will always be false, since the 3D Tiles culling system is used.
*
* @type {Boolean}
* @readonly
*
* @private
*/
cull: {
get: function () {
return this._cull;
},
},
/**
* The pass to use in the {@link DrawCommand} for the opaque portions of the model.
*
* @memberof ModelExperimental.prototype
*
* @type {Pass}
* @readonly
*
* @private
*/
opaquePass: {
get: function () {
return this._opaquePass;
},
},
/**
* Point cloud shading settings for controlling point cloud attenuation
* and lighting. For 3D Tiles, this is inherited from the
* {@link Cesium3DTileset}.
*
* @memberof ModelExperimental.prototype
*
* @type {PointCloudShading}
*/
pointCloudShading: {
get: function () {
return this._pointCloudShading;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug);
Check.defined("pointCloudShading", value);
//>>includeEnd('debug');
if (value !== this._pointCloudShading) {
this.resetDrawCommands();
}
this._pointCloudShading = value;
},
},
/**
* The model's custom shader, if it exists. Using custom shaders with a {@link Cesium3DTileStyle}
* may lead to undefined behavior.
*
* @memberof ModelExperimental.prototype
*
* @type {CustomShader}
*/
customShader: {
get: function () {
return this._customShader;
},
set: function (value) {
if (value !== this._customShader) {
this.resetDrawCommands();
}
this._customShader = value;
},
},
/**
* The scene graph of this model.
*
* @memberof ModelExperimental.prototype
*
* @type {ModelExperimentalSceneGraph}
* @private
*/
sceneGraph: {
get: function () {
return this._sceneGraph;
},
},
/**
* The tile content this model belongs to, if it is loaded as part of a {@link Cesium3DTileset}.
*
* @memberof ModelExperimental.prototype
*
* @type {Cesium3DTileContent}
* @readonly
*
* @private
*/
content: {
get: function () {
return this._content;
},
},
/**
* The height reference of the model, which determines how the model is drawn
* relative to terrain.
*
* @memberof ModelExperimental.prototype
*
* @type {HeightReference}
* @default {HeightReference.NONE}
*
*/
heightReference: {
get: function () {
return this._heightReference;
},
set: function (value) {
if (value !== this._heightReference) {
this._heightDirty = true;
}
this._heightReference = value;
},
},
/**
* Gets or sets the distance display condition, which specifies at what distance
* from the camera this model will be displayed.
*
* @memberof ModelExperimental.prototype
*
* @type {DistanceDisplayCondition}
*
* @default undefined
*
*/
distanceDisplayCondition: {
get: function () {
return this._distanceDisplayCondition;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug);
if (defined(value) && value.far <= value.near) {
throw new DeveloperError("far must be greater than near");
}
//>>includeEnd('debug');
this._distanceDisplayCondition = DistanceDisplayCondition.clone(
value,
this._distanceDisplayCondition
);
},
},
/**
* The structural metadata from the EXT_structural_metadata extension
*
* @memberof ModelExperimental.prototype
*
* @type {StructuralMetadata}
* @readonly
*
* @private
*/
structuralMetadata: {
get: function () {
return this._sceneGraph.components.structuralMetadata;
},
},
/**
* The ID for the feature table to use for picking and styling in this model.
*
* @memberof ModelExperimental.prototype
*
* @type {Number}
*
* @private
*/
featureTableId: {
get: function () {
return this._featureTableId;
},
set: function (value) {
this._featureTableId = value;
},
},
/**
* The feature tables for this model.
*
* @memberof ModelExperimental.prototype
*
* @type {Array}
* @readonly
*
* @private
*/
featureTables: {
get: function () {
return this._featureTables;
},
set: function (value) {
this._featureTables = value;
},
},
/**
* A user-defined object that is returned when the model is picked.
*
* @memberof ModelExperimental.prototype
*
* @type {Object}
*
* @default undefined
*
* @see Scene#pick
*/
id: {
get: function () {
return this._id;
},
set: function (value) {
if (value !== this._id) {
this._idDirty = true;
}
this._id = value;
},
},
/**
* When <code>true</code>, each primitive is pickable with {@link Scene#pick}. When <code>false</code>, GPU memory is saved.
*
* @memberof ModelExperimental.prototype
*
* @type {Boolean}
* @readonly
*
* @private
*/
allowPicking: {
get: function () {
return this._allowPicking;
},
},
/**
* The style to apply the to the features in the model. Cannot be applied if a {@link CustomShader} is also applied.
*
* @memberof ModelExperimental.prototype
*
* @type {Cesium3DTileStyle}
*/
style: {
get: function () {
return this._style;
},
set: function (value) {
if (value !== this._style) {
this.applyStyle(value);
}
this._style = value;
},
},
/**
* The color to blend with the model's rendered color.
*
* @memberof ModelExperimental.prototype
*
* @type {Color}
*/
color: {
get: function () {
return this._color;
},
set: function (value) {
if (!Color.equals(this._color, value)) {
this.resetDrawCommands();
}
this._color = Color.clone(value, this._color);
},
},
/**
* Defines how the color blends with the model.
*
* @memberof ModelExperimental.prototype
*
* @type {Cesium3DTileColorBlendMode|ColorBlendMode}
*
* @default ColorBlendMode.HIGHLIGHT
*/
colorBlendMode: {
get: function () {
return this._colorBlendMode;
},
set: function (value) {
this._colorBlendMode = value;
},
},
/**
* Value used to determine the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
*
* @memberof ModelExperimental.prototype
*
* @type {Number}
*
* @default 0.5
*/
colorBlendAmount: {
get: function () {
return this._colorBlendAmount;
},
set: function (value) {
this._colorBlendAmount = value;
},
},
/**
* The silhouette color.
*
* @memberof ModelExperimental.prototype
*
* @type {Color}
*
* @default Color.RED
*/
silhouetteColor: {
get: function () {
return this._silhouetteColor;
},
set: function (value) {
if (!Color.equals(value, this._silhouetteColor)) {
const alphaDirty = isColorAlphaDirty(value, this._silhouetteColor);
this._silhouetteDirty = this._silhouetteDirty || alphaDirty;
}
this._silhouetteColor = Color.clone(value, this._silhouetteColor);
},
},
/**
* The size of the silhouette in pixels.
*
* @memberof ModelExperimental.prototype
*
* @type {Number}
*
* @default 0.0
*/
silhouetteSize: {
get: function () {
return this._silhouetteSize;
},
set: function (value) {
if (value !== this._silhouetteSize) {
const currentSize = this._silhouetteSize;
const sizeDirty =
(value > 0.0 && currentSize === 0.0) ||
(value === 0.0 && currentSize > 0.0);
this._silhouetteDirty = this._silhouetteDirty || sizeDirty;
// Back-face culling needs to be updated in case the silhouette size
// is greater than zero.
this._backFaceCullingDirty = this._backFaceCullingDirty || sizeDirty;
}
this._silhouetteSize = value;
},
},
/**
* Gets the model's bounding sphere in world space. This does not take into account
* glTF animations, skins, or morph targets. It also does not account for
* {@link ModelExperimental#minimumPixelSize}.
*
* @memberof ModelExperimental.prototype
*
* @type {BoundingSphere}
* @readonly
*/
boundingSphere: {
get: function () {
//>>includeStart('debug', pragmas.debug);
if (!this._ready) {
throw new DeveloperError(
"The model is not loaded. Use ModelExperimental.readyPromise or wait for ModelExperimental.ready to be true."
);
}
//>>includeEnd('debug');
return this._boundingSphere;
},
},
/**
* This property is for debugging only; it is not for production use nor is it optimized.
* <p>
* Draws the bounding sphere for each draw command in the model.
* </p>
*
* @memberof ModelExperimental.prototype
*
* @type {Boolean}
*
* @default false
*/
debugShowBoundingVolume: {
get: function () {
return this._debugShowBoundingVolume;
},
set: function (value) {
if (this._debugShowBoundingVolume !== value) {
this._debugShowBoundingVolumeDirty = true;
}
this._debugShowBoundingVolume = value;
},
},
/**
* This property is for debugging only; it is not for production use nor is it optimized.
* <p>
* Draws the model in wireframe.
* </p>
*
* @memberof ModelExperimental.prototype
*
* @type {Boolean}
*
* @default false
*/
debugWireframe: {
get: function () {
return this._debugWireframe;
},
set: function (value) {
if (this._debugWireframe !== value) {
this.resetDrawCommands();
}
this._debugWireframe = value;
},
},
/**
* Whether or not to render the model.
*
* @memberof ModelExperimental.prototype
*
* @type {Boolean}
*
* @default true
*/
show: {
get: function () {
return this._show;
},
set: function (value) {
this._show = value;
},
},
/**
* Label of the feature ID set to use for picking and styling.
* <p>
* For EXT_mesh_features, this is the feature ID's label property, or
* "featureId_N" (where N is the index in the featureIds array) when not
* specified. EXT_feature_metadata did not have a label field, so such
* feature ID sets are always labeled "featureId_N" where N is the index in
* the list of all feature Ids, where feature ID attributes are listed before
* feature ID textures.
* </p>
* <p>
* If featureIdLabel is set to an integer N, it is converted to
* the string "featureId_N" automatically. If both per-primitive and
* per-instance feature IDs are present, the instance feature IDs take
* priority.
* </p>
*
* @memberof ModelExperimental.prototype
*
* @type {String}
* @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.
*/
featureIdLabel: {
get: function () {
return this._featureIdLabel;
},
set: function (value) {
// indices get converted into featureId_N
if (typeof value === "number") {
value = `featureId_${value}`;
}
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("value", value);
//>>includeEnd('debug');
if (value !== this._featureIdLabel) {
this._featureTableIdDirty = true;
}
this._featureIdLabel = value;
},
},
/**
* Label of the instance feature ID set used for picking and styling.
* <p>
* If instanceFeatureIdLabel is set to an integer N, it is converted to
* the string "instanceFeatureId_N" automatically.
* If both per-primitive and per-instance feature IDs are present, the
* instance feature IDs take priority.
* </p>
*
* @memberof ModelExperimental.prototype
*
* @type {String}
* @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.
*/
instanceFeatureIdLabel: {
get: function () {
return this._instanceFeatureIdLabel;
},
set: function (value) {
// indices get converted into instanceFeatureId_N
if (typeof value === "number") {
value = `instanceFeatureId_${value}`;
}
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("value", value);
//>>includeEnd('debug');
if (value !== this._instanceFeatureIdLabel) {
this._featureTableIdDirty = true;
}
this._instanceFeatureIdLabel = value;
},
},
/**
* The {@link ClippingPlaneCollection} used to selectively disable rendering the model.
*
* @memberof ModelExperimental.prototype
*
* @type {ClippingPlaneCollection}
*/
clippingPlanes: {
get: function () {
return this._clippingPlanes;
},
set: function (value) {
if (value !== this._clippingPlanes) {
// Handle destroying old clipping planes, new clipping planes ownership
ClippingPlaneCollection.setOwner(value, this, "_clippingPlanes");
this.resetDrawCommands();
}
},
},
/**
* The light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
* <p>
* Disabling additional light sources by setting
* <code>model.imageBasedLighting.imageBasedLightingFactor = new Cartesian2(0.0, 0.0)</code>
* will make the model much darker. Here, increasing the intensity of the light source will make the model brighter.
* </p>
* @memberof ModelExperimental.prototype
*
* @type {Cartesian3}
*
* @default undefined
*/
lightColor: {
get: function () {
return this._lightColor;
},
set: function (value) {
if (defined(value) !== defined(this._lightColor)) {
this.resetDrawCommands();
}
this._lightColor = Cartesian3.clone(value, this._lightColor);
},
},
/**
* The properties for managing image-based lighting on this model.
*
* @memberof ModelExperimental.prototype
*
* @type {ImageBasedLighting}
*/
imageBasedLighting: {
get: function () {
return this._imageBasedLighting;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("imageBasedLighting", this._imageBasedLighting);
//>>includeEnd('debug');
if (value !== this._imageBasedLighting) {
if (
this._shouldDestroyImageBasedLighting &&
!this._imageBasedLighting.isDestroyed()
) {
this._imageBasedLighting.destroy();
}
this._imageBasedLighting = value;
this._shouldDestroyImageBasedLighting = false;
this.resetDrawCommands();
}
},
},
/**
* Whether to cull back-facing geometry. When true, back face culling is
* determined by the material's doubleSided property; when false, back face
* culling is disabled. Back faces are not culled if {@link ModelExperimental#color}
* is translucent or {@link ModelExperimental#silhouetteSize} is greater than 0.0.
*
* @memberof ModelExperimental.prototype
*
* @type {Boolean}
*
* @default true
*/
backFaceCulling: {
get: function () {
return this._backFaceCulling;
},
set: function (value) {
if (value !== this._backFaceCulling) {
this._backFaceCullingDirty = true;
}
this._backFaceCulling = value;
},
},
/**
* A uniform scale applied to this model before the {@link Model#modelMatrix}.
* Values greater than <code>1.0</code> increase the size of the model; values
* less than <code>1.0</code> decrease.
*
* @memberof ModelExperimental.prototype
*
* @type {Number}
*
* @default 1.0
*/
scale: {
get: function () {
return this._scale;
},
set: function (value) {
if (value !== this._scale) {
this._updateModelMatrix = true;
}
this._scale = value;
},
},
/**
* The true scale of the model after being affected by the model's scale,
* minimum pixel size, and maximum scale parameters.
*
* @memberof ModelExperimental.prototype
*
* @type {Number}
* @readonly
*
* @private
*/
computedScale: {
get: function () {
return this._computedScale;
},
},
/**
* The approximate minimum pixel size of the model regardless of zoom.
* This can be used to ensure that a model is visible even when the viewer
* zooms out. When <code>0.0</code>, no minimum size is enforced.
*
* @memberof ModelExperimental.prototype
*
* @type {Number}
*
* @default 0.0
*/
minimumPixelSize: {
get: function () {
return this._minimumPixelSize;
},
set: function (value) {
if (value !== this._minimumPixelSize) {
this._updateModelMatrix = true;
}
this._minimumPixelSize = value;
},
},
/**
* The maximum scale size for a model. This can be used to give
* an upper limit to the {@link Model#minimumPixelSize}, ensuring that the model
* is never an unreasonable scale.
*
* @memberof ModelExperimental.prototype
*
* @type {Number}
*/
maximumScale: {
get: function () {
return this._maximumScale;
},
set: function (value) {
if (value !== this._maximumScale) {
this._updateModelMatrix = true;
}
this._maximumScale = value;
},
},
/**
* Determines whether the model casts or receives shadows from light sources.
* @memberof ModelExperimental.prototype
*
* @type {ShadowMode}
*
* @default ShadowMode.ENABLED
*/
shadows: {
get: function () {
return this._shadows;
},
set: function (value) {
if (value !== this._shadows) {
this._shadowsDirty = true;
}
this._shadows = value;
},
},
/**
* Gets the credit that will be displayed for the model
*
* @memberof ModelExperimental.prototype
*
* @type {Credit}
* @readonly
*/
credit: {
get: function () {
return this._credit;
},
},
/**
* Gets or sets whether the credits of the model will be displayed on the screen
*
* @memberof ModelExperimental.prototype
*
* @type {Boolean}
*
* @default false
*/
showCreditsOnScreen: {
get: function () {
return this._showCreditsOnScreen;
},
set: function (value) {
if (this._showCreditsOnScreen !== value) {
this._showCreditsOnScreenDirty = true;
}
this._showCreditsOnScreen = value;
},
},
/**
* The {@link SplitDirection} to apply to this model.
*
* @memberof ModelExperimental.prototype
*
* @type {SplitDirection}
*
* @default {@link SplitDirection.NONE}
*/
splitDirection: {
get: function () {
return this._splitDirection;
},
set: function (value) {
if (this._splitDirection !== value) {
this.resetDrawCommands();
}
this._splitDirection = value;
},
},
});
/**
* Sets the current value of an articulation stage. After setting one or
* multiple stage values, call ModelExperimental.applyArticulations() to
* cause the node matrices to be recalculated.
*
* @param {String} articulationStageKey The name of the articulation, a space, and the name of the stage.
* @param {Number} value The numeric value of this stage of the articulation.
*
* @exception {DeveloperError} The model is not loaded. Use ModelExperimental.readyPromise or wait for ModelExperimental.ready to be true.
*
* @see ModelExperimental#applyArticulations
*
* @example
* // Sets the value of the stage named "MoveX" belonging to the articulation named "SampleArticulation"
* model.setArticulationStage("SampleArticulation MoveX", 50.0);
*/
ModelExperimental.prototype.setArticulationStage = function (
articulationStageKey,
value
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("value", value);
if (!this._ready) {
throw new DeveloperError(
"The model is not loaded. Use ModelExperimental.readyPromise or wait for ModelExperimental.ready to be true."
);
}
//>>includeEnd('debug');
this._sceneGraph.setArticulationStage(articulationStageKey, value);
};
/**
* Applies any modified articulation stages to the matrix of each node that
* participates in any articulation. Note that this will overwrite any node
* transformations on participating nodes.
*
* @exception {DeveloperError} The model is not loaded. Use ModelExperimental.readyPromise or wait for ModelExperimental.ready to be true.
*/
ModelExperimental.prototype.applyArticulations = function () {
//>>includeStart('debug', pragmas.debug);
if (!this._ready) {
throw new DeveloperError(
"The model is not loaded. Use ModelExperimental.readyPromise or wait for ModelExperimental.ready to be true."
);
}
//>>includeEnd('debug');
this._sceneGraph.applyArticulations();
};
/**
* Resets the draw commands for this model.
*
* @private
*/
ModelExperimental.prototype.resetDrawCommands = function () {
this._drawCommandsBuilt = false;
};
const scratchIBLReferenceFrameMatrix4 = new Matrix4();
const scratchIBLReferenceFrameMatrix3 = new Matrix3();
const scratchClippingPlanesMatrix = new Matrix4();
/**
* Called when {@link Viewer} or {@link CesiumWidget} render the scene to
* get the draw commands needed to render this primitive.
* <p>
* Do not call this function directly. This is documented just to
* list the exceptions that may be propagated when the scene is rendered:
* </p>
*
* @exception {RuntimeError} Failed to load external reference.
*/
ModelExperimental.prototype.update = function (frameState) {
// Keep processing the model every frame until the main resources
// (buffer views) and textures (which may be loaded asynchronously)
// are processed.
processLoader(this, frameState);
// A custom shader may have to load texture uniforms.
updateCustomShader(this, frameState);
// The image-based lighting may have to load texture uniforms
// for specular maps.
updateImageBasedLighting(this, frameState);
// Short-circuit if the model resources aren't ready or the scene
// is currently morphing.
if (!this._resourcesLoaded || frameState.mode === SceneMode.MORPHING) {
return;
}
updatePointCloudAttenuation(this);
updateSilhouette(this, frameState);
updateClippingPlanes(this, frameState);
updateSceneMode(this, frameState);
updateFeatureTables(this, frameState);
this._defaultTexture = frameState.context.defaultTexture;
buildDrawCommands(this, frameState);
updateModelMatrix(this, frameState);
// Many features (e.g. image-based lighting, clipping planes) depend on the model
// matrix being updated for the current height reference, so update it first.
updateClamping(this);
updateBoundingSphereAndScale(this, frameState);
updateReferenceMatrices(this, frameState);
// This check occurs after the bounding sphere has been updated so that
// zooming to the bounding sphere can account for any modifications
// from the clamp-to-ground setting.
const model = this;
if (!model._ready) {
model._completeLoad(model, frameState);
// Don't render until the next frame after the ready promise is resolved
return;
}
updatePickIds(this);
// Update the scene graph and draw commands for any changes in model's properties
// (e.g. model matrix, back-face culling)
updateSceneGraph(this, frameState);
updateShowCreditsOnScreen(this);
submitDrawCommands(this, frameState);
};
function processLoader(model, frameState) {
if (!model._resourcesLoaded || !model._texturesLoaded) {
model._loader.process(frameState);
}
}
function updateCustomShader(model, frameState) {
if (defined(model._customShader)) {
model._customShader.update(frameState);
}
}
function updateImageBasedLighting(model, frameState) {
model._imageBasedLighting.update(frameState);
if (model._imageBasedLighting.shouldRegenerateShaders) {
model.resetDrawCommands();
}
}
function updatePointCloudAttenuation(model) {
// Check if the shader needs to be updated for point cloud attenuation
// settings.
if (model.pointCloudShading.attenuation !== model._attenuation) {
model.resetDrawCommands();
model._attenuation = model.pointCloudShading.attenuation;
}
}
function updateSilhouette(model, frameState) {
if (model._silhouetteDirty) {
// Only rebuild draw commands if silhouettes are supported in the first place.
if (supportsSilhouettes(frameState)) {
model.resetDrawCommands();
}
model._silhouetteDirty = false;
}
}
function updateClippingPlanes(model, frameState) {
// Update the clipping planes collection for this model to detect any changes.
if (model.isClippingEnabled()) {
if (model._clippingPlanes.owner === model) {
model._clippingPlanes.update(frameState);
}
const currentClippingPlanesState =
model._clippingPlanes.clippingPlanesState;
if (currentClippingPlanesState !==