@@ -14,10 +14,12 @@ uniform float particleHeight;
1414uniform float aspect;
1515uniform float pixelSize;
1616uniform float lineWidth;
17+ uniform vec2 lineLength;
18+ uniform vec2 domain;
1719uniform bool is3D;
1820
1921// 添加输出变量传递给片元着色器
20- out vec2 speed;
22+ out vec4 speed;
2123out float v_segmentPosition;
2224out vec2 textureCoordinate;
2325
@@ -91,6 +93,7 @@ void main() {
9193 vec2 flippedIndex = vec2(st.x, 1.0 - st.y);
9294
9395 vec2 particleIndex = flippedIndex;
96+ speed = texture(particlesSpeed, particleIndex);
9497
9598 vec2 previousPosition = texture(previousParticlesPosition, particleIndex).rg;
9699 vec2 currentPosition = texture(currentParticlesPosition, particleIndex).rg;
@@ -116,48 +119,46 @@ void main() {
116119 vec4 offset = vec4(0.0);
117120
118121 // 计算速度相关的宽度和长度因子
119- float speedFactor = max(0.3, speed.y);
120122 float widthFactor = pointToUse < 0 ? 1.0 : 0.5; // 头部更宽,尾部更窄
121123
122- // Modify length calculation with constraints
123- float baseLengthFactor = 10.0 ;
124- float speedBasedLength = baseLengthFactor * speedFactor ;
125- float lengthFactor = clamp(speedBasedLength, 10.0, 100.0) / frameRateAdjustment ;
124+ // Calculate length based on speed
125+ float speedLength = clamp(speed.b, domain.x, domain.y) ;
126+ float normalizedSpeed = (speedLength - domain.x) / (domain.y - domain.x) ;
127+ float lengthFactor = mix(lineLength.x, lineLength.y, normalizedSpeed) * pixelSize ;
126128
127129 if (pointToUse == 1) {
128130 // 头部位置
129131 offset = pixelSize * calculateOffsetOnNormalDirection(
130132 projectedCoordinates.previous,
131133 projectedCoordinates.current,
132134 offsetSign,
133- widthFactor * speedFactor
135+ widthFactor * normalizedSpeed
134136 );
135137 gl_Position = projectedCoordinates.previous + offset;
136138 v_segmentPosition = 0.0; // 头部
137139 } else if (pointToUse == -1) {
138- // 尾部位置,向后延伸,使用受限制的长度
139- vec4 direction = projectedCoordinates.next - projectedCoordinates.current;
140+ // Get direction and normalize it to length 1.0
141+ vec4 direction = normalize( projectedCoordinates.next - projectedCoordinates.current) ;
140142 vec4 extendedPosition = projectedCoordinates.current + direction * lengthFactor;
141143
142144 offset = pixelSize * calculateOffsetOnNormalDirection(
143145 projectedCoordinates.current,
144146 extendedPosition,
145147 offsetSign,
146- widthFactor * speedFactor
148+ widthFactor * normalizedSpeed
147149 );
148150 gl_Position = extendedPosition + offset;
149151 v_segmentPosition = 1.0; // 尾部
150152 }
151153
152- speed = texture(particlesSpeed, particleIndex).ba;
153154 textureCoordinate = st;
154155}
155156` ;
156157
157158export const renderParticlesFragmentShader = /*glsl*/ `#version 300 es
158159precision highp float;
159160
160- in vec2 speed;
161+ in vec4 speed;
161162in float v_segmentPosition;
162163in vec2 textureCoordinate;
163164
@@ -170,8 +171,8 @@ out vec4 fragColor;
170171
171172void main() {
172173 const float zero = 0.0;
173- if(speed.y > zero && speed.x > displayRange.x && speed.x < displayRange.y) {
174- float speedLength = clamp(speed.x , domain.x, domain.y);
174+ if(speed.a > zero && speed.b > displayRange.x && speed.b < displayRange.y) {
175+ float speedLength = clamp(speed.b , domain.x, domain.y);
175176 float normalizedSpeed = (speedLength - domain.x) / (domain.y - domain.x);
176177 vec4 baseColor = texture(colorTable, vec2(normalizedSpeed, zero));
177178
@@ -180,7 +181,7 @@ void main() {
180181 alpha = pow(alpha, 1.5); // 调整透明度渐变曲线
181182
182183 // 根据速度调整透明度
183- float speedAlpha = mix(0.3, 1.0, speed.y );
184+ float speedAlpha = mix(0.3, 1.0, speed.a );
184185
185186 // 组合颜色和透明度
186187 fragColor = vec4(baseColor.rgb, baseColor.a * alpha * speedAlpha);
0 commit comments