cesium/packages/engine/Source/Scene/Model/ModelSceneGraph.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1099 lines
32 KiB
JavaScript
Raw Permalink Normal View History

2021-08-20 07:40:34 +08:00
import BoundingSphere from "../../Core/BoundingSphere.js";
2022-05-03 01:48:25 +08:00
import Cartesian3 from "../../Core/Cartesian3.js";
2021-08-03 03:55:11 +08:00
import Check from "../../Core/Check.js";
import Frozen from "../../Core/Frozen.js";
import defined from "../../Core/defined.js";
import Matrix4 from "../../Core/Matrix4.js";
2022-08-17 01:52:46 +08:00
import Transforms from "../../Core/Transforms.js";
import SceneMode from "../SceneMode.js";
import SplitDirection from "../SplitDirection.js";
2022-08-17 03:35:06 +08:00
import TilesetPipelineStage from "./TilesetPipelineStage.js";
import AtmospherePipelineStage from "./AtmospherePipelineStage.js";
2022-08-17 01:52:46 +08:00
import ImageBasedLightingPipelineStage from "./ImageBasedLightingPipelineStage.js";
import ModelArticulation from "./ModelArticulation.js";
2021-10-29 03:27:46 +08:00
import ModelColorPipelineStage from "./ModelColorPipelineStage.js";
import ModelClippingPlanesPipelineStage from "./ModelClippingPlanesPipelineStage.js";
2024-01-09 23:39:09 +08:00
import ModelClippingPolygonsPipelineStage from "./ModelClippingPolygonsPipelineStage.js";
import ModelNode from "./ModelNode.js";
import ModelRuntimeNode from "./ModelRuntimeNode.js";
import ModelRuntimePrimitive from "./ModelRuntimePrimitive.js";
import ModelSkin from "./ModelSkin.js";
import ModelUtility from "./ModelUtility.js";
2021-08-20 07:40:34 +08:00
import ModelRenderResources from "./ModelRenderResources.js";
import ModelSilhouettePipelineStage from "./ModelSilhouettePipelineStage.js";
import ModelSplitterPipelineStage from "./ModelSplitterPipelineStage.js";
2022-08-17 01:52:46 +08:00
import ModelType from "./ModelType.js";
2021-08-20 07:40:34 +08:00
import NodeRenderResources from "./NodeRenderResources.js";
import PrimitiveRenderResources from "./PrimitiveRenderResources.js";
2024-07-16 04:05:00 +08:00
import ModelDrawCommands from "./ModelDrawCommands.js";
import addAllToArray from "../../Core/addAllToArray.js";
2021-08-03 03:55:11 +08:00
/**
2022-08-05 00:57:20 +08:00
* An in memory representation of the scene graph for a {@link Model}
2021-08-03 03:55:11 +08:00
*
* @param {object} options An object containing the following options
2022-08-05 00:57:20 +08:00
* @param {Model} options.model The model this scene graph belongs to
2021-08-03 03:55:11 +08:00
* @param {ModelComponents} options.modelComponents The model components describing the model
2021-08-19 05:15:22 +08:00
*
* @alias ModelSceneGraph
2021-08-19 05:15:22 +08:00
* @constructor
2021-08-03 03:55:11 +08:00
*
* @private
*/
2022-08-11 06:22:27 +08:00
function ModelSceneGraph(options) {
options = options ?? Frozen.EMPTY_OBJECT;
const components = options.modelComponents;
2021-08-03 03:55:11 +08:00
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("options.model", options.model);
Check.typeOf.object("options.modelComponents", components);
2021-08-03 03:55:11 +08:00
//>>includeEnd('debug');
/**
2022-08-05 00:57:20 +08:00
* A reference to the {@link Model} that owns this scene graph.
2021-08-03 03:55:11 +08:00
*
2022-08-05 00:57:20 +08:00
* @type {Model}
2021-08-03 03:55:11 +08:00
* @readonly
*
* @private
*/
this._model = options.model;
/**
2021-08-03 03:55:11 +08:00
* The model components that represent the contents of the 3D model file.
*
* @type {ModelComponents}
2021-08-03 03:55:11 +08:00
* @readonly
*
* @private
*/
this._components = components;
/**
* Pipeline stages to apply across the model.
2021-08-03 03:55:11 +08:00
*
* @type {object[]}
2021-08-03 03:55:11 +08:00
* @readonly
*
* @private
*/
this._pipelineStages = [];
/**
2022-02-02 02:19:03 +08:00
* Update stages to apply across the model.
*
* @type {object[]}
2022-02-02 02:19:03 +08:00
* @readonly
*
* @private
*/
this._updateStages = [];
/**
* The runtime nodes that make up the scene graph
2021-08-03 03:55:11 +08:00
*
* @type {ModelRuntimeNode[]}
2021-08-03 03:55:11 +08:00
* @readonly
*
* @private
*/
2021-08-20 07:40:34 +08:00
this._runtimeNodes = [];
/**
* The indices of the root nodes in the runtime nodes array.
*
* @type {number[]}
* @readonly
*
* @private
*/
this._rootNodes = [];
/**
2022-04-09 05:21:40 +08:00
* The indices of the skinned nodes in the runtime nodes array. These refer
2022-04-12 02:22:32 +08:00
* to the nodes that will be manipulated by their skin, as opposed to the nodes
2022-04-09 05:21:40 +08:00
* acting as joints for the skin.
*
* @type {number[]}
* @readonly
*
* @private
*/
this._skinnedNodes = [];
/**
2022-04-09 05:21:40 +08:00
* The runtime skins that affect nodes in the scene graph.
*
* @type {ModelSkin[]}
* @readonly
*
* @private
*/
this._runtimeSkins = [];
2022-01-11 05:04:27 +08:00
/**
* Pipeline stages to apply to this model. This
* is an array of classes, each with a static method called
* <code>process()</code>
*
* @type {object[]}
2022-01-11 05:04:27 +08:00
* @readonly
*
* @private
*/
this.modelPipelineStages = [];
// The scene graph's bounding sphere is model space, so that
// the model's bounding sphere can be recomputed when given a
// new model matrix.
this._boundingSphere = undefined;
// The 2D bounding sphere is in world space. This is checked
// by the draw commands to see if the model is over the IDL,
// and if so, renders the primitives using extra commands.
this._boundingSphere2D = undefined;
this._computedModelMatrix = Matrix4.clone(Matrix4.IDENTITY);
this._computedModelMatrix2D = Matrix4.clone(Matrix4.IDENTITY);
this._axisCorrectionMatrix = ModelUtility.getAxisCorrectionMatrix(
components.upAxis,
components.forwardAxis,
new Matrix4(),
);
// Store articulations from the AGI_articulations extension
// by name in a dictionary for easy retrieval.
this._runtimeArticulations = {};
2022-01-08 02:36:07 +08:00
initialize(this);
}
Object.defineProperties(ModelSceneGraph.prototype, {
2022-01-14 02:51:20 +08:00
/**
* The model components this scene graph represents.
*
* @type {ModelComponents}
* @readonly
*
* @private
*/
components: {
get: function () {
return this._components;
2022-01-14 02:51:20 +08:00
},
},
/**
2022-01-08 02:36:07 +08:00
* The axis-corrected model matrix.
*
2022-01-08 02:36:07 +08:00
* @type {Matrix4}
* @readonly
*
* @private
*/
2022-01-08 02:36:07 +08:00
computedModelMatrix: {
get: function () {
return this._computedModelMatrix;
},
},
/**
* A matrix to correct from y-up in some model formats (e.g. glTF) to the
* z-up coordinate system Cesium uses.
*
* @type {Matrix4}
* @readonly
*
* @private
*/
axisCorrectionMatrix: {
get: function () {
return this._axisCorrectionMatrix;
},
},
/**
* The bounding sphere containing all the primitives in the scene graph
* in model space.
*
2022-01-08 02:36:07 +08:00
* @type {BoundingSphere}
* @readonly
*
* @private
*/
2022-01-08 02:36:07 +08:00
boundingSphere: {
get: function () {
return this._boundingSphere;
},
},
});
function initialize(sceneGraph) {
const components = sceneGraph._components;
2022-01-07 21:46:30 +08:00
const scene = components.scene;
2022-07-12 05:44:31 +08:00
const model = sceneGraph._model;
// If the model has a height reference that modifies the model matrix,
// it will be accounted for in updateModelMatrix.
2022-07-12 05:44:31 +08:00
const modelMatrix = model.modelMatrix;
computeModelMatrix(sceneGraph, modelMatrix);
const articulations = components.articulations;
const articulationsLength = articulations.length;
const runtimeArticulations = sceneGraph._runtimeArticulations;
2022-06-28 01:56:12 +08:00
for (let i = 0; i < articulationsLength; i++) {
const articulation = articulations[i];
const runtimeArticulation = new ModelArticulation({
articulation: articulation,
sceneGraph: sceneGraph,
});
const name = runtimeArticulation.name;
runtimeArticulations[name] = runtimeArticulation;
}
const nodes = components.nodes;
const nodesLength = nodes.length;
2022-04-09 05:21:40 +08:00
// Initialize this array to be the same size as the nodes array in
2022-09-23 21:09:29 +08:00
// the model file. This is so the node indices remain the same. However,
// only nodes reachable from the scene's root node will be populated, the
// rest will be undefined
sceneGraph._runtimeNodes = new Array(nodesLength);
const rootNodes = scene.nodes;
2022-04-12 02:09:01 +08:00
const rootNodesLength = rootNodes.length;
const transformToRoot = Matrix4.IDENTITY;
2022-06-28 01:56:12 +08:00
for (let i = 0; i < rootNodesLength; i++) {
const rootNode = scene.nodes[i];
2022-07-13 02:09:40 +08:00
const rootNodeIndex = traverseAndCreateSceneGraph(
sceneGraph,
rootNode,
2022-07-13 04:04:17 +08:00
transformToRoot,
);
sceneGraph._rootNodes.push(rootNodeIndex);
}
// Handle skins after all runtime nodes are created
2022-04-08 05:13:38 +08:00
const skins = components.skins;
const runtimeSkins = sceneGraph._runtimeSkins;
2022-04-12 02:22:32 +08:00
const skinsLength = skins.length;
2022-06-28 01:56:12 +08:00
for (let i = 0; i < skinsLength; i++) {
const skin = skins[i];
2022-04-08 05:13:38 +08:00
runtimeSkins.push(
new ModelSkin({
2022-04-08 05:13:38 +08:00
skin: skin,
sceneGraph: sceneGraph,
}),
);
}
const skinnedNodes = sceneGraph._skinnedNodes;
2022-04-12 02:22:32 +08:00
const skinnedNodesLength = skinnedNodes.length;
2022-06-28 01:56:12 +08:00
for (let i = 0; i < skinnedNodesLength; i++) {
const skinnedNodeIndex = skinnedNodes[i];
const skinnedNode = sceneGraph._runtimeNodes[skinnedNodeIndex];
// Use the index of the skin in the model components to find
// the corresponding runtime skin.
const skin = nodes[skinnedNodeIndex].skin;
const skinIndex = skin.index;
skinnedNode._runtimeSkin = runtimeSkins[skinIndex];
2022-04-08 05:13:38 +08:00
skinnedNode.updateJointMatrices();
}
2022-06-25 03:11:29 +08:00
// Ensure articulations are applied with their initial values to their target nodes.
sceneGraph.applyArticulations();
}
function computeModelMatrix(sceneGraph, modelMatrix) {
const components = sceneGraph._components;
2022-01-08 02:36:07 +08:00
const model = sceneGraph._model;
sceneGraph._computedModelMatrix = Matrix4.multiplyTransformation(
modelMatrix,
2022-01-08 02:36:07 +08:00
components.transform,
2022-03-17 03:36:16 +08:00
sceneGraph._computedModelMatrix,
2022-01-08 02:36:07 +08:00
);
sceneGraph._computedModelMatrix = Matrix4.multiplyTransformation(
sceneGraph._computedModelMatrix,
sceneGraph._axisCorrectionMatrix,
sceneGraph._computedModelMatrix,
);
2022-03-18 21:57:36 +08:00
sceneGraph._computedModelMatrix = Matrix4.multiplyByUniformScale(
sceneGraph._computedModelMatrix,
model.computedScale,
sceneGraph._computedModelMatrix,
);
}
2022-06-03 04:01:59 +08:00
const scratchComputedTranslation = new Cartesian3();
function computeModelMatrix2D(sceneGraph, frameState) {
const computedModelMatrix = sceneGraph._computedModelMatrix;
2022-06-03 04:01:59 +08:00
const translation = Matrix4.getTranslation(
computedModelMatrix,
scratchComputedTranslation,
);
2022-06-03 04:01:59 +08:00
if (!Cartesian3.equals(translation, Cartesian3.ZERO)) {
sceneGraph._computedModelMatrix2D = Transforms.basisTo2D(
frameState.mapProjection,
computedModelMatrix,
sceneGraph._computedModelMatrix2D,
);
} else {
const center = sceneGraph.boundingSphere.center;
2024-05-20 22:19:14 +08:00
const to2D = Transforms.ellipsoidTo2DModelMatrix(
frameState.mapProjection,
center,
sceneGraph._computedModelMatrix2D,
);
2022-05-28 05:12:25 +08:00
sceneGraph._computedModelMatrix2D = Matrix4.multiply(
to2D,
computedModelMatrix,
sceneGraph._computedModelMatrix2D,
);
}
sceneGraph._boundingSphere2D = BoundingSphere.transform(
sceneGraph._boundingSphere,
sceneGraph._computedModelMatrix2D,
sceneGraph._boundingSphere2D,
);
}
/**
2022-07-13 02:09:40 +08:00
* Recursively traverse through the nodes in the scene graph to create
2022-07-14 04:11:25 +08:00
* their runtime versions, using a post-order depth-first traversal.
*
* @param {ModelSceneGraph} sceneGraph The scene graph
2021-08-03 03:55:11 +08:00
* @param {ModelComponents.Node} node The current node
2022-03-18 03:37:36 +08:00
* @param {Matrix4} transformToRoot The transforms of this node's ancestors.
* @returns {number} The index of this node in the runtimeNodes array.
*
2021-08-03 21:23:48 +08:00
* @private
*/
2022-07-13 04:04:17 +08:00
function traverseAndCreateSceneGraph(sceneGraph, node, transformToRoot) {
// The indices of the children of this node in the runtimeNodes array.
const childrenIndices = [];
const transform = ModelUtility.getNodeTransform(node);
// Traverse through scene graph.
2022-04-23 04:58:43 +08:00
const childrenLength = node.children.length;
for (let i = 0; i < childrenLength; i++) {
const childNode = node.children[i];
const childNodeTransformToRoot = Matrix4.multiplyTransformation(
transformToRoot,
transform,
new Matrix4(),
);
2022-07-13 02:09:40 +08:00
const childIndex = traverseAndCreateSceneGraph(
2022-04-23 04:58:43 +08:00
sceneGraph,
childNode,
2022-07-13 04:04:17 +08:00
childNodeTransformToRoot,
2022-04-23 04:58:43 +08:00
);
childrenIndices.push(childIndex);
}
2021-08-03 03:55:11 +08:00
// Process node and mesh primitives.
const runtimeNode = new ModelRuntimeNode({
node: node,
transform: transform,
transformToRoot: transformToRoot,
children: childrenIndices,
2022-01-05 04:56:55 +08:00
sceneGraph: sceneGraph,
});
2022-04-23 04:58:43 +08:00
const primitivesLength = node.primitives.length;
for (let i = 0; i < primitivesLength; i++) {
runtimeNode.runtimePrimitives.push(
new ModelRuntimePrimitive({
2022-04-23 04:58:43 +08:00
primitive: node.primitives[i],
node: node,
model: sceneGraph._model,
}),
);
}
const index = node.index;
sceneGraph._runtimeNodes[index] = runtimeNode;
if (defined(node.skin)) {
sceneGraph._skinnedNodes.push(index);
}
2022-07-12 05:44:31 +08:00
// Create and store the public version of the runtime node.
2022-07-14 04:11:25 +08:00
const name = node.name;
if (defined(name)) {
const model = sceneGraph._model;
const publicNode = new ModelNode(model, runtimeNode);
2022-07-14 04:11:25 +08:00
model._nodesByName[name] = publicNode;
}
2022-07-12 05:44:31 +08:00
return index;
}
const scratchModelPositionMin = new Cartesian3();
const scratchModelPositionMax = new Cartesian3();
const scratchPrimitivePositionMin = new Cartesian3();
const scratchPrimitivePositionMax = new Cartesian3();
/**
* Generates the {@link ModelDrawCommand} for each primitive in the model.
* If the model is used for classification, a {@link ClassificationModelDrawCommand}
* is generated for each primitive instead.
*
2021-08-03 03:55:11 +08:00
* @param {FrameState} frameState The current frame state. This is needed to
* allocate GPU resources as needed.
2021-08-03 21:23:48 +08:00
*
* @private
*/
ModelSceneGraph.prototype.buildDrawCommands = function (frameState) {
const modelRenderResources = this.buildRenderResources(frameState);
this.computeBoundingVolumes(modelRenderResources);
this.createDrawCommands(modelRenderResources, frameState);
};
/**
* Generates the {@link ModelRenderResources} for the model.
*
* This will traverse the model, nodes and primitives of the scene graph,
* and perform the following tasks:
*
* - configure the pipeline stages by calling `configurePipeline`,
* `runtimeNode.configurePipeline`, and `runtimePrimitive.configurePipeline`
* - create the `ModelRenderResources`, `NodeRenderResources`, and
* `PrimitiveRenderResources`
* - Process the render resources with the respective pipelines
*
* @param {FrameState} frameState The current frame state. This is needed to
* allocate GPU resources as needed.
* @returns {ModelRenderResources} The model render resources
*
* @private
*/
ModelSceneGraph.prototype.buildRenderResources = function (frameState) {
const model = this._model;
const modelRenderResources = new ModelRenderResources(model);
2022-05-17 21:35:38 +08:00
// Reset the memory counts before running the pipeline
model.statistics.clear();
this.configurePipeline(frameState);
2022-01-11 05:04:27 +08:00
const modelPipelineStages = this.modelPipelineStages;
for (let i = 0; i < modelPipelineStages.length; i++) {
2021-10-28 00:39:03 +08:00
const modelPipelineStage = modelPipelineStages[i];
2021-10-21 02:05:54 +08:00
modelPipelineStage.process(modelRenderResources, model, frameState);
}
for (let i = 0; i < this._runtimeNodes.length; i++) {
2021-08-20 07:40:34 +08:00
const runtimeNode = this._runtimeNodes[i];
2022-09-23 21:06:51 +08:00
2022-09-23 21:09:29 +08:00
// If a node in the model was unreachable from the scene graph, there will
2022-09-23 21:06:51 +08:00
// be no corresponding runtime node and therefore should be skipped.
if (!defined(runtimeNode)) {
continue;
}
2022-01-11 05:04:27 +08:00
runtimeNode.configurePipeline();
const nodePipelineStages = runtimeNode.pipelineStages;
2021-08-20 07:40:34 +08:00
const nodeRenderResources = new NodeRenderResources(
modelRenderResources,
2021-08-20 07:40:34 +08:00
runtimeNode,
);
modelRenderResources.nodeRenderResources[i] = nodeRenderResources;
for (let j = 0; j < nodePipelineStages.length; j++) {
2022-01-11 05:04:27 +08:00
const nodePipelineStage = nodePipelineStages[j];
2021-07-29 04:26:18 +08:00
nodePipelineStage.process(
nodeRenderResources,
2021-08-20 07:40:34 +08:00
runtimeNode.node,
2021-07-29 04:26:18 +08:00
frameState,
);
}
for (let j = 0; j < runtimeNode.runtimePrimitives.length; j++) {
2021-08-20 07:40:34 +08:00
const runtimePrimitive = runtimeNode.runtimePrimitives[j];
runtimePrimitive.configurePipeline(frameState);
2022-01-11 05:04:27 +08:00
const primitivePipelineStages = runtimePrimitive.pipelineStages;
2021-08-20 07:40:34 +08:00
const primitiveRenderResources = new PrimitiveRenderResources(
nodeRenderResources,
2021-08-20 07:40:34 +08:00
runtimePrimitive,
);
nodeRenderResources.primitiveRenderResources[j] =
primitiveRenderResources;
for (let k = 0; k < primitivePipelineStages.length; k++) {
2021-10-28 00:39:03 +08:00
const primitivePipelineStage = primitivePipelineStages[k];
primitivePipelineStage.process(
2021-08-20 07:40:34 +08:00
primitiveRenderResources,
runtimePrimitive.primitive,
2021-07-29 04:26:18 +08:00
frameState,
);
}
}
}
return modelRenderResources;
};
/**
* Computes the bounding volumes for the scene graph and the model.
*
* This will traverse the model, nodes and primitives of the scene graph,
* and compute the bounding volumes. Specifically, it will compute
*
* - this._boundingSphere
* - model._boundingSphere
*
* With the latter being modified as of
*
* - model._initialRadius = model._boundingSphere.radius;
* - model._boundingSphere.radius *= model._clampedScale;
*
* NOTE: This contains some bugs. See https://github.com/CesiumGS/cesium/issues/12108
*
* @param {ModelRenderResources} modelRenderResources The model render resources
*
* @private
*/
ModelSceneGraph.prototype.computeBoundingVolumes = function (
modelRenderResources,
) {
const model = this._model;
const modelPositionMin = Cartesian3.fromElements(
Number.MAX_VALUE,
Number.MAX_VALUE,
Number.MAX_VALUE,
scratchModelPositionMin,
);
const modelPositionMax = Cartesian3.fromElements(
-Number.MAX_VALUE,
-Number.MAX_VALUE,
-Number.MAX_VALUE,
scratchModelPositionMax,
);
for (let i = 0; i < this._runtimeNodes.length; i++) {
const runtimeNode = this._runtimeNodes[i];
// If a node in the model was unreachable from the scene graph, there will
// be no corresponding runtime node and therefore should be skipped.
if (!defined(runtimeNode)) {
continue;
}
const nodeRenderResources = modelRenderResources.nodeRenderResources[i];
const nodeTransform = runtimeNode.computedTransform;
for (let j = 0; j < runtimeNode.runtimePrimitives.length; j++) {
const runtimePrimitive = runtimeNode.runtimePrimitives[j];
const primitiveRenderResources =
nodeRenderResources.primitiveRenderResources[j];
runtimePrimitive.boundingSphere = BoundingSphere.clone(
2022-05-03 01:48:25 +08:00
primitiveRenderResources.boundingSphere,
new BoundingSphere(),
);
const primitivePositionMin = Matrix4.multiplyByPoint(
nodeTransform,
primitiveRenderResources.positionMin,
scratchPrimitivePositionMin,
);
const primitivePositionMax = Matrix4.multiplyByPoint(
nodeTransform,
primitiveRenderResources.positionMax,
scratchPrimitivePositionMax,
);
2022-05-03 01:48:25 +08:00
Cartesian3.minimumByComponent(
modelPositionMin,
primitivePositionMin,
2022-05-03 01:48:25 +08:00
modelPositionMin,
);
Cartesian3.maximumByComponent(
modelPositionMax,
primitivePositionMax,
2022-05-03 01:48:25 +08:00
modelPositionMax,
);
}
}
2022-05-03 01:48:25 +08:00
this._boundingSphere = BoundingSphere.fromCornerPoints(
modelPositionMin,
modelPositionMax,
new BoundingSphere(),
);
this._boundingSphere = BoundingSphere.transformWithoutScale(
this._boundingSphere,
this._axisCorrectionMatrix,
2022-05-03 03:03:42 +08:00
this._boundingSphere,
2022-05-03 01:48:25 +08:00
);
this._boundingSphere = BoundingSphere.transform(
this._boundingSphere,
this._components.transform,
this._boundingSphere,
);
2022-03-18 21:57:36 +08:00
model._boundingSphere = BoundingSphere.transform(
this._boundingSphere,
model.modelMatrix,
model._boundingSphere,
);
model._initialRadius = model._boundingSphere.radius;
2022-03-14 23:52:34 +08:00
model._boundingSphere.radius *= model._clampedScale;
};
2022-01-05 04:56:55 +08:00
/**
* Creates the draw commands for the primitives in the scene graph.
*
* This will traverse the model, nodes and primitives of the scene graph,
* and create the respective draw commands for the primitives, storing
* them as the `runtimePrimitive.drawCommand`, respectively.
*
* @param {ModelRenderResources} modelRenderResources The model render resources
*
* @private
*/
ModelSceneGraph.prototype.createDrawCommands = function (
modelRenderResources,
frameState,
) {
for (let i = 0; i < this._runtimeNodes.length; i++) {
const runtimeNode = this._runtimeNodes[i];
// If a node in the model was unreachable from the scene graph, there will
// be no corresponding runtime node and therefore should be skipped.
if (!defined(runtimeNode)) {
continue;
}
const nodeRenderResources = modelRenderResources.nodeRenderResources[i];
for (let j = 0; j < runtimeNode.runtimePrimitives.length; j++) {
const runtimePrimitive = runtimeNode.runtimePrimitives[j];
const primitiveRenderResources =
nodeRenderResources.primitiveRenderResources[j];
const drawCommand = ModelDrawCommands.buildModelDrawCommand(
primitiveRenderResources,
frameState,
);
runtimePrimitive.drawCommand = drawCommand;
}
}
};
2022-01-11 05:04:27 +08:00
/**
* Configure the model 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
2022-01-11 05:04:27 +08:00
* @private
*/
ModelSceneGraph.prototype.configurePipeline = function (frameState) {
2022-01-11 05:04:27 +08:00
const modelPipelineStages = this.modelPipelineStages;
modelPipelineStages.length = 0;
const model = this._model;
const fogRenderable = frameState.fog.enabled && frameState.fog.renderable;
2022-01-11 05:04:27 +08:00
if (defined(model.color)) {
modelPipelineStages.push(ModelColorPipelineStage);
}
2022-08-02 02:49:39 +08:00
// Skip these pipeline stages for classification models.
if (defined(model.classificationType)) {
return;
}
if (model.imageBasedLighting.enabled) {
modelPipelineStages.push(ImageBasedLightingPipelineStage);
}
if (model.isClippingEnabled()) {
modelPipelineStages.push(ModelClippingPlanesPipelineStage);
}
2024-01-09 23:39:09 +08:00
if (model.isClippingPolygonsEnabled()) {
modelPipelineStages.push(ModelClippingPolygonsPipelineStage);
}
2022-08-02 02:49:39 +08:00
if (model.hasSilhouette(frameState)) {
modelPipelineStages.push(ModelSilhouettePipelineStage);
}
if (
defined(model.splitDirection) &&
model.splitDirection !== SplitDirection.NONE
) {
modelPipelineStages.push(ModelSplitterPipelineStage);
}
2022-08-04 04:05:12 +08:00
if (ModelType.is3DTiles(model.type)) {
2022-08-17 03:35:06 +08:00
modelPipelineStages.push(TilesetPipelineStage);
}
if (fogRenderable) {
modelPipelineStages.push(AtmospherePipelineStage);
}
2022-01-11 05:04:27 +08:00
};
ModelSceneGraph.prototype.update = function (frameState, updateForAnimations) {
let i, j, k;
for (i = 0; i < this._runtimeNodes.length; i++) {
const runtimeNode = this._runtimeNodes[i];
2022-09-23 21:09:29 +08:00
// If a node in the model was unreachable from the scene graph, there will
2022-09-23 21:06:51 +08:00
// be no corresponding runtime node and therefore should be skipped.
if (!defined(runtimeNode)) {
continue;
}
for (j = 0; j < runtimeNode.updateStages.length; j++) {
const nodeUpdateStage = runtimeNode.updateStages[j];
nodeUpdateStage.update(runtimeNode, this, frameState);
}
const disableAnimations =
frameState.mode !== SceneMode.SCENE3D && this._model._projectTo2D;
if (updateForAnimations && !disableAnimations) {
this.updateJointMatrices();
}
for (j = 0; j < runtimeNode.runtimePrimitives.length; j++) {
const runtimePrimitive = runtimeNode.runtimePrimitives[j];
for (k = 0; k < runtimePrimitive.updateStages.length; k++) {
const stage = runtimePrimitive.updateStages[k];
stage.update(runtimePrimitive, this);
}
}
}
};
ModelSceneGraph.prototype.updateModelMatrix = function (
modelMatrix,
frameState,
) {
computeModelMatrix(this, modelMatrix);
if (frameState.mode !== SceneMode.SCENE3D) {
computeModelMatrix2D(this, frameState);
}
// Mark all root nodes as dirty. Any and all children will be
// affected recursively in the update stage.
const rootNodes = this._rootNodes;
for (let i = 0; i < rootNodes.length; i++) {
const node = this._runtimeNodes[rootNodes[i]];
node._transformDirty = true;
}
};
/**
* Updates the joint matrices for the skins and nodes of the model.
*
* @private
*/
ModelSceneGraph.prototype.updateJointMatrices = function () {
const skinnedNodes = this._skinnedNodes;
2022-04-12 02:09:01 +08:00
const length = skinnedNodes.length;
2022-04-12 02:09:01 +08:00
for (let i = 0; i < length; i++) {
const nodeIndex = skinnedNodes[i];
const runtimeNode = this._runtimeNodes[nodeIndex];
runtimeNode.updateJointMatrices();
}
};
2022-08-23 02:58:36 +08:00
/**
* A callback to be applied once at each runtime primitive in the
* scene graph
* @callback traverseSceneGraphCallback
*
* @param {ModelRuntimePrimitive} runtimePrimitive The runtime primitive for the current step of the traversal
* @param {object} [options] A dictionary of additional options to be passed to the callback, or undefined if the callback does not need any additional information.
2022-08-23 02:58:36 +08:00
*
* @private
*/
2022-07-13 02:09:40 +08:00
/**
* Recursively traverse through the runtime nodes in the scene graph
2022-07-14 04:11:25 +08:00
* using a post-order depth-first traversal to perform a callback on
2022-07-13 02:09:40 +08:00
* their runtime primitives.
*
* @param {ModelSceneGraph} sceneGraph The scene graph.
* @param {ModelRuntimeNode} runtimeNode The current runtime node.
* @param {boolean} visibleNodesOnly Whether to only traverse nodes that are visible.
2022-08-23 02:58:36 +08:00
* @param {traverseSceneGraphCallback} callback The callback to perform on the runtime primitives of the node.
* @param {object} [callbackOptions] A dictionary of additional options to be passed to the callback, if needed.
2022-07-13 02:09:40 +08:00
*
2022-08-23 03:00:11 +08:00
* @private
2022-07-13 02:09:40 +08:00
*/
function traverseSceneGraph(
sceneGraph,
runtimeNode,
visibleNodesOnly,
callback,
callbackOptions,
2022-07-13 02:09:40 +08:00
) {
if (visibleNodesOnly && !runtimeNode.show) {
return;
}
const childrenLength = runtimeNode.children.length;
for (let i = 0; i < childrenLength; i++) {
const childRuntimeNode = runtimeNode.getChild(i);
traverseSceneGraph(
sceneGraph,
childRuntimeNode,
visibleNodesOnly,
callback,
callbackOptions,
2022-07-13 02:09:40 +08:00
);
}
const runtimePrimitives = runtimeNode.runtimePrimitives;
const runtimePrimitivesLength = runtimePrimitives.length;
for (let j = 0; j < runtimePrimitivesLength; j++) {
const runtimePrimitive = runtimePrimitives[j];
callback(runtimePrimitive, callbackOptions);
2022-07-13 02:09:40 +08:00
}
}
function forEachRuntimePrimitive(
sceneGraph,
visibleNodesOnly,
callback,
callbackOptions,
) {
2022-07-13 02:09:40 +08:00
const rootNodes = sceneGraph._rootNodes;
const rootNodesLength = rootNodes.length;
for (let i = 0; i < rootNodesLength; i++) {
const rootNodeIndex = rootNodes[i];
const runtimeNode = sceneGraph._runtimeNodes[rootNodeIndex];
traverseSceneGraph(
sceneGraph,
runtimeNode,
visibleNodesOnly,
callback,
callbackOptions,
);
}
}
2022-08-22 22:09:50 +08:00
const scratchBackFaceCullingOptions = {
backFaceCulling: undefined,
};
/**
* Traverses through all draw commands and changes the back-face culling setting.
*
* @param {boolean} backFaceCulling The new value for the back-face culling setting.
*
* @private
*/
ModelSceneGraph.prototype.updateBackFaceCulling = function (backFaceCulling) {
2022-08-22 22:09:50 +08:00
const backFaceCullingOptions = scratchBackFaceCullingOptions;
backFaceCullingOptions.backFaceCulling = backFaceCulling;
forEachRuntimePrimitive(
this,
false,
updatePrimitiveBackFaceCulling,
backFaceCullingOptions,
);
};
// Callback is defined here to avoid allocating a closure in the render loop
function updatePrimitiveBackFaceCulling(runtimePrimitive, options) {
2024-07-30 23:01:30 +08:00
const drawCommand = runtimePrimitive.drawCommand;
drawCommand.backFaceCulling = options.backFaceCulling;
2022-08-22 22:09:50 +08:00
}
const scratchShadowOptions = {
shadowMode: undefined,
};
/**
* Traverses through all draw commands and changes the shadow settings.
*
* @param {ShadowMode} shadowMode The new shadow settings.
*
* @private
*/
ModelSceneGraph.prototype.updateShadows = function (shadowMode) {
2022-08-22 22:09:50 +08:00
const shadowOptions = scratchShadowOptions;
shadowOptions.shadowMode = shadowMode;
forEachRuntimePrimitive(this, false, updatePrimitiveShadows, shadowOptions);
};
// Callback is defined here to avoid allocating a closure in the render loop
function updatePrimitiveShadows(runtimePrimitive, options) {
2024-07-30 23:01:30 +08:00
const drawCommand = runtimePrimitive.drawCommand;
drawCommand.shadows = options.shadowMode;
2022-08-22 22:09:50 +08:00
}
const scratchShowBoundingVolumeOptions = {
debugShowBoundingVolume: undefined,
};
/**
* Traverses through all draw commands and changes whether to show the debug bounding volume.
*
* @param {boolean} debugShowBoundingVolume The new value for showing the debug bounding volume.
*
* @private
*/
ModelSceneGraph.prototype.updateShowBoundingVolume = function (
debugShowBoundingVolume,
) {
2022-08-22 22:09:50 +08:00
const showBoundingVolumeOptions = scratchShowBoundingVolumeOptions;
showBoundingVolumeOptions.debugShowBoundingVolume = debugShowBoundingVolume;
forEachRuntimePrimitive(
this,
false,
updatePrimitiveShowBoundingVolume,
showBoundingVolumeOptions,
);
};
2022-08-22 22:09:50 +08:00
// Callback is defined here to avoid allocating a closure in the render loop
function updatePrimitiveShowBoundingVolume(runtimePrimitive, options) {
2024-07-30 23:01:30 +08:00
const drawCommand = runtimePrimitive.drawCommand;
drawCommand.debugShowBoundingVolume = options.debugShowBoundingVolume;
2022-08-22 22:09:50 +08:00
}
2022-08-16 05:16:23 +08:00
const scratchSilhouetteCommands = [];
2025-08-07 13:26:52 +08:00
const scratchEdgeCommands = [];
const scratchPushDrawCommandOptions = {
frameState: undefined,
hasSilhouette: undefined,
};
2022-08-18 02:43:44 +08:00
2022-01-05 04:56:55 +08:00
/**
2022-08-17 01:52:46 +08:00
* Traverses through the scene graph and pushes the draw commands associated
* with each primitive to the frame state's command list.
2022-01-05 04:56:55 +08:00
*
2022-05-28 05:12:25 +08:00
* @param {FrameState} frameState The frame state.
*
2022-01-05 04:56:55 +08:00
* @private
*/
2022-08-11 05:07:27 +08:00
ModelSceneGraph.prototype.pushDrawCommands = function (frameState) {
// If a model has silhouettes, the commands that draw the silhouettes for
// each primitive can only be invoked after the entire model has drawn.
// Otherwise, the silhouette may draw on top of the model. This requires
// gathering the original commands and the silhouette commands separately.
2022-08-16 05:16:23 +08:00
const silhouetteCommands = scratchSilhouetteCommands;
silhouetteCommands.length = 0;
2025-08-07 13:26:52 +08:00
// Gather edge commands for the edge pass
const edgeCommands = scratchEdgeCommands;
edgeCommands.length = 0;
// Since this function is called each frame, the options object is
// preallocated in a scratch variable
const pushDrawCommandOptions = scratchPushDrawCommandOptions;
pushDrawCommandOptions.hasSilhouette = this._model.hasSilhouette(frameState);
pushDrawCommandOptions.frameState = frameState;
forEachRuntimePrimitive(
this,
true,
pushPrimitiveDrawCommands,
pushDrawCommandOptions,
);
addAllToArray(frameState.commandList, silhouetteCommands);
2025-08-07 13:26:52 +08:00
addAllToArray(frameState.commandList, edgeCommands);
2022-01-05 04:56:55 +08:00
};
2022-08-22 22:09:50 +08:00
// Callback is defined here to avoid allocating a closure in the render loop
function pushPrimitiveDrawCommands(runtimePrimitive, options) {
const frameState = options.frameState;
const hasSilhouette = options.hasSilhouette;
const passes = frameState.passes;
const silhouetteCommands = scratchSilhouetteCommands;
2025-08-07 13:26:52 +08:00
const edgeCommands = scratchEdgeCommands;
const primitiveDrawCommand = runtimePrimitive.drawCommand;
primitiveDrawCommand.pushCommands(frameState, frameState.commandList);
// If a model has silhouettes, the commands that draw the silhouettes for
// each primitive can only be invoked after the entire model has drawn.
// Otherwise, the silhouette may draw on top of the model. This requires
// gathering the original commands and the silhouette commands separately.
if (hasSilhouette && !passes.pick) {
primitiveDrawCommand.pushSilhouetteCommands(frameState, silhouetteCommands);
}
2025-08-07 13:26:52 +08:00
// Add edge commands to the edge pass
2025-08-28 05:01:38 +08:00
if (defined(primitiveDrawCommand.pushEdgeCommands)) {
primitiveDrawCommand.pushEdgeCommands(frameState, edgeCommands);
}
}
/**
* Sets the current value of an articulation stage.
*
* @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.
*
* @private
*/
ModelSceneGraph.prototype.setArticulationStage = function (
articulationStageKey,
value,
) {
const names = articulationStageKey.split(" ");
if (names.length !== 2) {
return;
}
const articulationName = names[0];
const stageName = names[1];
const runtimeArticulation = this._runtimeArticulations[articulationName];
if (defined(runtimeArticulation)) {
runtimeArticulation.setArticulationStage(stageName, value);
}
};
/**
* Applies any modified articulation stages to the matrix of each node that participates
* in any articulation. Note that this will overwrite any nodeTransformations on participating nodes.
*
* @private
*/
ModelSceneGraph.prototype.applyArticulations = function () {
const runtimeArticulations = this._runtimeArticulations;
for (const articulationName in runtimeArticulations) {
if (runtimeArticulations.hasOwnProperty(articulationName)) {
const articulation = runtimeArticulations[articulationName];
articulation.apply();
}
}
};
2022-08-11 06:22:27 +08:00
export default ModelSceneGraph;