bilca commited on
Commit
5acfa5b
·
verified ·
1 Parent(s): 2c24aec

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +33 -17
index.js CHANGED
@@ -284,7 +284,7 @@
284
  const camera = new SPLAT.Camera();
285
  const controls = new SPLAT.OrbitControls(camera, canvas);
286
 
287
- // Store the controls instance if needed.
288
  controlsInstance = controls;
289
  cameraInstance = camera;
290
 
@@ -293,25 +293,46 @@
293
  controls.minZoom = minZoom;
294
  controls.minAngle = minAngle;
295
  controls.maxAngle = maxAngle;
296
- // Pass the new azimuth limits to the controls (for reference, though the library doesn’t natively use them)
297
  controls.minAzimuth = minAzimuth;
298
  controls.maxAzimuth = maxAzimuth;
299
 
300
- // Monkey patch the update method to clamp horizontal (azimuth) rotation.
301
- // We assume that camera.rotation is a Quaternion that can be converted to Euler angles.
302
- // The azimuth (horizontal rotation) is assumed to be the Y component in Euler angles.
303
  const { Quaternion } = SPLAT;
304
  const originalUpdate = controls.update.bind(controls);
305
  controls.update = () => {
306
  originalUpdate();
307
- // Convert current rotation to Euler angles.
 
 
 
 
 
 
 
 
 
 
308
  let euler = camera.rotation.toEuler();
309
- // Convert the azimuth limits from degrees to radians.
310
- let minAzimuthRad = (minAzimuth * Math.PI) / 180;
311
- let maxAzimuthRad = (maxAzimuth * Math.PI) / 180;
312
- // Clamp the horizontal angle (euler.y).
313
- euler.y = Math.min(Math.max(euler.y, minAzimuthRad), maxAzimuthRad);
314
- // Update the camera rotation based on the clamped Euler.
 
 
 
 
 
 
 
 
 
 
 
315
  camera.rotation = Quaternion.FromEuler(euler);
316
  };
317
 
@@ -319,11 +340,6 @@
319
  initialCameraRotation = camera.rotation.clone();
320
 
321
  canvas.style.background = "#FEFEFD";
322
- controls.maxZoom = maxZoom;
323
- controls.minZoom = minZoom;
324
- controls.minAngle = minAngle;
325
- controls.maxAngle = maxAngle;
326
-
327
  controls.update();
328
 
329
  try {
 
284
  const camera = new SPLAT.Camera();
285
  const controls = new SPLAT.OrbitControls(camera, canvas);
286
 
287
+ // Store the controls and camera instances.
288
  controlsInstance = controls;
289
  cameraInstance = camera;
290
 
 
293
  controls.minZoom = minZoom;
294
  controls.minAngle = minAngle;
295
  controls.maxAngle = maxAngle;
296
+ // Pass the new azimuth limits to the controls (for reference)
297
  controls.minAzimuth = minAzimuth;
298
  controls.maxAzimuth = maxAzimuth;
299
 
300
+ // --- Monkey Patch: Throttled Damping on Azimuth Rotation ---
301
+ let throttleCounter = 0;
302
+ const throttleDelta = 2; // Apply azimuth clamping every 2 frames.
303
  const { Quaternion } = SPLAT;
304
  const originalUpdate = controls.update.bind(controls);
305
  controls.update = () => {
306
  originalUpdate();
307
+ throttleCounter++;
308
+ if (throttleCounter % throttleDelta !== 0) return;
309
+
310
+ // If azimuth limits are not effectively set, skip extra work.
311
+ if (minAzimuth <= -360 && maxAzimuth >= 360) return;
312
+
313
+ const minAzimuthRad = (minAzimuth * Math.PI) / 180;
314
+ const maxAzimuthRad = (maxAzimuth * Math.PI) / 180;
315
+ const threshold = 0.2; // threshold (in radians) for damping
316
+
317
+ // Convert the current camera rotation (Quaternion) to Euler angles.
318
  let euler = camera.rotation.toEuler();
319
+
320
+ // Compute the clamped target for azimuth (assumed on Y axis).
321
+ const clampedY = Math.min(Math.max(euler.y, minAzimuthRad), maxAzimuthRad);
322
+
323
+ // Compute damping factor when near the limits.
324
+ let damping = 1; // default: no damping
325
+ if (euler.y < minAzimuthRad + threshold) {
326
+ damping = (euler.y - minAzimuthRad) / threshold;
327
+ } else if (euler.y > maxAzimuthRad - threshold) {
328
+ damping = (maxAzimuthRad - euler.y) / threshold;
329
+ }
330
+ damping = Math.max(0, Math.min(1, damping));
331
+
332
+ // Blend current azimuth with the clamped target.
333
+ euler.y = damping * euler.y + (1 - damping) * clampedY;
334
+
335
+ // Update the camera rotation with the modified Euler angles.
336
  camera.rotation = Quaternion.FromEuler(euler);
337
  };
338
 
 
340
  initialCameraRotation = camera.rotation.clone();
341
 
342
  canvas.style.background = "#FEFEFD";
 
 
 
 
 
343
  controls.update();
344
 
345
  try {