···
1
+
// from https://github.com/KroneCorylus/ghostty-shader-playground/tree/eec84ccb34564ef4b41ef494338de1d1b7098638
3
+
float getSdfRectangle(in vec2 p, in vec2 xy, in vec2 b)
5
+
vec2 d = abs(p - xy) - b;
6
+
return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
9
+
// Based on Inigo Quilez's 2D distance functions article: https://iquilezles.org/articles/distfunctions2d/
10
+
// Potencially optimized by eliminating conditionals and loops to enhance performance and reduce branching
12
+
float seg(in vec2 p, in vec2 a, in vec2 b, inout float s, float d) {
15
+
vec2 proj = a + e * clamp(dot(w, e) / dot(e, e), 0.0, 1.0);
16
+
float segd = dot(p - proj, p - proj);
19
+
float c0 = step(0.0, p.y - a.y);
20
+
float c1 = 1.0 - step(0.0, p.y - b.y);
21
+
float c2 = 1.0 - step(0.0, e.x * w.y - e.y * w.x);
22
+
float allCond = c0 * c1 * c2;
23
+
float noneCond = (1.0 - c0) * (1.0 - c1) * (1.0 - c2);
24
+
float flip = mix(1.0, -1.0, step(0.5, allCond + noneCond));
29
+
float getSdfParallelogram(in vec2 p, in vec2 v0, in vec2 v1, in vec2 v2, in vec2 v3) {
31
+
float d = dot(p - v0, p - v0);
33
+
d = seg(p, v0, v3, s, d);
34
+
d = seg(p, v1, v0, s, d);
35
+
d = seg(p, v2, v1, s, d);
36
+
d = seg(p, v3, v2, s, d);
41
+
vec2 normalize(vec2 value, float isPosition) {
42
+
return (value * 2.0 - (iResolution.xy * isPosition)) / iResolution.y;
45
+
float antialising(float distance) {
46
+
return 1. - smoothstep(0., normalize(vec2(2., 2.), 0.).x, distance);
49
+
float determineStartVertexFactor(vec2 a, vec2 b) {
50
+
// Conditions using step
51
+
float condition1 = step(b.x, a.x) * step(a.y, b.y); // a.x < b.x && a.y > b.y
52
+
float condition2 = step(a.x, b.x) * step(b.y, a.y); // a.x > b.x && a.y < b.y
54
+
// If neither condition is met, return 1 (else case)
55
+
return 1.0 - max(condition1, condition2);
58
+
vec2 getRectangleCenter(vec4 rectangle) {
59
+
return vec2(rectangle.x + (rectangle.z / 2.), rectangle.y - (rectangle.w / 2.));
61
+
float ease(float x) {
62
+
return pow(1.0 - x, 3.0);
64
+
vec4 saturate(vec4 color, float factor) {
65
+
float gray = dot(color, vec4(0.299, 0.587, 0.114, 0.)); // luminance
66
+
return mix(vec4(gray), color, factor);
69
+
vec4 TRAIL_COLOR = iCurrentCursorColor;
70
+
const float OPACITY = 0.6;
71
+
const float DURATION = 0.3; //IN SECONDS
73
+
void mainImage(out vec4 fragColor, in vec2 fragCoord)
77
+
fragColor = texture(iChannel0, fragCoord.xy / iResolution.xy);
79
+
// Normalization for fragCoord to a space of -1 to 1;
80
+
vec2 vu = normalize(fragCoord, 1.);
81
+
vec2 offsetFactor = vec2(-.5, 0.5);
83
+
// Normalization for cursor position and size;
84
+
// cursor xy has the postion in a space of -1 to 1;
85
+
// zw has the width and height
86
+
vec4 currentCursor = vec4(normalize(iCurrentCursor.xy, 1.), normalize(iCurrentCursor.zw, 0.));
87
+
vec4 previousCursor = vec4(normalize(iPreviousCursor.xy, 1.), normalize(iPreviousCursor.zw, 0.));
89
+
// When drawing a parellelogram between cursors for the trail i need to determine where to start at the top-left or top-right vertex of the cursor
90
+
float vertexFactor = determineStartVertexFactor(currentCursor.xy, previousCursor.xy);
91
+
float invertedVertexFactor = 1.0 - vertexFactor;
93
+
// Set every vertex of my parellogram
94
+
vec2 v0 = vec2(currentCursor.x + currentCursor.z * vertexFactor, currentCursor.y - currentCursor.w);
95
+
vec2 v1 = vec2(currentCursor.x + currentCursor.z * invertedVertexFactor, currentCursor.y);
96
+
vec2 v2 = vec2(previousCursor.x + currentCursor.z * invertedVertexFactor, previousCursor.y);
97
+
vec2 v3 = vec2(previousCursor.x + currentCursor.z * vertexFactor, previousCursor.y - previousCursor.w);
99
+
float sdfCurrentCursor = getSdfRectangle(vu, currentCursor.xy - (currentCursor.zw * offsetFactor), currentCursor.zw * 0.5);
100
+
float sdfTrail = getSdfParallelogram(vu, v0, v1, v2, v3);
102
+
float progress = clamp((iTime - iTimeCursorChange) / DURATION, 0.0, 1.0);
103
+
float easedProgress = ease(progress);
104
+
// Distance between cursors determine the total length of the parallelogram;
105
+
vec2 centerCC = getRectangleCenter(currentCursor);
106
+
vec2 centerCP = getRectangleCenter(previousCursor);
107
+
float lineLength = distance(centerCC, centerCP);
109
+
vec4 newColor = vec4(fragColor);
111
+
vec4 trail = TRAIL_COLOR;
112
+
trail = saturate(trail, 2.5);
114
+
newColor = mix(newColor, trail, antialising(sdfTrail));
115
+
// Draw current cursor
116
+
newColor = mix(newColor, trail, antialising(sdfCurrentCursor));
117
+
newColor = mix(newColor, fragColor, step(sdfCurrentCursor, 0.));
118
+
// newColor = mix(fragColor, newColor, OPACITY);
119
+
fragColor = mix(fragColor, newColor, step(sdfCurrentCursor, easedProgress * lineLength));