Skip to content

Commit

Permalink
fix: normalize particle speed and length across different refresh rates
Browse files Browse the repository at this point in the history
- Add frameRateAdjustment uniform to normalize particle movement
- Adjust trail length calculation based on frame rate
- Ensure consistent particle behavior regardless of display refresh rate
  • Loading branch information
hongfaqiu committed Nov 1, 2024
1 parent 6d42f4b commit 3fdaa14
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 9 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.4.7

### Patch Changes

- Updated dependencies
- [email protected]

## 0.4.6

### 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.4.6",
"version": "0.4.7",
"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.7.6

### Patch Changes

- fix: normalize particle speed and length across different refresh rates

- Add frameRateAdjustment uniform to normalize particle movement
- Adjust trail length calculation based on frame rate
- Ensure consistent particle behavior regardless of display refresh rate

## 0.7.5

### Patch 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.7.5",
"version": "0.7.6",
"publishConfig": {
"access": "public"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cesium-wind-layer/src/shaders/calculateSpeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uniform vec2 minimum; // minimum of each dimension
uniform vec2 maximum; // maximum of each dimension
uniform float speedScaleFactor;
uniform float frameRateAdjustment;
in vec2 v_textureCoordinates;
Expand Down Expand Up @@ -142,10 +143,9 @@ void main() {
// texture coordinate must be normalized
vec2 lonLat = texture(currentParticlesPosition, v_textureCoordinates).rg;
vec2 speedOrigin = bilinearInterpolation(lonLat);
vec2 speed = calculateSpeedByRungeKutta2(lonLat);
vec2 speed = calculateSpeedByRungeKutta2(lonLat) * frameRateAdjustment;
vec2 speedInLonLat = convertSpeedUnitToLonLat(lonLat, speed);
vec4 particleSpeed = vec4(speedInLonLat, calculateWindNorm(speed / speedScaleFactor));
fragColor = vec4(speedInLonLat, calculateWindNorm(speedOrigin));
}
`;
5 changes: 3 additions & 2 deletions packages/cesium-wind-layer/src/shaders/segmentDraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ uniform sampler2D currentParticlesPosition;
uniform sampler2D postProcessingPosition;
uniform sampler2D particlesSpeed;
uniform float frameRateAdjustment;
uniform float particleHeight;
uniform float aspect;
uniform float pixelSize;
Expand Down Expand Up @@ -117,11 +118,11 @@ void main() {
// 计算速度相关的宽度和长度因子
float speedFactor = max(0.3, speed.y);
float widthFactor = pointToUse < 0 ? 1.0 : 0.5; // 头部更宽,尾部更窄
// Modify length calculation with constraints
float baseLengthFactor = 10.0;
float speedBasedLength = baseLengthFactor * speedFactor;
float lengthFactor = clamp(speedBasedLength, 10.0, 100.0);
float lengthFactor = clamp(speedBasedLength, 10.0, 100.0) / frameRateAdjustment;
if (pointToUse == 1) {
// 头部位置
Expand Down
7 changes: 4 additions & 3 deletions packages/cesium-wind-layer/src/windParticlesComputing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class WindParticlesComputing {
};
windData: Required<WindData>;
private frameRateMonitor: FrameRateMonitor;
private frameRate: number = 60;
private frameRateAdjustment: number = 1;
frameRate: number = 60;
frameRateAdjustment: number = 1;

constructor(context: any, windData: Required<WindData>, options: WindLayerOptions, viewerParameters: any, scene: any) {
this.context = context;
Expand Down Expand Up @@ -150,8 +150,9 @@ export class WindParticlesComputing {
speedRange: () => new Cartesian2(this.windData.speed.min, this.windData.speed.max),
currentParticlesPosition: () => this.particlesTextures.currentParticlesPosition,
speedScaleFactor: () => {
return (this.viewerParameters.pixelSize + 50) * this.options.speedFactor * this.frameRateAdjustment;
return (this.viewerParameters.pixelSize + 50) * this.options.speedFactor;
},
frameRateAdjustment: () => this.frameRateAdjustment,
dimension: () => new Cartesian2(this.windData.width, this.windData.height),
minimum: () => new Cartesian2(this.windData.bounds.west, this.windData.bounds.south),
maximum: () => new Cartesian2(this.windData.bounds.east, this.windData.bounds.north),
Expand Down
1 change: 1 addition & 0 deletions packages/cesium-wind-layer/src/windParticlesRendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export class WindParticlesRendering {
currentParticlesPosition: () => this.computing.particlesTextures.currentParticlesPosition,
postProcessingPosition: () => this.computing.particlesTextures.postProcessingPosition,
particlesSpeed: () => this.computing.particlesTextures.particlesSpeed,
frameRateAdjustment: () => this.computing.frameRateAdjustment,
colorTable: () => this.colorTable,
domain: () => {
const domain = new Cartesian2(this.options.domain?.min ?? this.computing.windData.speed.min, this.options.domain?.max ?? this.computing.windData.speed.max);
Expand Down

0 comments on commit 3fdaa14

Please sign in to comment.