MikaFil commited on
Commit
f7836d7
·
verified ·
1 Parent(s): cfc39a2

Update tooltips.js

Browse files
Files changed (1) hide show
  1. tooltips.js +35 -30
tooltips.js CHANGED
@@ -155,43 +155,45 @@ export async function initializeTooltips(options) {
155
  const targetPos = tooltipEnt.getPosition().clone();
156
  // Compute current state
157
  const startPivot = orbitCam.pivotPoint.clone();
158
- const startYaw = orbitCam._yaw;
159
- const startPitch = orbitCam._pitch;
160
- const startDist = orbitCam._distance;
161
 
162
  // Compute direction & candidate distance:
163
  const worldRadius = 0.5 * tooltipEnt.getLocalScale().x;
164
  const minZoom = orbitCam.distanceMin;
165
  const desiredDistance = Math.max(minZoom * 1.2, worldRadius * 4);
166
 
167
- // Compute target yaw/pitch from camera pointing at targetPos
168
- // Reuse reset logic: place a temp entity at camera’s current position, have it look at target
169
  const camWorldPos = cameraEntity.getPosition().clone();
170
  const tempEnt = new pc.Entity();
171
  tempEnt.setPosition(camWorldPos);
172
  tempEnt.lookAt(targetPos);
173
  const rotation = tempEnt.getRotation();
174
- const forward = new pc.Vec3();
175
- rotation.transformVector(pc.Vec3.FORWARD, forward);
176
- // Raw yaw and pitch from tempEnt
177
- let tgtYaw = Math.atan2(-forward.x, -forward.z) * pc.math.RAD_TO_DEG;
178
- // Compute pitch (the _calcPitch logic):
179
- const yawQuat = new pc.Quat().setFromEulerAngles(0, -tgtYaw, 0);
180
  const rotNoYaw = new pc.Quat().mul2(yawQuat, rotation);
181
  const fNoYaw = new pc.Vec3();
182
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
183
- let tgtPitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
 
184
  tempEnt.destroy();
185
 
186
- // Adjust yaw to follow the shortest path from current yaw to target yaw
187
- const currentYaw = startYaw;
188
- let deltaYaw = tgtYaw - currentYaw;
189
- // Wrap into [-180, +180]
190
- deltaYaw = ((deltaYaw + 180) % 360) - 180;
191
- const endYaw = currentYaw + deltaYaw;
 
 
 
192
 
193
- // For pitch, since it’s usually within [-90, +90], direct assignment is fine
194
- const endPitch = tgtPitch;
195
  const endDist = desiredDistance;
196
 
197
  let elapsed = 0;
@@ -215,17 +217,20 @@ export async function initializeTooltips(options) {
215
  const newPivot = new pc.Vec3().lerp(orgPivot, targetPos, t);
216
  orbitCam.pivotPoint.copy(newPivot);
217
 
218
- // Interpolate yaw/pitch/distance (simple lerp)
219
- const newYaw = pc.math.lerp(orgYaw, endYaw, t);
 
220
  const newPitch = pc.math.lerp(orgPitch, endPitch, t);
221
- const newDist = pc.math.lerp(orgDist, endDist, t);
222
-
223
- orbitCam._targetYaw = newYaw;
224
- orbitCam._yaw = newYaw;
225
- orbitCam._targetPitch = newPitch;
226
- orbitCam._pitch = newPitch;
227
- orbitCam._targetDistance = newDist;
228
- orbitCam._distance = newDist;
 
 
229
 
230
  orbitCam._updatePosition();
231
 
 
155
  const targetPos = tooltipEnt.getPosition().clone();
156
  // Compute current state
157
  const startPivot = orbitCam.pivotPoint.clone();
158
+ const startYaw = orbitCam._yaw; // current yaw in degrees
159
+ const startPitch = orbitCam._pitch; // current pitch in degrees
160
+ const startDist = orbitCam._distance; // current distance
161
 
162
  // Compute direction & candidate distance:
163
  const worldRadius = 0.5 * tooltipEnt.getLocalScale().x;
164
  const minZoom = orbitCam.distanceMin;
165
  const desiredDistance = Math.max(minZoom * 1.2, worldRadius * 4);
166
 
167
+ // Compute raw desired yaw/pitch from camera pointing at targetPos
168
+ // Create a temporary entity to compute the correct orientation
169
  const camWorldPos = cameraEntity.getPosition().clone();
170
  const tempEnt = new pc.Entity();
171
  tempEnt.setPosition(camWorldPos);
172
  tempEnt.lookAt(targetPos);
173
  const rotation = tempEnt.getRotation();
174
+
175
+ // Use OrbitCamera's own _calcYaw to get a yaw in [-180, 180]
176
+ const rawDesiredYaw = orbitCam._calcYaw(rotation);
177
+
178
+ // Compute raw desired pitch using same approach as OrbitCamera._calcPitch
179
+ const yawQuat = new pc.Quat().setFromEulerAngles(0, -rawDesiredYaw, 0);
180
  const rotNoYaw = new pc.Quat().mul2(yawQuat, rotation);
181
  const fNoYaw = new pc.Vec3();
182
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
183
+ const rawDesiredPitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
184
+
185
  tempEnt.destroy();
186
 
187
+ // Compute shortest-path delta for yaw: wrap difference into [-180, 180]
188
+ let deltaYaw = rawDesiredYaw - startYaw;
189
+ deltaYaw = ((deltaYaw + 180) % 360) - 180; // now in [-180, 180]
190
+
191
+ // endYaw remains continuous: startYaw + deltaYaw (no further wrapping)
192
+ const endYaw = startYaw + deltaYaw;
193
+
194
+ // For pitch, we can assign rawDesiredPitch directly; it's within valid range
195
+ const endPitch = rawDesiredPitch;
196
 
 
 
197
  const endDist = desiredDistance;
198
 
199
  let elapsed = 0;
 
217
  const newPivot = new pc.Vec3().lerp(orgPivot, targetPos, t);
218
  orbitCam.pivotPoint.copy(newPivot);
219
 
220
+ // Interpolate yaw numerically: since endYaw is continuous relative to startYaw
221
+ const newYaw = pc.math.lerp(orgYaw, endYaw, t);
222
+ // Interpolate pitch normally (no wrapping needed)
223
  const newPitch = pc.math.lerp(orgPitch, endPitch, t);
224
+ // Interpolate distance
225
+ const newDist = pc.math.lerp(orgDist, endDist, t);
226
+
227
+ // Assign to orbitCam so that _updatePosition() uses them
228
+ orbitCam._yaw = newYaw;
229
+ orbitCam._targetYaw = endYaw;
230
+ orbitCam._pitch = newPitch;
231
+ orbitCam._targetPitch = endPitch;
232
+ orbitCam._distance = newDist;
233
+ orbitCam._targetDistance = endDist;
234
 
235
  orbitCam._updatePosition();
236