Advertisement
Guest User

cusror_blaze_no_trail.glsl

a guest
Jun 28th, 2025
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1.  
  2. float sdBox(in vec2 p, in vec2 xy, in vec2 b)
  3. {
  4. vec2 d = abs(p - xy) - b;
  5. return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
  6. }
  7.  
  8. // //Author: https://iquilezles.org/articles/distfunctions2d/
  9. float sdTrail(in vec2 p, in vec2 v0, in vec2 v1, in vec2 v2, in vec2 v3)
  10. {
  11. float d = dot(p - v0, p - v0);
  12. float s = 1.0;
  13.  
  14. // Edge from v3 to v0
  15. {
  16. vec2 e = v3 - v0;
  17. vec2 w = p - v0;
  18. vec2 b = w - e * clamp(dot(w, e) / dot(e, e), 0.0, 1.0);
  19. d = min(d, dot(b, b));
  20.  
  21. // Compute branchless boolean conditions:
  22. float c0 = step(0.0, p.y - v0.y); // 1 if (p.y >= v0.y)
  23. float c1 = 1.0 - step(0.0, p.y - v3.y); // 1 if (p.y < v3.y)
  24. float c2 = 1.0 - step(0.0, e.x * w.y - e.y * w.x); // 1 if (e.x*w.y > e.y*w.x)
  25. float allCond = c0 * c1 * c2;
  26. float noneCond = (1.0 - c0) * (1.0 - c1) * (1.0 - c2);
  27. // If either allCond or noneCond is 1, then flip factor becomes -1.
  28. float flip = mix(1.0, -1.0, step(0.5, allCond + noneCond));
  29. s *= flip;
  30. }
  31.  
  32. // Edge from v0 to v1
  33. {
  34. vec2 e = v0 - v1;
  35. vec2 w = p - v1;
  36. vec2 b = w - e * clamp(dot(w, e) / dot(e, e), 0.0, 1.0);
  37. d = min(d, dot(b, b));
  38.  
  39. float c0 = step(0.0, p.y - v1.y);
  40. float c1 = 1.0 - step(0.0, p.y - v0.y);
  41. float c2 = 1.0 - step(0.0, e.x * w.y - e.y * w.x);
  42. float allCond = c0 * c1 * c2;
  43. float noneCond = (1.0 - c0) * (1.0 - c1) * (1.0 - c2);
  44. float flip = mix(1.0, -1.0, step(0.5, allCond + noneCond));
  45. s *= flip;
  46. }
  47.  
  48. // Edge from v1 to v2
  49. {
  50. vec2 e = v1 - v2;
  51. vec2 w = p - v2;
  52. vec2 b = w - e * clamp(dot(w, e) / dot(e, e), 0.0, 1.0);
  53. d = min(d, dot(b, b));
  54.  
  55. float c0 = step(0.0, p.y - v2.y);
  56. float c1 = 1.0 - step(0.0, p.y - v1.y);
  57. float c2 = 1.0 - step(0.0, e.x * w.y - e.y * w.x);
  58. float allCond = c0 * c1 * c2;
  59. float noneCond = (1.0 - c0) * (1.0 - c1) * (1.0 - c2);
  60. float flip = mix(1.0, -1.0, step(0.5, allCond + noneCond));
  61. s *= flip;
  62. }
  63.  
  64. // Edge from v2 to v3
  65. {
  66. vec2 e = v2 - v3;
  67. vec2 w = p - v3;
  68. vec2 b = w - e * clamp(dot(w, e) / dot(e, e), 0.0, 1.0);
  69. d = min(d, dot(b, b));
  70.  
  71. float c0 = step(0.0, p.y - v3.y);
  72. float c1 = 1.0 - step(0.0, p.y - v2.y);
  73. float c2 = 1.0 - step(0.0, e.x * w.y - e.y * w.x);
  74. float allCond = c0 * c1 * c2;
  75. float noneCond = (1.0 - c0) * (1.0 - c1) * (1.0 - c2);
  76. float flip = mix(1.0, -1.0, step(0.5, allCond + noneCond));
  77. s *= flip;
  78. }
  79.  
  80. return s * sqrt(d);
  81. }
  82.  
  83. vec2 normalize(vec2 value, float isPosition) {
  84. return (value * 2.0 - (iResolution.xy * isPosition)) / iResolution.y;
  85. }
  86.  
  87. float ParametricBlend(float t)
  88. {
  89. float sqr = t * t;
  90. return sqr / (2.0 * (sqr - t) + 1.0);
  91. }
  92.  
  93. float antialising(float distance) {
  94. return 1. - smoothstep(0., normalize(vec2(2., 2.), 0.).x, distance);
  95. }
  96.  
  97. float determineStartVertexFactor(vec2 a, vec2 b) {
  98. // Conditions using step
  99. float condition1 = step(b.x, a.x) * step(a.y, b.y); // a.x < b.x && a.y > b.y
  100. float condition2 = step(a.x, b.x) * step(b.y, a.y); // a.x > b.x && a.y < b.y
  101.  
  102. // If neither condition is met, return 1 (else case)
  103. return 1.0 - max(condition1, condition2);
  104. }
  105.  
  106. // red
  107. const vec4 TRAIL_COLOR = vec4(0.8, 0.725, 0.161, 1.0);
  108. const vec4 TRAIL_COLOR_ACCENT = vec4(1.0, 0., 0., 1.0);
  109. // custom
  110. //const vec4 TRAIL_COLOR = vec4(0.5, 0.625, 0.161, 1.0);
  111.  
  112. // blue
  113. // const vec4 TRAIL_COLOR = vec4(0.482, 0.886, 1.0, 1.0);
  114. // const vec4 TRAIL_COLOR_ACCENT = vec4(0.0, 0.424, 1.0, 1.0);
  115. const vec4 CURRENT_CURSOR_COLOR = TRAIL_COLOR;
  116. const vec4 PREVIOUS_CURSOR_COLOR = TRAIL_COLOR;
  117. const float DURATION = 0.1;
  118.  
  119. void mainImage(out vec4 fragColor, in vec2 fragCoord)
  120. {
  121. #if !defined(WEB)
  122. fragColor = texture(iChannel0, fragCoord.xy / iResolution.xy);
  123. #endif
  124. //Normalization for fragCoord to a space of -1 to 1;
  125. vec2 vu = normalize(fragCoord, 1.);
  126. vec2 offsetFactor = vec2(-.5, 0.5);
  127.  
  128. //Normalization for cursor position and size;
  129. //cursor xy has the postion in a space of -1 to 1;
  130. //zw has the width and height
  131. vec4 currentCursor = vec4(normalize(iCurrentCursor.xy, 1.), normalize(iCurrentCursor.zw, 0.));
  132. vec4 previousCursor = vec4(normalize(iPreviousCursor.xy, 1.), normalize(iPreviousCursor.zw, 0.));
  133.  
  134. //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
  135. float vertexFactor = determineStartVertexFactor(currentCursor.xy, previousCursor.xy);
  136. float invertedVertexFactor = 1.0 - vertexFactor;
  137.  
  138. //Set every vertex of my parellogram
  139. vec2 v0 = vec2(currentCursor.x + currentCursor.z * vertexFactor, currentCursor.y - currentCursor.w);
  140. vec2 v1 = vec2(currentCursor.x + currentCursor.z * invertedVertexFactor, currentCursor.y);
  141. vec2 v2 = vec2(previousCursor.x + currentCursor.z * invertedVertexFactor, previousCursor.y);
  142. vec2 v3 = vec2(previousCursor.x + currentCursor.z * vertexFactor, previousCursor.y - previousCursor.w);
  143.  
  144. vec4 newColor = vec4(fragColor);
  145.  
  146. float progress = ParametricBlend(clamp((iTime - iTimeCursorChange) / DURATION, 0.0, 1.0));
  147.  
  148. //Distance between cursors determine the total length of the parallelogram;
  149. float lineLength = distance(currentCursor.xy, previousCursor.xy);
  150. float distanceToEnd = distance(vu.xy, vec2(currentCursor.x + (currentCursor.z / 2.), currentCursor.y - (currentCursor.w / 2.)));
  151. float alphaModifier = distanceToEnd / (lineLength * (1.0 - progress));
  152.  
  153. // float d2 = sdTrail(vu, v0, v1, v2, v3);
  154. // newColor = mix(newColor, TRAIL_COLOR_ACCENT, 1.0 - smoothstep(d2, -0.01, 0.001));
  155. // newColor = mix(newColor, TRAIL_COLOR, 1.0 - smoothstep(d2, -0.01, 0.001));
  156. // newColor = mix(newColor, TRAIL_COLOR, antialising(d2));
  157.  
  158. float cCursorDistance = sdBox(vu, currentCursor.xy - (currentCursor.zw * offsetFactor), currentCursor.zw * 0.5);
  159. newColor = mix(newColor, TRAIL_COLOR_ACCENT, 1.0 - smoothstep(cCursorDistance, -0.000, 0.003 * (1. - progress)));
  160. newColor = mix(newColor, CURRENT_CURSOR_COLOR, 1.0 - smoothstep(cCursorDistance, -0.000, 0.003 * (1. - progress)));
  161.  
  162. // float pCursorDistance = sdBox(vu, previousCursor.xy - (previousCursor.zw * offsetFactor), previousCursor.zw * 0.5);
  163. // newColor = mix(newColor, PREVIOUS_CURSOR_COLOR, antialising(pCursorDistance));
  164.  
  165. fragColor = mix(fragColor, newColor, 1.);
  166. // fragColor = mix(fragColor, newColor, 1.0 - alphaModifier);
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement