Skip to content

Commit 5df7a99

Browse files
committed
fix: calculate frame rate only during initialization
- Move frame rate calculation to initialization phase - Use timestamp array to measure frames in the last second - Calculate FPS once with 120 frames sample for better accuracy - Store frame rate adjustment factor for consistent particle speed
1 parent fb34e43 commit 5df7a99

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

example/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# example
22

3+
## 0.3.7
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- cesium-wind-layer@0.5.3
9+
310
## 0.3.6
411

512
### Patch Changes

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "example",
33
"private": true,
4-
"version": "0.3.6",
4+
"version": "0.3.7",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

packages/cesium-wind-layer/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# cesium-wind-layer
22

3+
## 0.5.3
4+
5+
### Patch Changes
6+
7+
- fix: calculate frame rate only during initialization
8+
9+
- Move frame rate calculation to initialization phase
10+
- Use timestamp array to measure frames in the last second
11+
- Calculate FPS once with 120 frames sample for better accuracy
12+
- Store frame rate adjustment factor for consistent particle speed
13+
314
## 0.5.2
415

516
### Patch Changes

packages/cesium-wind-layer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cesium-wind-layer",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"publishConfig": {
55
"access": "public"
66
},

packages/cesium-wind-layer/src/windParticlesComputing.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ export class WindParticlesComputing {
2626
private bounds: WindData['bounds'];
2727
windData: Required<WindData>;
2828
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;
3330

3431
constructor(context: any, windData: Required<WindData>, options: WindLayerOptions, viewerParameters: any) {
3532
this.context = context;
@@ -38,22 +35,35 @@ export class WindParticlesComputing {
3835
this.bounds = windData.bounds;
3936
this.windData = windData;
4037

41-
this.frameRateStartTime = performance.now();
38+
this.initFrameRate();
4239
this.createWindTextures();
4340
this.createParticlesTextures();
4441
this.createComputingPrimitives();
4542
}
4643

47-
private updateFrameRate() {
48-
const currentTime = performance.now();
49-
this.frameCount++;
44+
private initFrameRate() {
45+
let times: number[] = [];
46+
let frameCount = 0;
5047

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);
5767
}
5868

5969
createWindTextures() {
@@ -136,9 +146,7 @@ export class WindParticlesComputing {
136146
speedRange: () => new Cartesian2(this.windData.speed.min, this.windData.speed.max),
137147
currentParticlesPosition: () => this.particlesTextures.currentParticlesPosition,
138148
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;
142150
},
143151
dimension: () => dimension,
144152
minimum: () => minimum,

0 commit comments

Comments
 (0)