sobinist

Untitled

Dec 18th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. // The MIT License
  2. // Copyright © 2020 Anders Riggelsen
  3. // Based uppon Inigo Quilez original code
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  5.  
  6.  
  7. struct Result{
  8. float dist; //Regular SDF distance
  9. float side; //Which side of the line segment the point is (-1,0,1)
  10. };
  11.  
  12. Result udSegment( in vec2 p, in vec2 a, in vec2 b )
  13. {
  14. Result res;
  15. //All this is basically Inigo's regular line SDF function - but store it in 'dist' instead:
  16. vec2 ba = b-a;
  17. vec2 pa = p-a;
  18. float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
  19. res.dist = length(pa-h*ba);
  20. //Is the movement (a->b->p) a righthand turn? (cross product)
  21. res.side = sign( (b.x - a.x)*(p.y - a.y) - (b.y - a.y)*(p.x - a.x) );
  22. return res;
  23. }
  24.  
  25. vec2 jiggle(vec2 p)
  26. {
  27. return p + vec2(sin(iTime*p.y), cos(iTime*p.x))*0.25;
  28. }
  29.  
  30. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  31. {
  32. vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
  33.  
  34. //List of points. All could be put into a loop if need be.
  35. //Jiggled for your viewing pleasure.
  36. vec2 pA = jiggle(vec2(-0.8, 0.7));
  37. vec2 pB = jiggle(vec2( 1.0, 0.8));
  38. vec2 pC = jiggle(vec2( 1.0, -0.4));
  39. vec2 pD = jiggle(vec2( 0.0, -1.2));
  40.  
  41. //Get the SDF distances for the line segments along with the 'side':
  42. Result rA = udSegment( p, pA, pB);
  43. Result rB = udSegment( p, pB, pC );
  44. Result rC = udSegment( p, pC, pD );
  45. Result rD = udSegment( p, pD, pA );
  46.  
  47. rB.side = -1.0;
  48.  
  49. //Union of all distances
  50. float d = rA.dist;
  51. d = min(d, rB.dist);
  52. d = min(d, rC.dist);
  53. d = min(d, rD.dist);
  54. //At this point all we have is a SDF of an infinitely thin shape.
  55. //To make it "solid" we have to give all the points on the inside a negative distance.
  56.  
  57. //But which side is the point on?
  58. //If the "side" is negative for *all* N segments the sum will be -N.
  59. //Adding N and subtracting a small value ]0-1[ before getting the sign
  60. //of this sum makes it either 1 (outside of shape) or -1 (inside of shape)
  61. d = d * sign(rA.side + rB.side + rC.side + rD.side + 4.0 - 0.5);
  62.  
  63. //Inigo's pretty colors:
  64. vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
  65. col *= 1.0 - exp(-3.0*abs(d));
  66. col *= 0.8 + 0.2*cos(120.0*d);
  67. col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );
  68. fragColor = vec4(col,1.0);
  69. }
Add Comment
Please, Sign In to add comment