Skip to content

Commit

Permalink
removed zoom scaling (for now) fixed panning rotation and near clippi…
Browse files Browse the repository at this point in the history
…ng for zoom
  • Loading branch information
kpal81xd committed Apr 17, 2024
1 parent 948c162 commit ea9a0ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/cameras/multi-camera.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Vec2, Vec3, math } from 'playcanvas';
import { Entity, Vec2, Vec3, Quat, math } from 'playcanvas';
import { BaseCamera } from './base-camera';

type PointerMoveEvent = PointerEvent & {
Expand All @@ -10,6 +10,7 @@ type PointerMoveEvent = PointerEvent & {

const tmpVa = new Vec2();
const tmpV1 = new Vec3();
const tmpQ1 = new Quat();

const PASSIVE: any = { passive: false };

Expand Down Expand Up @@ -128,8 +129,7 @@ class MultiCamera extends BaseCamera {
// pinch zoom
const pinchDist = this._getPinchDist();
if (this._lastPinchDist > 0) {
const zoomMult = (this._lastPinchDist - pinchDist) * this.sceneSize * this.pinchSpeed;
this._zoom = Math.max(this._zoom + zoomMult * (this._zoom * this.zoomExp + this.zoomThreshold), 0);
this._focusZoom(this._lastPinchDist - pinchDist);
}
this._lastPinchDist = pinchDist;
}
Expand All @@ -155,8 +155,7 @@ class MultiCamera extends BaseCamera {

private _onWheel(event: WheelEvent) {
event.preventDefault();
const zoomMult = event.deltaY * this.sceneSize * this.wheelSpeed;
this._zoom = Math.max(this._zoom + zoomMult * (this._zoom * this.zoomExp + this.zoomThreshold), 0);
this._focusZoom(event.deltaY);
}

private _onKeyDown(event: KeyboardEvent) {
Expand Down Expand Up @@ -254,19 +253,21 @@ class MultiCamera extends BaseCamera {
}

private _pan(pos: Vec2, speed = 1) {
const distance = Math.abs(this._zoom);

const last = this._camera.camera.screenToWorld(this._lastPosition.x, this._lastPosition.y, distance);
const current = this._camera.camera.screenToWorld(pos.x, pos.y, distance);

tmpV1.sub2(last, current);
tmpV1.mulScalar(speed * this.sceneSize);
tmpV1.set(0, 0, 0);
tmpV1.x = (this._lastPosition.x - pos.x) * speed;
tmpV1.y = (pos.y - this._lastPosition.y) * speed;

tmpQ1.copy(this._camera.getRotation()).transformVector(tmpV1, tmpV1);
this._origin.add(tmpV1);

this._lastPosition.copy(pos);
}

private _focusZoom(delta: number) {
const zoomMult = delta * this.sceneSize * this.wheelSpeed;
this._zoom = Math.max(this._zoom + zoomMult, this.zoomThreshold);
}

focus(point: Vec3, start?: Vec3, dir?: Vec2, snap?: boolean) {
if (!this._camera) {
return;
Expand Down
5 changes: 3 additions & 2 deletions src/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ class Viewer {
camera.addComponent("camera", {
fov: 75,
frustumCulling: true,
clearColor: new Color(0, 0, 0, 0)
clearColor: new Color(0, 0, 0, 0),
nearClip: 0.001
});
camera.camera.requestSceneColorMap(true);

Expand All @@ -237,7 +238,7 @@ class Viewer {
lookSensitity: 0.3,
lookDamping: 0.97,
moveDamping: 0.97,
mousePanSpeed: 1,
mousePanSpeed: 0.25,
mobilePanSpeed: 0.025,
pinchSpeed: 0.00025,
wheelSpeed: 0.005,
Expand Down

0 comments on commit ea9a0ad

Please sign in to comment.