Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shader_type canvas_item;
- //////////////////////////////////////////////////////////////////////////////////////////
- //
- // noise
- //
- //////////////////////////////////////////////////////////////////////////////////////////
- // @pasted https://godotshaders.com/snippet/2d-noise/
- vec2 random(vec2 uv){
- uv = vec2( dot(uv, vec2(127.1,311.7) ),
- dot(uv, vec2(269.5,183.3) ) );
- return -1.0 + 2.0 * fract(sin(uv) * 43758.5453123);
- }
- float noise(vec2 uv) {
- vec2 uv_index = floor(uv);
- vec2 uv_fract = fract(uv);
- vec2 blur = smoothstep(0.0, 1.0, uv_fract);
- return mix( mix( dot( random(uv_index + vec2(0.0,0.0) ), uv_fract - vec2(0.0,0.0) ),
- dot( random(uv_index + vec2(1.0,0.0) ), uv_fract - vec2(1.0,0.0) ), blur.x),
- mix( dot( random(uv_index + vec2(0.0,1.0) ), uv_fract - vec2(0.0,1.0) ),
- dot( random(uv_index + vec2(1.0,1.0) ), uv_fract - vec2(1.0,1.0) ), blur.x), blur.y) + 0.5;
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- //
- // utilities
- //
- //////////////////////////////////////////////////////////////////////////////////////////
- /*
- This is pretty cool:
- float a0 = -(xy.y);//-xy.y)+(xy.x+xy.y);
- float a125 = (xy.x-xy.y);//+(xy.x+xy.y);
- float a25 = (xy.x-xy.y)+(xy.x+xy.y);
- float a375 = (xy.x+xy.y);//+(xy.x+xy.y);
- float a5 = xy.y;
- float a625 = (-xy.x+xy.y);//+(xy.x+xy.y);
- float a75 = -xy.x;
- float a875 = -xy.x+-xy.y;
- float sum =
- a0
- + a125 //
- + a25 //= (xy.x-xy.y)+(xy.x+xy.y);
- + a375 //= (xy.x+xy.y);//+(xy.x+xy.y);
- + a5 //= xy.y;
- + a625 //= (-xy.x+xy.y);//+(xy.x+xy.y);
- + a75 //= -xy.x;
- + a875 // = -xy.x+-xy.y;
- ;
- //return (xy.x-xy.y)/(xy.x+xy.y);
- // return (xy.x-xy.y)*(xy.x+xy.y); // almost expon
- */
- // if equals
- float ifEquals(float a, float b) {
- return (abs(a-b)<.02)?1.:0.;
- }
- // if true
- float ifTrue(bool x){
- return x?1.:0.;
- }
- // if then else
- float ifThenElse(bool x, float if_true, float if_false){
- return x?if_true:if_false;
- }
- //float angleFromXY(vec2 xy) {
- // float a;
- //
- // /*
- // float a2 = atan(xy.y/xy.x); // -1..1 ->
- // a=ifTrue(a2<0.);//?0.:1.;
- // a=ifTrue(xy.x>0.5);
- // a=ifEquals(a2,-3.1415926/2.);//-1.5);//(a2+1.)/2.;
- // a=ifEquals(xy.x,0.);
- // a=ifEquals(xy.y,0.);
- // a=a2;
- // a=ifTrue(xy.y>0.);
- // a=ifTrue(xy.x>0.);
- // a=a2/(2.*PI);
- // a=ifEquals(a2,0.9*(PI/2.));
- // */
- //
- // a=0.; // falllback
- //
- // if(xy.x>0.&&xy.y<0.){
- // a=1.;
- // a=.0+.25*(atan(-xy.x/xy.y)/(.5*PI));// 0..0.25 * (0..1)
- // } else if(xy.x>0.&&xy.y>0.){
- // a=1.;
- // a=atan(xy.y/xy.x); // 0..1
- // a=.25+.25*(atan(xy.y/xy.x)/(.5*PI));// 0.25..0.5 * (0..1)
- // } else if(xy.x<0.&&xy.y>0.){
- // a=1.;
- // a=.5+.25*(atan(-xy.x/xy.y)/(.5*PI));// 0.25..0.5 * (0..1)
- // } else {
- // a=1.;
- // a=.75+.25*(atan(xy.y/xy.x)/(.5*PI));// 0.25..0.5 * (0..1)
- // }
- //
- // return a;
- //}
- // angle, distance
- //vec2 polarFromXY(vec2 xy) {
- // return vec2(angleFromXY(xy),length(xy));
- //}
- // pass color unchaged if test fails
- //vec4 test(vec4 color_if_false, bool test, vec4 color_if_true) {
- // return test? color_if_true: color_if_false;
- //}
- //////////////////////////////////////////////////////////////////////////////////////////
- //
- // SDF
- //
- //////////////////////////////////////////////////////////////////////////////////////////
- float circle(vec2 xy, vec2 c, float r) {
- float dx=xy.x - c.x;
- float dy=xy.y - c.y;
- return (dx*dx+dy*dy<r*r)?1.:0.;
- }
- float circle2(vec2 xy, vec2 c, float r) {
- float dx=xy.x - c.x;
- float dy=xy.y - c.y;
- float ratio = sqrt(dx*dx+dy*dy)/r;
- if(ratio>1.) return 0.;
- return (1.-ratio);
- }
- // 0..1
- float box(vec2 xy, vec2 center, float w, float h) {
- return (
- ifTrue(xy.x>center.x-w*.5)*ifTrue(xy.y>center.y-h*.5)*
- ifTrue(xy.x<center.x+w*.5)*ifTrue(xy.y<center.y+h*.5)
- );
- }
- float within(float x, float minn, float maxx, float if_true) {
- if(x<minn) return 0.;
- if(x>maxx) return 0.;
- return if_true;
- }
- // 0..1, smooth
- float box2(vec2 xy, vec2 center, float w, float h,float blur) {
- return ( // @fixme
- (
- 1.
- // inner box
- *ifTrue(xy.x>center.x-w*.5+blur)//,1.,0.)
- *ifTrue(xy.y>center.y-h*.5+blur)//,1.,0.)
- *ifTrue(xy.x<center.x+w*.5-blur)//,1.,0.)
- *ifTrue(xy.y<center.y+h*.5-blur)//,1.,0.)
- // smooth corners
- ) + (
- 0.
- +(within(xy.y,
- center.y-h*.5,//-blur,
- center.y+h*.5,//-blur,
- within(
- xy.x,
- center.x-w*.5,
- center.x-w*.5+blur,
- (xy.x - (center.x-w*.5))/blur
- )
- ))
- +(within(xy.y,
- center.y-h*.5,//-blur,
- center.y+h*.5,//-blur,
- within(
- xy.x,
- center.x+w*.5-blur,
- center.x+w*.5,
- (center.x+w*.5 - xy.x)/blur
- )
- ))
- +(within(xy.x,
- center.x-h*.5+blur,//-blur,
- center.x+h*.5-blur,//-blur,
- within(
- xy.y,
- center.y-h*.5,
- center.y-h*.5+blur,
- (xy.y - (center.y-h*.5))/blur
- )
- ))
- +(within(xy.x,
- center.x-w*.5,//-blur,
- center.x+w*.5,//-blur,
- within(
- xy.y,
- center.y+h*.5-blur,
- center.y+h*.5,
- (center.y+h*.5 - xy.y)/blur
- )
- ))
- )
- );
- }
- // 0..1
- //float roundedBox(vec2 xy, vec2 center, float w, float h, float blur) {
- // return (
- // (
- // box(xy,center,w,h)
- // // remove corners ... using and not, which is * (1-x)
- // *(1.-(box(xy,center+vec2(w,h)*.5,blur*2.,blur*2.)))
- // *(1.-(box(xy,center+vec2(w,-h)*.5,blur*2.,blur*2.)))
- // *(1.-(box(xy,center+vec2(-w,h)*.5,blur*2.,blur*2.)))
- // *(1.-(box(xy,center+vec2(-w,-h)*.5,blur*2.,blur*2.)))
- // ) + (
- // // add rounded corners
- // 0.
- // +circle(xy,center+vec2(w,h)*.5-vec2(blur,blur)*1.,blur)
- // +circle(xy,center+vec2(w,-h)*.5-vec2(blur,-blur),blur)
- // +circle(xy,center+vec2(-w,h)*.5-vec2(-blur,blur),blur)
- // +circle(xy,center+vec2(-w,-h)*.5-vec2(-blur,-blur),blur)
- // )
- // );
- //}
- // 0..1, smooth
- float roundedBox2(vec2 xy, vec2 center, float w, float h, float blur) {
- return (
- (
- box2(xy,center,w,h,blur)
- // remove corners ... * (1-x)
- *(1.-(box(xy,center+vec2(w,h)*.5,blur*2.,blur*2.)))
- *(1.-(box(xy,center+vec2(w,-h)*.5,blur*2.,blur*2.)))
- *(1.-(box(xy,center+vec2(-w,h)*.5,blur*2.,blur*2.)))
- *(1.-(box(xy,center+vec2(-w,-h)*.5,blur*2.,blur*2.)))
- ) + (
- // add rounded corners
- 0.
- +(
- circle2(xy,center+vec2(w,h)*.5-vec2(blur,blur), blur)
- // clip only 1/4 of a cirle
- *box( xy,center+vec2(w,h)*.5-vec2(blur,blur)*.5, blur,blur)
- )
- +(
- circle2(xy,center+vec2(w,-h)*.5-vec2(blur,-blur),blur)
- *box( xy,center+vec2(w,-h)*.5-vec2(blur,-blur)*.5, blur,blur)
- )
- +(
- circle2(xy,center+vec2(-w,h)*.5-vec2(-blur,blur),blur)
- *box( xy,center+vec2(-w,h)*.5-vec2(-blur,blur)*.5, blur,blur)
- )
- +(
- circle2(xy,center+vec2(-w,-h)*.5-vec2(-blur,-blur),blur)
- *box( xy,center+vec2(-w,-h)*.5-vec2(-blur,-blur)*.5, blur,blur)
- )
- )
- );
- }
- void fragment() {
- vec4 red = vec4(1,0,0,1);
- vec4 blood = red * noise(UV * 10.0);
- COLOR =
- blood * clamp(
- (
- 0.
- +roundedBox2(UV,vec2(.5,.5),.4,.8,0.12)
- +circle2(UV,vec2(.5,.6),.4)
- +circle2(UV,vec2(.8,.8),.2)
- )
- ,0.,1.
- )
- ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement