Advertisement
nullpainter

blood splatter glsl godot shader

Jul 20th, 2024 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shader_type canvas_item;
  2.  
  3. //////////////////////////////////////////////////////////////////////////////////////////
  4. //
  5. //                                        noise
  6. //
  7. //////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. // @pasted https://godotshaders.com/snippet/2d-noise/
  10.  
  11. vec2 random(vec2 uv){
  12.     uv = vec2( dot(uv, vec2(127.1,311.7) ),
  13.                dot(uv, vec2(269.5,183.3) ) );
  14.     return -1.0 + 2.0 * fract(sin(uv) * 43758.5453123);
  15. }
  16.  
  17. float noise(vec2 uv) {
  18.     vec2 uv_index = floor(uv);
  19.     vec2 uv_fract = fract(uv);
  20.  
  21.     vec2 blur = smoothstep(0.0, 1.0, uv_fract);
  22.  
  23.     return mix( mix( dot( random(uv_index + vec2(0.0,0.0) ), uv_fract - vec2(0.0,0.0) ),
  24.                      dot( random(uv_index + vec2(1.0,0.0) ), uv_fract - vec2(1.0,0.0) ), blur.x),
  25.                 mix( dot( random(uv_index + vec2(0.0,1.0) ), uv_fract - vec2(0.0,1.0) ),
  26.                      dot( random(uv_index + vec2(1.0,1.0) ), uv_fract - vec2(1.0,1.0) ), blur.x), blur.y) + 0.5;
  27. }
  28.  
  29. //////////////////////////////////////////////////////////////////////////////////////////
  30. //
  31. //                                        utilities
  32. //
  33. //////////////////////////////////////////////////////////////////////////////////////////
  34.  
  35. /*
  36. This is pretty cool:
  37.  
  38.     float a0   =      -(xy.y);//-xy.y)+(xy.x+xy.y);
  39.     float a125 = (xy.x-xy.y);//+(xy.x+xy.y);
  40.     float a25  = (xy.x-xy.y)+(xy.x+xy.y);
  41.     float a375 = (xy.x+xy.y);//+(xy.x+xy.y);
  42.     float a5   =       xy.y;
  43.     float a625 = (-xy.x+xy.y);//+(xy.x+xy.y);
  44.     float a75  = -xy.x;
  45.     float a875 = -xy.x+-xy.y;
  46.  
  47.     float sum =
  48.     a0
  49.     + a125 //
  50.     + a25  //= (xy.x-xy.y)+(xy.x+xy.y);
  51.     + a375 //= (xy.x+xy.y);//+(xy.x+xy.y);
  52.     + a5   //=       xy.y;
  53.     + a625 //= (-xy.x+xy.y);//+(xy.x+xy.y);
  54.     + a75  //= -xy.x;
  55.     + a875 // = -xy.x+-xy.y;
  56.     ;
  57.     //return (xy.x-xy.y)/(xy.x+xy.y);
  58.     // return (xy.x-xy.y)*(xy.x+xy.y); // almost expon
  59. */
  60.  
  61. // if equals
  62. float ifEquals(float a, float b) {
  63.     return (abs(a-b)<.02)?1.:0.;
  64. }
  65.  
  66. // if true
  67. float ifTrue(bool x){
  68.     return x?1.:0.;
  69. }
  70.  
  71. // if then else
  72. float ifThenElse(bool x, float if_true, float if_false){
  73.     return x?if_true:if_false; 
  74. }
  75.  
  76. //float angleFromXY(vec2 xy) {
  77. //  float a;
  78. //
  79. //      /*
  80. //  float a2 = atan(xy.y/xy.x); // -1..1 ->
  81. //  a=ifTrue(a2<0.);//?0.:1.;
  82. //  a=ifTrue(xy.x>0.5);
  83. //  a=ifEquals(a2,-3.1415926/2.);//-1.5);//(a2+1.)/2.;
  84. //  a=ifEquals(xy.x,0.);
  85. //  a=ifEquals(xy.y,0.);
  86. //  a=a2;
  87. //  a=ifTrue(xy.y>0.);
  88. //  a=ifTrue(xy.x>0.);
  89. //  a=a2/(2.*PI);
  90. //  a=ifEquals(a2,0.9*(PI/2.));
  91. //  */
  92. //
  93. //  a=0.; // falllback
  94. //
  95. //  if(xy.x>0.&&xy.y<0.){
  96. //      a=1.;
  97. //      a=.0+.25*(atan(-xy.x/xy.y)/(.5*PI));// 0..0.25 * (0..1)
  98. //  } else if(xy.x>0.&&xy.y>0.){
  99. //      a=1.;
  100. //      a=atan(xy.y/xy.x); // 0..1
  101. //      a=.25+.25*(atan(xy.y/xy.x)/(.5*PI));// 0.25..0.5 * (0..1)
  102. //  } else if(xy.x<0.&&xy.y>0.){
  103. //      a=1.;
  104. //      a=.5+.25*(atan(-xy.x/xy.y)/(.5*PI));// 0.25..0.5 * (0..1)
  105. //  } else {
  106. //      a=1.;
  107. //      a=.75+.25*(atan(xy.y/xy.x)/(.5*PI));// 0.25..0.5 * (0..1)      
  108. //  }
  109. //
  110. //  return a;
  111. //}
  112.  
  113. // angle, distance
  114. //vec2 polarFromXY(vec2 xy) {
  115. //  return vec2(angleFromXY(xy),length(xy));
  116. //}
  117.  
  118. // pass color unchaged if test fails
  119. //vec4 test(vec4 color_if_false, bool test, vec4 color_if_true) {
  120. //  return test? color_if_true: color_if_false;
  121. //}
  122.  
  123. //////////////////////////////////////////////////////////////////////////////////////////
  124. //
  125. //                                         SDF
  126. //
  127. //////////////////////////////////////////////////////////////////////////////////////////
  128.  
  129. float circle(vec2 xy, vec2 c, float r) {
  130.     float dx=xy.x - c.x;
  131.     float dy=xy.y - c.y;
  132.     return (dx*dx+dy*dy<r*r)?1.:0.;
  133. }
  134.  
  135. float circle2(vec2 xy, vec2 c, float r) {
  136.     float dx=xy.x - c.x;
  137.     float dy=xy.y - c.y;
  138.     float ratio = sqrt(dx*dx+dy*dy)/r;
  139.     if(ratio>1.) return 0.;
  140.     return (1.-ratio);
  141. }
  142.  
  143. // 0..1
  144. float box(vec2 xy, vec2 center, float w, float h) {
  145.     return (
  146.         ifTrue(xy.x>center.x-w*.5)*ifTrue(xy.y>center.y-h*.5)*
  147.         ifTrue(xy.x<center.x+w*.5)*ifTrue(xy.y<center.y+h*.5)
  148.     );
  149. }
  150.  
  151. float within(float x, float minn, float maxx, float if_true) {
  152.     if(x<minn) return 0.;
  153.     if(x>maxx) return 0.;
  154.     return if_true;
  155. }
  156.  
  157. // 0..1, smooth
  158. float box2(vec2 xy, vec2 center, float w, float h,float blur) {
  159.     return ( // @fixme
  160.         (
  161.             1.
  162.             // inner box
  163.             *ifTrue(xy.x>center.x-w*.5+blur)//,1.,0.)
  164.             *ifTrue(xy.y>center.y-h*.5+blur)//,1.,0.)
  165.             *ifTrue(xy.x<center.x+w*.5-blur)//,1.,0.)
  166.             *ifTrue(xy.y<center.y+h*.5-blur)//,1.,0.)
  167.             // smooth corners
  168.         ) + (
  169.             0.
  170.             +(within(xy.y,
  171.                 center.y-h*.5,//-blur,
  172.                 center.y+h*.5,//-blur,
  173.                 within(
  174.                     xy.x,
  175.                     center.x-w*.5,
  176.                     center.x-w*.5+blur,
  177.                     (xy.x - (center.x-w*.5))/blur
  178.                 )
  179.             ))
  180.             +(within(xy.y,
  181.                 center.y-h*.5,//-blur,
  182.                 center.y+h*.5,//-blur,
  183.                 within(
  184.                     xy.x,
  185.                     center.x+w*.5-blur,
  186.                     center.x+w*.5,
  187.                     (center.x+w*.5 - xy.x)/blur
  188.                 )
  189.             ))
  190.             +(within(xy.x,
  191.                 center.x-h*.5+blur,//-blur,
  192.                 center.x+h*.5-blur,//-blur,
  193.                 within(
  194.                     xy.y,
  195.                     center.y-h*.5,
  196.                     center.y-h*.5+blur,
  197.                     (xy.y - (center.y-h*.5))/blur
  198.                 )
  199.             ))
  200.             +(within(xy.x,
  201.                 center.x-w*.5,//-blur,
  202.                 center.x+w*.5,//-blur,
  203.                 within(
  204.                     xy.y,
  205.                     center.y+h*.5-blur,
  206.                     center.y+h*.5,
  207.                     (center.y+h*.5 - xy.y)/blur
  208.                 )
  209.             ))
  210.         )
  211.     );
  212. }
  213.  
  214. // 0..1
  215. //float roundedBox(vec2 xy, vec2 center, float w, float h, float blur) {
  216. //  return (
  217. //      (
  218. //          box(xy,center,w,h)
  219. //          // remove corners ... using and not, which is * (1-x)
  220. //          *(1.-(box(xy,center+vec2(w,h)*.5,blur*2.,blur*2.)))
  221. //          *(1.-(box(xy,center+vec2(w,-h)*.5,blur*2.,blur*2.)))
  222. //          *(1.-(box(xy,center+vec2(-w,h)*.5,blur*2.,blur*2.)))
  223. //          *(1.-(box(xy,center+vec2(-w,-h)*.5,blur*2.,blur*2.)))
  224. //      )  + (
  225. //          // add rounded corners
  226. //          0.
  227. //          +circle(xy,center+vec2(w,h)*.5-vec2(blur,blur)*1.,blur)
  228. //          +circle(xy,center+vec2(w,-h)*.5-vec2(blur,-blur),blur)
  229. //          +circle(xy,center+vec2(-w,h)*.5-vec2(-blur,blur),blur)
  230. //          +circle(xy,center+vec2(-w,-h)*.5-vec2(-blur,-blur),blur)
  231. //      )
  232. //  );
  233. //}
  234.  
  235. // 0..1, smooth
  236. float roundedBox2(vec2 xy, vec2 center, float w, float h, float blur) {
  237.     return (
  238.         (
  239.             box2(xy,center,w,h,blur)
  240.             // remove corners ... * (1-x)
  241.             *(1.-(box(xy,center+vec2(w,h)*.5,blur*2.,blur*2.)))
  242.             *(1.-(box(xy,center+vec2(w,-h)*.5,blur*2.,blur*2.)))
  243.             *(1.-(box(xy,center+vec2(-w,h)*.5,blur*2.,blur*2.)))
  244.             *(1.-(box(xy,center+vec2(-w,-h)*.5,blur*2.,blur*2.)))
  245.         )  + (
  246.             // add rounded corners
  247.             0.
  248.             +(
  249.                  circle2(xy,center+vec2(w,h)*.5-vec2(blur,blur),    blur)
  250.                 // clip only 1/4 of a cirle
  251.                 *box(    xy,center+vec2(w,h)*.5-vec2(blur,blur)*.5, blur,blur)
  252.             )
  253.             +(
  254.                 circle2(xy,center+vec2(w,-h)*.5-vec2(blur,-blur),blur)
  255.                 *box(   xy,center+vec2(w,-h)*.5-vec2(blur,-blur)*.5, blur,blur)
  256.             )
  257.             +(
  258.                 circle2(xy,center+vec2(-w,h)*.5-vec2(-blur,blur),blur)
  259.                 *box(   xy,center+vec2(-w,h)*.5-vec2(-blur,blur)*.5, blur,blur)
  260.             )
  261.             +(
  262.                 circle2(xy,center+vec2(-w,-h)*.5-vec2(-blur,-blur),blur)
  263.                 *box(   xy,center+vec2(-w,-h)*.5-vec2(-blur,-blur)*.5, blur,blur)
  264.             )
  265.         )
  266.     );
  267. }
  268.  
  269. void fragment() {  
  270.     vec4 red = vec4(1,0,0,1);
  271.     vec4 blood = red * noise(UV * 10.0);
  272.     COLOR =
  273.         blood * clamp(
  274.             (
  275.                 0.
  276.                 +roundedBox2(UV,vec2(.5,.5),.4,.8,0.12)
  277.                 +circle2(UV,vec2(.5,.6),.4)
  278.                 +circle2(UV,vec2(.8,.8),.2)
  279.             )
  280.             ,0.,1.
  281.         )
  282.     ;
  283. }
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement