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

update camera's target when the mouse wheel is rolling for zoomtomouselocation #14653

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,6 +13,7 @@ import type { IWheelEvent } from "../../Events/deviceInputEvents";
import { EventConstants } from "../../Events/deviceInputEvents";
import { Scalar } from "../../Maths/math.scalar";
import { Tools } from "../../Misc/tools";
import { Ray } from "../../Culling/ray";

/**
* Firefox uses a different scheme to report scroll distances to other
Expand Down Expand Up @@ -61,6 +62,7 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
private _wheel: Nullable<(p: PointerInfo, s: EventState) => void>;
private _observer: Nullable<Observer<PointerInfo>>;
private _hitPlane: Nullable<Plane>;
private _ray: Nullable<Ray>;
private _viewOffset: Vector3 = new Vector3(0, 0, 0);
private _globalOffset: Vector3 = new Vector3(0, 0, 0);

Expand Down Expand Up @@ -167,6 +169,13 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
const camera = this.camera;
const motion = 0.0 + camera.inertialAlphaOffset + camera.inertialBetaOffset + camera.inertialRadiusOffset;
if (motion) {
//If the user manually set the camera target before
//in order to ensure the normal use of this function
//you need to automatically calculate the correct camera in this mode.
if (camera.targetSetManually && camera.inertialRadiusOffset) {
this._updateCameraTarget();
}

// if zooming is still happening as a result of inertia, then we also need to update
// the hit plane.
this._updateHitPlane();
Expand Down Expand Up @@ -202,31 +211,47 @@ export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCam
this._hitPlane = Plane.FromPositionAndNormal(camera.target, direction);
}

// Get position on the hit plane
private _getPosition(): Vector3 {
//Reset the camera's target (the direction of sight and the focus of the ground).
private _updateCameraTarget() {
const camera = this.camera;
ecojust marked this conversation as resolved.
Show resolved Hide resolved
const scene = camera.getScene();
const direction = TmpVectors.Vector3[6];
camera.target.subtractToRef(camera.position, direction);
this._ray = new Ray(camera.position, direction.normalize(), Number.MAX_SAFE_INTEGER);
this._hitPlane = Plane.FromPositionAndNormal(Vector3.Zero(), camera.upVector);
const distance = this._getIntersectionDistance(this._ray, this._hitPlane, false);
camera.setTarget(this._ray.origin.add(this._ray.direction.scale(distance)));
}

// since the _hitPlane is always updated to be orthogonal to the camera position vector
// we don't have to worry about this ray shooting off to infinity. This ray creates
// a vector defining where we want to zoom to.
const ray = scene.createPickingRay(scene.pointerX, scene.pointerY, Matrix.Identity(), camera, false);
// Get the distance from the ray to the point of intersection with the plane
private _getIntersectionDistance(ray: Ray, plane: Nullable<Plane>, considerOffset: Boolean): number {
const camera = this.camera;
// Since the camera is the origin of the picking ray, we need to offset it by the camera's offset manually
// 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) {
if (considerOffset && (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;
if (plane) {
distance = ray.intersectsPlane(plane) ?? 0;
}
return distance;
}

// Get position on the hit plane
private _getPosition(): Vector3 {
const camera = this.camera;
const scene = camera.getScene();

// since the _hitPlane is always updated to be orthogonal to the camera position vector
// we don't have to worry about this ray shooting off to infinity. This ray creates
// a vector defining where we want to zoom to.
this._ray = scene.createPickingRay(scene.pointerX, scene.pointerY, Matrix.Identity(), camera, false);
const distance = this._getIntersectionDistance(this._ray, this._hitPlane, true);
// not using this ray again, so modifying its vectors here is fine
return ray.origin.addInPlace(ray.direction.scaleInPlace(distance));
return this._ray.origin.addInPlace(this._ray.direction.scaleInPlace(distance));
}

private _inertialPanning: Vector3 = Vector3.Zero();
Expand Down
18 changes: 16 additions & 2 deletions packages/dev/core/src/Cameras/arcRotateCamera.ts
Expand Up @@ -62,6 +62,8 @@ export class ArcRotateCamera extends TargetCamera {
protected _target: Vector3;
@serializeAsMeshReference("targetHost")
protected _targetHost: Nullable<TransformNode>;
@serialize("targetSetManually")
protected _targetSetManually: Boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason why this can't just be a public variable instead of a property with a protected variable?

Copy link
Author

Choose a reason for hiding this comment

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

It is considered here that this variable is used in conjunction with other functions and should not be explicitly modified separately
For example, modify this parameter with setTarget


/**
* Defines the target point of the camera.
Expand All @@ -88,6 +90,17 @@ export class ArcRotateCamera extends TargetCamera {
}
}

/**
* Defines the target is Automatic calculation through the program or Setting by the user
* In some scenarios, you need to use this parameter to determine whether the camera target needs to be automatically calculated again
*/
public get targetSetManually(): Boolean {
return this._targetSetManually;
}
public set targetSetManually(value: Boolean) {
this._targetSetManually = value;
}

/**
* Return the current target position of the camera. This value is expressed in local space.
* @returns the target position
Expand Down Expand Up @@ -1083,10 +1096,11 @@ export class ArcRotateCamera extends TargetCamera {
* @param toBoundingCenter In case of a mesh target, defines whether to target the mesh position or its bounding information center
* @param allowSamePosition If false, prevents reapplying the new computed position if it is identical to the current one (optim)
* @param cloneAlphaBetaRadius If true, replicate the current setup (alpha, beta, radius) on the new target
* @param setManually Defines the target is Automatic calculation through the program or Setting by the user
*/
public setTarget(target: TransformNode | Vector3, toBoundingCenter = false, allowSamePosition = false, cloneAlphaBetaRadius = false): void {
public setTarget(target: TransformNode | Vector3, toBoundingCenter = false, allowSamePosition = false, cloneAlphaBetaRadius = false, setManually = false): void {
cloneAlphaBetaRadius = this.overrideCloneAlphaBetaRadius ?? cloneAlphaBetaRadius;

this._targetSetManually = setManually;
if ((target as TransformNode).computeWorldMatrix) {
if (toBoundingCenter && (<any>target).getBoundingInfo) {
this._targetBoundingCenter = (<any>target).getBoundingInfo().boundingBox.centerWorld.clone();
Expand Down