@@ -26,10 +26,7 @@ export class WindParticlesComputing {
26
26
private bounds : WindData [ 'bounds' ] ;
27
27
windData : Required < WindData > ;
28
28
private frameRate : number = 60 ;
29
- private lastFrameTime : number = 0 ;
30
- private frameRateUpdateInterval : number = 500 ; // Update frame rate every 500ms
31
- private frameCount : number = 0 ;
32
- private frameRateStartTime : number = 0 ;
29
+ private frameRateAdjustment : number = 1 ;
33
30
34
31
constructor ( context : any , windData : Required < WindData > , options : WindLayerOptions , viewerParameters : any ) {
35
32
this . context = context ;
@@ -38,22 +35,35 @@ export class WindParticlesComputing {
38
35
this . bounds = windData . bounds ;
39
36
this . windData = windData ;
40
37
41
- this . frameRateStartTime = performance . now ( ) ;
38
+ this . initFrameRate ( ) ;
42
39
this . createWindTextures ( ) ;
43
40
this . createParticlesTextures ( ) ;
44
41
this . createComputingPrimitives ( ) ;
45
42
}
46
43
47
- private updateFrameRate ( ) {
48
- const currentTime = performance . now ( ) ;
49
- this . frameCount ++ ;
44
+ private initFrameRate ( ) {
45
+ let times : number [ ] = [ ] ;
46
+ let frameCount = 0 ;
50
47
51
- // Update frame rate every 500ms
52
- if ( currentTime - this . frameRateStartTime >= this . frameRateUpdateInterval ) {
53
- this . frameRate = Math . round ( ( this . frameCount * 1000 ) / ( currentTime - this . frameRateStartTime ) ) ;
54
- this . frameCount = 0 ;
55
- this . frameRateStartTime = currentTime ;
56
- }
48
+ const measureFrameRate = ( timestamp : number ) => {
49
+ times . push ( timestamp ) ;
50
+ frameCount ++ ;
51
+
52
+ // Keep only the times in the last second
53
+ const now = timestamp ;
54
+ times = times . filter ( t => now - t < 1000 ) ;
55
+
56
+ if ( frameCount >= 120 ) { // Measure over more frames for better accuracy
57
+ // Calculate FPS based on the number of frames in the last second
58
+ this . frameRate = Math . round ( times . length ) ;
59
+ this . frameRateAdjustment = 60 / this . frameRate ;
60
+ return ;
61
+ }
62
+
63
+ requestAnimationFrame ( measureFrameRate ) ;
64
+ } ;
65
+
66
+ requestAnimationFrame ( measureFrameRate ) ;
57
67
}
58
68
59
69
createWindTextures ( ) {
@@ -136,9 +146,7 @@ export class WindParticlesComputing {
136
146
speedRange : ( ) => new Cartesian2 ( this . windData . speed . min , this . windData . speed . max ) ,
137
147
currentParticlesPosition : ( ) => this . particlesTextures . currentParticlesPosition ,
138
148
speedScaleFactor : ( ) => {
139
- this . updateFrameRate ( ) ;
140
- const frameRateAdjustment = 60 / this . frameRate ;
141
- return ( this . viewerParameters . pixelSize + 50 ) * this . options . speedFactor * frameRateAdjustment ;
149
+ return ( this . viewerParameters . pixelSize + 50 ) * this . options . speedFactor * this . frameRateAdjustment ;
142
150
} ,
143
151
dimension : ( ) => dimension ,
144
152
minimum : ( ) => minimum ,
0 commit comments