Skip to content

Commit

Permalink
ArcRotateCamera: Modify offset math to correctly zoom to point (#14209)
Browse files Browse the repository at this point in the history
* Added conversion to convert screen to world offset

* Added vectors to avoid GC

* Added conditional to offset math
  • Loading branch information
PolygonalSun committed Aug 29, 2023
1 parent 0bd05af commit 01120a4
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
private _wheel: Nullable<(p: PointerInfo, s: EventState) => void>;
private _observer: Nullable<Observer<PointerInfo>>;
private _hitPlane: Nullable<Plane>;
private _viewOffset: Vector3 = new Vector3(0, 0, 0);
private _globalOffset: Vector3 = new Vector3(0, 0, 0);

protected _computeDeltaFromMouseWheelLegacyEvent(mouseWheelDelta: number, radius: number) {
let delta = 0;
Expand Down Expand Up @@ -210,8 +212,14 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
// a vector defining where we want to zoom to.
const ray = scene.createPickingRay(scene.pointerX, scene.pointerY, Matrix.Identity(), camera, false);
// Since the camera is the origin of the picking ray, we need to offset it by the camera's offset manually
ray.origin.x -= camera.targetScreenOffset.x;
ray.origin.y -= camera.targetScreenOffset.y;
// Because the offset is in view space, we need to convert it to world space first
if (camera.targetScreenOffset.x !== 0 || camera.targetScreenOffset.y !== 0) {
this._viewOffset.set(camera.targetScreenOffset.x, camera.targetScreenOffset.y, 0);
camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
this._globalOffset = Vector3.TransformNormal(this._viewOffset, camera._cameraTransformMatrix);
ray.origin.addInPlace(this._globalOffset);
}

let distance = 0;
if (this._hitPlane) {
distance = ray.intersectsPlane(this._hitPlane) ?? 0;
Expand Down

0 comments on commit 01120a4

Please sign in to comment.