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

update camera aspect ratio in Plan View and Cross Section #694

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ Inspector.prototype = {
this.debugUndoRedo = false;

this.viewport = new Viewport(this);
Events.emit('windowresize');

this.sceneEl.object3D.traverse((node) => {
this.addHelper(node);
Expand Down
1 change: 0 additions & 1 deletion src/editor/lib/cameras.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,4 @@ function setOrthoCamera(camera, dir, ratio) {
camera.bottom = info.bottom || -40;
camera.position.copy(info.position);
camera.rotation.copy(info.rotation);
camera.updateProjectionMatrix();
}
29 changes: 25 additions & 4 deletions src/editor/lib/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export function Viewport(inspector) {
Events.on('cameratoggle', (data) => {
controls.setCamera(data.camera);
transformControls.setCamera(data.camera);

updateAspectRatio();
// quick solution to change 3d tiles camera
const tilesElem = document.querySelector('a-entity[loader-3dtiles]');
if (tilesElem) {
Expand Down Expand Up @@ -312,11 +312,32 @@ export function Viewport(inspector) {
updateHelpers(object);
});

Events.on('windowresize', () => {
camera.aspect =
function updateAspectRatio() {
if (!inspector.opened) return;
// Modifying aspect for perspective camera is done by aframe a-scene.resize function
// when the perspective camera is the active camera, so we actually do it a second time here,
// but we need to modify it ourself when we switch from ortho camera to perspective camera (updateAspectRatio() is called in cameratoggle handler).
const camera = inspector.camera;
const aspect =
inspector.container.offsetWidth / inspector.container.offsetHeight;
if (camera.isPerspectiveCamera) {
camera.aspect = aspect;
} else if (camera.isOrthographicCamera) {
const frustumSize = camera.top - camera.bottom;
camera.left = (-frustumSize * aspect) / 2;
camera.right = (frustumSize * aspect) / 2;
camera.top = frustumSize / 2;
camera.bottom = -frustumSize / 2;
}

controls.setAspectRatio(aspect); // for zoom in/out to work correctly for orthographic camera
camera.updateProjectionMatrix();
});

const cameraHelper = inspector.helpers[camera.uuid];
if (cameraHelper) cameraHelper.update();
}

inspector.sceneEl.addEventListener('rendererresize', updateAspectRatio);

Events.on('gridvisibilitychanged', (showGrid) => {
grid.visible = showGrid;
Expand Down
Loading