Skip to content

Commit ddb9787

Browse files
committed
Particles now wrap across the pacific if that is the shortest route.
1 parent 4defcbb commit ddb9787

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

public/shaders/particle.vert

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ void main() {
4040
// get start and end positions
4141
vec2 startPos = aPositions.xy;
4242
vec2 endPos = aPositions.zw;
43+
// get signed horizontal distance between start and end positions
44+
float a = endPos.x - startPos.x;
45+
// determine if the path the particle takes should wrap around the
46+
// pacific ocean
47+
if (startPos.x < 0.5 && endPos.x > 0.5) {
48+
float b = (1.0 - endPos.x) + startPos.x;
49+
if (a > b) {
50+
// wrap endpoint west around the pacific
51+
endPos.x -= 1.0;
52+
}
53+
} else if (startPos.x > 0.5 && endPos.x < 0.5) {
54+
float b = startPos.x - (1.0 + endPos.x);
55+
if (a < b) {
56+
// wrap endpoint east around the pacific
57+
endPos.x += 1.0;
58+
}
59+
}
4360
// get difference vector and distance
4461
vec2 diff = endPos - startPos;
4562
float dist = length( diff );
@@ -62,6 +79,9 @@ void main() {
6279
float t = mod( uTime + tOffset, nSpeed ) / nSpeed;
6380
// set point size
6481
gl_PointSize = uPointSize;
65-
// set position
66-
gl_Position = uProjectionMatrix * vec4( getBezier(t, startPos, p1, p2, endPos), 0.0, 1.0 );
82+
// get position along the curve
83+
vec2 pos = getBezier(t, startPos, p1, p2, endPos);
84+
// set position, be sure to modulos it so that it does not extent beyond
85+
// the bounds of the map.
86+
gl_Position = uProjectionMatrix * vec4( mod(pos.x, 1.0), pos.y, 0.0, 1.0 );
6787
}

0 commit comments

Comments
 (0)