@@ -66,15 +66,29 @@ export function defaultWindowResize (ppl: BasicPipeline, window: RenderWindow, w
66
66
ppl . addDepthStencil ( `ShadowDepth${ id } ` , Format . DEPTH_STENCIL , shadowSize . x , shadowSize . y ) ;
67
67
}
68
68
69
- const _resizedWindows : RenderWindow [ ] = [ ] ;
69
+ // Map to store throttle timers by cameraId
70
+ const throttleTimers = new Map ( ) ;
71
+ // Map to store last resize time by cameraId
72
+ let lastResizeTimes = 0 ;
70
73
74
+ // Throttle delay in milliseconds (when detected frequent resizing)
75
+ const THROTTLE_DELAY = 500 ;
76
+ // Window time in milliseconds to detect frequent resizing
77
+ const FREQUENCY_WINDOW = 200 ;
78
+
79
+ // Adaptive throttled version of dispatchResizeEvents function using cameraId
71
80
export function dispatchResizeEvents ( cameras : Camera [ ] , builder : PipelineBuilder , ppl : BasicPipeline ) : void {
72
- if ( ! builder . windowResize ) {
81
+ if ( ! builder . windowResize || ! cameras . some ( ( camera ) => camera . window . isRenderWindowResized ( ) ) ) {
73
82
// No game window resize handler defined.
74
- // Following old prodecure , do nothing
83
+ // Following old procedure , do nothing
75
84
return ;
76
85
}
77
-
86
+ const now = Date . now ( ) ;
87
+ let isFrequentResizing = false ;
88
+ if ( now - lastResizeTimes <= FREQUENCY_WINDOW ) {
89
+ isFrequentResizing = true ;
90
+ }
91
+ lastResizeTimes = now ;
78
92
// Resize all windows.
79
93
// Notice: A window might be resized multiple times with different cameras.
80
94
// User should avoid resource collision between different cameras.
@@ -83,22 +97,29 @@ export function dispatchResizeEvents (cameras: Camera[], builder: PipelineBuilde
83
97
continue ;
84
98
}
85
99
86
- const width = Math . max ( Math . floor ( camera . window . width ) , 1 ) ;
87
- const height = Math . max ( Math . floor ( camera . window . height ) , 1 ) ;
100
+ const cameraId = camera . cameraId ; // Unique identifier for each camera
88
101
89
- builder . windowResize ( ppl , camera . window , camera , width , height ) ;
102
+ // Skip execution if a timer exists and we're in frequent resizing mode
103
+ if ( isFrequentResizing && throttleTimers . has ( cameraId ) ) {
104
+ continue ;
105
+ }
90
106
91
- _resizedWindows . push ( camera . window ) ;
92
- }
107
+ const width = Math . max ( Math . floor ( camera . window . width ) , 1 ) ;
108
+ const height = Math . max ( Math . floor ( camera . window . height ) , 1 ) ;
93
109
94
- // Reset resize flags
95
- for ( const window of _resizedWindows ) {
96
- window . setRenderWindowResizeHandled ( ) ;
110
+ if ( isFrequentResizing ) {
111
+ // Apply throttle only when frequent resizing is detected
112
+ throttleTimers . set ( cameraId , setTimeout ( ( ) => {
113
+ builder . windowResize ! ( ppl , camera . window , camera , width , height ) ;
114
+ camera . window . setRenderWindowResizeHandled ( ) ;
115
+ throttleTimers . delete ( cameraId ) ;
116
+ } , THROTTLE_DELAY ) ) ;
117
+ } else {
118
+ // Normal resize - execute immediately
119
+ builder . windowResize ( ppl , camera . window , camera , width , height ) ;
120
+ camera . window . setRenderWindowResizeHandled ( ) ;
121
+ }
97
122
}
98
-
99
- // Clear resized windows
100
- _resizedWindows . length = 0 ;
101
-
102
123
// For editor preview
103
124
forceResize = false ;
104
125
}
0 commit comments