Skip to content

Commit

Permalink
fix: adjust particle speed based on actual frame rate
Browse files Browse the repository at this point in the history
- Add real-time frame rate measurement to normalize particle speed
- Update frame rate calculation every 500ms for better performance
- Apply frame rate adjustment (60/fps) to maintain consistent particle movement speed across different devices
  • Loading branch information
hongfaqiu committed Oct 29, 2024
1 parent dc0b017 commit b0abf43
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
7 changes: 7 additions & 0 deletions example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# example

## 0.3.5

### Patch Changes

- Updated dependencies
- [email protected]

## 0.3.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "example",
"private": true,
"version": "0.3.4",
"version": "0.3.5",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
10 changes: 10 additions & 0 deletions packages/cesium-wind-layer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# cesium-wind-layer

## 0.5.1

### Patch Changes

- fix: adjust particle speed based on actual frame rate

- Add real-time frame rate measurement to normalize particle speed
- Update frame rate calculation every 500ms for better performance
- Apply frame rate adjustment (60/fps) to maintain consistent particle movement speed across different devices

## 0.5.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cesium-wind-layer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cesium-wind-layer",
"version": "0.5.0",
"version": "0.5.1",
"publishConfig": {
"access": "public"
},
Expand Down
25 changes: 23 additions & 2 deletions packages/cesium-wind-layer/src/windParticlesComputing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class WindParticlesComputing {
};
private bounds: WindData['bounds'];
windData: Required<WindData>;
private frameRate: number = 60;
private lastFrameTime: number = 0;
private frameRateUpdateInterval: number = 500; // Update frame rate every 500ms
private frameCount: number = 0;
private frameRateStartTime: number = 0;

constructor(context: any, windData: Required<WindData>, options: WindLayerOptions, viewerParameters: any) {
this.context = context;
Expand All @@ -33,11 +38,24 @@ export class WindParticlesComputing {
this.bounds = windData.bounds;
this.windData = windData;

this.frameRateStartTime = performance.now();
this.createWindTextures();
this.createParticlesTextures();
this.createComputingPrimitives();
}

private updateFrameRate() {
const currentTime = performance.now();
this.frameCount++;

// Update frame rate every 500ms
if (currentTime - this.frameRateStartTime >= this.frameRateUpdateInterval) {
this.frameRate = Math.round((this.frameCount * 1000) / (currentTime - this.frameRateStartTime));
this.frameCount = 0;
this.frameRateStartTime = currentTime;
}
}

createWindTextures() {
const options = {
context: this.context,
Expand Down Expand Up @@ -107,7 +125,6 @@ export class WindParticlesComputing {
(maximum.y - minimum.y) / (dimension.y - 1)
);


this.primitives = {
calculateSpeed: new CustomPrimitive({
commandType: 'Compute',
Expand All @@ -118,7 +135,11 @@ export class WindParticlesComputing {
vRange: () => new Cartesian2(this.windData.v.min, this.windData.v.max),
speedRange: () => new Cartesian2(this.windData.speed.min, this.windData.speed.max),
currentParticlesPosition: () => this.particlesTextures.currentParticlesPosition,
speedScaleFactor: () => (this.viewerParameters.pixelSize + 50) * this.options.speedFactor,
speedScaleFactor: () => {
this.updateFrameRate();
const frameRateAdjustment = 60 / this.frameRate;
return (this.viewerParameters.pixelSize + 50) * this.options.speedFactor * frameRateAdjustment;
},
dimension: () => dimension,
minimum: () => minimum,
maximum: () => maximum,
Expand Down

0 comments on commit b0abf43

Please sign in to comment.