@@ -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