fix(OrthoPerspectiveCamera): safeguard zero size - #790
Open
ShaMan123 wants to merge 2 commits into
Open
Conversation
Contributor
Author
iimport * as OBC from "@thatopen/components";
import * as THREE from "three";
import { describe, expect, it } from "vitest";
const W = 1600;
const H = 900;
/** The slice of a world the aspect path reads. */
function fakeWorld(size: THREE.Vector2) {
return { renderer: { getSize: () => size, isResizeable: () => true } };
}
/**
* Seed the state `worlds.onItemSet` would have set, without assigning
* `currentWorld` — that path also builds the navigation modes and their
* `CameraControls`, which the frustum math has nothing to do with.
*/
function attachWorld(camera: OBC.OrthoPerspectiveCamera, size: THREE.Vector2) {
const internals = camera as unknown as {
_currentWorld: unknown;
previousSize: THREE.Vector2;
};
internals._currentWorld = fakeWorld(size);
internals.previousSize = size.clone();
}
/** Both cameras the app can render through, whichever `three` currently is. */
function expectFinite(camera: OBC.OrthoPerspectiveCamera) {
for (const three of [camera.threePersp, camera.threeOrtho]) {
expect(three.projectionMatrix.elements.every(Number.isFinite)).toBe(true);
}
}
describe("OrthoPerspectiveCamera aspect", () => {
it("keeps both projections finite across a collapse and re-expand", () => {
const camera = new OBC.OrthoPerspectiveCamera(new OBC.Components());
const size = new THREE.Vector2(W, H);
attachWorld(camera, size);
// The app never switches projection, so this is the one being rendered.
expect(camera.three).toBe(camera.threePersp);
camera.updateAspect();
const framed = camera.threeOrtho.right;
const aspect = camera.threePersp.aspect;
expect(framed).toBeGreaterThan(0);
expect(aspect).toBeCloseTo(W / H);
// Collapse: the panel reports no width at all.
size.set(0, H);
camera.updateAspect();
expect(camera.threePersp.aspect).toBeCloseTo(aspect);
expectFinite(camera);
// Re-expand to the original size — the frustum must come back unchanged.
size.set(W, H);
camera.updateAspect();
expect(camera.threeOrtho.right).toBeCloseTo(framed);
expect(camera.threeOrtho.top).toBeGreaterThan(0);
expect(camera.threePersp.aspect).toBeCloseTo(aspect);
expectFinite(camera);
});
it("re-baselines a zero size cached before the first real resize", () => {
// The layout is persisted, so the viewer can MOUNT collapsed: the baseline
// `worlds.onItemSet` captures is already zero-width before any resize.
const camera = new OBC.OrthoPerspectiveCamera(new OBC.Components());
const size = new THREE.Vector2(0, H);
attachWorld(camera, size);
const framed = camera.threeOrtho.right;
size.set(W, H);
camera.updateAspect();
expect(camera.threeOrtho.right).toBeCloseTo(framed);
expect(camera.threePersp.aspect).toBeCloseTo(W / H);
expectFinite(camera);
});
it("still tracks a real resize", () => {
const camera = new OBC.OrthoPerspectiveCamera(new OBC.Components());
const size = new THREE.Vector2(W, H);
attachWorld(camera, size);
const framed = camera.threeOrtho.right;
size.set(W / 2, H);
camera.updateAspect();
expect(camera.threeOrtho.right).toBeCloseTo(framed / 2);
expect(camera.threePersp.aspect).toBeCloseTo(W / 2 / H);
});
});
|
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Safeguards against a zero size canvas.
The use case: collapsing a canvas and resizing back would permanently damage the camera.
I wanted to add a test but vitest is not setup.
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following:
feat(examples): add hello-world example).fixes #123).