Skip to content

Commit 731a358

Browse files
implemented distance based scaling so panel always look the same size regardless of the distance from camera
1 parent 6384d7b commit 731a358

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/model-viewer/src/three-components/XRMenuPanel.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ const PANEL_CONFIG = {
2828
height: 0.07,
2929
cornerRadius: 0.03,
3030
opacity: 1,
31-
color: 0x000000
31+
color: 0x000000,
32+
// Distance-based scaling configuration
33+
minDistance: 0.5, // Minimum distance for scaling (meters)
34+
maxDistance: 10.0, // Maximum distance for scaling (meters)
35+
baseScale: 1.0, // Base scale factor
36+
distanceScaleFactor: 0.3 // How much to scale per meter of distance
3237
} as const;
3338

3439
// Button configuration
@@ -251,6 +256,15 @@ export class XRMenuPanel extends Object3D {
251256
.add(new Vector3(0, offsetUp, 0)) // Move up
252257
.add(directionToCamera.multiplyScalar(offsetForward)); // Move forward
253258
this.position.copy(panelPosition);
259+
260+
// Calculate distance-based scaling
261+
const distanceToCamera = camera.position.distanceTo(panelPosition);
262+
const clampedDistance = Math.max(PANEL_CONFIG.minDistance, Math.min(PANEL_CONFIG.maxDistance, distanceToCamera));
263+
const scaleFactor = PANEL_CONFIG.baseScale + (clampedDistance - PANEL_CONFIG.minDistance) * PANEL_CONFIG.distanceScaleFactor;
264+
265+
// Apply scaling to the entire panel (including buttons)
266+
this.scale.set(scaleFactor, scaleFactor, scaleFactor);
267+
254268
// Make the menu panel face the camera
255269
this.lookAt(camera.position);
256270
}

0 commit comments

Comments
 (0)