Skip to content

Commit 7486fb6

Browse files
committed
fix: optimize frame rate calculation and shader performance
- Remove redundant interval uniform from shader - Calculate interval directly in shader to reduce data transfer - Only update frame rate when FPS > 50 to avoid performance degradation - Replace requestAnimationFrame with setInterval for stable frame rate updates
1 parent e0fffb9 commit 7486fb6

File tree

6 files changed

+47
-33
lines changed

6 files changed

+47
-33
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.4.5
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- cesium-wind-layer@0.7.4
9+
310
## 0.4.4
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.4.4",
4+
"version": "0.4.5",
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.7.4
4+
5+
### Patch Changes
6+
7+
- fix: optimize frame rate calculation and shader performance
8+
9+
- Remove redundant interval uniform from shader
10+
- Calculate interval directly in shader to reduce data transfer
11+
- Only update frame rate when FPS > 50 to avoid performance degradation
12+
- Replace requestAnimationFrame with setInterval for stable frame rate updates
13+
314
## 0.7.3
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.7.3",
3+
"version": "0.7.4",
44
"publishConfig": {
55
"access": "public"
66
},

packages/cesium-wind-layer/src/shaders/calculateSpeed.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ uniform vec2 speedRange; // (min, max)
1111
uniform vec2 dimension; // (lon, lat)
1212
uniform vec2 minimum; // minimum of each dimension
1313
uniform vec2 maximum; // maximum of each dimension
14-
uniform vec2 interval; // interval of each dimension
1514
1615
uniform float speedScaleFactor;
1716
1817
in vec2 v_textureCoordinates;
1918
19+
vec2 getInterval(vec2 maximum, vec2 minimum, vec2 dimension) {
20+
return (maximum - minimum) / (dimension - 1.0);
21+
}
22+
2023
vec2 mapPositionToNormalizedIndex2D(vec2 lonLat) {
2124
// ensure the range of longitude and latitude
2225
lonLat.x = clamp(lonLat.x, minimum.x, maximum.x);
2326
lonLat.y = clamp(lonLat.y, minimum.y, maximum.y);
2427
28+
vec2 interval = getInterval(maximum, minimum, dimension);
29+
2530
vec2 index2D = vec2(0.0);
2631
index2D.x = (lonLat.x - minimum.x) / interval.x;
2732
index2D.y = (lonLat.y - minimum.y) / interval.y;
@@ -47,6 +52,8 @@ vec2 bilinearInterpolation(vec2 lonLat) {
4752
float lon = lonLat.x;
4853
float lat = lonLat.y;
4954
55+
vec2 interval = getInterval(maximum, minimum, dimension);
56+
5057
// Calculate grid cell coordinates
5158
float lon0 = floor(lon / interval.x) * interval.x;
5259
float lon1 = lon0 + interval.x;

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

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export class WindParticlesComputing {
2424
updatePosition: CustomPrimitive;
2525
postProcessingPosition: CustomPrimitive;
2626
};
27-
private bounds: WindData['bounds'];
2827
windData: Required<WindData>;
2928
private frameRateMonitor: FrameRateMonitor;
3029
private frameRate: number = 60;
@@ -34,7 +33,6 @@ export class WindParticlesComputing {
3433
this.context = context;
3534
this.options = options;
3635
this.viewerParameters = viewerParameters;
37-
this.bounds = windData.bounds;
3836
this.windData = windData;
3937

4038
this.frameRateMonitor = new FrameRateMonitor({
@@ -49,27 +47,20 @@ export class WindParticlesComputing {
4947
}
5048

5149
private initFrameRate() {
52-
let lastUpdate = performance.now();
53-
const updateInterval = 1000; // Update every 1000ms (1 second)
54-
5550
const updateFrameRate = () => {
56-
const currentTime = performance.now();
57-
const deltaTime = currentTime - lastUpdate;
58-
59-
// Only update frame rate once per second
60-
if (deltaTime >= updateInterval) {
61-
if (this.frameRateMonitor.lastFramesPerSecond) {
62-
this.frameRate = this.frameRateMonitor.lastFramesPerSecond;
63-
this.frameRateAdjustment = 60 / Math.max(this.frameRate, 1);
64-
}
65-
lastUpdate = currentTime;
51+
// avoid update frame rate when frame rate is too low
52+
if (this.frameRateMonitor.lastFramesPerSecond > 50) {
53+
this.frameRate = this.frameRateMonitor.lastFramesPerSecond;
54+
this.frameRateAdjustment = 60 / Math.max(this.frameRate, 1);
6655
}
56+
}
6757

68-
requestAnimationFrame(updateFrameRate);
69-
};
70-
58+
// Initial frame rate calculation
7159
updateFrameRate();
7260

61+
// Use setInterval instead of requestAnimationFrame
62+
const intervalId = setInterval(updateFrameRate, 1000);
63+
7364
// Monitor frame rate changes
7465
this.frameRateMonitor.lowFrameRate.addEventListener((scene, frameRate) => {
7566
console.warn(`Low frame rate detected: ${frameRate} FPS`);
@@ -78,6 +69,13 @@ export class WindParticlesComputing {
7869
this.frameRateMonitor.nominalFrameRate.addEventListener((scene, frameRate) => {
7970
console.log(`Frame rate returned to normal: ${frameRate} FPS`);
8071
});
72+
73+
// Add cleanup method to destroy
74+
const originalDestroy = this.destroy.bind(this);
75+
this.destroy = () => {
76+
clearInterval(intervalId);
77+
originalDestroy();
78+
};
8179
}
8280

8381
createWindTextures() {
@@ -141,14 +139,6 @@ export class WindParticlesComputing {
141139
}
142140

143141
createComputingPrimitives() {
144-
const dimension = new Cartesian2(this.windData.width, this.windData.height);
145-
const minimum = new Cartesian2(this.bounds.west, this.bounds.south);
146-
const maximum = new Cartesian2(this.bounds.east, this.bounds.north);
147-
const interval = new Cartesian2(
148-
(maximum.x - minimum.x) / (dimension.x - 1),
149-
(maximum.y - minimum.y) / (dimension.y - 1)
150-
);
151-
152142
this.primitives = {
153143
calculateSpeed: new CustomPrimitive({
154144
commandType: 'Compute',
@@ -162,10 +152,9 @@ export class WindParticlesComputing {
162152
speedScaleFactor: () => {
163153
return (this.viewerParameters.pixelSize + 50) * this.options.speedFactor * this.frameRateAdjustment;
164154
},
165-
dimension: () => dimension,
166-
minimum: () => minimum,
167-
maximum: () => maximum,
168-
interval: () => interval,
155+
dimension: () => new Cartesian2(this.windData.width, this.windData.height),
156+
minimum: () => new Cartesian2(this.windData.bounds.west, this.windData.bounds.south),
157+
maximum: () => new Cartesian2(this.windData.bounds.east, this.windData.bounds.north),
169158
},
170159
fragmentShaderSource: ShaderManager.getCalculateSpeedShader(),
171160
outputTexture: this.particlesTextures.particlesSpeed,

0 commit comments

Comments
 (0)