Skip to content

Commit 75c2e93

Browse files
committed
FIX PERF fix critical perf issues with many waypoints
1 parent 2da6ef5 commit 75c2e93

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

renderer/viewer/three/waypointSprite.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export function createWaypointSprite (options: {
6161
sprite.renderOrder = 10
6262
let currentLabel = options.label ?? ''
6363

64+
// Performance optimization: cache distance text to avoid unnecessary updates
65+
let lastDistanceText = '0m'
66+
let lastDistance = 0
67+
6468
// Offscreen arrow (detached by default)
6569
let arrowSprite: THREE.Sprite | undefined
6670
let arrowParent: THREE.Object3D | null = null
@@ -94,6 +98,12 @@ export function createWaypointSprite (options: {
9498
}
9599

96100
function updateDistanceText (label: string, distanceText: string) {
101+
// Performance optimization: only update if distance text actually changed
102+
if (distanceText === lastDistanceText) {
103+
return
104+
}
105+
lastDistanceText = distanceText
106+
97107
const canvas = drawCombinedCanvas(color, label, distanceText)
98108
const texture = new THREE.CanvasTexture(canvas)
99109
const mat = sprite.material
@@ -288,8 +298,14 @@ export function createWaypointSprite (options: {
288298
const distance = computeDistance(cameraPosition)
289299
// Keep constant pixel size
290300
updateScaleScreenPixels(cameraPosition, camera.fov, distance, viewportHeightPx)
291-
// Update text
292-
updateDistanceText(currentLabel, `${Math.round(distance)}m`)
301+
302+
// Performance optimization: only update distance text if distance changed significantly
303+
const roundedDistance = Math.round(distance)
304+
if (Math.abs(roundedDistance - lastDistance) >= 1) {
305+
lastDistance = roundedDistance
306+
updateDistanceText(currentLabel, `${roundedDistance}m`)
307+
}
308+
293309
// Update arrow and visibility
294310
const onScreen = updateOffscreenArrow(camera, viewportWidthPx, viewportHeightPx)
295311
setVisible(onScreen)

renderer/viewer/three/waypoints.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,31 @@ export class WaypointsRenderer {
2424
private readonly waypoints = new Map<string, Waypoint>()
2525
private readonly waypointScene = new THREE.Scene()
2626

27+
// Performance optimization: cache camera position to reduce update frequency
28+
private readonly lastCameraPosition = new THREE.Vector3()
29+
private lastUpdateTime = 0
30+
private readonly UPDATE_THROTTLE_MS = 16 // ~60fps max update rate
31+
2732
constructor (
2833
private readonly worldRenderer: WorldRendererThree
2934
) {
3035
}
3136

3237
private updateWaypoints () {
38+
const currentTime = performance.now()
3339
const playerPos = this.worldRenderer.cameraObject.position
40+
41+
// Performance optimization: throttle updates and check for significant camera movement
42+
const cameraMovedSignificantly = this.lastCameraPosition.distanceTo(playerPos) > 0.5
43+
const timeToUpdate = currentTime - this.lastUpdateTime > this.UPDATE_THROTTLE_MS
44+
45+
if (!cameraMovedSignificantly && !timeToUpdate) {
46+
return // Skip update if camera hasn't moved much and not enough time passed
47+
}
48+
49+
this.lastCameraPosition.copy(playerPos)
50+
this.lastUpdateTime = currentTime
51+
3452
const sizeVec = this.worldRenderer.renderer.getSize(new THREE.Vector2())
3553

3654
for (const waypoint of this.waypoints.values()) {
@@ -112,6 +130,8 @@ export class WaypointsRenderer {
112130
this.addWaypoint('Test Point', 0, 70, 0, { color: 0x00_FF_00, label: 'Test Point' })
113131
this.addWaypoint('Spawn', 0, 64, 0, { color: 0xFF_FF_00, label: 'Spawn' })
114132
this.addWaypoint('Far Point', 100, 70, 100, { color: 0x00_00_FF, label: 'Far Point' })
133+
this.addWaypoint('Far Point 2', 180, 170, 100, { color: 0x00_00_FF, label: 'Far Point 2' })
134+
this.addWaypoint('Far Point 3', 1000, 100, 1000, { color: 0x00_00_FF, label: 'Far Point 3' })
115135
}
116136

117137
getWaypoint (id: string): Waypoint | undefined {

0 commit comments

Comments
 (0)