Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): Update recalculateBoundingBox in Cognite3DViewer #4715

Merged
merged 12 commits into from
Aug 29, 2024
76 changes: 50 additions & 26 deletions viewer/packages/api/src/public/migration/Cognite3DViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,19 @@ export class Cognite3DViewer {
this.fitCameraToBoundingBox(boundingBox, duration);
}

/**
* Move camera to a place where a all objects in the scene are visible
* @param duration The duration of the animation moving the camera. Set this to 0 (zero) to disable animation.
*/
fitCameraToSceneBoundingBox(duration?: number): void {
this.recalculateBoundingBox();
const boundingBox = this.getSceneBoundingBox();
if (boundingBox.isEmpty()) {
return;
}
this.fitCameraToBoundingBox(boundingBox, duration);
}

/**
* Move camera to a place where the content of a bounding box is visible to the camera.
* @param box The bounding box in world space.
Expand Down Expand Up @@ -1880,34 +1893,45 @@ export class Cognite3DViewer {
nearFarPlaneBoundingBox.makeEmpty();
sceneBoundingBox.makeEmpty();

this._models.forEach(model => {
model.getModelBoundingBox(temporaryBox);
if (temporaryBox.isEmpty()) {
return;
}
nearFarPlaneBoundingBox.union(temporaryBox);
for (let pass = 0; pass < 2; pass++) {
// On the first pass, use visible models only
// If no bounding box is found, use all models on the second pass
// By this way, a bounding box is forced to be calculated
this._models.forEach(model => {
if (pass === 0 && !model.visible) {
return;
}
model.getModelBoundingBox(temporaryBox);
if (temporaryBox.isEmpty()) {
return;
}
nearFarPlaneBoundingBox.union(temporaryBox);

// The getModelBoundingBox is using restrictToMostGeometry = true
model.getModelBoundingBox(temporaryBox, true);
if (temporaryBox.isEmpty()) {
return;
}
sceneBoundingBox.union(temporaryBox);
});
this._sceneHandler.customObjects.forEach(customObject => {
if (!customObject.object.visible) {
return;
}
customObject.getBoundingBox(temporaryBox);
if (temporaryBox.isEmpty()) {
return;
}
nearFarPlaneBoundingBox.union(temporaryBox);
if (!customObject.isPartOfBoundingBox) {
return;
// The getModelBoundingBox is using restrictToMostGeometry = true
model.getModelBoundingBox(temporaryBox, true);
if (temporaryBox.isEmpty()) {
return;
}
sceneBoundingBox.union(temporaryBox);
});
this._sceneHandler.customObjects.forEach(customObject => {
if (!customObject.object.visible) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to have the pass === 0-check here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't have that from before. It always used the visible object in these cases.

return;
}
customObject.getBoundingBox(temporaryBox);
if (temporaryBox.isEmpty()) {
return;
}
nearFarPlaneBoundingBox.union(temporaryBox);
if (!customObject.isPartOfBoundingBox) {
return;
}
sceneBoundingBox.union(temporaryBox);
});
if (!sceneBoundingBox.isEmpty()) {
break;
}
sceneBoundingBox.union(temporaryBox);
});
}
}

/** @private */
Expand Down
Loading