Skip to content

Commit

Permalink
feat: Update the far/near parameters when the distance changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Aug 7, 2024
1 parent abb6ac1 commit d479338
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/chili-three/src/cameraController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class CameraController implements ICameraController {

private newCamera() {
return this._cameraType === "perspective"
? new PerspectiveCamera(this._fov, 1, 0.0001, 1e6)
: new OrthographicCamera(-0.5, 0.5, 0.5, -0.5, 0.0001, 1e6);
? new PerspectiveCamera(this._fov, 1, 100, 1e6)
: new OrthographicCamera(-0.5, 0.5, 0.5, -0.5, 100, 1e6);
}

pan(dx: number, dy: number): void {
Expand Down Expand Up @@ -128,6 +128,7 @@ export class CameraController implements ICameraController {

this._target.copy(sphere.center);
this._position.copy(this._target.clone().sub(direction.clone().multiplyScalar(distance)));
this.updateCameraNearFar();
this.update();
}

Expand Down Expand Up @@ -161,10 +162,28 @@ export class CameraController implements ICameraController {
let vector = this._target.clone().sub(mouse).multiplyScalar(scale);
this._target.add(vector);
this._position.copy(this._target.clone().sub(direction.clone().multiplyScalar(1 + scale)));
this.updateCameraNearFar();

this.update();
}

private updateCameraNearFar() {
let distance = this._position.distanceTo(this._target);
if (distance < 1000.0) {
this.camera.near = 0.1;
this.camera.far = 10000.0;
} else if (distance < 100000.0) {
this.camera.near = 10;
this.camera.far = 1000000.0;
} else if (distance < 1000000.0) {
this.camera.near = 1000.0;
this.camera.far = 10000000.0;
} else {
this.camera.near = 10000.0;
this.camera.far = 100000000.0;
}
}

private caculePerspectiveCameraMouse(direction: Vector3, mouse: Vector3) {
let directionNormal = direction.clone().normalize();
let dot = mouse.clone().sub(this._position).dot(directionNormal);
Expand Down

0 comments on commit d479338

Please sign in to comment.