Skip to content

Commit e400fbe

Browse files
georginahalpernGeorgina Halpern
andauthored
[GeospatialCamera] Normalize yaw/pitch [-pi, pi) (#17513)
- Normalize yaw/pitch and avoid unnecessary setOrientation call if the set value is the same as current value #17451 Co-authored-by: Georgina Halpern <[email protected]>
1 parent 4e59372 commit e400fbe

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

packages/dev/core/src/Cameras/geospatialCamera.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GeospatialLimits } from "./Limits/geospatialLimits";
99
import { ClampCenterFromPolesInPlace, ComputeLocalBasisToRefs, GeospatialCameraMovement } from "./geospatialCameraMovement";
1010
import type { IVector3Like } from "../Maths/math.like";
1111
import { Vector3CopyToRef, Vector3Distance } from "../Maths/math.vector.functions";
12-
import { Clamp } from "../Maths/math.scalar.functions";
12+
import { Clamp, NormalizeRadians } from "../Maths/math.scalar.functions";
1313
import type { AllowedAnimValue } from "../Behaviors/Cameras/interpolatingBehavior";
1414
import { InterpolatingBehavior } from "../Behaviors/Cameras/interpolatingBehavior";
1515
import type { EasingFunction } from "../Animations/easing";
@@ -86,11 +86,11 @@ export class GeospatialCamera extends Camera {
8686
}
8787

8888
/**
89-
* Sets the camera's yaw (rotation around the geocentric normal)
89+
* Sets the camera's yaw (rotation around the geocentric normal). Will wrap value to [-π, π)
9090
* @param yaw The desired yaw angle in radians (0 = north, π/2 = east)
9191
*/
9292
public set yaw(yaw: number) {
93-
this._setOrientation(yaw, this.pitch, this.radius, this.center);
93+
yaw !== this._yaw && this._setOrientation(yaw, this.pitch, this.radius, this.center);
9494
}
9595

9696
private _pitch: number = 0;
@@ -107,11 +107,11 @@ export class GeospatialCamera extends Camera {
107107
}
108108

109109
/**
110-
* Sets the camera's pitch (angle from looking straight at globe)
110+
* Sets the camera's pitch (angle from looking straight at globe). Will wrap value to [-π, π)
111111
* @param pitch The desired pitch angle in radians (0 = looking at planet center, π/2 = looking at horizon)
112112
*/
113113
public set pitch(pitch: number) {
114-
this._setOrientation(this.yaw, pitch, this.radius, this.center);
114+
pitch !== this._pitch && this._setOrientation(this.yaw, pitch, this.radius, this.center);
115115
}
116116

117117
private _radius: number = 0;
@@ -124,7 +124,7 @@ export class GeospatialCamera extends Camera {
124124
* @param radius The desired radius
125125
*/
126126
public set radius(radius: number) {
127-
this._setOrientation(this.yaw, this.pitch, radius, this.center);
127+
radius !== this._radius && this._setOrientation(this.yaw, this.pitch, radius, this.center);
128128
}
129129

130130
protected _checkLimits() {
@@ -141,8 +141,9 @@ export class GeospatialCamera extends Camera {
141141
private _tempUp = new Vector3();
142142

143143
private _setOrientation(yaw: number, pitch: number, radius: number, center: DeepImmutable<IVector3Like>): void {
144-
this._yaw = yaw;
145-
this._pitch = pitch;
144+
// Wrap yaw and pitch to [-π, π)
145+
this._yaw = NormalizeRadians(yaw);
146+
this._pitch = NormalizeRadians(pitch);
146147
this._radius = radius;
147148

148149
Vector3CopyToRef(center, this._center);
@@ -205,8 +206,8 @@ export class GeospatialCamera extends Camera {
205206
public updateFlyToDestination(targetYaw?: number, targetPitch?: number, targetRadius?: number, targetCenter?: Vector3): void {
206207
this._flyToTargets.clear();
207208

208-
this._flyToTargets.set("yaw", targetYaw);
209-
this._flyToTargets.set("pitch", targetPitch);
209+
this._flyToTargets.set("yaw", targetYaw != undefined ? NormalizeRadians(targetYaw) : undefined);
210+
this._flyToTargets.set("pitch", targetPitch != undefined ? NormalizeRadians(targetPitch) : undefined);
210211
this._flyToTargets.set("radius", targetRadius);
211212
this._flyToTargets.set("center", targetCenter);
212213

@@ -235,8 +236,8 @@ export class GeospatialCamera extends Camera {
235236
): Promise<void> {
236237
this._flyToTargets.clear();
237238

238-
this._flyToTargets.set("yaw", targetYaw);
239-
this._flyToTargets.set("pitch", targetPitch);
239+
this._flyToTargets.set("yaw", targetYaw !== undefined ? NormalizeRadians(targetYaw) : undefined);
240+
this._flyToTargets.set("pitch", targetPitch !== undefined ? NormalizeRadians(targetPitch) : undefined);
240241
this._flyToTargets.set("radius", targetRadius);
241242
this._flyToTargets.set("center", targetCenter);
242243

0 commit comments

Comments
 (0)