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

Handle zoom while camera change #492

Merged
merged 2 commits into from
Oct 17, 2024
Merged
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
6 changes: 6 additions & 0 deletions packages/base/src/3dview/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ export class MainView extends React.Component<IProps, IStates> {
private _updateCamera() {
const position = new THREE.Vector3().copy(this._camera.position);
const up = new THREE.Vector3().copy(this._camera.up);
const target = this._controls.target.clone();

this._camera.remove(this._cameraLight);
this._scene.remove(this._camera);
Expand All @@ -1323,12 +1324,17 @@ export class MainView extends React.Component<IProps, IStates> {
const width = this.divRef.current?.clientWidth || 0;
const height = this.divRef.current?.clientHeight || 0;

const distance = position.distanceTo(target);
const zoomFactor = 1000 / distance;
Copy link
Member

Choose a reason for hiding this comment

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

Do you know why we need this magic 1000 number?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is kind of an arbitrary number after some hit and trials, I noticed that more you zoom the object in PerspectiveCamera more further it goes on OrthographicCamera

Copy link
Member Author

Choose a reason for hiding this comment

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

So it meant that the distance was inversely proportional to the zooming in that i needed to do

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good! I tested on my side and this seems to work well. We have other magic numbers in JupyterCAD


this._camera = new THREE.OrthographicCamera(
width / -2,
width / 2,
height / 2,
height / -2
);
this._camera.zoom = zoomFactor;
this._camera.updateProjectionMatrix();
}

this._camera.add(this._cameraLight);
Expand Down
Loading