id
stringlengths 6
6
| author
stringclasses 55
values | date
timestamp[ns] | image_code
stringlengths 746
52.3k
| license
stringclasses 7
values | func_bytes
sequencelengths 5
5
| functions
sequencelengths 1
32
| comment
stringlengths 7
1.29k
| header
stringlengths 18
169
| body
stringlengths 18
2.14k
| model_inp
stringlengths 30
1.35k
| function_frequency
int64 1
176
| header_frequency
int64 1
16.3k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
lldyzM | ollj | 2018-07-20T10:46:29 | //self https://www.shadertoy.com/view/lldyzM
//analytical filter kernel,triangular
//https://www.shadertoy.com/view/llffWs
// Similar to https://www.shadertoy.com/view/XlXBWs,but with a triangular filter kernel,
// which produces less flickering animations that a box filter. Luckily,it's still easily
// http://iquilezles.org/www/articles/morecheckerfiltering/morecheckerfiltering.htm
// checker,2D,box filter: https://www.shadertoy.com/view/XlcSz2
// checker,3D,box filter: https://www.shadertoy.com/view/XlXBWs
// checker,3D,tri filter: https://www.shadertoy.com/view/llffWs
// grid,2D,box filter: https://www.shadertoy.com/view/XtBfzz
// The MIT License
//https://www.shadertoy.com/view/llffWs
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files(the "Software"),to dealthe 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 includedall 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.NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHERAN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF ORCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGSTHE SOFTWARE.
/* ---snippety blog summary esplanation
//fwidth(a,b)=abs(dfdx(p))+abs(dfdy(p))
#define maab(a,b)max(abs(a),abs(b))
the m-parameter is a value,returned from maab(),which may be calculated for multiple textures to be mixed,so it is moved out of the function.
//llffWs is tri-filtering AND box filtering,but it does not need the double integral,but other shaders calculate a double integral.
//there is this double-integral blog post this snioppet is all about:
the basic idea is to smoothen a discontinuity with an antiderivative
"cubic filters" are most common in CG,but this isr "tiangle-filter"ed weights,worse than cubic,better than the box filtering above
f(x)is the square-wave base signal that begs to be filtered.
box-filter formula is integralfromToOf(-w/2,w/2,f(x)dx)
tri-filter formula is integralfromToOf(-w,0,f(x)dx*(w+x)/w)+integralfromtoOf(0,w,f(x)dx*(w-x)/w)
these integrals are integralfromToOf(uv-w/2,uv+w/2,...),but shifting the center simplifies this function.
these integrals are done by [integration by parts],which has lots of symmetries that cancel each other out to:
tri-filter formula is(p(-w*.5)-2.*p(0.)+p(w*.5))/w/w
where p(x)is the antidetivative to f(x)(the striangle wave to the square wave.)
where s(x)is the double-integral of f(x)== the antidefivative of p(x)=an infinite smoothstep function.
//still no double-integrals needed!
vec3 sqrAndIntegrals(float x
){x*=.5;float h=fract(x)-.5,s=-sign(h),t=abs(h)*2.-1.)
;return(s,t,x+h*t);}//return vec3(square,tri(integral),smoothsteps(doubleIntegral))
vec2 fTri(vec2 x){vec2 h=fract(x*.5)-.5);return x*.5-h*(abs(h)*2.-1.);}//;return x*.5+h*(1.-2.*abs(h))
float TriFilteredCheckers(vec2 uv,vec2 w//w=maab(dpdx,dpdy)filter kernel
){w+.001
;vec2 i=(fTri(uw+w)-2.*fTri(uw)+ftri(uv-w))/(w*w)//analytic integral,3TapFilter function
;return .5-.5*i.x-i.y//xor-pattern
;}
//still no double-integrals needed!
//anyways,that would be curvature,what use is curvature for surface filtering?
*/
#define scale 5.
// spheres
const vec4 sc0=vec4(2,.5,.8,.5);
const vec4 sc1=vec4(-6,1,-4.,3);
const vec4 sc2=vec4(-16,1,7,4);
const vec4 sc3=vec4(-25,8,0,9);
struct v33{vec3 a;vec3 b;};
//and this ray-transpose function is the strangest of em all to be useful here:
void rayTransp(inout v33 a,inout v33 b){vec3 s=a.b;a.b=b.a;b.a=s;}//swap direction(.b)of [a] with origin(.a)of [b]
v33 sub(v33 a,vec3 b){return v33(a.a-b,a.b-b);}//substract b from all ray components
//component wise ray substraction(this one is a bit odd,differential wise,is basically scaling a rays points)
v33 subc(v33 a,v33 b){return v33(a.a-b.a,a.b-b.b);}//it makes sense in
v33 subc(vec2 a,v33 b){return v33(a.x-b.a,a.x-b.b);}//the context of
v33 subc(v33 a,vec2 b){return v33(a.a-b.x,a.b-b.y);}//v33-differentials for AA
vec2 dt(v33 a,v33 b){return vec2(dot(a.a,b.a),dot(a.b,b.b));}//dual dotprodiuct on v33s
vec2 dt(v33 a,vec3 b){return dt(a,v33(b,b));}
v33 div(v33 a,vec2 b){return v33(a.a/b.x,a.b/b.y);}
v33 mul(v33 a,v33 b){return v33(a.a*b.a,a.b*b.b);}//dual mult
v33 mul(v33 a,vec2 b){return v33(a.a*b.x,a.b*b.y);}
v33 mul(v33 a,float b){return v33(a.a*b,a.b*b);}
float sat(float a){return clamp(a,0.,1.);}
#define dd(a)dot(a,a)
//half-identity-scaling,labeled uN because it scales uv space,usually within a modulo context.
#define u2(a)((a)*2.-1.)
#define u5(a)((a)*.5+.5)
//u3(a)=1.-u2(a)!
//u6(a)=1.-u2(a)!
#define u3(a)(1.-(a)*2.)
#define u6(a)(.5-(a)*.5)
#define maab(a,b)max(abs(a),abs(b))
vec3 maab2(v33 a){return maab(a.a,a.b);}
float suv(vec3 a){return a.x+a.y+a.z;}
float prv(vec3 a){return a.x*a.y*a.z;}
float miv(vec2 a){return min(a.x,a.y);}
float miv(vec4 a){return min(miv(a.xy),miv(a.zw));}
float ss01(float a){return smoothstep(0.,1.,a);}
// ---unfiltered checkerboard ---
#define checker(a)mod(suv(floor(a)),2.)
//analytically triangle-filtered checkerboard: https://www.shadertoy.com/view/MtffWs
#define Fa(a,b)u2(abs(b-.5))
#define Fb(a,b)((a)*.5-((b)-.5)*Fa(a,b))
#define tri(a,b)b(a,fract((a)*.5))
//noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
float checkerF2(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p-.5*w,Fa)-tri(p+.5*w,Fa))/w));}//analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
float checkerF3(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p+w,Fb)-2.*tri(p,Fb)+tri(p-w,Fb))/(w*w)));}// analytical integral(tri filter),xor pattern
//sphere softShadow of(ray,sphere)
float sssp(v33 r,vec4 s){vec3 oc=s.xyz-r.a;float b=dot(oc,r.b),z=1.;if(b>0.){float h=dd(oc)-b*b-s.w*s.w;z=ss01(2.*h/b);}return z;}
//sphere occlusion
float occSphere(vec3 u,vec3 n,vec4 s){vec3 i=s.xyz-u;return 1.-dot(n,normalize(i))*s.w*s.w/dd(i);}
float iSphere(v33 r,vec4 s){vec3 e=r.a-s.xyz;float b=dot(r.b,e),h=b*b-dd(e)+s.w*s.w,t=-1.;if(h>0.)t=-b-sqrt(h);return t;}
float intersect(vec3 ro,vec3 rd,out vec3 pos,out vec3 n,out float occ,out float matid
){
;mat4 sc=mat4(sc0,sc1,sc2,sc3)
;float tmin=10000.
;n=vec3(0)
;occ=1.
;pos=vec3(0)
;float h=(.01-ro.y)/rd.y//plane
;if(h>0.
){tmin=h
;n=vec3(0,1,0)
;pos=ro+h*rd
;matid=0.
;occ=occSphere(pos,n,sc[0])*occSphere(pos,n,sc[1])*occSphere(pos,n,sc[2])*occSphere(pos,n,sc[3])
;}
;for(int i=0;i<4;i++){
;float h=iSphere(v33(ro,rd),sc[i])
;bool b=abs(h-.5*tmin)<tmin*.5//==h>0.&&h<tmin
;if(b){tmin=h;pos=ro+tmin*rd;n=normalize(ro+h*rd-sc[i].xyz);matid=1.;occ=u5(n.y);}}
;return tmin;}
void calcCamera(out vec3 ro,out vec3 ta){float an=.3*sin(.04*iTime);ro=vec3(5.5*cos(an),1.,5.5*sin(an));ta=vec3(0,1,0);}
vec3 doLighting(vec3 pos,vec3 rd,vec3 n,float occ
){ ;v33 rrr=v33(pos,rd)//seems to be a shared light source position
;float sh=miv(vec4(sssp(rrr,sc0),sssp(rrr,sc1),sssp(rrr,sc2),sssp(rrr,sc3)))
,dif=sat(dot(n,vec3(.57703)));float bac=sat(dot(n,vec3(-.707,.0,-.707)))
;vec3 lin=dif*sh*vec3(1.5,1.4,1.3)
;lin+=occ*vec3(.15,.2,.3);lin+=bac*vec3(.1);return lin;}
v33 calcRayForPixel(vec2 pix,vec2 res
){vec2 p=(-res.xy+2.0*pix)/res.y
;vec3 ro,ta
;calcCamera(ro,ta)
;vec3 w=normalize(ta-ro)
;vec3 u=normalize(cross(w,vec3(0,1,0)))
;vec3 rd=normalize(p.x*u+p.y*normalize(cross(u,w))+1.5*w)
;return v33(ro,rd);}
void mainImage(out vec4 fragColor,vec2 fragCoord
){vec2 res=vec2(iResolution.x/3.0,iResolution.y)
;int id=int(floor(fragCoord.x/res.x))
;vec2 px=vec2(fragCoord.x-float(id)*res.x,fragCoord.y)
;v33 r0=calcRayForPixel(px+vec2(0,0),res)
;vec3 pos,nor
;float occ,mid;float t=intersect(r0.a,r0.b,pos,nor,occ,mid)
;vec3 col=vec3(.9)
;if(t<100.
//todo,measure angle between normal and rayDirection,and only do #if 1 for anggles>45deg;
//todo,there is a precision fix for near-orthogonal normals to camera that may be good here.
#if 1
){vec3 uvw=pos*scale//analytic ray-differential is in object-space
;v33 rx=calcRayForPixel(px+vec2(1,0),res);
;v33 ry=calcRayForPixel(px+vec2(0,1),res);
;rayTransp(rx,ry)//swap rx.b with ry.a and the lines below become more symmetric: yes,this swaps the origin of one ray with the direction of another.
;v33 w=mul(ry,dt(sub(rx,pos),nor)/dt(ry,nor))
;w=subc(rx,w)
;w=mul(sub(w,pos),scale)
#else
){vec3 uvw=pos*scale;v33 w=v33(dFdx(uvw),dFdy(uvw))//semi-analogously use dFdx()dFdy()in screenspace has bad borders
#endif
;vec3 m=vec3(0)
;w.a=maab2(w)
;if(id==0)m=vec3(1)*checker(uvw)
;else if(id==1)m=vec3(1)*checkerF2(uvw,w.a)
;else if(id==2)m=vec3(1)*checkerF3(uvw,w.a)
;col=m*doLighting(pos,vec3(.57703),nor,occ)
//;col=mix(col,vec3(.9),1.-exp(-.0001*t*t))// fog
;}
;col=pow(col,vec3(.4545))//gamma
;col*=smoothstep(2.,3.,abs(px.x))//frame border lines
;fragColor=vec4(col,1);}
| mit | [
4543,
4570,
4592,
4592,
4615
] | [
[
3872,
3952,
3992,
3992,
4018
],
[
4018,
4067,
4089,
4089,
4114
],
[
4114,
4262,
4285,
4285,
4314
],
[
4314,
4334,
4357,
4357,
4386
],
[
4386,
4403,
4426,
4426,
4455
],
[
4455,
4482,
4503,
4503,
4543
],
[
4543,
4570,
4592,
4592,
4615
],
[
4616,
4616,
4638,
4638,
4667
],
[
4668,
4668,
4689,
4689,
4718
],
[
4718,
4730,
4752,
4752,
4781
],
[
4782,
4782,
4805,
4805,
4830
],
[
4833,
4833,
4852,
4852,
4875
],
[
5165,
5165,
5183,
5183,
5205
],
[
5206,
5206,
5224,
5224,
5244
],
[
5245,
5245,
5263,
5263,
5283
],
[
5284,
5284,
5302,
5302,
5323
],
[
5324,
5324,
5342,
5342,
5375
],
[
5376,
5376,
5396,
5396,
5424
],
[
5691,
5837,
5868,
5868,
6013
],
[
6013,
6130,
6161,
6161,
6317
],
[
6365,
6400,
6425,
6425,
6530
],
[
6531,
6550,
6588,
6588,
6648
],
[
6650,
6650,
6678,
6678,
6771
],
[
6773,
6773,
6861,
6861,
7360
],
[
7362,
7362,
7403,
7403,
7482
],
[
7484,
7484,
7536,
7536,
7847
],
[
7849,
7849,
7889,
7889,
8100
]
] | //dual dotprodiuct on v33s
| vec2 dt(v33 a,vec3 b){ | return dt(a,v33(b,b));} | //dual dotprodiuct on v33s
vec2 dt(v33 a,vec3 b){ | 1 | 1 |
lldyzM | ollj | 2018-07-20T10:46:29 | //self https://www.shadertoy.com/view/lldyzM
//analytical filter kernel,triangular
//https://www.shadertoy.com/view/llffWs
// Similar to https://www.shadertoy.com/view/XlXBWs,but with a triangular filter kernel,
// which produces less flickering animations that a box filter. Luckily,it's still easily
// http://iquilezles.org/www/articles/morecheckerfiltering/morecheckerfiltering.htm
// checker,2D,box filter: https://www.shadertoy.com/view/XlcSz2
// checker,3D,box filter: https://www.shadertoy.com/view/XlXBWs
// checker,3D,tri filter: https://www.shadertoy.com/view/llffWs
// grid,2D,box filter: https://www.shadertoy.com/view/XtBfzz
// The MIT License
//https://www.shadertoy.com/view/llffWs
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files(the "Software"),to dealthe 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 includedall 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.NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHERAN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF ORCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGSTHE SOFTWARE.
/* ---snippety blog summary esplanation
//fwidth(a,b)=abs(dfdx(p))+abs(dfdy(p))
#define maab(a,b)max(abs(a),abs(b))
the m-parameter is a value,returned from maab(),which may be calculated for multiple textures to be mixed,so it is moved out of the function.
//llffWs is tri-filtering AND box filtering,but it does not need the double integral,but other shaders calculate a double integral.
//there is this double-integral blog post this snioppet is all about:
the basic idea is to smoothen a discontinuity with an antiderivative
"cubic filters" are most common in CG,but this isr "tiangle-filter"ed weights,worse than cubic,better than the box filtering above
f(x)is the square-wave base signal that begs to be filtered.
box-filter formula is integralfromToOf(-w/2,w/2,f(x)dx)
tri-filter formula is integralfromToOf(-w,0,f(x)dx*(w+x)/w)+integralfromtoOf(0,w,f(x)dx*(w-x)/w)
these integrals are integralfromToOf(uv-w/2,uv+w/2,...),but shifting the center simplifies this function.
these integrals are done by [integration by parts],which has lots of symmetries that cancel each other out to:
tri-filter formula is(p(-w*.5)-2.*p(0.)+p(w*.5))/w/w
where p(x)is the antidetivative to f(x)(the striangle wave to the square wave.)
where s(x)is the double-integral of f(x)== the antidefivative of p(x)=an infinite smoothstep function.
//still no double-integrals needed!
vec3 sqrAndIntegrals(float x
){x*=.5;float h=fract(x)-.5,s=-sign(h),t=abs(h)*2.-1.)
;return(s,t,x+h*t);}//return vec3(square,tri(integral),smoothsteps(doubleIntegral))
vec2 fTri(vec2 x){vec2 h=fract(x*.5)-.5);return x*.5-h*(abs(h)*2.-1.);}//;return x*.5+h*(1.-2.*abs(h))
float TriFilteredCheckers(vec2 uv,vec2 w//w=maab(dpdx,dpdy)filter kernel
){w+.001
;vec2 i=(fTri(uw+w)-2.*fTri(uw)+ftri(uv-w))/(w*w)//analytic integral,3TapFilter function
;return .5-.5*i.x-i.y//xor-pattern
;}
//still no double-integrals needed!
//anyways,that would be curvature,what use is curvature for surface filtering?
*/
#define scale 5.
// spheres
const vec4 sc0=vec4(2,.5,.8,.5);
const vec4 sc1=vec4(-6,1,-4.,3);
const vec4 sc2=vec4(-16,1,7,4);
const vec4 sc3=vec4(-25,8,0,9);
struct v33{vec3 a;vec3 b;};
//and this ray-transpose function is the strangest of em all to be useful here:
void rayTransp(inout v33 a,inout v33 b){vec3 s=a.b;a.b=b.a;b.a=s;}//swap direction(.b)of [a] with origin(.a)of [b]
v33 sub(v33 a,vec3 b){return v33(a.a-b,a.b-b);}//substract b from all ray components
//component wise ray substraction(this one is a bit odd,differential wise,is basically scaling a rays points)
v33 subc(v33 a,v33 b){return v33(a.a-b.a,a.b-b.b);}//it makes sense in
v33 subc(vec2 a,v33 b){return v33(a.x-b.a,a.x-b.b);}//the context of
v33 subc(v33 a,vec2 b){return v33(a.a-b.x,a.b-b.y);}//v33-differentials for AA
vec2 dt(v33 a,v33 b){return vec2(dot(a.a,b.a),dot(a.b,b.b));}//dual dotprodiuct on v33s
vec2 dt(v33 a,vec3 b){return dt(a,v33(b,b));}
v33 div(v33 a,vec2 b){return v33(a.a/b.x,a.b/b.y);}
v33 mul(v33 a,v33 b){return v33(a.a*b.a,a.b*b.b);}//dual mult
v33 mul(v33 a,vec2 b){return v33(a.a*b.x,a.b*b.y);}
v33 mul(v33 a,float b){return v33(a.a*b,a.b*b);}
float sat(float a){return clamp(a,0.,1.);}
#define dd(a)dot(a,a)
//half-identity-scaling,labeled uN because it scales uv space,usually within a modulo context.
#define u2(a)((a)*2.-1.)
#define u5(a)((a)*.5+.5)
//u3(a)=1.-u2(a)!
//u6(a)=1.-u2(a)!
#define u3(a)(1.-(a)*2.)
#define u6(a)(.5-(a)*.5)
#define maab(a,b)max(abs(a),abs(b))
vec3 maab2(v33 a){return maab(a.a,a.b);}
float suv(vec3 a){return a.x+a.y+a.z;}
float prv(vec3 a){return a.x*a.y*a.z;}
float miv(vec2 a){return min(a.x,a.y);}
float miv(vec4 a){return min(miv(a.xy),miv(a.zw));}
float ss01(float a){return smoothstep(0.,1.,a);}
// ---unfiltered checkerboard ---
#define checker(a)mod(suv(floor(a)),2.)
//analytically triangle-filtered checkerboard: https://www.shadertoy.com/view/MtffWs
#define Fa(a,b)u2(abs(b-.5))
#define Fb(a,b)((a)*.5-((b)-.5)*Fa(a,b))
#define tri(a,b)b(a,fract((a)*.5))
//noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
float checkerF2(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p-.5*w,Fa)-tri(p+.5*w,Fa))/w));}//analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
float checkerF3(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p+w,Fb)-2.*tri(p,Fb)+tri(p-w,Fb))/(w*w)));}// analytical integral(tri filter),xor pattern
//sphere softShadow of(ray,sphere)
float sssp(v33 r,vec4 s){vec3 oc=s.xyz-r.a;float b=dot(oc,r.b),z=1.;if(b>0.){float h=dd(oc)-b*b-s.w*s.w;z=ss01(2.*h/b);}return z;}
//sphere occlusion
float occSphere(vec3 u,vec3 n,vec4 s){vec3 i=s.xyz-u;return 1.-dot(n,normalize(i))*s.w*s.w/dd(i);}
float iSphere(v33 r,vec4 s){vec3 e=r.a-s.xyz;float b=dot(r.b,e),h=b*b-dd(e)+s.w*s.w,t=-1.;if(h>0.)t=-b-sqrt(h);return t;}
float intersect(vec3 ro,vec3 rd,out vec3 pos,out vec3 n,out float occ,out float matid
){
;mat4 sc=mat4(sc0,sc1,sc2,sc3)
;float tmin=10000.
;n=vec3(0)
;occ=1.
;pos=vec3(0)
;float h=(.01-ro.y)/rd.y//plane
;if(h>0.
){tmin=h
;n=vec3(0,1,0)
;pos=ro+h*rd
;matid=0.
;occ=occSphere(pos,n,sc[0])*occSphere(pos,n,sc[1])*occSphere(pos,n,sc[2])*occSphere(pos,n,sc[3])
;}
;for(int i=0;i<4;i++){
;float h=iSphere(v33(ro,rd),sc[i])
;bool b=abs(h-.5*tmin)<tmin*.5//==h>0.&&h<tmin
;if(b){tmin=h;pos=ro+tmin*rd;n=normalize(ro+h*rd-sc[i].xyz);matid=1.;occ=u5(n.y);}}
;return tmin;}
void calcCamera(out vec3 ro,out vec3 ta){float an=.3*sin(.04*iTime);ro=vec3(5.5*cos(an),1.,5.5*sin(an));ta=vec3(0,1,0);}
vec3 doLighting(vec3 pos,vec3 rd,vec3 n,float occ
){ ;v33 rrr=v33(pos,rd)//seems to be a shared light source position
;float sh=miv(vec4(sssp(rrr,sc0),sssp(rrr,sc1),sssp(rrr,sc2),sssp(rrr,sc3)))
,dif=sat(dot(n,vec3(.57703)));float bac=sat(dot(n,vec3(-.707,.0,-.707)))
;vec3 lin=dif*sh*vec3(1.5,1.4,1.3)
;lin+=occ*vec3(.15,.2,.3);lin+=bac*vec3(.1);return lin;}
v33 calcRayForPixel(vec2 pix,vec2 res
){vec2 p=(-res.xy+2.0*pix)/res.y
;vec3 ro,ta
;calcCamera(ro,ta)
;vec3 w=normalize(ta-ro)
;vec3 u=normalize(cross(w,vec3(0,1,0)))
;vec3 rd=normalize(p.x*u+p.y*normalize(cross(u,w))+1.5*w)
;return v33(ro,rd);}
void mainImage(out vec4 fragColor,vec2 fragCoord
){vec2 res=vec2(iResolution.x/3.0,iResolution.y)
;int id=int(floor(fragCoord.x/res.x))
;vec2 px=vec2(fragCoord.x-float(id)*res.x,fragCoord.y)
;v33 r0=calcRayForPixel(px+vec2(0,0),res)
;vec3 pos,nor
;float occ,mid;float t=intersect(r0.a,r0.b,pos,nor,occ,mid)
;vec3 col=vec3(.9)
;if(t<100.
//todo,measure angle between normal and rayDirection,and only do #if 1 for anggles>45deg;
//todo,there is a precision fix for near-orthogonal normals to camera that may be good here.
#if 1
){vec3 uvw=pos*scale//analytic ray-differential is in object-space
;v33 rx=calcRayForPixel(px+vec2(1,0),res);
;v33 ry=calcRayForPixel(px+vec2(0,1),res);
;rayTransp(rx,ry)//swap rx.b with ry.a and the lines below become more symmetric: yes,this swaps the origin of one ray with the direction of another.
;v33 w=mul(ry,dt(sub(rx,pos),nor)/dt(ry,nor))
;w=subc(rx,w)
;w=mul(sub(w,pos),scale)
#else
){vec3 uvw=pos*scale;v33 w=v33(dFdx(uvw),dFdy(uvw))//semi-analogously use dFdx()dFdy()in screenspace has bad borders
#endif
;vec3 m=vec3(0)
;w.a=maab2(w)
;if(id==0)m=vec3(1)*checker(uvw)
;else if(id==1)m=vec3(1)*checkerF2(uvw,w.a)
;else if(id==2)m=vec3(1)*checkerF3(uvw,w.a)
;col=m*doLighting(pos,vec3(.57703),nor,occ)
//;col=mix(col,vec3(.9),1.-exp(-.0001*t*t))// fog
;}
;col=pow(col,vec3(.4545))//gamma
;col*=smoothstep(2.,3.,abs(px.x))//frame border lines
;fragColor=vec4(col,1);}
| mit | [
4718,
4730,
4752,
4752,
4781
] | [
[
3872,
3952,
3992,
3992,
4018
],
[
4018,
4067,
4089,
4089,
4114
],
[
4114,
4262,
4285,
4285,
4314
],
[
4314,
4334,
4357,
4357,
4386
],
[
4386,
4403,
4426,
4426,
4455
],
[
4455,
4482,
4503,
4503,
4543
],
[
4543,
4570,
4592,
4592,
4615
],
[
4616,
4616,
4638,
4638,
4667
],
[
4668,
4668,
4689,
4689,
4718
],
[
4718,
4730,
4752,
4752,
4781
],
[
4782,
4782,
4805,
4805,
4830
],
[
4833,
4833,
4852,
4852,
4875
],
[
5165,
5165,
5183,
5183,
5205
],
[
5206,
5206,
5224,
5224,
5244
],
[
5245,
5245,
5263,
5263,
5283
],
[
5284,
5284,
5302,
5302,
5323
],
[
5324,
5324,
5342,
5342,
5375
],
[
5376,
5376,
5396,
5396,
5424
],
[
5691,
5837,
5868,
5868,
6013
],
[
6013,
6130,
6161,
6161,
6317
],
[
6365,
6400,
6425,
6425,
6530
],
[
6531,
6550,
6588,
6588,
6648
],
[
6650,
6650,
6678,
6678,
6771
],
[
6773,
6773,
6861,
6861,
7360
],
[
7362,
7362,
7403,
7403,
7482
],
[
7484,
7484,
7536,
7536,
7847
],
[
7849,
7849,
7889,
7889,
8100
]
] | //dual mult
| v33 mul(v33 a,vec2 b){ | return v33(a.a*b.x,a.b*b.y);} | //dual mult
v33 mul(v33 a,vec2 b){ | 1 | 1 |
lldyzM | ollj | 2018-07-20T10:46:29 | //self https://www.shadertoy.com/view/lldyzM
//analytical filter kernel,triangular
//https://www.shadertoy.com/view/llffWs
// Similar to https://www.shadertoy.com/view/XlXBWs,but with a triangular filter kernel,
// which produces less flickering animations that a box filter. Luckily,it's still easily
// http://iquilezles.org/www/articles/morecheckerfiltering/morecheckerfiltering.htm
// checker,2D,box filter: https://www.shadertoy.com/view/XlcSz2
// checker,3D,box filter: https://www.shadertoy.com/view/XlXBWs
// checker,3D,tri filter: https://www.shadertoy.com/view/llffWs
// grid,2D,box filter: https://www.shadertoy.com/view/XtBfzz
// The MIT License
//https://www.shadertoy.com/view/llffWs
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files(the "Software"),to dealthe 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 includedall 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.NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHERAN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF ORCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGSTHE SOFTWARE.
/* ---snippety blog summary esplanation
//fwidth(a,b)=abs(dfdx(p))+abs(dfdy(p))
#define maab(a,b)max(abs(a),abs(b))
the m-parameter is a value,returned from maab(),which may be calculated for multiple textures to be mixed,so it is moved out of the function.
//llffWs is tri-filtering AND box filtering,but it does not need the double integral,but other shaders calculate a double integral.
//there is this double-integral blog post this snioppet is all about:
the basic idea is to smoothen a discontinuity with an antiderivative
"cubic filters" are most common in CG,but this isr "tiangle-filter"ed weights,worse than cubic,better than the box filtering above
f(x)is the square-wave base signal that begs to be filtered.
box-filter formula is integralfromToOf(-w/2,w/2,f(x)dx)
tri-filter formula is integralfromToOf(-w,0,f(x)dx*(w+x)/w)+integralfromtoOf(0,w,f(x)dx*(w-x)/w)
these integrals are integralfromToOf(uv-w/2,uv+w/2,...),but shifting the center simplifies this function.
these integrals are done by [integration by parts],which has lots of symmetries that cancel each other out to:
tri-filter formula is(p(-w*.5)-2.*p(0.)+p(w*.5))/w/w
where p(x)is the antidetivative to f(x)(the striangle wave to the square wave.)
where s(x)is the double-integral of f(x)== the antidefivative of p(x)=an infinite smoothstep function.
//still no double-integrals needed!
vec3 sqrAndIntegrals(float x
){x*=.5;float h=fract(x)-.5,s=-sign(h),t=abs(h)*2.-1.)
;return(s,t,x+h*t);}//return vec3(square,tri(integral),smoothsteps(doubleIntegral))
vec2 fTri(vec2 x){vec2 h=fract(x*.5)-.5);return x*.5-h*(abs(h)*2.-1.);}//;return x*.5+h*(1.-2.*abs(h))
float TriFilteredCheckers(vec2 uv,vec2 w//w=maab(dpdx,dpdy)filter kernel
){w+.001
;vec2 i=(fTri(uw+w)-2.*fTri(uw)+ftri(uv-w))/(w*w)//analytic integral,3TapFilter function
;return .5-.5*i.x-i.y//xor-pattern
;}
//still no double-integrals needed!
//anyways,that would be curvature,what use is curvature for surface filtering?
*/
#define scale 5.
// spheres
const vec4 sc0=vec4(2,.5,.8,.5);
const vec4 sc1=vec4(-6,1,-4.,3);
const vec4 sc2=vec4(-16,1,7,4);
const vec4 sc3=vec4(-25,8,0,9);
struct v33{vec3 a;vec3 b;};
//and this ray-transpose function is the strangest of em all to be useful here:
void rayTransp(inout v33 a,inout v33 b){vec3 s=a.b;a.b=b.a;b.a=s;}//swap direction(.b)of [a] with origin(.a)of [b]
v33 sub(v33 a,vec3 b){return v33(a.a-b,a.b-b);}//substract b from all ray components
//component wise ray substraction(this one is a bit odd,differential wise,is basically scaling a rays points)
v33 subc(v33 a,v33 b){return v33(a.a-b.a,a.b-b.b);}//it makes sense in
v33 subc(vec2 a,v33 b){return v33(a.x-b.a,a.x-b.b);}//the context of
v33 subc(v33 a,vec2 b){return v33(a.a-b.x,a.b-b.y);}//v33-differentials for AA
vec2 dt(v33 a,v33 b){return vec2(dot(a.a,b.a),dot(a.b,b.b));}//dual dotprodiuct on v33s
vec2 dt(v33 a,vec3 b){return dt(a,v33(b,b));}
v33 div(v33 a,vec2 b){return v33(a.a/b.x,a.b/b.y);}
v33 mul(v33 a,v33 b){return v33(a.a*b.a,a.b*b.b);}//dual mult
v33 mul(v33 a,vec2 b){return v33(a.a*b.x,a.b*b.y);}
v33 mul(v33 a,float b){return v33(a.a*b,a.b*b);}
float sat(float a){return clamp(a,0.,1.);}
#define dd(a)dot(a,a)
//half-identity-scaling,labeled uN because it scales uv space,usually within a modulo context.
#define u2(a)((a)*2.-1.)
#define u5(a)((a)*.5+.5)
//u3(a)=1.-u2(a)!
//u6(a)=1.-u2(a)!
#define u3(a)(1.-(a)*2.)
#define u6(a)(.5-(a)*.5)
#define maab(a,b)max(abs(a),abs(b))
vec3 maab2(v33 a){return maab(a.a,a.b);}
float suv(vec3 a){return a.x+a.y+a.z;}
float prv(vec3 a){return a.x*a.y*a.z;}
float miv(vec2 a){return min(a.x,a.y);}
float miv(vec4 a){return min(miv(a.xy),miv(a.zw));}
float ss01(float a){return smoothstep(0.,1.,a);}
// ---unfiltered checkerboard ---
#define checker(a)mod(suv(floor(a)),2.)
//analytically triangle-filtered checkerboard: https://www.shadertoy.com/view/MtffWs
#define Fa(a,b)u2(abs(b-.5))
#define Fb(a,b)((a)*.5-((b)-.5)*Fa(a,b))
#define tri(a,b)b(a,fract((a)*.5))
//noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
float checkerF2(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p-.5*w,Fa)-tri(p+.5*w,Fa))/w));}//analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
float checkerF3(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p+w,Fb)-2.*tri(p,Fb)+tri(p-w,Fb))/(w*w)));}// analytical integral(tri filter),xor pattern
//sphere softShadow of(ray,sphere)
float sssp(v33 r,vec4 s){vec3 oc=s.xyz-r.a;float b=dot(oc,r.b),z=1.;if(b>0.){float h=dd(oc)-b*b-s.w*s.w;z=ss01(2.*h/b);}return z;}
//sphere occlusion
float occSphere(vec3 u,vec3 n,vec4 s){vec3 i=s.xyz-u;return 1.-dot(n,normalize(i))*s.w*s.w/dd(i);}
float iSphere(v33 r,vec4 s){vec3 e=r.a-s.xyz;float b=dot(r.b,e),h=b*b-dd(e)+s.w*s.w,t=-1.;if(h>0.)t=-b-sqrt(h);return t;}
float intersect(vec3 ro,vec3 rd,out vec3 pos,out vec3 n,out float occ,out float matid
){
;mat4 sc=mat4(sc0,sc1,sc2,sc3)
;float tmin=10000.
;n=vec3(0)
;occ=1.
;pos=vec3(0)
;float h=(.01-ro.y)/rd.y//plane
;if(h>0.
){tmin=h
;n=vec3(0,1,0)
;pos=ro+h*rd
;matid=0.
;occ=occSphere(pos,n,sc[0])*occSphere(pos,n,sc[1])*occSphere(pos,n,sc[2])*occSphere(pos,n,sc[3])
;}
;for(int i=0;i<4;i++){
;float h=iSphere(v33(ro,rd),sc[i])
;bool b=abs(h-.5*tmin)<tmin*.5//==h>0.&&h<tmin
;if(b){tmin=h;pos=ro+tmin*rd;n=normalize(ro+h*rd-sc[i].xyz);matid=1.;occ=u5(n.y);}}
;return tmin;}
void calcCamera(out vec3 ro,out vec3 ta){float an=.3*sin(.04*iTime);ro=vec3(5.5*cos(an),1.,5.5*sin(an));ta=vec3(0,1,0);}
vec3 doLighting(vec3 pos,vec3 rd,vec3 n,float occ
){ ;v33 rrr=v33(pos,rd)//seems to be a shared light source position
;float sh=miv(vec4(sssp(rrr,sc0),sssp(rrr,sc1),sssp(rrr,sc2),sssp(rrr,sc3)))
,dif=sat(dot(n,vec3(.57703)));float bac=sat(dot(n,vec3(-.707,.0,-.707)))
;vec3 lin=dif*sh*vec3(1.5,1.4,1.3)
;lin+=occ*vec3(.15,.2,.3);lin+=bac*vec3(.1);return lin;}
v33 calcRayForPixel(vec2 pix,vec2 res
){vec2 p=(-res.xy+2.0*pix)/res.y
;vec3 ro,ta
;calcCamera(ro,ta)
;vec3 w=normalize(ta-ro)
;vec3 u=normalize(cross(w,vec3(0,1,0)))
;vec3 rd=normalize(p.x*u+p.y*normalize(cross(u,w))+1.5*w)
;return v33(ro,rd);}
void mainImage(out vec4 fragColor,vec2 fragCoord
){vec2 res=vec2(iResolution.x/3.0,iResolution.y)
;int id=int(floor(fragCoord.x/res.x))
;vec2 px=vec2(fragCoord.x-float(id)*res.x,fragCoord.y)
;v33 r0=calcRayForPixel(px+vec2(0,0),res)
;vec3 pos,nor
;float occ,mid;float t=intersect(r0.a,r0.b,pos,nor,occ,mid)
;vec3 col=vec3(.9)
;if(t<100.
//todo,measure angle between normal and rayDirection,and only do #if 1 for anggles>45deg;
//todo,there is a precision fix for near-orthogonal normals to camera that may be good here.
#if 1
){vec3 uvw=pos*scale//analytic ray-differential is in object-space
;v33 rx=calcRayForPixel(px+vec2(1,0),res);
;v33 ry=calcRayForPixel(px+vec2(0,1),res);
;rayTransp(rx,ry)//swap rx.b with ry.a and the lines below become more symmetric: yes,this swaps the origin of one ray with the direction of another.
;v33 w=mul(ry,dt(sub(rx,pos),nor)/dt(ry,nor))
;w=subc(rx,w)
;w=mul(sub(w,pos),scale)
#else
){vec3 uvw=pos*scale;v33 w=v33(dFdx(uvw),dFdy(uvw))//semi-analogously use dFdx()dFdy()in screenspace has bad borders
#endif
;vec3 m=vec3(0)
;w.a=maab2(w)
;if(id==0)m=vec3(1)*checker(uvw)
;else if(id==1)m=vec3(1)*checkerF2(uvw,w.a)
;else if(id==2)m=vec3(1)*checkerF3(uvw,w.a)
;col=m*doLighting(pos,vec3(.57703),nor,occ)
//;col=mix(col,vec3(.9),1.-exp(-.0001*t*t))// fog
;}
;col=pow(col,vec3(.4545))//gamma
;col*=smoothstep(2.,3.,abs(px.x))//frame border lines
;fragColor=vec4(col,1);}
| mit | [
5691,
5837,
5868,
5868,
6013
] | [
[
3872,
3952,
3992,
3992,
4018
],
[
4018,
4067,
4089,
4089,
4114
],
[
4114,
4262,
4285,
4285,
4314
],
[
4314,
4334,
4357,
4357,
4386
],
[
4386,
4403,
4426,
4426,
4455
],
[
4455,
4482,
4503,
4503,
4543
],
[
4543,
4570,
4592,
4592,
4615
],
[
4616,
4616,
4638,
4638,
4667
],
[
4668,
4668,
4689,
4689,
4718
],
[
4718,
4730,
4752,
4752,
4781
],
[
4782,
4782,
4805,
4805,
4830
],
[
4833,
4833,
4852,
4852,
4875
],
[
5165,
5165,
5183,
5183,
5205
],
[
5206,
5206,
5224,
5224,
5244
],
[
5245,
5245,
5263,
5263,
5283
],
[
5284,
5284,
5302,
5302,
5323
],
[
5324,
5324,
5342,
5342,
5375
],
[
5376,
5376,
5396,
5396,
5424
],
[
5691,
5837,
5868,
5868,
6013
],
[
6013,
6130,
6161,
6161,
6317
],
[
6365,
6400,
6425,
6425,
6530
],
[
6531,
6550,
6588,
6588,
6648
],
[
6650,
6650,
6678,
6678,
6771
],
[
6773,
6773,
6861,
6861,
7360
],
[
7362,
7362,
7403,
7403,
7482
],
[
7484,
7484,
7536,
7536,
7847
],
[
7849,
7849,
7889,
7889,
8100
]
] | //noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
| float checkerF2(vec3 p,vec3 w){ | w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p-.5*w,Fa)-tri(p+.5*w,Fa))/w));} | //noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
float checkerF2(vec3 p,vec3 w){ | 1 | 1 |
lldyzM | ollj | 2018-07-20T10:46:29 | //self https://www.shadertoy.com/view/lldyzM
//analytical filter kernel,triangular
//https://www.shadertoy.com/view/llffWs
// Similar to https://www.shadertoy.com/view/XlXBWs,but with a triangular filter kernel,
// which produces less flickering animations that a box filter. Luckily,it's still easily
// http://iquilezles.org/www/articles/morecheckerfiltering/morecheckerfiltering.htm
// checker,2D,box filter: https://www.shadertoy.com/view/XlcSz2
// checker,3D,box filter: https://www.shadertoy.com/view/XlXBWs
// checker,3D,tri filter: https://www.shadertoy.com/view/llffWs
// grid,2D,box filter: https://www.shadertoy.com/view/XtBfzz
// The MIT License
//https://www.shadertoy.com/view/llffWs
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files(the "Software"),to dealthe 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 includedall 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.NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHERAN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF ORCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGSTHE SOFTWARE.
/* ---snippety blog summary esplanation
//fwidth(a,b)=abs(dfdx(p))+abs(dfdy(p))
#define maab(a,b)max(abs(a),abs(b))
the m-parameter is a value,returned from maab(),which may be calculated for multiple textures to be mixed,so it is moved out of the function.
//llffWs is tri-filtering AND box filtering,but it does not need the double integral,but other shaders calculate a double integral.
//there is this double-integral blog post this snioppet is all about:
the basic idea is to smoothen a discontinuity with an antiderivative
"cubic filters" are most common in CG,but this isr "tiangle-filter"ed weights,worse than cubic,better than the box filtering above
f(x)is the square-wave base signal that begs to be filtered.
box-filter formula is integralfromToOf(-w/2,w/2,f(x)dx)
tri-filter formula is integralfromToOf(-w,0,f(x)dx*(w+x)/w)+integralfromtoOf(0,w,f(x)dx*(w-x)/w)
these integrals are integralfromToOf(uv-w/2,uv+w/2,...),but shifting the center simplifies this function.
these integrals are done by [integration by parts],which has lots of symmetries that cancel each other out to:
tri-filter formula is(p(-w*.5)-2.*p(0.)+p(w*.5))/w/w
where p(x)is the antidetivative to f(x)(the striangle wave to the square wave.)
where s(x)is the double-integral of f(x)== the antidefivative of p(x)=an infinite smoothstep function.
//still no double-integrals needed!
vec3 sqrAndIntegrals(float x
){x*=.5;float h=fract(x)-.5,s=-sign(h),t=abs(h)*2.-1.)
;return(s,t,x+h*t);}//return vec3(square,tri(integral),smoothsteps(doubleIntegral))
vec2 fTri(vec2 x){vec2 h=fract(x*.5)-.5);return x*.5-h*(abs(h)*2.-1.);}//;return x*.5+h*(1.-2.*abs(h))
float TriFilteredCheckers(vec2 uv,vec2 w//w=maab(dpdx,dpdy)filter kernel
){w+.001
;vec2 i=(fTri(uw+w)-2.*fTri(uw)+ftri(uv-w))/(w*w)//analytic integral,3TapFilter function
;return .5-.5*i.x-i.y//xor-pattern
;}
//still no double-integrals needed!
//anyways,that would be curvature,what use is curvature for surface filtering?
*/
#define scale 5.
// spheres
const vec4 sc0=vec4(2,.5,.8,.5);
const vec4 sc1=vec4(-6,1,-4.,3);
const vec4 sc2=vec4(-16,1,7,4);
const vec4 sc3=vec4(-25,8,0,9);
struct v33{vec3 a;vec3 b;};
//and this ray-transpose function is the strangest of em all to be useful here:
void rayTransp(inout v33 a,inout v33 b){vec3 s=a.b;a.b=b.a;b.a=s;}//swap direction(.b)of [a] with origin(.a)of [b]
v33 sub(v33 a,vec3 b){return v33(a.a-b,a.b-b);}//substract b from all ray components
//component wise ray substraction(this one is a bit odd,differential wise,is basically scaling a rays points)
v33 subc(v33 a,v33 b){return v33(a.a-b.a,a.b-b.b);}//it makes sense in
v33 subc(vec2 a,v33 b){return v33(a.x-b.a,a.x-b.b);}//the context of
v33 subc(v33 a,vec2 b){return v33(a.a-b.x,a.b-b.y);}//v33-differentials for AA
vec2 dt(v33 a,v33 b){return vec2(dot(a.a,b.a),dot(a.b,b.b));}//dual dotprodiuct on v33s
vec2 dt(v33 a,vec3 b){return dt(a,v33(b,b));}
v33 div(v33 a,vec2 b){return v33(a.a/b.x,a.b/b.y);}
v33 mul(v33 a,v33 b){return v33(a.a*b.a,a.b*b.b);}//dual mult
v33 mul(v33 a,vec2 b){return v33(a.a*b.x,a.b*b.y);}
v33 mul(v33 a,float b){return v33(a.a*b,a.b*b);}
float sat(float a){return clamp(a,0.,1.);}
#define dd(a)dot(a,a)
//half-identity-scaling,labeled uN because it scales uv space,usually within a modulo context.
#define u2(a)((a)*2.-1.)
#define u5(a)((a)*.5+.5)
//u3(a)=1.-u2(a)!
//u6(a)=1.-u2(a)!
#define u3(a)(1.-(a)*2.)
#define u6(a)(.5-(a)*.5)
#define maab(a,b)max(abs(a),abs(b))
vec3 maab2(v33 a){return maab(a.a,a.b);}
float suv(vec3 a){return a.x+a.y+a.z;}
float prv(vec3 a){return a.x*a.y*a.z;}
float miv(vec2 a){return min(a.x,a.y);}
float miv(vec4 a){return min(miv(a.xy),miv(a.zw));}
float ss01(float a){return smoothstep(0.,1.,a);}
// ---unfiltered checkerboard ---
#define checker(a)mod(suv(floor(a)),2.)
//analytically triangle-filtered checkerboard: https://www.shadertoy.com/view/MtffWs
#define Fa(a,b)u2(abs(b-.5))
#define Fb(a,b)((a)*.5-((b)-.5)*Fa(a,b))
#define tri(a,b)b(a,fract((a)*.5))
//noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
float checkerF2(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p-.5*w,Fa)-tri(p+.5*w,Fa))/w));}//analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
float checkerF3(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p+w,Fb)-2.*tri(p,Fb)+tri(p-w,Fb))/(w*w)));}// analytical integral(tri filter),xor pattern
//sphere softShadow of(ray,sphere)
float sssp(v33 r,vec4 s){vec3 oc=s.xyz-r.a;float b=dot(oc,r.b),z=1.;if(b>0.){float h=dd(oc)-b*b-s.w*s.w;z=ss01(2.*h/b);}return z;}
//sphere occlusion
float occSphere(vec3 u,vec3 n,vec4 s){vec3 i=s.xyz-u;return 1.-dot(n,normalize(i))*s.w*s.w/dd(i);}
float iSphere(v33 r,vec4 s){vec3 e=r.a-s.xyz;float b=dot(r.b,e),h=b*b-dd(e)+s.w*s.w,t=-1.;if(h>0.)t=-b-sqrt(h);return t;}
float intersect(vec3 ro,vec3 rd,out vec3 pos,out vec3 n,out float occ,out float matid
){
;mat4 sc=mat4(sc0,sc1,sc2,sc3)
;float tmin=10000.
;n=vec3(0)
;occ=1.
;pos=vec3(0)
;float h=(.01-ro.y)/rd.y//plane
;if(h>0.
){tmin=h
;n=vec3(0,1,0)
;pos=ro+h*rd
;matid=0.
;occ=occSphere(pos,n,sc[0])*occSphere(pos,n,sc[1])*occSphere(pos,n,sc[2])*occSphere(pos,n,sc[3])
;}
;for(int i=0;i<4;i++){
;float h=iSphere(v33(ro,rd),sc[i])
;bool b=abs(h-.5*tmin)<tmin*.5//==h>0.&&h<tmin
;if(b){tmin=h;pos=ro+tmin*rd;n=normalize(ro+h*rd-sc[i].xyz);matid=1.;occ=u5(n.y);}}
;return tmin;}
void calcCamera(out vec3 ro,out vec3 ta){float an=.3*sin(.04*iTime);ro=vec3(5.5*cos(an),1.,5.5*sin(an));ta=vec3(0,1,0);}
vec3 doLighting(vec3 pos,vec3 rd,vec3 n,float occ
){ ;v33 rrr=v33(pos,rd)//seems to be a shared light source position
;float sh=miv(vec4(sssp(rrr,sc0),sssp(rrr,sc1),sssp(rrr,sc2),sssp(rrr,sc3)))
,dif=sat(dot(n,vec3(.57703)));float bac=sat(dot(n,vec3(-.707,.0,-.707)))
;vec3 lin=dif*sh*vec3(1.5,1.4,1.3)
;lin+=occ*vec3(.15,.2,.3);lin+=bac*vec3(.1);return lin;}
v33 calcRayForPixel(vec2 pix,vec2 res
){vec2 p=(-res.xy+2.0*pix)/res.y
;vec3 ro,ta
;calcCamera(ro,ta)
;vec3 w=normalize(ta-ro)
;vec3 u=normalize(cross(w,vec3(0,1,0)))
;vec3 rd=normalize(p.x*u+p.y*normalize(cross(u,w))+1.5*w)
;return v33(ro,rd);}
void mainImage(out vec4 fragColor,vec2 fragCoord
){vec2 res=vec2(iResolution.x/3.0,iResolution.y)
;int id=int(floor(fragCoord.x/res.x))
;vec2 px=vec2(fragCoord.x-float(id)*res.x,fragCoord.y)
;v33 r0=calcRayForPixel(px+vec2(0,0),res)
;vec3 pos,nor
;float occ,mid;float t=intersect(r0.a,r0.b,pos,nor,occ,mid)
;vec3 col=vec3(.9)
;if(t<100.
//todo,measure angle between normal and rayDirection,and only do #if 1 for anggles>45deg;
//todo,there is a precision fix for near-orthogonal normals to camera that may be good here.
#if 1
){vec3 uvw=pos*scale//analytic ray-differential is in object-space
;v33 rx=calcRayForPixel(px+vec2(1,0),res);
;v33 ry=calcRayForPixel(px+vec2(0,1),res);
;rayTransp(rx,ry)//swap rx.b with ry.a and the lines below become more symmetric: yes,this swaps the origin of one ray with the direction of another.
;v33 w=mul(ry,dt(sub(rx,pos),nor)/dt(ry,nor))
;w=subc(rx,w)
;w=mul(sub(w,pos),scale)
#else
){vec3 uvw=pos*scale;v33 w=v33(dFdx(uvw),dFdy(uvw))//semi-analogously use dFdx()dFdy()in screenspace has bad borders
#endif
;vec3 m=vec3(0)
;w.a=maab2(w)
;if(id==0)m=vec3(1)*checker(uvw)
;else if(id==1)m=vec3(1)*checkerF2(uvw,w.a)
;else if(id==2)m=vec3(1)*checkerF3(uvw,w.a)
;col=m*doLighting(pos,vec3(.57703),nor,occ)
//;col=mix(col,vec3(.9),1.-exp(-.0001*t*t))// fog
;}
;col=pow(col,vec3(.4545))//gamma
;col*=smoothstep(2.,3.,abs(px.x))//frame border lines
;fragColor=vec4(col,1);}
| mit | [
6013,
6130,
6161,
6161,
6317
] | [
[
3872,
3952,
3992,
3992,
4018
],
[
4018,
4067,
4089,
4089,
4114
],
[
4114,
4262,
4285,
4285,
4314
],
[
4314,
4334,
4357,
4357,
4386
],
[
4386,
4403,
4426,
4426,
4455
],
[
4455,
4482,
4503,
4503,
4543
],
[
4543,
4570,
4592,
4592,
4615
],
[
4616,
4616,
4638,
4638,
4667
],
[
4668,
4668,
4689,
4689,
4718
],
[
4718,
4730,
4752,
4752,
4781
],
[
4782,
4782,
4805,
4805,
4830
],
[
4833,
4833,
4852,
4852,
4875
],
[
5165,
5165,
5183,
5183,
5205
],
[
5206,
5206,
5224,
5224,
5244
],
[
5245,
5245,
5263,
5263,
5283
],
[
5284,
5284,
5302,
5302,
5323
],
[
5324,
5324,
5342,
5342,
5375
],
[
5376,
5376,
5396,
5396,
5424
],
[
5691,
5837,
5868,
5868,
6013
],
[
6013,
6130,
6161,
6161,
6317
],
[
6365,
6400,
6425,
6425,
6530
],
[
6531,
6550,
6588,
6588,
6648
],
[
6650,
6650,
6678,
6678,
6771
],
[
6773,
6773,
6861,
6861,
7360
],
[
7362,
7362,
7403,
7403,
7482
],
[
7484,
7484,
7536,
7536,
7847
],
[
7849,
7849,
7889,
7889,
8100
]
] | //analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
| float checkerF3(vec3 p,vec3 w){ | w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p+w,Fb)-2.*tri(p,Fb)+tri(p-w,Fb))/(w*w)));} | //analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
float checkerF3(vec3 p,vec3 w){ | 1 | 1 |
lldyzM | ollj | 2018-07-20T10:46:29 | //self https://www.shadertoy.com/view/lldyzM
//analytical filter kernel,triangular
//https://www.shadertoy.com/view/llffWs
// Similar to https://www.shadertoy.com/view/XlXBWs,but with a triangular filter kernel,
// which produces less flickering animations that a box filter. Luckily,it's still easily
// http://iquilezles.org/www/articles/morecheckerfiltering/morecheckerfiltering.htm
// checker,2D,box filter: https://www.shadertoy.com/view/XlcSz2
// checker,3D,box filter: https://www.shadertoy.com/view/XlXBWs
// checker,3D,tri filter: https://www.shadertoy.com/view/llffWs
// grid,2D,box filter: https://www.shadertoy.com/view/XtBfzz
// The MIT License
//https://www.shadertoy.com/view/llffWs
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files(the "Software"),to dealthe 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 includedall 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.NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHERAN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF ORCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGSTHE SOFTWARE.
/* ---snippety blog summary esplanation
//fwidth(a,b)=abs(dfdx(p))+abs(dfdy(p))
#define maab(a,b)max(abs(a),abs(b))
the m-parameter is a value,returned from maab(),which may be calculated for multiple textures to be mixed,so it is moved out of the function.
//llffWs is tri-filtering AND box filtering,but it does not need the double integral,but other shaders calculate a double integral.
//there is this double-integral blog post this snioppet is all about:
the basic idea is to smoothen a discontinuity with an antiderivative
"cubic filters" are most common in CG,but this isr "tiangle-filter"ed weights,worse than cubic,better than the box filtering above
f(x)is the square-wave base signal that begs to be filtered.
box-filter formula is integralfromToOf(-w/2,w/2,f(x)dx)
tri-filter formula is integralfromToOf(-w,0,f(x)dx*(w+x)/w)+integralfromtoOf(0,w,f(x)dx*(w-x)/w)
these integrals are integralfromToOf(uv-w/2,uv+w/2,...),but shifting the center simplifies this function.
these integrals are done by [integration by parts],which has lots of symmetries that cancel each other out to:
tri-filter formula is(p(-w*.5)-2.*p(0.)+p(w*.5))/w/w
where p(x)is the antidetivative to f(x)(the striangle wave to the square wave.)
where s(x)is the double-integral of f(x)== the antidefivative of p(x)=an infinite smoothstep function.
//still no double-integrals needed!
vec3 sqrAndIntegrals(float x
){x*=.5;float h=fract(x)-.5,s=-sign(h),t=abs(h)*2.-1.)
;return(s,t,x+h*t);}//return vec3(square,tri(integral),smoothsteps(doubleIntegral))
vec2 fTri(vec2 x){vec2 h=fract(x*.5)-.5);return x*.5-h*(abs(h)*2.-1.);}//;return x*.5+h*(1.-2.*abs(h))
float TriFilteredCheckers(vec2 uv,vec2 w//w=maab(dpdx,dpdy)filter kernel
){w+.001
;vec2 i=(fTri(uw+w)-2.*fTri(uw)+ftri(uv-w))/(w*w)//analytic integral,3TapFilter function
;return .5-.5*i.x-i.y//xor-pattern
;}
//still no double-integrals needed!
//anyways,that would be curvature,what use is curvature for surface filtering?
*/
#define scale 5.
// spheres
const vec4 sc0=vec4(2,.5,.8,.5);
const vec4 sc1=vec4(-6,1,-4.,3);
const vec4 sc2=vec4(-16,1,7,4);
const vec4 sc3=vec4(-25,8,0,9);
struct v33{vec3 a;vec3 b;};
//and this ray-transpose function is the strangest of em all to be useful here:
void rayTransp(inout v33 a,inout v33 b){vec3 s=a.b;a.b=b.a;b.a=s;}//swap direction(.b)of [a] with origin(.a)of [b]
v33 sub(v33 a,vec3 b){return v33(a.a-b,a.b-b);}//substract b from all ray components
//component wise ray substraction(this one is a bit odd,differential wise,is basically scaling a rays points)
v33 subc(v33 a,v33 b){return v33(a.a-b.a,a.b-b.b);}//it makes sense in
v33 subc(vec2 a,v33 b){return v33(a.x-b.a,a.x-b.b);}//the context of
v33 subc(v33 a,vec2 b){return v33(a.a-b.x,a.b-b.y);}//v33-differentials for AA
vec2 dt(v33 a,v33 b){return vec2(dot(a.a,b.a),dot(a.b,b.b));}//dual dotprodiuct on v33s
vec2 dt(v33 a,vec3 b){return dt(a,v33(b,b));}
v33 div(v33 a,vec2 b){return v33(a.a/b.x,a.b/b.y);}
v33 mul(v33 a,v33 b){return v33(a.a*b.a,a.b*b.b);}//dual mult
v33 mul(v33 a,vec2 b){return v33(a.a*b.x,a.b*b.y);}
v33 mul(v33 a,float b){return v33(a.a*b,a.b*b);}
float sat(float a){return clamp(a,0.,1.);}
#define dd(a)dot(a,a)
//half-identity-scaling,labeled uN because it scales uv space,usually within a modulo context.
#define u2(a)((a)*2.-1.)
#define u5(a)((a)*.5+.5)
//u3(a)=1.-u2(a)!
//u6(a)=1.-u2(a)!
#define u3(a)(1.-(a)*2.)
#define u6(a)(.5-(a)*.5)
#define maab(a,b)max(abs(a),abs(b))
vec3 maab2(v33 a){return maab(a.a,a.b);}
float suv(vec3 a){return a.x+a.y+a.z;}
float prv(vec3 a){return a.x*a.y*a.z;}
float miv(vec2 a){return min(a.x,a.y);}
float miv(vec4 a){return min(miv(a.xy),miv(a.zw));}
float ss01(float a){return smoothstep(0.,1.,a);}
// ---unfiltered checkerboard ---
#define checker(a)mod(suv(floor(a)),2.)
//analytically triangle-filtered checkerboard: https://www.shadertoy.com/view/MtffWs
#define Fa(a,b)u2(abs(b-.5))
#define Fb(a,b)((a)*.5-((b)-.5)*Fa(a,b))
#define tri(a,b)b(a,fract((a)*.5))
//noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
float checkerF2(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p-.5*w,Fa)-tri(p+.5*w,Fa))/w));}//analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
float checkerF3(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p+w,Fb)-2.*tri(p,Fb)+tri(p-w,Fb))/(w*w)));}// analytical integral(tri filter),xor pattern
//sphere softShadow of(ray,sphere)
float sssp(v33 r,vec4 s){vec3 oc=s.xyz-r.a;float b=dot(oc,r.b),z=1.;if(b>0.){float h=dd(oc)-b*b-s.w*s.w;z=ss01(2.*h/b);}return z;}
//sphere occlusion
float occSphere(vec3 u,vec3 n,vec4 s){vec3 i=s.xyz-u;return 1.-dot(n,normalize(i))*s.w*s.w/dd(i);}
float iSphere(v33 r,vec4 s){vec3 e=r.a-s.xyz;float b=dot(r.b,e),h=b*b-dd(e)+s.w*s.w,t=-1.;if(h>0.)t=-b-sqrt(h);return t;}
float intersect(vec3 ro,vec3 rd,out vec3 pos,out vec3 n,out float occ,out float matid
){
;mat4 sc=mat4(sc0,sc1,sc2,sc3)
;float tmin=10000.
;n=vec3(0)
;occ=1.
;pos=vec3(0)
;float h=(.01-ro.y)/rd.y//plane
;if(h>0.
){tmin=h
;n=vec3(0,1,0)
;pos=ro+h*rd
;matid=0.
;occ=occSphere(pos,n,sc[0])*occSphere(pos,n,sc[1])*occSphere(pos,n,sc[2])*occSphere(pos,n,sc[3])
;}
;for(int i=0;i<4;i++){
;float h=iSphere(v33(ro,rd),sc[i])
;bool b=abs(h-.5*tmin)<tmin*.5//==h>0.&&h<tmin
;if(b){tmin=h;pos=ro+tmin*rd;n=normalize(ro+h*rd-sc[i].xyz);matid=1.;occ=u5(n.y);}}
;return tmin;}
void calcCamera(out vec3 ro,out vec3 ta){float an=.3*sin(.04*iTime);ro=vec3(5.5*cos(an),1.,5.5*sin(an));ta=vec3(0,1,0);}
vec3 doLighting(vec3 pos,vec3 rd,vec3 n,float occ
){ ;v33 rrr=v33(pos,rd)//seems to be a shared light source position
;float sh=miv(vec4(sssp(rrr,sc0),sssp(rrr,sc1),sssp(rrr,sc2),sssp(rrr,sc3)))
,dif=sat(dot(n,vec3(.57703)));float bac=sat(dot(n,vec3(-.707,.0,-.707)))
;vec3 lin=dif*sh*vec3(1.5,1.4,1.3)
;lin+=occ*vec3(.15,.2,.3);lin+=bac*vec3(.1);return lin;}
v33 calcRayForPixel(vec2 pix,vec2 res
){vec2 p=(-res.xy+2.0*pix)/res.y
;vec3 ro,ta
;calcCamera(ro,ta)
;vec3 w=normalize(ta-ro)
;vec3 u=normalize(cross(w,vec3(0,1,0)))
;vec3 rd=normalize(p.x*u+p.y*normalize(cross(u,w))+1.5*w)
;return v33(ro,rd);}
void mainImage(out vec4 fragColor,vec2 fragCoord
){vec2 res=vec2(iResolution.x/3.0,iResolution.y)
;int id=int(floor(fragCoord.x/res.x))
;vec2 px=vec2(fragCoord.x-float(id)*res.x,fragCoord.y)
;v33 r0=calcRayForPixel(px+vec2(0,0),res)
;vec3 pos,nor
;float occ,mid;float t=intersect(r0.a,r0.b,pos,nor,occ,mid)
;vec3 col=vec3(.9)
;if(t<100.
//todo,measure angle between normal and rayDirection,and only do #if 1 for anggles>45deg;
//todo,there is a precision fix for near-orthogonal normals to camera that may be good here.
#if 1
){vec3 uvw=pos*scale//analytic ray-differential is in object-space
;v33 rx=calcRayForPixel(px+vec2(1,0),res);
;v33 ry=calcRayForPixel(px+vec2(0,1),res);
;rayTransp(rx,ry)//swap rx.b with ry.a and the lines below become more symmetric: yes,this swaps the origin of one ray with the direction of another.
;v33 w=mul(ry,dt(sub(rx,pos),nor)/dt(ry,nor))
;w=subc(rx,w)
;w=mul(sub(w,pos),scale)
#else
){vec3 uvw=pos*scale;v33 w=v33(dFdx(uvw),dFdy(uvw))//semi-analogously use dFdx()dFdy()in screenspace has bad borders
#endif
;vec3 m=vec3(0)
;w.a=maab2(w)
;if(id==0)m=vec3(1)*checker(uvw)
;else if(id==1)m=vec3(1)*checkerF2(uvw,w.a)
;else if(id==2)m=vec3(1)*checkerF3(uvw,w.a)
;col=m*doLighting(pos,vec3(.57703),nor,occ)
//;col=mix(col,vec3(.9),1.-exp(-.0001*t*t))// fog
;}
;col=pow(col,vec3(.4545))//gamma
;col*=smoothstep(2.,3.,abs(px.x))//frame border lines
;fragColor=vec4(col,1);}
| mit | [
6365,
6400,
6425,
6425,
6530
] | [
[
3872,
3952,
3992,
3992,
4018
],
[
4018,
4067,
4089,
4089,
4114
],
[
4114,
4262,
4285,
4285,
4314
],
[
4314,
4334,
4357,
4357,
4386
],
[
4386,
4403,
4426,
4426,
4455
],
[
4455,
4482,
4503,
4503,
4543
],
[
4543,
4570,
4592,
4592,
4615
],
[
4616,
4616,
4638,
4638,
4667
],
[
4668,
4668,
4689,
4689,
4718
],
[
4718,
4730,
4752,
4752,
4781
],
[
4782,
4782,
4805,
4805,
4830
],
[
4833,
4833,
4852,
4852,
4875
],
[
5165,
5165,
5183,
5183,
5205
],
[
5206,
5206,
5224,
5224,
5244
],
[
5245,
5245,
5263,
5263,
5283
],
[
5284,
5284,
5302,
5302,
5323
],
[
5324,
5324,
5342,
5342,
5375
],
[
5376,
5376,
5396,
5396,
5424
],
[
5691,
5837,
5868,
5868,
6013
],
[
6013,
6130,
6161,
6161,
6317
],
[
6365,
6400,
6425,
6425,
6530
],
[
6531,
6550,
6588,
6588,
6648
],
[
6650,
6650,
6678,
6678,
6771
],
[
6773,
6773,
6861,
6861,
7360
],
[
7362,
7362,
7403,
7403,
7482
],
[
7484,
7484,
7536,
7536,
7847
],
[
7849,
7849,
7889,
7889,
8100
]
] | //sphere softShadow of(ray,sphere)
| float sssp(v33 r,vec4 s){ | vec3 oc=s.xyz-r.a;float b=dot(oc,r.b),z=1.;if(b>0.){float h=dd(oc)-b*b-s.w*s.w;z=ss01(2.*h/b);}return z;} | //sphere softShadow of(ray,sphere)
float sssp(v33 r,vec4 s){ | 1 | 1 |
lldyzM | ollj | 2018-07-20T10:46:29 | //self https://www.shadertoy.com/view/lldyzM
//analytical filter kernel,triangular
//https://www.shadertoy.com/view/llffWs
// Similar to https://www.shadertoy.com/view/XlXBWs,but with a triangular filter kernel,
// which produces less flickering animations that a box filter. Luckily,it's still easily
// http://iquilezles.org/www/articles/morecheckerfiltering/morecheckerfiltering.htm
// checker,2D,box filter: https://www.shadertoy.com/view/XlcSz2
// checker,3D,box filter: https://www.shadertoy.com/view/XlXBWs
// checker,3D,tri filter: https://www.shadertoy.com/view/llffWs
// grid,2D,box filter: https://www.shadertoy.com/view/XtBfzz
// The MIT License
//https://www.shadertoy.com/view/llffWs
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files(the "Software"),to dealthe 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 includedall 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.NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHERAN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF ORCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGSTHE SOFTWARE.
/* ---snippety blog summary esplanation
//fwidth(a,b)=abs(dfdx(p))+abs(dfdy(p))
#define maab(a,b)max(abs(a),abs(b))
the m-parameter is a value,returned from maab(),which may be calculated for multiple textures to be mixed,so it is moved out of the function.
//llffWs is tri-filtering AND box filtering,but it does not need the double integral,but other shaders calculate a double integral.
//there is this double-integral blog post this snioppet is all about:
the basic idea is to smoothen a discontinuity with an antiderivative
"cubic filters" are most common in CG,but this isr "tiangle-filter"ed weights,worse than cubic,better than the box filtering above
f(x)is the square-wave base signal that begs to be filtered.
box-filter formula is integralfromToOf(-w/2,w/2,f(x)dx)
tri-filter formula is integralfromToOf(-w,0,f(x)dx*(w+x)/w)+integralfromtoOf(0,w,f(x)dx*(w-x)/w)
these integrals are integralfromToOf(uv-w/2,uv+w/2,...),but shifting the center simplifies this function.
these integrals are done by [integration by parts],which has lots of symmetries that cancel each other out to:
tri-filter formula is(p(-w*.5)-2.*p(0.)+p(w*.5))/w/w
where p(x)is the antidetivative to f(x)(the striangle wave to the square wave.)
where s(x)is the double-integral of f(x)== the antidefivative of p(x)=an infinite smoothstep function.
//still no double-integrals needed!
vec3 sqrAndIntegrals(float x
){x*=.5;float h=fract(x)-.5,s=-sign(h),t=abs(h)*2.-1.)
;return(s,t,x+h*t);}//return vec3(square,tri(integral),smoothsteps(doubleIntegral))
vec2 fTri(vec2 x){vec2 h=fract(x*.5)-.5);return x*.5-h*(abs(h)*2.-1.);}//;return x*.5+h*(1.-2.*abs(h))
float TriFilteredCheckers(vec2 uv,vec2 w//w=maab(dpdx,dpdy)filter kernel
){w+.001
;vec2 i=(fTri(uw+w)-2.*fTri(uw)+ftri(uv-w))/(w*w)//analytic integral,3TapFilter function
;return .5-.5*i.x-i.y//xor-pattern
;}
//still no double-integrals needed!
//anyways,that would be curvature,what use is curvature for surface filtering?
*/
#define scale 5.
// spheres
const vec4 sc0=vec4(2,.5,.8,.5);
const vec4 sc1=vec4(-6,1,-4.,3);
const vec4 sc2=vec4(-16,1,7,4);
const vec4 sc3=vec4(-25,8,0,9);
struct v33{vec3 a;vec3 b;};
//and this ray-transpose function is the strangest of em all to be useful here:
void rayTransp(inout v33 a,inout v33 b){vec3 s=a.b;a.b=b.a;b.a=s;}//swap direction(.b)of [a] with origin(.a)of [b]
v33 sub(v33 a,vec3 b){return v33(a.a-b,a.b-b);}//substract b from all ray components
//component wise ray substraction(this one is a bit odd,differential wise,is basically scaling a rays points)
v33 subc(v33 a,v33 b){return v33(a.a-b.a,a.b-b.b);}//it makes sense in
v33 subc(vec2 a,v33 b){return v33(a.x-b.a,a.x-b.b);}//the context of
v33 subc(v33 a,vec2 b){return v33(a.a-b.x,a.b-b.y);}//v33-differentials for AA
vec2 dt(v33 a,v33 b){return vec2(dot(a.a,b.a),dot(a.b,b.b));}//dual dotprodiuct on v33s
vec2 dt(v33 a,vec3 b){return dt(a,v33(b,b));}
v33 div(v33 a,vec2 b){return v33(a.a/b.x,a.b/b.y);}
v33 mul(v33 a,v33 b){return v33(a.a*b.a,a.b*b.b);}//dual mult
v33 mul(v33 a,vec2 b){return v33(a.a*b.x,a.b*b.y);}
v33 mul(v33 a,float b){return v33(a.a*b,a.b*b);}
float sat(float a){return clamp(a,0.,1.);}
#define dd(a)dot(a,a)
//half-identity-scaling,labeled uN because it scales uv space,usually within a modulo context.
#define u2(a)((a)*2.-1.)
#define u5(a)((a)*.5+.5)
//u3(a)=1.-u2(a)!
//u6(a)=1.-u2(a)!
#define u3(a)(1.-(a)*2.)
#define u6(a)(.5-(a)*.5)
#define maab(a,b)max(abs(a),abs(b))
vec3 maab2(v33 a){return maab(a.a,a.b);}
float suv(vec3 a){return a.x+a.y+a.z;}
float prv(vec3 a){return a.x*a.y*a.z;}
float miv(vec2 a){return min(a.x,a.y);}
float miv(vec4 a){return min(miv(a.xy),miv(a.zw));}
float ss01(float a){return smoothstep(0.,1.,a);}
// ---unfiltered checkerboard ---
#define checker(a)mod(suv(floor(a)),2.)
//analytically triangle-filtered checkerboard: https://www.shadertoy.com/view/MtffWs
#define Fa(a,b)u2(abs(b-.5))
#define Fb(a,b)((a)*.5-((b)-.5)*Fa(a,b))
#define tri(a,b)b(a,fract((a)*.5))
//noe to self,maybe replace iMouse.y by abs(angleBetween(rayDirection,Normal))/quaterRotation
//tri(a,Fa)2xTap for box-filtering,used a lot in CG
float checkerF2(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p-.5*w,Fa)-tri(p+.5*w,Fa))/w));}//analytical integral(box filter),xor pattern
//tri(a,Fb)3xTap for tri-filtering,is slightly better than checkerF2()
float checkerF3(vec3 p,vec3 w){w+=iMouse.y/iResolution.y//filter kernel increase this value over inverse squared distance?
;return u6(prv((tri(p+w,Fb)-2.*tri(p,Fb)+tri(p-w,Fb))/(w*w)));}// analytical integral(tri filter),xor pattern
//sphere softShadow of(ray,sphere)
float sssp(v33 r,vec4 s){vec3 oc=s.xyz-r.a;float b=dot(oc,r.b),z=1.;if(b>0.){float h=dd(oc)-b*b-s.w*s.w;z=ss01(2.*h/b);}return z;}
//sphere occlusion
float occSphere(vec3 u,vec3 n,vec4 s){vec3 i=s.xyz-u;return 1.-dot(n,normalize(i))*s.w*s.w/dd(i);}
float iSphere(v33 r,vec4 s){vec3 e=r.a-s.xyz;float b=dot(r.b,e),h=b*b-dd(e)+s.w*s.w,t=-1.;if(h>0.)t=-b-sqrt(h);return t;}
float intersect(vec3 ro,vec3 rd,out vec3 pos,out vec3 n,out float occ,out float matid
){
;mat4 sc=mat4(sc0,sc1,sc2,sc3)
;float tmin=10000.
;n=vec3(0)
;occ=1.
;pos=vec3(0)
;float h=(.01-ro.y)/rd.y//plane
;if(h>0.
){tmin=h
;n=vec3(0,1,0)
;pos=ro+h*rd
;matid=0.
;occ=occSphere(pos,n,sc[0])*occSphere(pos,n,sc[1])*occSphere(pos,n,sc[2])*occSphere(pos,n,sc[3])
;}
;for(int i=0;i<4;i++){
;float h=iSphere(v33(ro,rd),sc[i])
;bool b=abs(h-.5*tmin)<tmin*.5//==h>0.&&h<tmin
;if(b){tmin=h;pos=ro+tmin*rd;n=normalize(ro+h*rd-sc[i].xyz);matid=1.;occ=u5(n.y);}}
;return tmin;}
void calcCamera(out vec3 ro,out vec3 ta){float an=.3*sin(.04*iTime);ro=vec3(5.5*cos(an),1.,5.5*sin(an));ta=vec3(0,1,0);}
vec3 doLighting(vec3 pos,vec3 rd,vec3 n,float occ
){ ;v33 rrr=v33(pos,rd)//seems to be a shared light source position
;float sh=miv(vec4(sssp(rrr,sc0),sssp(rrr,sc1),sssp(rrr,sc2),sssp(rrr,sc3)))
,dif=sat(dot(n,vec3(.57703)));float bac=sat(dot(n,vec3(-.707,.0,-.707)))
;vec3 lin=dif*sh*vec3(1.5,1.4,1.3)
;lin+=occ*vec3(.15,.2,.3);lin+=bac*vec3(.1);return lin;}
v33 calcRayForPixel(vec2 pix,vec2 res
){vec2 p=(-res.xy+2.0*pix)/res.y
;vec3 ro,ta
;calcCamera(ro,ta)
;vec3 w=normalize(ta-ro)
;vec3 u=normalize(cross(w,vec3(0,1,0)))
;vec3 rd=normalize(p.x*u+p.y*normalize(cross(u,w))+1.5*w)
;return v33(ro,rd);}
void mainImage(out vec4 fragColor,vec2 fragCoord
){vec2 res=vec2(iResolution.x/3.0,iResolution.y)
;int id=int(floor(fragCoord.x/res.x))
;vec2 px=vec2(fragCoord.x-float(id)*res.x,fragCoord.y)
;v33 r0=calcRayForPixel(px+vec2(0,0),res)
;vec3 pos,nor
;float occ,mid;float t=intersect(r0.a,r0.b,pos,nor,occ,mid)
;vec3 col=vec3(.9)
;if(t<100.
//todo,measure angle between normal and rayDirection,and only do #if 1 for anggles>45deg;
//todo,there is a precision fix for near-orthogonal normals to camera that may be good here.
#if 1
){vec3 uvw=pos*scale//analytic ray-differential is in object-space
;v33 rx=calcRayForPixel(px+vec2(1,0),res);
;v33 ry=calcRayForPixel(px+vec2(0,1),res);
;rayTransp(rx,ry)//swap rx.b with ry.a and the lines below become more symmetric: yes,this swaps the origin of one ray with the direction of another.
;v33 w=mul(ry,dt(sub(rx,pos),nor)/dt(ry,nor))
;w=subc(rx,w)
;w=mul(sub(w,pos),scale)
#else
){vec3 uvw=pos*scale;v33 w=v33(dFdx(uvw),dFdy(uvw))//semi-analogously use dFdx()dFdy()in screenspace has bad borders
#endif
;vec3 m=vec3(0)
;w.a=maab2(w)
;if(id==0)m=vec3(1)*checker(uvw)
;else if(id==1)m=vec3(1)*checkerF2(uvw,w.a)
;else if(id==2)m=vec3(1)*checkerF3(uvw,w.a)
;col=m*doLighting(pos,vec3(.57703),nor,occ)
//;col=mix(col,vec3(.9),1.-exp(-.0001*t*t))// fog
;}
;col=pow(col,vec3(.4545))//gamma
;col*=smoothstep(2.,3.,abs(px.x))//frame border lines
;fragColor=vec4(col,1);}
| mit | [
6531,
6550,
6588,
6588,
6648
] | [
[
3872,
3952,
3992,
3992,
4018
],
[
4018,
4067,
4089,
4089,
4114
],
[
4114,
4262,
4285,
4285,
4314
],
[
4314,
4334,
4357,
4357,
4386
],
[
4386,
4403,
4426,
4426,
4455
],
[
4455,
4482,
4503,
4503,
4543
],
[
4543,
4570,
4592,
4592,
4615
],
[
4616,
4616,
4638,
4638,
4667
],
[
4668,
4668,
4689,
4689,
4718
],
[
4718,
4730,
4752,
4752,
4781
],
[
4782,
4782,
4805,
4805,
4830
],
[
4833,
4833,
4852,
4852,
4875
],
[
5165,
5165,
5183,
5183,
5205
],
[
5206,
5206,
5224,
5224,
5244
],
[
5245,
5245,
5263,
5263,
5283
],
[
5284,
5284,
5302,
5302,
5323
],
[
5324,
5324,
5342,
5342,
5375
],
[
5376,
5376,
5396,
5396,
5424
],
[
5691,
5837,
5868,
5868,
6013
],
[
6013,
6130,
6161,
6161,
6317
],
[
6365,
6400,
6425,
6425,
6530
],
[
6531,
6550,
6588,
6588,
6648
],
[
6650,
6650,
6678,
6678,
6771
],
[
6773,
6773,
6861,
6861,
7360
],
[
7362,
7362,
7403,
7403,
7482
],
[
7484,
7484,
7536,
7536,
7847
],
[
7849,
7849,
7889,
7889,
8100
]
] | //sphere occlusion
| float occSphere(vec3 u,vec3 n,vec4 s){ | vec3 i=s.xyz-u;return 1.-dot(n,normalize(i))*s.w*s.w/dd(i);} | //sphere occlusion
float occSphere(vec3 u,vec3 n,vec4 s){ | 1 | 1 |
lsyfWc | iq | 2018-07-05T10:21:12 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Computes the exact bounding box to a quadratic Bezier curve. Since the bezier is quadratic,
// the bbox can be compute with a linear equation:
//
// Yellow: naive bbox of the 3 control points
// Blue: exact bbox
//
// More info here: http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm
//
// Related Shaders:
// Quadratic Bezier - 3D : https://www.shadertoy.com/view/ldj3Wh
// Cubic Bezier - 2D BBox : https://www.shadertoy.com/view/XdVBWd
// Cubic Bezier - 3D BBox : https://www.shadertoy.com/view/MdKBWt
// Quadratic Bezier - 2D BBox : https://www.shadertoy.com/view/lsyfWc
// Quadratic Bezier - 3D BBox : https://www.shadertoy.com/view/tsBfRD
// Exact BBox to a quadratic bezier
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2 )
{
// extremes
vec2 mi = min(p0,p2);
vec2 ma = max(p0,p2);
// maxima/minima point, if p1 is outside the current bbox/hull
if( p1.x<mi.x || p1.x>ma.x || p1.y<mi.y || p1.y>ma.y )
{
// p = (1-t)^2*p0 + 2(1-t)t*p1 + t^2*p2
// dp/dt = 2(t-1)*p0 + 2(1-2t)*p1 + 2t*p2 = t*(2*p0-4*p1+2*p2) + 2*(p1-p0)
// dp/dt = 0 -> t*(p0-2*p1+p2) = (p0-p1);
vec2 t = clamp((p0-p1)/(p0-2.0*p1+p2),0.0,1.0);
vec2 s = 1.0 - t;
vec2 q = s*s*p0 + 2.0*s*t*p1 + t*t*p2;
mi = min(mi,q);
ma = max(ma,q);
}
return vec4( mi, ma );
}
// Approximated BBox to a quadratic bezier
vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2 )
{
vec2 mi = min(p0,min(p1,p2));
vec2 ma = max(p0,max(p1,p2));
return vec4( mi, ma );
}
//---------------------------------------------------------------------------------------
float sdBox( in vec2 p, in vec2 b )
{
vec2 q = abs(p) - b;
vec2 m = vec2( min(q.x,q.y), max(q.x,q.y) );
return (m.x > 0.0) ? length(q) : m.y;
}
float sdSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
vec2 udBezier(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 pos)
{
// p(t) = (1-t)^2*p0 + 2(1-t)t*p1 + t^2*p2
// p'(t) = 2*t*(p0-2*p1+p2) + 2*(p1-p0)
// p'(0) = 2(p1-p0)
// p'(1) = 2(p2-p1)
// p'(1/2) = 2(p2-p0)
vec2 a = p1 - p0;
vec2 b = p0 - 2.0*p1 + p2;
vec2 c = p0 - pos;
float kk = 1.0 / dot(b,b);
float kx = kk * dot(a,b);
float ky = kk * (2.0*dot(a,a)+dot(c,b)) / 3.0;
float kz = kk * dot(c,a);
vec2 res;
float p = ky - kx*kx;
float p3 = p*p*p;
float q = kx*(2.0*kx*kx - 3.0*ky) + kz;
float h = q*q + 4.0*p3;
if(h >= 0.0)
{
h = sqrt(h);
vec2 x = (vec2(h, -h) - q) / 2.0;
vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0));
float t = uv.x + uv.y - kx;
t = clamp( t, 0.0, 1.0 );
// 1 root
vec2 qos = c + (2.0*a + b*t)*t;
res = vec2( length(qos),t);
}
else
{
float z = sqrt(-p);
float v = acos( q/(p*z*2.0) ) / 3.0;
float m = cos(v);
float n = sin(v)*1.732050808;
vec3 t = vec3(m + m, -n - m, n - m) * z - kx;
t = clamp( t, 0.0, 1.0 );
// 3 roots
vec2 qos = c + (2.0*a + b*t.x)*t.x;
float dis = dot(qos,qos);
res = vec2(dis,t.x);
qos = c + (2.0*a + b*t.y)*t.y;
dis = dot(qos,qos);
if( dis<res.x ) res = vec2(dis,t.y );
qos = c + (2.0*a + b*t.z)*t.z;
dis = dot(qos,qos);
if( dis<res.x ) res = vec2(dis,t.z );
res.x = sqrt( res.x );
}
return res;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
//--------
// animate
//--------
float time = iTime*0.5 + 20.0;
vec2 p0 = 0.8*sin( time*0.7 + vec2(3.0,1.0) );
vec2 p1 = 0.8*sin( time*1.1 + vec2(0.0,6.0) );
vec2 p2 = 0.8*sin( time*1.3 + vec2(4.0,2.0) );
//-------------
// compute bbox
//-------------
vec4 b1 = bboxBezierSimple(p0,p1,p2);
vec4 b2 = bboxBezier(p0,p1,p2);
//--------
// render
//--------
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
// background
vec3 col = vec3(0.15);
float be = udBezier( p0, p1, p2, p ).x;
col += 0.03*sin(be*150.0);
col *= 1.0 - 0.3*length(p);
// naive bbox
float d = sdBox( p-(b1.xy+b1.zw)*0.5, (b1.zw-b1.xy)*0.5 );
col = mix( col, vec3(1.0,0.6,0.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// exact bbox
d = sdBox( p-(b2.xy+b2.zw)*0.5, (b2.zw-b2.xy)*0.5 );
col = mix( col, vec3(0.2,0.5,1.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// control cage
d = sdSegment( p, p0, p1 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
d = sdSegment( p, p1, p2 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
// bezier
d = be;
col = mix( col, vec3(1.0), 1.0-smoothstep(0.003,0.003+px*1.5,d) );
// control points
d = length(p0-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p1-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p2-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
fragColor = vec4(col,1.0);
} | mit | [
1781,
1817,
1871,
1887,
2480
] | [
[
1781,
1817,
1871,
1887,
2480
],
[
2483,
2526,
2586,
2586,
2688
],
[
2781,
2781,
2819,
2819,
2938
],
[
2940,
2940,
2992,
2992,
3102
],
[
3104,
3104,
3168,
3346,
4686
],
[
4688,
4688,
4745,
4790,
6378
]
] | // Exact BBox to a quadratic bezier
| vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2 )
{ |
vec2 mi = min(p0,p2);
vec2 ma = max(p0,p2);
// maxima/minima point, if p1 is outside the current bbox/hull
if( p1.x<mi.x || p1.x>ma.x || p1.y<mi.y || p1.y>ma.y )
{
// p = (1-t)^2*p0 + 2(1-t)t*p1 + t^2*p2
// dp/dt = 2(t-1)*p0 + 2(1-2t)*p1 + 2t*p2 = t*(2*p0-4*p1+2*p2) + 2*(p1-p0)
// dp/dt = 0 -> t*(p0-2*p1+p2) = (p0-p1);
vec2 t = clamp((p0-p1)/(p0-2.0*p1+p2),0.0,1.0);
vec2 s = 1.0 - t;
vec2 q = s*s*p0 + 2.0*s*t*p1 + t*t*p2;
mi = min(mi,q);
ma = max(ma,q);
}
return vec4( mi, ma );
} | // Exact BBox to a quadratic bezier
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2 )
{ | 1 | 1 |
lsyfWc | iq | 2018-07-05T10:21:12 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Computes the exact bounding box to a quadratic Bezier curve. Since the bezier is quadratic,
// the bbox can be compute with a linear equation:
//
// Yellow: naive bbox of the 3 control points
// Blue: exact bbox
//
// More info here: http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm
//
// Related Shaders:
// Quadratic Bezier - 3D : https://www.shadertoy.com/view/ldj3Wh
// Cubic Bezier - 2D BBox : https://www.shadertoy.com/view/XdVBWd
// Cubic Bezier - 3D BBox : https://www.shadertoy.com/view/MdKBWt
// Quadratic Bezier - 2D BBox : https://www.shadertoy.com/view/lsyfWc
// Quadratic Bezier - 3D BBox : https://www.shadertoy.com/view/tsBfRD
// Exact BBox to a quadratic bezier
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2 )
{
// extremes
vec2 mi = min(p0,p2);
vec2 ma = max(p0,p2);
// maxima/minima point, if p1 is outside the current bbox/hull
if( p1.x<mi.x || p1.x>ma.x || p1.y<mi.y || p1.y>ma.y )
{
// p = (1-t)^2*p0 + 2(1-t)t*p1 + t^2*p2
// dp/dt = 2(t-1)*p0 + 2(1-2t)*p1 + 2t*p2 = t*(2*p0-4*p1+2*p2) + 2*(p1-p0)
// dp/dt = 0 -> t*(p0-2*p1+p2) = (p0-p1);
vec2 t = clamp((p0-p1)/(p0-2.0*p1+p2),0.0,1.0);
vec2 s = 1.0 - t;
vec2 q = s*s*p0 + 2.0*s*t*p1 + t*t*p2;
mi = min(mi,q);
ma = max(ma,q);
}
return vec4( mi, ma );
}
// Approximated BBox to a quadratic bezier
vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2 )
{
vec2 mi = min(p0,min(p1,p2));
vec2 ma = max(p0,max(p1,p2));
return vec4( mi, ma );
}
//---------------------------------------------------------------------------------------
float sdBox( in vec2 p, in vec2 b )
{
vec2 q = abs(p) - b;
vec2 m = vec2( min(q.x,q.y), max(q.x,q.y) );
return (m.x > 0.0) ? length(q) : m.y;
}
float sdSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
vec2 udBezier(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 pos)
{
// p(t) = (1-t)^2*p0 + 2(1-t)t*p1 + t^2*p2
// p'(t) = 2*t*(p0-2*p1+p2) + 2*(p1-p0)
// p'(0) = 2(p1-p0)
// p'(1) = 2(p2-p1)
// p'(1/2) = 2(p2-p0)
vec2 a = p1 - p0;
vec2 b = p0 - 2.0*p1 + p2;
vec2 c = p0 - pos;
float kk = 1.0 / dot(b,b);
float kx = kk * dot(a,b);
float ky = kk * (2.0*dot(a,a)+dot(c,b)) / 3.0;
float kz = kk * dot(c,a);
vec2 res;
float p = ky - kx*kx;
float p3 = p*p*p;
float q = kx*(2.0*kx*kx - 3.0*ky) + kz;
float h = q*q + 4.0*p3;
if(h >= 0.0)
{
h = sqrt(h);
vec2 x = (vec2(h, -h) - q) / 2.0;
vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0));
float t = uv.x + uv.y - kx;
t = clamp( t, 0.0, 1.0 );
// 1 root
vec2 qos = c + (2.0*a + b*t)*t;
res = vec2( length(qos),t);
}
else
{
float z = sqrt(-p);
float v = acos( q/(p*z*2.0) ) / 3.0;
float m = cos(v);
float n = sin(v)*1.732050808;
vec3 t = vec3(m + m, -n - m, n - m) * z - kx;
t = clamp( t, 0.0, 1.0 );
// 3 roots
vec2 qos = c + (2.0*a + b*t.x)*t.x;
float dis = dot(qos,qos);
res = vec2(dis,t.x);
qos = c + (2.0*a + b*t.y)*t.y;
dis = dot(qos,qos);
if( dis<res.x ) res = vec2(dis,t.y );
qos = c + (2.0*a + b*t.z)*t.z;
dis = dot(qos,qos);
if( dis<res.x ) res = vec2(dis,t.z );
res.x = sqrt( res.x );
}
return res;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
//--------
// animate
//--------
float time = iTime*0.5 + 20.0;
vec2 p0 = 0.8*sin( time*0.7 + vec2(3.0,1.0) );
vec2 p1 = 0.8*sin( time*1.1 + vec2(0.0,6.0) );
vec2 p2 = 0.8*sin( time*1.3 + vec2(4.0,2.0) );
//-------------
// compute bbox
//-------------
vec4 b1 = bboxBezierSimple(p0,p1,p2);
vec4 b2 = bboxBezier(p0,p1,p2);
//--------
// render
//--------
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
// background
vec3 col = vec3(0.15);
float be = udBezier( p0, p1, p2, p ).x;
col += 0.03*sin(be*150.0);
col *= 1.0 - 0.3*length(p);
// naive bbox
float d = sdBox( p-(b1.xy+b1.zw)*0.5, (b1.zw-b1.xy)*0.5 );
col = mix( col, vec3(1.0,0.6,0.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// exact bbox
d = sdBox( p-(b2.xy+b2.zw)*0.5, (b2.zw-b2.xy)*0.5 );
col = mix( col, vec3(0.2,0.5,1.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// control cage
d = sdSegment( p, p0, p1 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
d = sdSegment( p, p1, p2 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
// bezier
d = be;
col = mix( col, vec3(1.0), 1.0-smoothstep(0.003,0.003+px*1.5,d) );
// control points
d = length(p0-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p1-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p2-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
fragColor = vec4(col,1.0);
} | mit | [
2483,
2526,
2586,
2586,
2688
] | [
[
1781,
1817,
1871,
1887,
2480
],
[
2483,
2526,
2586,
2586,
2688
],
[
2781,
2781,
2819,
2819,
2938
],
[
2940,
2940,
2992,
2992,
3102
],
[
3104,
3104,
3168,
3346,
4686
],
[
4688,
4688,
4745,
4790,
6378
]
] | // Approximated BBox to a quadratic bezier
| vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2 )
{ |
vec2 mi = min(p0,min(p1,p2));
vec2 ma = max(p0,max(p1,p2));
return vec4( mi, ma );
} | // Approximated BBox to a quadratic bezier
vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2 )
{ | 1 | 1 |
MdKBWt | iq | 2018-07-09T04:51:27 | // The MIT License
// Copyright © 2017 Inigo Quilez
// 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.
// Analytical computation of the exact bounding box for a cubic bezier segment
//
// See http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm
// Other bounding box functions:
//
// Disk - 3D BBox : https://www.shadertoy.com/view/ll3Xzf
// Cylinder - 3D BBox : https://www.shadertoy.com/view/MtcXRf
// Ellipse - 3D BBox : https://www.shadertoy.com/view/Xtjczw
// Cone boundong - 3D BBox : https://www.shadertoy.com/view/WdjSRK
// Cubic Bezier - 2D BBox : https://www.shadertoy.com/view/XdVBWd
// Cubic Bezier - 3D BBox : https://www.shadertoy.com/view/MdKBWt
// Quadratic Bezier - 2D BBox : https://www.shadertoy.com/view/lsyfWc
// Quadratic Bezier - 3D BBox : https://www.shadertoy.com/view/tsBfRD
#define AA 3
struct bound3
{
vec3 mMin;
vec3 mMax;
};
//---------------------------------------------------------------------------------------
// bounding box for a bezier (http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm)
//---------------------------------------------------------------------------------------
bound3 BezierAABB( in vec3 p0, in vec3 p1, in vec3 p2, in vec3 p3 )
{
// extremes
vec3 mi = min(p0,p3);
vec3 ma = max(p0,p3);
// note pascal triangle coefficnets
vec3 c = -1.0*p0 + 1.0*p1;
vec3 b = 1.0*p0 - 2.0*p1 + 1.0*p2;
vec3 a = -1.0*p0 + 3.0*p1 - 3.0*p2 + 1.0*p3;
vec3 h = b*b - a*c;
// real solutions
if( any(greaterThan(h,vec3(0.0))))
{
vec3 g = sqrt(abs(h));
vec3 t1 = clamp((-b - g)/a,0.0,1.0); vec3 s1 = 1.0-t1;
vec3 t2 = clamp((-b + g)/a,0.0,1.0); vec3 s2 = 1.0-t2;
vec3 q1 = s1*s1*s1*p0 + 3.0*s1*s1*t1*p1 + 3.0*s1*t1*t1*p2 + t1*t1*t1*p3;
vec3 q2 = s2*s2*s2*p0 + 3.0*s2*s2*t2*p1 + 3.0*s2*t2*t2*p2 + t2*t2*t2*p3;
if( h.x > 0.0 )
{
mi.x = min(mi.x,min(q1.x,q2.x));
ma.x = max(ma.x,max(q1.x,q2.x));
}
if( h.y > 0.0 )
{
mi.y = min(mi.y,min(q1.y,q2.y));
ma.y = max(ma.y,max(q1.y,q2.y));
}
if( h.z > 0.0 )
{
mi.z = min(mi.z,min(q1.z,q2.z));
ma.z = max(ma.z,max(q1.z,q2.z));
}
}
return bound3( mi, ma );
}
// ray-ellipse intersection
float iEllipse( in vec3 ro, in vec3 rd, // ray: origin, direction
in vec3 c, in vec3 u, in vec3 v ) // disk: center, 1st axis, 2nd axis
{
vec3 q = ro - c;
vec3 r = vec3(
dot( cross(u,v), q ),
dot( cross(q,u), rd ),
dot( cross(v,q), rd ) ) /
dot( cross(v,u), rd );
return (dot(r.yz,r.yz)<1.0) ? r.x : -1.0;
}
// ray-box intersection (simplified)
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
// ray-box intersection in box space
vec3 m = 1.0/rd;
vec3 n = m*(ro-cen);
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
}
float length2( in vec3 v ) { return dot(v,v); }
vec3 iSegment( in vec3 ro, in vec3 rd, in vec3 a, in vec3 b )
{
vec3 ba = b - a;
vec3 oa = ro - a;
float oad = dot( oa, rd );
float dba = dot( rd, ba );
float baba = dot( ba, ba );
float oaba = dot( oa, ba );
vec2 th = vec2( -oad*baba + dba*oaba, oaba - oad*dba ) / (baba - dba*dba);
th.x = max( th.x, 0.0 );
th.y = clamp( th.y, 0.0, 1.0 );
vec3 p = a + ba*th.y;
vec3 q = ro + rd*th.x;
return vec3( th, length2( p-q ) );
}
float iBezier( in vec3 ro, in vec3 rd, in vec3 p0, in vec3 p1, in vec3 p2, in vec3 p3, in float width)
{
const int kNum = 50;
float hit = -1.0;
float res = 1e10;
vec3 a = p0;
for( int i=1; i<kNum; i++ )
{
float t = float(i)/float(kNum-1);
float s = 1.0-t;
vec3 b = p0*s*s*s + p1*3.0*s*s*t + p2*3.0*s*t*t + p3*t*t*t;
vec3 r = iSegment( ro, rd, a, b );
if( r.z<width*width )
{
res = min( res, r.x );
hit = 1.0;
}
a = b;
}
return res*hit;
}
float hash1( in vec2 p )
{
return fract(sin(dot(p, vec2(12.9898, 78.233)))*43758.5453);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// camera position
vec3 ro = vec3( -0.5, 0.4, 1.5 );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// bezier animation
float time = iTime*0.5;
vec3 p0 = vec3(0.8,0.6,0.8)*sin( time*0.7 + vec3(3.0,1.0,2.0) );
vec3 p1 = vec3(0.8,0.6,0.8)*sin( time*1.1 + vec3(0.0,6.0,1.0) );
vec3 p2 = vec3(0.8,0.6,0.8)*sin( time*1.3 + vec3(4.0,2.0,3.0) );
vec3 p3 = vec3(0.8,0.6,0.8)*sin( time*1.5 + vec3(1.0,5.0,4.0) );
float thickness = 0.01;
// render
vec3 col = vec3(0.4)*(1.0-0.3*length(p));
// raytrace bezier
float t = iBezier( ro, rd, p0, p1, p2, p3, thickness);
float tmin = 1e10;
if( t>0.0 )
{
tmin = t;
col = vec3(1.0,0.75,0.3);
}
// compute bounding box for bezier
bound3 bbox = BezierAABB( p0, p1, p2, p3 );
bbox.mMin -= thickness;
bbox.mMax += thickness;
// raytrace bounding box
vec3 bcen = 0.5*(bbox.mMin+bbox.mMax);
vec3 brad = 0.5*(bbox.mMax-bbox.mMin);
vec2 tbox = iBox( ro, rd, bcen, brad );
if( tbox.x>0.0 )
{
// back face
if( tbox.y < tmin )
{
vec3 pos = ro + rd*tbox.y;
vec3 e = smoothstep( brad-0.03, brad-0.02, abs(pos-bcen) );
float al = 1.0 - (1.0-e.x*e.y)*(1.0-e.y*e.z)*(1.0-e.z*e.x);
col = mix( col, vec3(0.0), 0.25 + 0.75*al );
}
// front face
if( tbox.x < tmin )
{
vec3 pos = ro + rd*tbox.x;
vec3 e = smoothstep( brad-0.03, brad-0.02, abs(pos-bcen) );
float al = 1.0 - (1.0-e.x*e.y)*(1.0-e.y*e.z)*(1.0-e.z*e.x);
col = mix( col, vec3(0.0), 0.15 + 0.85*al );
}
}
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// dithering
tot += ((hash1(fragCoord.xy)+hash1(fragCoord.yx+13.1))/2.0-0.5)/256.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
1895,
2167,
2236,
2252,
3324
] | [
[
1895,
2167,
2236,
2252,
3324
],
[
3327,
3355,
3514,
3514,
3716
],
[
3719,
3756,
3820,
3858,
4138
],
[
4140,
4140,
4168,
4168,
4187
],
[
4189,
4189,
4252,
4252,
4643
],
[
4646,
4646,
4750,
4750,
5223
],
[
5225,
5225,
5251,
5251,
5318
]
] | //---------------------------------------------------------------------------------------
// bounding box for a bezier (http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm)
//---------------------------------------------------------------------------------------
| bound3 BezierAABB( in vec3 p0, in vec3 p1, in vec3 p2, in vec3 p3 )
{ |
vec3 mi = min(p0,p3);
vec3 ma = max(p0,p3);
// note pascal triangle coefficnets
vec3 c = -1.0*p0 + 1.0*p1;
vec3 b = 1.0*p0 - 2.0*p1 + 1.0*p2;
vec3 a = -1.0*p0 + 3.0*p1 - 3.0*p2 + 1.0*p3;
vec3 h = b*b - a*c;
// real solutions
if( any(greaterThan(h,vec3(0.0))))
{
vec3 g = sqrt(abs(h));
vec3 t1 = clamp((-b - g)/a,0.0,1.0); vec3 s1 = 1.0-t1;
vec3 t2 = clamp((-b + g)/a,0.0,1.0); vec3 s2 = 1.0-t2;
vec3 q1 = s1*s1*s1*p0 + 3.0*s1*s1*t1*p1 + 3.0*s1*t1*t1*p2 + t1*t1*t1*p3;
vec3 q2 = s2*s2*s2*p0 + 3.0*s2*s2*t2*p1 + 3.0*s2*t2*t2*p2 + t2*t2*t2*p3;
if( h.x > 0.0 )
{
mi.x = min(mi.x,min(q1.x,q2.x));
ma.x = max(ma.x,max(q1.x,q2.x));
}
if( h.y > 0.0 )
{
mi.y = min(mi.y,min(q1.y,q2.y));
ma.y = max(ma.y,max(q1.y,q2.y));
}
if( h.z > 0.0 )
{
mi.z = min(mi.z,min(q1.z,q2.z));
ma.z = max(ma.z,max(q1.z,q2.z));
}
}
return bound3( mi, ma );
} | //---------------------------------------------------------------------------------------
// bounding box for a bezier (http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm)
//---------------------------------------------------------------------------------------
bound3 BezierAABB( in vec3 p0, in vec3 p1, in vec3 p2, in vec3 p3 )
{ | 1 | 1 |
XdVBWd | iq | 2018-07-09T00:11:27 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Computes the exact axis aligned bounding box to a cubic Bezier curve. Since
// the bezier is cubic, the bbox can be compute with a quadratic equation:
//
// Yellow: naive bbox of the 4 control points
// Blue: exact bbox
//
// More info here: http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm
//
// Related Shaders:
// Quadratic Bezier - 3D : https://www.shadertoy.com/view/ldj3Wh
// Cubic Bezier - 2D BBox : https://www.shadertoy.com/view/XdVBWd
// Cubic Bezier - 3D BBox : https://www.shadertoy.com/view/MdKBWt
// Quadratic Bezier - 2D BBox : https://www.shadertoy.com/view/lsyfWc
// Quadratic Bezier - 3D BBox : https://www.shadertoy.com/view/tsBfRD
#if 1
// Exact BBox to a quadratic bezier
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{
// extremes
vec2 mi = min(p0,p3);
vec2 ma = max(p0,p3);
vec2 k0 = -1.0*p0 + 1.0*p1;
vec2 k1 = 1.0*p0 - 2.0*p1 + 1.0*p2;
vec2 k2 = -1.0*p0 + 3.0*p1 - 3.0*p2 + 1.0*p3;
vec2 h = k1*k1 - k0*k2;
if( h.x>0.0 )
{
h.x = sqrt(h.x);
//float t = (-k1.x - h.x)/k2.x;
float t = k0.x/(-k1.x-h.x);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.x + 3.0*s*s*t*p1.x + 3.0*s*t*t*p2.x + t*t*t*p3.x;
mi.x = min(mi.x,q);
ma.x = max(ma.x,q);
}
//t = (-k1.x + h.x)/k2.x;
t = k0.x/(-k1.x+h.x);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.x + 3.0*s*s*t*p1.x + 3.0*s*t*t*p2.x + t*t*t*p3.x;
mi.x = min(mi.x,q);
ma.x = max(ma.x,q);
}
}
if( h.y>0.0)
{
h.y = sqrt(h.y);
//float t = (-k1.y - h.y)/k2.y;
float t = k0.y/(-k1.y-h.y);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.y + 3.0*s*s*t*p1.y + 3.0*s*t*t*p2.y + t*t*t*p3.y;
mi.y = min(mi.y,q);
ma.y = max(ma.y,q);
}
//t = (-k1.y + h.y)/k2.y;
t = k0.y/(-k1.y+h.y);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.y + 3.0*s*s*t*p1.y + 3.0*s*t*t*p2.y + t*t*t*p3.y;
mi.y = min(mi.y,q);
ma.y = max(ma.y,q);
}
}
return vec4( mi, ma );
}
#else
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{
// extremes
vec2 mi = min(p0,p3);
vec2 ma = max(p0,p3);
// note pascal triangle coefficnets
vec2 c = -1.0*p0 + 1.0*p1;
vec2 b = 1.0*p0 - 2.0*p1 + 1.0*p2;
vec2 a = -1.0*p0 + 3.0*p1 - 3.0*p2 + 1.0*p3;
vec2 h = b*b - a*c;
// real solutions
if( any(greaterThan(h,vec2(0.0))))
{
vec2 g = sqrt(abs(h));
vec2 t1 = clamp((-b - g)/a,0.0,1.0); vec2 s1 = 1.0-t1;
vec2 t2 = clamp((-b + g)/a,0.0,1.0); vec2 s2 = 1.0-t2;
vec2 q1 = s1*s1*s1*p0 + 3.0*s1*s1*t1*p1 + 3.0*s1*t1*t1*p2 + t1*t1*t1*p3;
vec2 q2 = s2*s2*s2*p0 + 3.0*s2*s2*t2*p1 + 3.0*s2*t2*t2*p2 + t2*t2*t2*p3;
if( h.x > 0.0 )
{
mi.x = min(mi.x,min(q1.x,q2.x));
ma.x = max(ma.x,max(q1.x,q2.x));
}
if( h.y > 0.0 )
{
mi.y = min(mi.y,min(q1.y,q2.y));
ma.y = max(ma.y,max(q1.y,q2.y));
}
}
return vec4( mi, ma );
}
#endif
// Approximated conservative BBox to a cubic bezier
vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{
vec2 mi = min(min(p0,p1),min(p2,p3));
vec2 ma = max(max(p0,p1),max(p2,p3));
return vec4( mi, ma );
}
//---------------------------------------------------------------------------------------
float sdBox( in vec2 p, in vec2 b )
{
vec2 q = abs(p) - b;
vec2 m = vec2( min(q.x,q.y), max(q.x,q.y) );
return (m.x > 0.0) ? length(q) : m.y;
}
float length2( in vec2 v ) { return dot(v,v); }
float sdSegmentSq( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length2( pa - ba*h );
}
float sdSegment( in vec2 p, in vec2 a, in vec2 b )
{
return sqrt(sdSegmentSq(p,a,b));
}
// slow, do not use in production. Can probably do better than
// tesselation in linear segments.
vec2 udBezier(vec2 p0, vec2 p1, vec2 p2, in vec2 p3, vec2 pos)
{
const int kNum = 50;
vec2 res = vec2(1e10,0.0);
vec2 a = p0;
for( int i=1; i<kNum; i++ )
{
float t = float(i)/float(kNum-1);
float s = 1.0-t;
vec2 b = p0*s*s*s + p1*3.0*s*s*t + p2*3.0*s*t*t + p3*t*t*t;
float d = sdSegmentSq( pos, a, b );
if( d<res.x ) res = vec2(d,t);
a = b;
}
return vec2(sqrt(res.x),res.y);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
//--------
// animate
//--------
float time = iTime*0.5 - 0.7;
vec2 p0 = 0.8*sin( time*0.7 + vec2(3.0,1.0) );
vec2 p1 = 0.8*sin( time*1.1 + vec2(0.0,6.0) );
vec2 p2 = 0.8*sin( time*1.3 + vec2(4.0,2.0) );
vec2 p3 = 0.8*sin( time*1.5 + vec2(1.0,5.0) );
//-------------
// compute bbox
//-------------
vec4 b1 = bboxBezierSimple(p0,p1,p2,p3);
vec4 b2 = bboxBezier(p0,p1,p2,p3);
//--------
// render
//--------
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
// background
vec3 col = vec3(0.15);
float be = udBezier( p0, p1, p2, p3, p ).x;
col += 0.03*sin(be*150.0);
col *= 1.0 - 0.3*length(p);
// naive bbox
float d = sdBox( p-(b1.xy+b1.zw)*0.5, (b1.zw-b1.xy)*0.5 );
col = mix( col, vec3(1.0,0.6,0.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// exact bbox
d = sdBox( p-(b2.xy+b2.zw)*0.5, (b2.zw-b2.xy)*0.5 );
col = mix( col, vec3(0.2,0.5,1.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// control cage
d = sdSegment( p, p0, p1 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
d = sdSegment( p, p1, p2 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
d = sdSegment( p, p2, p3 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
// bezier
d = be;
col = mix( col, vec3(1.0), 1.0-smoothstep(0.003,0.003+px*1.5,d) );
// control points
d = length(p0-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p1-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p2-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p3-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
fragColor = vec4(col,1.0);
} | mit | [
4468,
4520,
4592,
4592,
4710
] | [
[
4468,
4520,
4592,
4592,
4710
],
[
4803,
4803,
4841,
4841,
4960
],
[
4962,
4962,
4990,
4990,
5009
],
[
5011,
5011,
5065,
5065,
5176
],
[
5178,
5178,
5230,
5230,
5266
],
[
5268,
5366,
5430,
5430,
5826
],
[
5828,
5828,
5885,
5930,
7761
]
] | // Approximated conservative BBox to a cubic bezier
| vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{ |
vec2 mi = min(min(p0,p1),min(p2,p3));
vec2 ma = max(max(p0,p1),max(p2,p3));
return vec4( mi, ma );
} | // Approximated conservative BBox to a cubic bezier
vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{ | 1 | 1 |
XdVBWd | iq | 2018-07-09T00:11:27 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Computes the exact axis aligned bounding box to a cubic Bezier curve. Since
// the bezier is cubic, the bbox can be compute with a quadratic equation:
//
// Yellow: naive bbox of the 4 control points
// Blue: exact bbox
//
// More info here: http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm
//
// Related Shaders:
// Quadratic Bezier - 3D : https://www.shadertoy.com/view/ldj3Wh
// Cubic Bezier - 2D BBox : https://www.shadertoy.com/view/XdVBWd
// Cubic Bezier - 3D BBox : https://www.shadertoy.com/view/MdKBWt
// Quadratic Bezier - 2D BBox : https://www.shadertoy.com/view/lsyfWc
// Quadratic Bezier - 3D BBox : https://www.shadertoy.com/view/tsBfRD
#if 1
// Exact BBox to a quadratic bezier
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{
// extremes
vec2 mi = min(p0,p3);
vec2 ma = max(p0,p3);
vec2 k0 = -1.0*p0 + 1.0*p1;
vec2 k1 = 1.0*p0 - 2.0*p1 + 1.0*p2;
vec2 k2 = -1.0*p0 + 3.0*p1 - 3.0*p2 + 1.0*p3;
vec2 h = k1*k1 - k0*k2;
if( h.x>0.0 )
{
h.x = sqrt(h.x);
//float t = (-k1.x - h.x)/k2.x;
float t = k0.x/(-k1.x-h.x);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.x + 3.0*s*s*t*p1.x + 3.0*s*t*t*p2.x + t*t*t*p3.x;
mi.x = min(mi.x,q);
ma.x = max(ma.x,q);
}
//t = (-k1.x + h.x)/k2.x;
t = k0.x/(-k1.x+h.x);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.x + 3.0*s*s*t*p1.x + 3.0*s*t*t*p2.x + t*t*t*p3.x;
mi.x = min(mi.x,q);
ma.x = max(ma.x,q);
}
}
if( h.y>0.0)
{
h.y = sqrt(h.y);
//float t = (-k1.y - h.y)/k2.y;
float t = k0.y/(-k1.y-h.y);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.y + 3.0*s*s*t*p1.y + 3.0*s*t*t*p2.y + t*t*t*p3.y;
mi.y = min(mi.y,q);
ma.y = max(ma.y,q);
}
//t = (-k1.y + h.y)/k2.y;
t = k0.y/(-k1.y+h.y);
if( t>0.0 && t<1.0 )
{
float s = 1.0-t;
float q = s*s*s*p0.y + 3.0*s*s*t*p1.y + 3.0*s*t*t*p2.y + t*t*t*p3.y;
mi.y = min(mi.y,q);
ma.y = max(ma.y,q);
}
}
return vec4( mi, ma );
}
#else
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{
// extremes
vec2 mi = min(p0,p3);
vec2 ma = max(p0,p3);
// note pascal triangle coefficnets
vec2 c = -1.0*p0 + 1.0*p1;
vec2 b = 1.0*p0 - 2.0*p1 + 1.0*p2;
vec2 a = -1.0*p0 + 3.0*p1 - 3.0*p2 + 1.0*p3;
vec2 h = b*b - a*c;
// real solutions
if( any(greaterThan(h,vec2(0.0))))
{
vec2 g = sqrt(abs(h));
vec2 t1 = clamp((-b - g)/a,0.0,1.0); vec2 s1 = 1.0-t1;
vec2 t2 = clamp((-b + g)/a,0.0,1.0); vec2 s2 = 1.0-t2;
vec2 q1 = s1*s1*s1*p0 + 3.0*s1*s1*t1*p1 + 3.0*s1*t1*t1*p2 + t1*t1*t1*p3;
vec2 q2 = s2*s2*s2*p0 + 3.0*s2*s2*t2*p1 + 3.0*s2*t2*t2*p2 + t2*t2*t2*p3;
if( h.x > 0.0 )
{
mi.x = min(mi.x,min(q1.x,q2.x));
ma.x = max(ma.x,max(q1.x,q2.x));
}
if( h.y > 0.0 )
{
mi.y = min(mi.y,min(q1.y,q2.y));
ma.y = max(ma.y,max(q1.y,q2.y));
}
}
return vec4( mi, ma );
}
#endif
// Approximated conservative BBox to a cubic bezier
vec4 bboxBezierSimple(in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p3 )
{
vec2 mi = min(min(p0,p1),min(p2,p3));
vec2 ma = max(max(p0,p1),max(p2,p3));
return vec4( mi, ma );
}
//---------------------------------------------------------------------------------------
float sdBox( in vec2 p, in vec2 b )
{
vec2 q = abs(p) - b;
vec2 m = vec2( min(q.x,q.y), max(q.x,q.y) );
return (m.x > 0.0) ? length(q) : m.y;
}
float length2( in vec2 v ) { return dot(v,v); }
float sdSegmentSq( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length2( pa - ba*h );
}
float sdSegment( in vec2 p, in vec2 a, in vec2 b )
{
return sqrt(sdSegmentSq(p,a,b));
}
// slow, do not use in production. Can probably do better than
// tesselation in linear segments.
vec2 udBezier(vec2 p0, vec2 p1, vec2 p2, in vec2 p3, vec2 pos)
{
const int kNum = 50;
vec2 res = vec2(1e10,0.0);
vec2 a = p0;
for( int i=1; i<kNum; i++ )
{
float t = float(i)/float(kNum-1);
float s = 1.0-t;
vec2 b = p0*s*s*s + p1*3.0*s*s*t + p2*3.0*s*t*t + p3*t*t*t;
float d = sdSegmentSq( pos, a, b );
if( d<res.x ) res = vec2(d,t);
a = b;
}
return vec2(sqrt(res.x),res.y);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
//--------
// animate
//--------
float time = iTime*0.5 - 0.7;
vec2 p0 = 0.8*sin( time*0.7 + vec2(3.0,1.0) );
vec2 p1 = 0.8*sin( time*1.1 + vec2(0.0,6.0) );
vec2 p2 = 0.8*sin( time*1.3 + vec2(4.0,2.0) );
vec2 p3 = 0.8*sin( time*1.5 + vec2(1.0,5.0) );
//-------------
// compute bbox
//-------------
vec4 b1 = bboxBezierSimple(p0,p1,p2,p3);
vec4 b2 = bboxBezier(p0,p1,p2,p3);
//--------
// render
//--------
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
// background
vec3 col = vec3(0.15);
float be = udBezier( p0, p1, p2, p3, p ).x;
col += 0.03*sin(be*150.0);
col *= 1.0 - 0.3*length(p);
// naive bbox
float d = sdBox( p-(b1.xy+b1.zw)*0.5, (b1.zw-b1.xy)*0.5 );
col = mix( col, vec3(1.0,0.6,0.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// exact bbox
d = sdBox( p-(b2.xy+b2.zw)*0.5, (b2.zw-b2.xy)*0.5 );
col = mix( col, vec3(0.2,0.5,1.0), 1.0-smoothstep(0.003,0.003+px,abs(d)) );
// control cage
d = sdSegment( p, p0, p1 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
d = sdSegment( p, p1, p2 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
d = sdSegment( p, p2, p3 );
col = mix( col, vec3(0.3), 1.0-smoothstep(0.003,0.003+px,d) );
// bezier
d = be;
col = mix( col, vec3(1.0), 1.0-smoothstep(0.003,0.003+px*1.5,d) );
// control points
d = length(p0-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p1-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p2-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
d = length(p3-p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.04,0.04+px,d) );
fragColor = vec4(col,1.0);
} | mit | [
5268,
5366,
5430,
5430,
5826
] | [
[
4468,
4520,
4592,
4592,
4710
],
[
4803,
4803,
4841,
4841,
4960
],
[
4962,
4962,
4990,
4990,
5009
],
[
5011,
5011,
5065,
5065,
5176
],
[
5178,
5178,
5230,
5230,
5266
],
[
5268,
5366,
5430,
5430,
5826
],
[
5828,
5828,
5885,
5930,
7761
]
] | // slow, do not use in production. Can probably do better than
// tesselation in linear segments.
| vec2 udBezier(vec2 p0, vec2 p1, vec2 p2, in vec2 p3, vec2 pos)
{ |
const int kNum = 50;
vec2 res = vec2(1e10,0.0);
vec2 a = p0;
for( int i=1; i<kNum; i++ )
{
float t = float(i)/float(kNum-1);
float s = 1.0-t;
vec2 b = p0*s*s*s + p1*3.0*s*s*t + p2*3.0*s*t*t + p3*t*t*t;
float d = sdSegmentSq( pos, a, b );
if( d<res.x ) res = vec2(d,t);
a = b;
}
return vec2(sqrt(res.x),res.y);
} | // slow, do not use in production. Can probably do better than
// tesselation in linear segments.
vec2 udBezier(vec2 p0, vec2 p1, vec2 p2, in vec2 p3, vec2 pos)
{ | 2 | 2 |
MlGcDz | iq | 2018-09-12T08:22:36 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// List of ray-surface intersectors at https://www.shadertoy.com/playlist/l3dXRf
// and http://iquilezles.org/www/articles/intersectors/intersectors.htm
// Other triangle functions:
//
// Occlusion: https://www.shadertoy.com/view/XdjSDy
// Distance: https://www.shadertoy.com/view/4sXXRN
// Intersection: https://www.shadertoy.com/view/MlGcDz
// Closest: https://www.shadertoy.com/view/ttfGWl
//=====================================================
// Triangle intersection. Returns { t, u, v }
vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{
vec3 v1v0 = v1 - v0;
vec3 v2v0 = v2 - v0;
vec3 rov0 = ro - v0;
#if 0
// Cramer's rule for solcing p(t) = ro+t·rd = p(u,v) = vo + u·(v1-v0) + v·(v2-v1)
float d = 1.0/determinant(mat3(v1v0, v2v0, -rd ));
float u = d*determinant(mat3(rov0, v2v0, -rd ));
float v = d*determinant(mat3(v1v0, rov0, -rd ));
float t = d*determinant(mat3(v1v0, v2v0, rov0));
#else
// The four determinants above have lots of terms in common. Knowing the changing
// the order of the columns/rows doesn't change the volume/determinant, and that
// the volume is dot(cross(a,b,c)), we can precompute some common terms and reduce
// it all to:
vec3 n = cross( v1v0, v2v0 );
vec3 q = cross( rov0, rd );
float d = 1.0/dot( rd, n );
float u = d*dot( -q, v2v0 );
float v = d*dot( q, v1v0 );
float t = d*dot( -n, rov0 );
#endif
if( u<0.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
return vec3( t, u, v );
}
// Triangle occlusion (if fully visible)
float triOcclusion( in vec3 pos, in vec3 nor, in vec3 v0, in vec3 v1, in vec3 v2 )
{
vec3 a = normalize(v0-pos);
vec3 b = normalize(v1-pos);
vec3 c = normalize(v2-pos);
float s = -sign(dot(v0-pos,cross(v0-v1,v2-v1))); // other side of the triangle
// page 300 in http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.466.963&rep=rep1&type=pdf
float r = dot(nor,normalize(cross(a,b))) * acos(dot(a,b)) +
dot(nor,normalize(cross(b,c))) * acos(dot(b,c)) +
dot(nor,normalize(cross(c,a))) * acos(dot(c,a));
return 1.0-max(0.0,s*r)/6.2831;
}
//=====================================================
float iPlane( in vec3 ro, in vec3 rd )
{
return (-1.0 - ro.y)/rd.y;
}
//=====================================================
vec3 pattern( in vec2 uv )
{
vec3 col = vec3(0.6);
col += 0.4*smoothstep(-0.01,0.01,cos(uv.x*0.5)*cos(uv.y*0.5));
col *= smoothstep(-1.0,-0.98,cos(uv.x))*smoothstep(-1.0,-0.98,cos(uv.y));
return col;
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
vec3 ro = vec3(0.0, 0.0, 4.0 );
vec3 rd = normalize( vec3(p,-2.0) );
// triangle animation
vec3 v1 = cos( iTime*1.0 + vec3(2.0,1.0,1.0) + 1.0 )*vec3(1.5,1.0,1.0);
vec3 v2 = cos( iTime*1.0 + vec3(5.0,2.0,3.0) + 2.0 )*vec3(1.5,1.0,1.0);
vec3 v3 = cos( iTime*1.2 + vec3(1.0,3.0,5.0) + 4.0 )*vec3(1.5,1.0,1.0);
vec3 col = vec3(0.08) + 0.02*rd.y;
float tmin = 1e10;
float t1 = iPlane( ro, rd );
if( t1>0.0 )
{
tmin = t1;
vec3 pos = ro + tmin*rd;
vec3 nor = vec3(0.0,1.0,0.0);
float occ = triOcclusion( pos, nor, v1, v2, v3 );
col = mix( col*3.0*occ*occ, col, 1.0-exp(-0.02*tmin) );
}
col *= 1.0-0.3*length(p);
vec3 res = triIntersect( ro, rd, v1, v2, v3 );
float t2 = res.x;
if( t2>0.0 && t2<tmin )
{
tmin = t2;
float t = t2;
vec3 pos = ro + t*rd;
vec3 nor = normalize( cross( v2-v1, v3-v1 ) );
col = pattern(64.0*res.yz);
col *= 0.55 + 0.45*faceforward(-nor, -rd, nor).y;
}
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// dither to remove banding in the background
tot += fract(sin(fragCoord.x*vec3(13,1,11)+fragCoord.y*vec3(1,7,5))*158.391832)/255.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
1547,
1593,
1674,
1674,
2641
] | [
[
1547,
1593,
1674,
1674,
2641
],
[
2645,
2686,
2770,
2770,
3291
],
[
3350,
3350,
3390,
3390,
3423
],
[
3482,
3482,
3510,
3510,
3700
]
] | // Triangle intersection. Returns { t, u, v }
| vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{ |
vec3 v1v0 = v1 - v0;
vec3 v2v0 = v2 - v0;
vec3 rov0 = ro - v0;
#if 0
// Cramer's rule for solcing p(t) = ro+t·rd = p(u,v) = vo + u·(v1-v0) + v·(v2-v1)
float d = 1.0/determinant(mat3(v1v0, v2v0, -rd ));
float u = d*determinant(mat3(rov0, v2v0, -rd ));
float v = d*determinant(mat3(v1v0, rov0, -rd ));
float t = d*determinant(mat3(v1v0, v2v0, rov0));
#else
// The four determinants above have lots of terms in common. Knowing the changing
// the order of the columns/rows doesn't change the volume/determinant, and that
// the volume is dot(cross(a,b,c)), we can precompute some common terms and reduce
// it all to:
vec3 n = cross( v1v0, v2v0 );
vec3 q = cross( rov0, rd );
float d = 1.0/dot( rd, n );
float u = d*dot( -q, v2v0 );
float v = d*dot( q, v1v0 );
float t = d*dot( -n, rov0 );
#endif
if( u<0.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
return vec3( t, u, v );
} | // Triangle intersection. Returns { t, u, v }
vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{ | 3 | 5 |
MlGcDz | iq | 2018-09-12T08:22:36 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// List of ray-surface intersectors at https://www.shadertoy.com/playlist/l3dXRf
// and http://iquilezles.org/www/articles/intersectors/intersectors.htm
// Other triangle functions:
//
// Occlusion: https://www.shadertoy.com/view/XdjSDy
// Distance: https://www.shadertoy.com/view/4sXXRN
// Intersection: https://www.shadertoy.com/view/MlGcDz
// Closest: https://www.shadertoy.com/view/ttfGWl
//=====================================================
// Triangle intersection. Returns { t, u, v }
vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{
vec3 v1v0 = v1 - v0;
vec3 v2v0 = v2 - v0;
vec3 rov0 = ro - v0;
#if 0
// Cramer's rule for solcing p(t) = ro+t·rd = p(u,v) = vo + u·(v1-v0) + v·(v2-v1)
float d = 1.0/determinant(mat3(v1v0, v2v0, -rd ));
float u = d*determinant(mat3(rov0, v2v0, -rd ));
float v = d*determinant(mat3(v1v0, rov0, -rd ));
float t = d*determinant(mat3(v1v0, v2v0, rov0));
#else
// The four determinants above have lots of terms in common. Knowing the changing
// the order of the columns/rows doesn't change the volume/determinant, and that
// the volume is dot(cross(a,b,c)), we can precompute some common terms and reduce
// it all to:
vec3 n = cross( v1v0, v2v0 );
vec3 q = cross( rov0, rd );
float d = 1.0/dot( rd, n );
float u = d*dot( -q, v2v0 );
float v = d*dot( q, v1v0 );
float t = d*dot( -n, rov0 );
#endif
if( u<0.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
return vec3( t, u, v );
}
// Triangle occlusion (if fully visible)
float triOcclusion( in vec3 pos, in vec3 nor, in vec3 v0, in vec3 v1, in vec3 v2 )
{
vec3 a = normalize(v0-pos);
vec3 b = normalize(v1-pos);
vec3 c = normalize(v2-pos);
float s = -sign(dot(v0-pos,cross(v0-v1,v2-v1))); // other side of the triangle
// page 300 in http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.466.963&rep=rep1&type=pdf
float r = dot(nor,normalize(cross(a,b))) * acos(dot(a,b)) +
dot(nor,normalize(cross(b,c))) * acos(dot(b,c)) +
dot(nor,normalize(cross(c,a))) * acos(dot(c,a));
return 1.0-max(0.0,s*r)/6.2831;
}
//=====================================================
float iPlane( in vec3 ro, in vec3 rd )
{
return (-1.0 - ro.y)/rd.y;
}
//=====================================================
vec3 pattern( in vec2 uv )
{
vec3 col = vec3(0.6);
col += 0.4*smoothstep(-0.01,0.01,cos(uv.x*0.5)*cos(uv.y*0.5));
col *= smoothstep(-1.0,-0.98,cos(uv.x))*smoothstep(-1.0,-0.98,cos(uv.y));
return col;
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
vec3 ro = vec3(0.0, 0.0, 4.0 );
vec3 rd = normalize( vec3(p,-2.0) );
// triangle animation
vec3 v1 = cos( iTime*1.0 + vec3(2.0,1.0,1.0) + 1.0 )*vec3(1.5,1.0,1.0);
vec3 v2 = cos( iTime*1.0 + vec3(5.0,2.0,3.0) + 2.0 )*vec3(1.5,1.0,1.0);
vec3 v3 = cos( iTime*1.2 + vec3(1.0,3.0,5.0) + 4.0 )*vec3(1.5,1.0,1.0);
vec3 col = vec3(0.08) + 0.02*rd.y;
float tmin = 1e10;
float t1 = iPlane( ro, rd );
if( t1>0.0 )
{
tmin = t1;
vec3 pos = ro + tmin*rd;
vec3 nor = vec3(0.0,1.0,0.0);
float occ = triOcclusion( pos, nor, v1, v2, v3 );
col = mix( col*3.0*occ*occ, col, 1.0-exp(-0.02*tmin) );
}
col *= 1.0-0.3*length(p);
vec3 res = triIntersect( ro, rd, v1, v2, v3 );
float t2 = res.x;
if( t2>0.0 && t2<tmin )
{
tmin = t2;
float t = t2;
vec3 pos = ro + t*rd;
vec3 nor = normalize( cross( v2-v1, v3-v1 ) );
col = pattern(64.0*res.yz);
col *= 0.55 + 0.45*faceforward(-nor, -rd, nor).y;
}
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// dither to remove banding in the background
tot += fract(sin(fragCoord.x*vec3(13,1,11)+fragCoord.y*vec3(1,7,5))*158.391832)/255.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
2645,
2686,
2770,
2770,
3291
] | [
[
1547,
1593,
1674,
1674,
2641
],
[
2645,
2686,
2770,
2770,
3291
],
[
3350,
3350,
3390,
3390,
3423
],
[
3482,
3482,
3510,
3510,
3700
]
] | // Triangle occlusion (if fully visible)
| float triOcclusion( in vec3 pos, in vec3 nor, in vec3 v0, in vec3 v1, in vec3 v2 )
{ |
vec3 a = normalize(v0-pos);
vec3 b = normalize(v1-pos);
vec3 c = normalize(v2-pos);
float s = -sign(dot(v0-pos,cross(v0-v1,v2-v1))); // other side of the triangle
// page 300 in http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.466.963&rep=rep1&type=pdf
float r = dot(nor,normalize(cross(a,b))) * acos(dot(a,b)) +
dot(nor,normalize(cross(b,c))) * acos(dot(b,c)) +
dot(nor,normalize(cross(c,a))) * acos(dot(c,a));
return 1.0-max(0.0,s*r)/6.2831;
} | // Triangle occlusion (if fully visible)
float triOcclusion( in vec3 pos, in vec3 nor, in vec3 v0, in vec3 v1, in vec3 v2 )
{ | 1 | 2 |
MlKcDD | iq | 2018-09-08T04:57:14 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Distance to a quadratic bezier segment, which can be solved analyically with a cubic.
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float dot2( in vec2 v ) { return dot(v,v); }
float cross2( in vec2 a, in vec2 b ) { return a.x*b.y - a.y*b.x; }
// signed distance to a quadratic bezier
float sdBezier( in vec2 pos, in vec2 A, in vec2 B, in vec2 C )
{
vec2 a = B - A;
vec2 b = A - 2.0*B + C;
vec2 c = a * 2.0;
vec2 d = A - pos;
float kk = 1.0/dot(b,b);
float kx = kk * dot(a,b);
float ky = kk * (2.0*dot(a,a)+dot(d,b))/3.0;
float kz = kk * dot(d,a);
float res = 0.0;
float sgn = 0.0;
float p = ky - kx*kx;
float q = kx*(2.0*kx*kx - 3.0*ky) + kz;
float p3 = p*p*p;
float q2 = q*q;
float h = q2 + 4.0*p3;
if( h>=0.0 )
{ // 1 root
h = sqrt(h);
vec2 x = (vec2(h,-h)-q)/2.0;
#if 0
// When p≈0 and p<0, h-q has catastrophic cancelation. So, we do
// h=√(q²+4p³)=q·√(1+4p³/q²)=q·√(1+w) instead. Now we approximate
// √ by a linear Taylor expansion into h≈q(1+½w) so that the q's
// cancel each other in h-q. Expanding and simplifying further we
// get x=vec2(p³/q,-p³/q-q). And using a second degree Taylor
// expansion instead: x=vec2(k,-k-q) with k=(1-p³/q²)·p³/q
if( abs(p)<0.001 )
{
float k = p3/q; // linear approx
//float k = (1.0-p3/q2)*p3/q; // quadratic approx
x = vec2(k,-k-q);
}
#endif
vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0));
float t = clamp( uv.x+uv.y-kx, 0.0, 1.0 );
vec2 q = d+(c+b*t)*t;
res = dot2(q);
sgn = cross2(c+2.0*b*t,q);
}
else
{ // 3 roots
float z = sqrt(-p);
float v = acos(q/(p*z*2.0))/3.0;
float m = cos(v);
float n = sin(v)*1.732050808;
vec3 t = clamp( vec3(m+m,-n-m,n-m)*z-kx, 0.0, 1.0 );
vec2 qx=d+(c+b*t.x)*t.x; float dx=dot2(qx), sx = cross2(c+2.0*b*t.x,qx);
vec2 qy=d+(c+b*t.y)*t.y; float dy=dot2(qy), sy = cross2(c+2.0*b*t.y,qy);
if( dx<dy ) { res=dx; sgn=sx; } else {res=dy; sgn=sy; }
}
return sqrt( res )*sign(sgn);
}
float udSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
vec2 v0 = vec2(1.3,0.9)*cos(iTime*0.5 + vec2(0.0,5.0) );
vec2 v1 = vec2(1.3,0.9)*cos(iTime*0.6 + vec2(3.0,4.0) );
vec2 v2 = vec2(1.3,0.9)*cos(iTime*0.7 + vec2(2.0,0.0) );
float d = sdBezier( p, v0,v1,v2 );
float f = smoothstep(-0.2,0.2,cos(2.0*iTime));
vec3 col = vec3(1.0) - vec3(0.1,0.4,0.7)*mix(sign(d),1.0,f);
col *= 1.0 - exp(-4.0*abs(d));
col *= 0.8 + 0.2*cos(140.0*d);
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );
if( iMouse.z>0.001 )
{
d = sdBezier(m, v0,v1,v2 );
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025));
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015));
}
if( cos(0.5*iTime)<-0.5 )
{
d = min( udSegment(p,v0,v1),
udSegment(p,v1,v2) );
d = min( d, length(p-v0)-0.02 );
d = min( d, length(p-v1)-0.02 );
d = min( d, length(p-v2)-0.02 );
col = mix( col, vec3(1,0,0), 1.0-smoothstep(0.0,0.007,d) );
}
fragColor = vec4(col,1.0);
} | mit | [
1439,
1480,
1544,
1544,
3448
] | [
[
1325,
1325,
1350,
1350,
1369
],
[
1370,
1370,
1408,
1408,
1436
],
[
1439,
1480,
1544,
1544,
3448
],
[
3450,
3450,
3502,
3502,
3622
],
[
3624,
3624,
3681,
3681,
4877
]
] | // signed distance to a quadratic bezier
| float sdBezier( in vec2 pos, in vec2 A, in vec2 B, in vec2 C )
{ |
vec2 a = B - A;
vec2 b = A - 2.0*B + C;
vec2 c = a * 2.0;
vec2 d = A - pos;
float kk = 1.0/dot(b,b);
float kx = kk * dot(a,b);
float ky = kk * (2.0*dot(a,a)+dot(d,b))/3.0;
float kz = kk * dot(d,a);
float res = 0.0;
float sgn = 0.0;
float p = ky - kx*kx;
float q = kx*(2.0*kx*kx - 3.0*ky) + kz;
float p3 = p*p*p;
float q2 = q*q;
float h = q2 + 4.0*p3;
if( h>=0.0 )
{ // 1 root
h = sqrt(h);
vec2 x = (vec2(h,-h)-q)/2.0;
#if 0
// When p≈0 and p<0, h-q has catastrophic cancelation. So, we do
// h=√(q²+4p³)=q·√(1+4p³/q²)=q·√(1+w) instead. Now we approximate
// √ by a linear Taylor expansion into h≈q(1+½w) so that the q's
// cancel each other in h-q. Expanding and simplifying further we
// get x=vec2(p³/q,-p³/q-q). And using a second degree Taylor
// expansion instead: x=vec2(k,-k-q) with k=(1-p³/q²)·p³/q
if( abs(p)<0.001 )
{
float k = p3/q; // linear approx
//float k = (1.0-p3/q2)*p3/q; // quadratic approx
x = vec2(k,-k-q);
}
#endif
vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0));
float t = clamp( uv.x+uv.y-kx, 0.0, 1.0 );
vec2 q = d+(c+b*t)*t;
res = dot2(q);
sgn = cross2(c+2.0*b*t,q);
}
else
{ // 3 roots
float z = sqrt(-p);
float v = acos(q/(p*z*2.0))/3.0;
float m = cos(v);
float n = sin(v)*1.732050808;
vec3 t = clamp( vec3(m+m,-n-m,n-m)*z-kx, 0.0, 1.0 );
vec2 qx=d+(c+b*t.x)*t.x; float dx=dot2(qx), sx = cross2(c+2.0*b*t.x,qx);
vec2 qy=d+(c+b*t.y)*t.y; float dy=dot2(qy), sy = cross2(c+2.0*b*t.y,qy);
if( dx<dy ) { res=dx; sgn=sx; } else {res=dy; sgn=sy; }
}
return sqrt( res )*sign(sgn);
} | // signed distance to a quadratic bezier
float sdBezier( in vec2 pos, in vec2 A, in vec2 B, in vec2 C )
{ | 1 | 7 |
MlycD3 | iq | 2018-09-26T08:53:33 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Distance to an isosceles trapezoid
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float dot2(in vec2 v ) { return dot(v,v); }
// trapezoid / capped cone, specialized for Y alignment
float sdTrapezoid( in vec2 p, in float r1, float r2, float he )
{
vec2 k1 = vec2(r2,he);
vec2 k2 = vec2(r2-r1,2.0*he);
p.x = abs(p.x);
vec2 ca = vec2(max(0.0,p.x-((p.y<0.0)?r1:r2)), abs(p.y)-he);
vec2 cb = p - k1 + k2*clamp( dot(k1-p,k2)/dot2(k2), 0.0, 1.0 );
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(dot2(ca),dot2(cb)) );
}
// trapezoid / capped cone
float sdTrapezoid( in vec2 p, in vec2 a, in vec2 b, in float ra, float rb )
{
float rba = rb-ra;
float baba = dot(b-a,b-a);
float papa = dot(p-a,p-a);
float paba = dot(p-a,b-a)/baba;
float x = sqrt( papa - paba*paba*baba );
float cax = max(0.0,x-((paba<0.5)?ra:rb));
float cay = abs(paba-0.5)-0.5;
float k = rba*rba + baba;
float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 );
float cbx = x-ra - f*rba;
float cby = paba - f;
float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(cax*cax + cay*cay*baba,
cbx*cbx + cby*cby*baba) );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
float ra = 0.2+0.15*sin(iTime*1.3+0.0);
float rb = 0.2+0.15*sin(iTime*1.4+1.1);
vec2 pa = vec2(-0.6,0.0)+0.4*sin(iTime*1.1+vec2(0.0,2.0));
vec2 pb = vec2(-0.6,0.0)+0.4*sin(iTime*1.2+vec2(1.0,2.5));
vec2 pc = vec2(0.8,0.0);
// axis aligned trapezoid
float d = sdTrapezoid( p-pc, ra, rb, 0.5+0.2*sin(1.3*iTime) );
// aribitrary trapezoid
d = min( d, sdTrapezoid( p, pa, pb , ra, rb ) );
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-4.0*abs(d));
col *= 0.8 + 0.2*cos(140.0*d);
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.005,abs(d)) );
if( iMouse.z>0.001 )
{
d = sdTrapezoid( m, pa, pb , ra, rb );
d = min( d, sdTrapezoid( m-pc, ra, rb, 0.5+0.2*sin(1.3*iTime) ) );
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025));
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015));
}
fragColor = vec4(col,1.0);
} | mit | [
1321,
1377,
1442,
1442,
1766
] | [
[
1276,
1276,
1300,
1300,
1319
],
[
1321,
1377,
1442,
1442,
1766
],
[
1768,
1795,
1872,
1872,
2418
],
[
2419,
2419,
2476,
2476,
3645
]
] | // trapezoid / capped cone, specialized for Y alignment
| float sdTrapezoid( in vec2 p, in float r1, float r2, float he )
{ |
vec2 k1 = vec2(r2,he);
vec2 k2 = vec2(r2-r1,2.0*he);
p.x = abs(p.x);
vec2 ca = vec2(max(0.0,p.x-((p.y<0.0)?r1:r2)), abs(p.y)-he);
vec2 cb = p - k1 + k2*clamp( dot(k1-p,k2)/dot2(k2), 0.0, 1.0 );
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(dot2(ca),dot2(cb)) );
} | // trapezoid / capped cone, specialized for Y alignment
float sdTrapezoid( in vec2 p, in float r1, float r2, float he )
{ | 3 | 28 |
MlycD3 | iq | 2018-09-26T08:53:33 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Distance to an isosceles trapezoid
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float dot2(in vec2 v ) { return dot(v,v); }
// trapezoid / capped cone, specialized for Y alignment
float sdTrapezoid( in vec2 p, in float r1, float r2, float he )
{
vec2 k1 = vec2(r2,he);
vec2 k2 = vec2(r2-r1,2.0*he);
p.x = abs(p.x);
vec2 ca = vec2(max(0.0,p.x-((p.y<0.0)?r1:r2)), abs(p.y)-he);
vec2 cb = p - k1 + k2*clamp( dot(k1-p,k2)/dot2(k2), 0.0, 1.0 );
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(dot2(ca),dot2(cb)) );
}
// trapezoid / capped cone
float sdTrapezoid( in vec2 p, in vec2 a, in vec2 b, in float ra, float rb )
{
float rba = rb-ra;
float baba = dot(b-a,b-a);
float papa = dot(p-a,p-a);
float paba = dot(p-a,b-a)/baba;
float x = sqrt( papa - paba*paba*baba );
float cax = max(0.0,x-((paba<0.5)?ra:rb));
float cay = abs(paba-0.5)-0.5;
float k = rba*rba + baba;
float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 );
float cbx = x-ra - f*rba;
float cby = paba - f;
float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(cax*cax + cay*cay*baba,
cbx*cbx + cby*cby*baba) );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
float ra = 0.2+0.15*sin(iTime*1.3+0.0);
float rb = 0.2+0.15*sin(iTime*1.4+1.1);
vec2 pa = vec2(-0.6,0.0)+0.4*sin(iTime*1.1+vec2(0.0,2.0));
vec2 pb = vec2(-0.6,0.0)+0.4*sin(iTime*1.2+vec2(1.0,2.5));
vec2 pc = vec2(0.8,0.0);
// axis aligned trapezoid
float d = sdTrapezoid( p-pc, ra, rb, 0.5+0.2*sin(1.3*iTime) );
// aribitrary trapezoid
d = min( d, sdTrapezoid( p, pa, pb , ra, rb ) );
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-4.0*abs(d));
col *= 0.8 + 0.2*cos(140.0*d);
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.005,abs(d)) );
if( iMouse.z>0.001 )
{
d = sdTrapezoid( m, pa, pb , ra, rb );
d = min( d, sdTrapezoid( m-pc, ra, rb, 0.5+0.2*sin(1.3*iTime) ) );
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025));
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015));
}
fragColor = vec4(col,1.0);
} | mit | [
1768,
1795,
1872,
1872,
2418
] | [
[
1276,
1276,
1300,
1300,
1319
],
[
1321,
1377,
1442,
1442,
1766
],
[
1768,
1795,
1872,
1872,
2418
],
[
2419,
2419,
2476,
2476,
3645
]
] | // trapezoid / capped cone
| float sdTrapezoid( in vec2 p, in vec2 a, in vec2 b, in float ra, float rb )
{ |
float rba = rb-ra;
float baba = dot(b-a,b-a);
float papa = dot(p-a,p-a);
float paba = dot(p-a,b-a)/baba;
float x = sqrt( papa - paba*paba*baba );
float cax = max(0.0,x-((paba<0.5)?ra:rb));
float cay = abs(paba-0.5)-0.5;
float k = rba*rba + baba;
float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 );
float cbx = x-ra - f*rba;
float cby = paba - f;
float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(cax*cax + cay*cay*baba,
cbx*cbx + cby*cby*baba) );
} | // trapezoid / capped cone
float sdTrapezoid( in vec2 p, in vec2 a, in vec2 b, in float ra, float rb )
{ | 3 | 3 |
lt3BW2 | iq | 2018-11-01T04:49:23 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Smooth vs sharp boolean operations for combining shapes
// Related techniques:
//
// Elongation : https://www.shadertoy.com/view/Ml3fWj
// Rounding : https://www.shadertoy.com/view/Mt3BDj
// Onion : https://www.shadertoy.com/view/MlcBDj
// Metric : https://www.shadertoy.com/view/ltcfDj
// Combination : https://www.shadertoy.com/view/lt3BW2
// Repetition : https://www.shadertoy.com/view/3syGzz
// Extrusion2D : https://www.shadertoy.com/view/4lyfzw
// Revolution2D: https://www.shadertoy.com/view/4lyfzw
//
// More information here: http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float opUnion( float d1, float d2 )
{
return min(d1,d2);
}
float opSubtraction( float d1, float d2 )
{
return max(-d1,d2);
}
float opIntersection( float d1, float d2 )
{
return max(d1,d2);
}
float opSmoothUnion( float d1, float d2, float k )
{
float h = max(k-abs(d1-d2),0.0);
return min(d1, d2) - h*h*0.25/k;
//float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
//return mix( d2, d1, h ) - k*h*(1.0-h);
}
float opSmoothSubtraction( float d1, float d2, float k )
{
float h = max(k-abs(-d1-d2),0.0);
return max(-d1, d2) + h*h*0.25/k;
//float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 );
//return mix( d2, -d1, h ) + k*h*(1.0-h);
}
float opSmoothIntersection( float d1, float d2, float k )
{
float h = max(k-abs(d1-d2),0.0);
return max(d1, d2) + h*h*0.25/k;
//float h = clamp( 0.5 - 0.5*(d2-d1)/k, 0.0, 1.0 );
//return mix( d2, d1, h ) + k*h*(1.0-h);
}
//-------------------------------------------------
float sdSphere( in vec3 p, in float r )
{
return length(p)-r;
}
float sdRoundBox( vec3 p, vec3 b, float r )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)) - r;
}
//---------------------------------
float map(in vec3 pos)
{
float d = 1e10;
float an = sin(iTime);
// opUnion
{
vec3 q = pos - vec3(-2.0,0.0,-1.3);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opUnion(d1,d2);
d = min( d, dt );
}
// opSmoothUnion
{
vec3 q = pos - vec3(-2.0,0.0,1.0);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opSmoothUnion(d1,d2, 0.25);
d = min( d, dt );
}
// opSubtraction
{
vec3 q = pos - vec3(0.0,0.0,-1.3);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opSubtraction(d1,d2);
d = min( d, dt );
}
// opSmoothSubtraction
{
vec3 q = pos - vec3(0.0,0.0,1.0);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opSmoothSubtraction(d1,d2, 0.25);
d = min( d, dt );
}
// opIntersection
{
vec3 q = pos - vec3(2.0,0.0,-1.3);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opIntersection(d1,d2);
d = min( d, dt );
}
// opSmoothIntersection
{
vec3 q = pos - vec3(2.0,0.0,1.0);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q-vec3(0.0,0.5,0.0), vec3(0.6,0.2,0.7), 0.1 );
float dt = opSmoothIntersection(d1,d2, 0.25);
d = min( d, dt );
}
return d;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
const float ep = 0.0001;
vec2 e = vec2(1.0,-1.0)*0.5773;
return normalize( e.xyy*map( pos + e.xyy*ep ) +
e.yyx*map( pos + e.yyx*ep ) +
e.yxy*map( pos + e.yxy*ep ) +
e.xxx*map( pos + e.xxx*ep ) );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, float tmin, float tmax, const float k )
{
float res = 1.0;
float t = tmin;
for( int i=0; i<50; i++ )
{
float h = map( ro + rd*t );
res = min( res, k*h/t );
t += clamp( h, 0.02, 0.20 );
if( res<0.005 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
#define AA 2
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
vec3 ro = vec3(0.0,4.0,8.0);
vec3 rd = normalize(vec3(p-vec2(0.0,1.8),-3.5));
float t = 7.0;
for( int i=0; i<64; i++ )
{
vec3 p = ro + t*rd;
float h = map(p);
if( abs(h)<0.001 || t>11.0 ) break;
t += h;
}
vec3 col = vec3(0.0);
if( t<11.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal(pos);
vec3 lig = normalize(vec3(1.0,0.8,-0.2));
float dif = clamp(dot(nor,lig),0.0,1.0);
float sha = calcSoftshadow( pos, lig, 0.001, 1.0, 16.0 );
float amb = 0.5 + 0.5*nor.y;
col = vec3(0.05,0.1,0.15)*amb +
vec3(1.00,0.9,0.80)*dif*sha;
}
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
4550,
4614,
4646,
4646,
4880
] | [
[
1702,
1702,
1739,
1739,
1764
],
[
1766,
1766,
1809,
1809,
1835
],
[
1837,
1837,
1881,
1881,
1906
],
[
1908,
1908,
1960,
1960,
2131
],
[
2133,
2133,
2191,
2191,
2365
],
[
2367,
2367,
2426,
2426,
2597
],
[
2652,
2652,
2693,
2693,
2719
],
[
2722,
2722,
2767,
2767,
2858
],
[
2897,
2897,
2921,
2921,
4548
],
[
4550,
4614,
4646,
4646,
4880
],
[
4882,
4944,
5031,
5031,
5289
]
] | // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
| vec3 calcNormal( in vec3 pos )
{ |
const float ep = 0.0001;
vec2 e = vec2(1.0,-1.0)*0.5773;
return normalize( e.xyy*map( pos + e.xyy*ep ) +
e.yyx*map( pos + e.yyx*ep ) +
e.yxy*map( pos + e.yxy*ep ) +
e.xxx*map( pos + e.xxx*ep ) );
} | // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{ | 6 | 360 |
lt3BW2 | iq | 2018-11-01T04:49:23 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Smooth vs sharp boolean operations for combining shapes
// Related techniques:
//
// Elongation : https://www.shadertoy.com/view/Ml3fWj
// Rounding : https://www.shadertoy.com/view/Mt3BDj
// Onion : https://www.shadertoy.com/view/MlcBDj
// Metric : https://www.shadertoy.com/view/ltcfDj
// Combination : https://www.shadertoy.com/view/lt3BW2
// Repetition : https://www.shadertoy.com/view/3syGzz
// Extrusion2D : https://www.shadertoy.com/view/4lyfzw
// Revolution2D: https://www.shadertoy.com/view/4lyfzw
//
// More information here: http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float opUnion( float d1, float d2 )
{
return min(d1,d2);
}
float opSubtraction( float d1, float d2 )
{
return max(-d1,d2);
}
float opIntersection( float d1, float d2 )
{
return max(d1,d2);
}
float opSmoothUnion( float d1, float d2, float k )
{
float h = max(k-abs(d1-d2),0.0);
return min(d1, d2) - h*h*0.25/k;
//float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
//return mix( d2, d1, h ) - k*h*(1.0-h);
}
float opSmoothSubtraction( float d1, float d2, float k )
{
float h = max(k-abs(-d1-d2),0.0);
return max(-d1, d2) + h*h*0.25/k;
//float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 );
//return mix( d2, -d1, h ) + k*h*(1.0-h);
}
float opSmoothIntersection( float d1, float d2, float k )
{
float h = max(k-abs(d1-d2),0.0);
return max(d1, d2) + h*h*0.25/k;
//float h = clamp( 0.5 - 0.5*(d2-d1)/k, 0.0, 1.0 );
//return mix( d2, d1, h ) + k*h*(1.0-h);
}
//-------------------------------------------------
float sdSphere( in vec3 p, in float r )
{
return length(p)-r;
}
float sdRoundBox( vec3 p, vec3 b, float r )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)) - r;
}
//---------------------------------
float map(in vec3 pos)
{
float d = 1e10;
float an = sin(iTime);
// opUnion
{
vec3 q = pos - vec3(-2.0,0.0,-1.3);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opUnion(d1,d2);
d = min( d, dt );
}
// opSmoothUnion
{
vec3 q = pos - vec3(-2.0,0.0,1.0);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opSmoothUnion(d1,d2, 0.25);
d = min( d, dt );
}
// opSubtraction
{
vec3 q = pos - vec3(0.0,0.0,-1.3);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opSubtraction(d1,d2);
d = min( d, dt );
}
// opSmoothSubtraction
{
vec3 q = pos - vec3(0.0,0.0,1.0);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opSmoothSubtraction(d1,d2, 0.25);
d = min( d, dt );
}
// opIntersection
{
vec3 q = pos - vec3(2.0,0.0,-1.3);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q, vec3(0.6,0.2,0.7), 0.1 );
float dt = opIntersection(d1,d2);
d = min( d, dt );
}
// opSmoothIntersection
{
vec3 q = pos - vec3(2.0,0.0,1.0);
float d1 = sdSphere( q-vec3(0.0,0.5+0.3*an,0.0), 0.55 );
float d2 = sdRoundBox(q-vec3(0.0,0.5,0.0), vec3(0.6,0.2,0.7), 0.1 );
float dt = opSmoothIntersection(d1,d2, 0.25);
d = min( d, dt );
}
return d;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
const float ep = 0.0001;
vec2 e = vec2(1.0,-1.0)*0.5773;
return normalize( e.xyy*map( pos + e.xyy*ep ) +
e.yyx*map( pos + e.yyx*ep ) +
e.yxy*map( pos + e.yxy*ep ) +
e.xxx*map( pos + e.xxx*ep ) );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, float tmin, float tmax, const float k )
{
float res = 1.0;
float t = tmin;
for( int i=0; i<50; i++ )
{
float h = map( ro + rd*t );
res = min( res, k*h/t );
t += clamp( h, 0.02, 0.20 );
if( res<0.005 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
#define AA 2
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
vec3 ro = vec3(0.0,4.0,8.0);
vec3 rd = normalize(vec3(p-vec2(0.0,1.8),-3.5));
float t = 7.0;
for( int i=0; i<64; i++ )
{
vec3 p = ro + t*rd;
float h = map(p);
if( abs(h)<0.001 || t>11.0 ) break;
t += h;
}
vec3 col = vec3(0.0);
if( t<11.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal(pos);
vec3 lig = normalize(vec3(1.0,0.8,-0.2));
float dif = clamp(dot(nor,lig),0.0,1.0);
float sha = calcSoftshadow( pos, lig, 0.001, 1.0, 16.0 );
float amb = 0.5 + 0.5*nor.y;
col = vec3(0.05,0.1,0.15)*amb +
vec3(1.00,0.9,0.80)*dif*sha;
}
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
4882,
4944,
5031,
5031,
5289
] | [
[
1702,
1702,
1739,
1739,
1764
],
[
1766,
1766,
1809,
1809,
1835
],
[
1837,
1837,
1881,
1881,
1906
],
[
1908,
1908,
1960,
1960,
2131
],
[
2133,
2133,
2191,
2191,
2365
],
[
2367,
2367,
2426,
2426,
2597
],
[
2652,
2652,
2693,
2693,
2719
],
[
2722,
2722,
2767,
2767,
2858
],
[
2897,
2897,
2921,
2921,
4548
],
[
4550,
4614,
4646,
4646,
4880
],
[
4882,
4944,
5031,
5031,
5289
]
] | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
| float calcSoftshadow( in vec3 ro, in vec3 rd, float tmin, float tmax, const float k )
{ |
float res = 1.0;
float t = tmin;
for( int i=0; i<50; i++ )
{
float h = map( ro + rd*t );
res = min( res, k*h/t );
t += clamp( h, 0.02, 0.20 );
if( res<0.005 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
} | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, float tmin, float tmax, const float k )
{ | 4 | 10 |
WslGz4 | iq | 2018-12-19T08:01:23 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Analytic fake soft shadow for boxes. With a single ray, we can compute a soft shadow.
// It is "plausible", but not accurate. It can probably be optimized further, right now
// I am testing the 12 edges, but generaly you only need to test between 1 and 6.
// Other Box functions:
//
// Intersection: https://www.shadertoy.com/view/ld23DV
// Occlusion: https://www.shadertoy.com/view/4sSXDV
// Occlusion: https://www.shadertoy.com/view/4djXDy
// Density: https://www.shadertoy.com/view/Ml3GR8
// Fake soft shadow: https://www.shadertoy.com/view/WslGz4
// Other Soft Shadow functions:
//
// Sphere: https://www.shadertoy.com/view/4d2XWV
// Ellipsoid: https://www.shadertoy.com/view/llsSzn
// Box: https://www.shadertoy.com/view/WslGz4
// Capsule: https://www.shadertoy.com/view/MlGczG
#define SHADOW_TYPE 2
// SHADOW_TYPE = 2 ---> great soft shadow
// SHADOW_TYPE = 1 ---> super cheap soft shadow
// SHADOW_TYPE = 0 ---> regular hard shadows
#if SHADOW_TYPE==2
float dot2( in vec3 v ) { return dot(v,v); }
float segShadow( in vec3 ro, in vec3 rd, in vec3 pa, float sh )
{
float dm = dot(rd.yz,rd.yz); // dm = 1.0 - rd.x*rd.x
float k1 = (ro.x-pa.x)*dm;
float k2 = (ro.x+pa.x)*dm;
vec2 k5 = (ro.yz+pa.yz)*dm;
float k3 = dot(ro.yz+pa.yz,rd.yz);
vec2 k4 = (pa.yz+pa.yz)*rd.yz;
vec2 k6 = (pa.yz+pa.yz)*dm;
for( int i=0; i<4; i++ )
{
vec2 s = vec2(i&1,i>>1);
float t = dot(s,k4) - k3;
if( t>0.0 )
sh = min(sh,dot2(vec3(clamp(-rd.x*t,k1,k2),k5-k6*s)+rd*t)/(t*t));
}
return sh;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN<tF && tF>0.0) return 0.0;
float sh = 1.0;
sh = segShadow( roo.xyz, rdd.xyz, rad.xyz, sh );
sh = segShadow( roo.yzx, rdd.yzx, rad.yzx, sh );
sh = segShadow( roo.zxy, rdd.zxy, rad.zxy, sh );
sh = clamp(sk*sqrt(sh),0.0,1.0);
return sh*sh*(3.0-2.0*sh);
}
#endif
#if SHADOW_TYPE==1
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
// fake soft shadow
if( tF<0.0) return 1.0;
float sh = clamp(0.3*sk*(tN-tF)/tN,0.0,1.0);
return sh*sh*(3.0-2.0*sh);
}
#endif
#if SHADOW_TYPE==0
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN>tF || tF<0.0) return 1.0;
return 0.0;
}
#endif
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
bool boxIntersect( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad,
out vec2 outT, out vec3 outNor, out vec2 outST, out int outFaceID )
{
// convert from ray to box space
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 s = vec3((rdd.x<0.0)?1.0:-1.0,
(rdd.y<0.0)?1.0:-1.0,
(rdd.z<0.0)?1.0:-1.0);
// ray-box intersection in box space
vec3 m = 1.0/rdd;
vec3 t1 = m*(-roo + s*rad);
vec3 t2 = m*(-roo - s*rad);
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return false;
// compute normal (in world space), face and UV
if( t1.x>t1.y && t1.x>t1.z ) { outNor = txi[0].xyz*s.x; outST = roo.yz+rdd.yz*t1.x; outFaceID=(1+int(s.x))/2; /* 0, 1 */ }
else if( t1.y>t1.z ) { outNor = txi[1].xyz*s.y; outST = roo.zx+rdd.zx*t1.y; outFaceID=(5+int(s.y))/2; /* 2, 3 */ }
else { outNor = txi[2].xyz*s.z; outST = roo.xy+rdd.xy*t1.z; outFaceID=(9+int(s.z))/2; /* 4, 5 */ }
outT = vec2(tN,tF);
return true;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxOcclusion( in vec3 pos, in vec3 nor, in mat4 txx, in mat4 txi, in vec3 rad )
{
vec3 p = (txx*vec4(pos,1.0)).xyz;
vec3 n = (txx*vec4(nor,0.0)).xyz;
// 8 verts
vec3 v0 = normalize( vec3(-1.0,-1.0,-1.0)*rad - p);
vec3 v1 = normalize( vec3( 1.0,-1.0,-1.0)*rad - p);
vec3 v2 = normalize( vec3(-1.0, 1.0,-1.0)*rad - p);
vec3 v3 = normalize( vec3( 1.0, 1.0,-1.0)*rad - p);
vec3 v4 = normalize( vec3(-1.0,-1.0, 1.0)*rad - p);
vec3 v5 = normalize( vec3( 1.0,-1.0, 1.0)*rad - p);
vec3 v6 = normalize( vec3(-1.0, 1.0, 1.0)*rad - p);
vec3 v7 = normalize( vec3( 1.0, 1.0, 1.0)*rad - p);
// 12 edges
float k02 = dot( n, normalize( cross(v2,v0)) ) * acos( clamp(dot(v0,v2),-1.0,1.0) );
float k23 = dot( n, normalize( cross(v3,v2)) ) * acos( clamp(dot(v2,v3),-1.0,1.0) );
float k31 = dot( n, normalize( cross(v1,v3)) ) * acos( clamp(dot(v3,v1),-1.0,1.0) );
float k10 = dot( n, normalize( cross(v0,v1)) ) * acos( clamp(dot(v1,v0),-1.0,1.0) );
float k45 = dot( n, normalize( cross(v5,v4)) ) * acos( clamp(dot(v4,v5),-1.0,1.0) );
float k57 = dot( n, normalize( cross(v7,v5)) ) * acos( clamp(dot(v5,v7),-1.0,1.0) );
float k76 = dot( n, normalize( cross(v6,v7)) ) * acos( clamp(dot(v7,v6),-1.0,1.0) );
float k37 = dot( n, normalize( cross(v7,v3)) ) * acos( clamp(dot(v3,v7),-1.0,1.0) );
float k64 = dot( n, normalize( cross(v4,v6)) ) * acos( clamp(dot(v6,v4),-1.0,1.0) );
float k51 = dot( n, normalize( cross(v1,v5)) ) * acos( clamp(dot(v5,v1),-1.0,1.0) );
float k04 = dot( n, normalize( cross(v4,v0)) ) * acos( clamp(dot(v0,v4),-1.0,1.0) );
float k62 = dot( n, normalize( cross(v2,v6)) ) * acos( clamp(dot(v6,v2),-1.0,1.0) );
// 6 faces
float occ = 0.0;
occ += ( k02 + k23 + k31 + k10) * step( 0.0, v0.z );
occ += ( k45 + k57 + k76 + k64) * step( 0.0, -v4.z );
occ += ( k51 - k31 + k37 - k57) * step( 0.0, -v5.x );
occ += ( k04 - k64 + k62 - k02) * step( 0.0, v0.x );
occ += (-k76 - k37 - k23 - k62) * step( 0.0, -v6.y );
occ += (-k10 - k51 - k45 - k04) * step( 0.0, v0.y );
return occ / 6.2831;
}
//-----------------------------------------------------------------------------------------
mat4 rotationAxisAngle( vec3 v, float angle )
{
float s = sin( angle );
float c = cos( angle );
float ic = 1.0 - c;
return mat4( v.x*v.x*ic + c, v.y*v.x*ic - s*v.z, v.z*v.x*ic + s*v.y, 0.0,
v.x*v.y*ic + s*v.z, v.y*v.y*ic + c, v.z*v.y*ic - s*v.x, 0.0,
v.x*v.z*ic - s*v.y, v.y*v.z*ic + s*v.x, v.z*v.z*ic + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
}
mat4 translate( float x, float y, float z )
{
return mat4( 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0 );
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
#define AA 2
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.4*iTime;
vec3 ro = vec3( 2.5*cos(an), 1.0, 2.5*sin(an) );
vec3 ta = vec3( 0.0, 0.8, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = ( cross(uu,ww));
// animate box
mat4 rot = rotationAxisAngle( normalize(vec3(1.0,1.0,0.0)), iTime );
mat4 tra = translate( 0.0, 1.0, 0.0 );
mat4 txi = tra * rot;
mat4 txx = inverse( txi );
vec3 box = vec3(0.4,0.6,0.8);
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
// raytrace
float tmin = 10000.0;
vec3 nor = vec3(0.0);
vec3 pos = vec3(0.0);
vec2 uv = vec2(0.0);
// raytrace-plane
float oid = 0.0;
float h = (0.0-ro.y)/rd.y;
if( h>0.0 )
{
tmin = h;
nor = vec3(0.0,1.0,0.0);
oid = 1.0;
pos = ro + rd*h;
uv = pos.xz;
}
// raytrace box
vec3 bnor; vec2 buv;
int outFaceID;
vec2 tnf;
if( boxIntersect( ro, rd, txx, txi, box, tnf, bnor, buv, outFaceID) )
{
if( tnf.x>0.0 && tnf.x<tmin )
{
tmin = tnf.x;
nor = bnor;
uv = buv;
oid = 2.0;
}
}
// shading/lighting
vec3 col = vec3(0.9);
if( tmin<100.0 )
{
vec3 lig = normalize(vec3(0.3,0.5,0.8));
pos = ro + tmin*rd;
// material
float occ = 1.0;
vec3 mate = vec3(1.0);
if( oid<1.5 ) // plane
{
mate = vec3(0.6)*(0.8+0.2*checkersGradBox( 2.0*uv ));
// analytic ambient occlusion
occ = 1.0-boxOcclusion( pos, nor, txx, txi, box );
}
else // box
{
mate = vec3(0.5)*(0.8+0.2*checkersGradBox( 5.0*uv ));
// fake ambient occlusion
occ = 0.5 + 0.5*nor.y;
occ *= 0.5 + 0.5*clamp(pos.y,0.0,1.0);
}
// lighting
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
dif *= boxSoftShadow( pos+0.01*nor, lig, txx, box, 4.0 );
col = vec3(0.1,0.2,0.3)*occ + 2.0*dif*vec3(1.0,0.8,0.7);
// material * lighting
col *= mate;
// fog
col = mix( col, vec3(0.9), 1.0-exp( -0.003*tmin*tmin ) );
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
4547,
4615,
4784,
4821,
5767
] | [
[
4547,
4615,
4784,
4821,
5767
],
[
5770,
5838,
5926,
5926,
7986
],
[
8081,
8081,
8128,
8128,
8532
],
[
8534,
8534,
8579,
8579,
8695
],
[
8697,
8773,
8809,
8830,
9053
]
] | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
| bool boxIntersect( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad,
out vec2 outT, out vec3 outNor, out vec2 outST, out int outFaceID )
{ |
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 s = vec3((rdd.x<0.0)?1.0:-1.0,
(rdd.y<0.0)?1.0:-1.0,
(rdd.z<0.0)?1.0:-1.0);
// ray-box intersection in box space
vec3 m = 1.0/rdd;
vec3 t1 = m*(-roo + s*rad);
vec3 t2 = m*(-roo - s*rad);
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return false;
// compute normal (in world space), face and UV
if( t1.x>t1.y && t1.x>t1.z ) { outNor = txi[0].xyz*s.x; outST = roo.yz+rdd.yz*t1.x; outFaceID=(1+int(s.x))/2; /* 0, 1 */ }
else if( t1.y>t1.z ) { outNor = txi[1].xyz*s.y; outST = roo.zx+rdd.zx*t1.y; outFaceID=(5+int(s.y))/2; /* 2, 3 */ }
else { outNor = txi[2].xyz*s.z; outST = roo.xy+rdd.xy*t1.z; outFaceID=(9+int(s.z))/2; /* 4, 5 */ }
outT = vec2(tN,tF);
return true;
} | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
bool boxIntersect( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad,
out vec2 outT, out vec3 outNor, out vec2 outST, out int outFaceID )
{ | 1 | 1 |
WslGz4 | iq | 2018-12-19T08:01:23 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Analytic fake soft shadow for boxes. With a single ray, we can compute a soft shadow.
// It is "plausible", but not accurate. It can probably be optimized further, right now
// I am testing the 12 edges, but generaly you only need to test between 1 and 6.
// Other Box functions:
//
// Intersection: https://www.shadertoy.com/view/ld23DV
// Occlusion: https://www.shadertoy.com/view/4sSXDV
// Occlusion: https://www.shadertoy.com/view/4djXDy
// Density: https://www.shadertoy.com/view/Ml3GR8
// Fake soft shadow: https://www.shadertoy.com/view/WslGz4
// Other Soft Shadow functions:
//
// Sphere: https://www.shadertoy.com/view/4d2XWV
// Ellipsoid: https://www.shadertoy.com/view/llsSzn
// Box: https://www.shadertoy.com/view/WslGz4
// Capsule: https://www.shadertoy.com/view/MlGczG
#define SHADOW_TYPE 2
// SHADOW_TYPE = 2 ---> great soft shadow
// SHADOW_TYPE = 1 ---> super cheap soft shadow
// SHADOW_TYPE = 0 ---> regular hard shadows
#if SHADOW_TYPE==2
float dot2( in vec3 v ) { return dot(v,v); }
float segShadow( in vec3 ro, in vec3 rd, in vec3 pa, float sh )
{
float dm = dot(rd.yz,rd.yz); // dm = 1.0 - rd.x*rd.x
float k1 = (ro.x-pa.x)*dm;
float k2 = (ro.x+pa.x)*dm;
vec2 k5 = (ro.yz+pa.yz)*dm;
float k3 = dot(ro.yz+pa.yz,rd.yz);
vec2 k4 = (pa.yz+pa.yz)*rd.yz;
vec2 k6 = (pa.yz+pa.yz)*dm;
for( int i=0; i<4; i++ )
{
vec2 s = vec2(i&1,i>>1);
float t = dot(s,k4) - k3;
if( t>0.0 )
sh = min(sh,dot2(vec3(clamp(-rd.x*t,k1,k2),k5-k6*s)+rd*t)/(t*t));
}
return sh;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN<tF && tF>0.0) return 0.0;
float sh = 1.0;
sh = segShadow( roo.xyz, rdd.xyz, rad.xyz, sh );
sh = segShadow( roo.yzx, rdd.yzx, rad.yzx, sh );
sh = segShadow( roo.zxy, rdd.zxy, rad.zxy, sh );
sh = clamp(sk*sqrt(sh),0.0,1.0);
return sh*sh*(3.0-2.0*sh);
}
#endif
#if SHADOW_TYPE==1
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
// fake soft shadow
if( tF<0.0) return 1.0;
float sh = clamp(0.3*sk*(tN-tF)/tN,0.0,1.0);
return sh*sh*(3.0-2.0*sh);
}
#endif
#if SHADOW_TYPE==0
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN>tF || tF<0.0) return 1.0;
return 0.0;
}
#endif
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
bool boxIntersect( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad,
out vec2 outT, out vec3 outNor, out vec2 outST, out int outFaceID )
{
// convert from ray to box space
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 s = vec3((rdd.x<0.0)?1.0:-1.0,
(rdd.y<0.0)?1.0:-1.0,
(rdd.z<0.0)?1.0:-1.0);
// ray-box intersection in box space
vec3 m = 1.0/rdd;
vec3 t1 = m*(-roo + s*rad);
vec3 t2 = m*(-roo - s*rad);
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return false;
// compute normal (in world space), face and UV
if( t1.x>t1.y && t1.x>t1.z ) { outNor = txi[0].xyz*s.x; outST = roo.yz+rdd.yz*t1.x; outFaceID=(1+int(s.x))/2; /* 0, 1 */ }
else if( t1.y>t1.z ) { outNor = txi[1].xyz*s.y; outST = roo.zx+rdd.zx*t1.y; outFaceID=(5+int(s.y))/2; /* 2, 3 */ }
else { outNor = txi[2].xyz*s.z; outST = roo.xy+rdd.xy*t1.z; outFaceID=(9+int(s.z))/2; /* 4, 5 */ }
outT = vec2(tN,tF);
return true;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxOcclusion( in vec3 pos, in vec3 nor, in mat4 txx, in mat4 txi, in vec3 rad )
{
vec3 p = (txx*vec4(pos,1.0)).xyz;
vec3 n = (txx*vec4(nor,0.0)).xyz;
// 8 verts
vec3 v0 = normalize( vec3(-1.0,-1.0,-1.0)*rad - p);
vec3 v1 = normalize( vec3( 1.0,-1.0,-1.0)*rad - p);
vec3 v2 = normalize( vec3(-1.0, 1.0,-1.0)*rad - p);
vec3 v3 = normalize( vec3( 1.0, 1.0,-1.0)*rad - p);
vec3 v4 = normalize( vec3(-1.0,-1.0, 1.0)*rad - p);
vec3 v5 = normalize( vec3( 1.0,-1.0, 1.0)*rad - p);
vec3 v6 = normalize( vec3(-1.0, 1.0, 1.0)*rad - p);
vec3 v7 = normalize( vec3( 1.0, 1.0, 1.0)*rad - p);
// 12 edges
float k02 = dot( n, normalize( cross(v2,v0)) ) * acos( clamp(dot(v0,v2),-1.0,1.0) );
float k23 = dot( n, normalize( cross(v3,v2)) ) * acos( clamp(dot(v2,v3),-1.0,1.0) );
float k31 = dot( n, normalize( cross(v1,v3)) ) * acos( clamp(dot(v3,v1),-1.0,1.0) );
float k10 = dot( n, normalize( cross(v0,v1)) ) * acos( clamp(dot(v1,v0),-1.0,1.0) );
float k45 = dot( n, normalize( cross(v5,v4)) ) * acos( clamp(dot(v4,v5),-1.0,1.0) );
float k57 = dot( n, normalize( cross(v7,v5)) ) * acos( clamp(dot(v5,v7),-1.0,1.0) );
float k76 = dot( n, normalize( cross(v6,v7)) ) * acos( clamp(dot(v7,v6),-1.0,1.0) );
float k37 = dot( n, normalize( cross(v7,v3)) ) * acos( clamp(dot(v3,v7),-1.0,1.0) );
float k64 = dot( n, normalize( cross(v4,v6)) ) * acos( clamp(dot(v6,v4),-1.0,1.0) );
float k51 = dot( n, normalize( cross(v1,v5)) ) * acos( clamp(dot(v5,v1),-1.0,1.0) );
float k04 = dot( n, normalize( cross(v4,v0)) ) * acos( clamp(dot(v0,v4),-1.0,1.0) );
float k62 = dot( n, normalize( cross(v2,v6)) ) * acos( clamp(dot(v6,v2),-1.0,1.0) );
// 6 faces
float occ = 0.0;
occ += ( k02 + k23 + k31 + k10) * step( 0.0, v0.z );
occ += ( k45 + k57 + k76 + k64) * step( 0.0, -v4.z );
occ += ( k51 - k31 + k37 - k57) * step( 0.0, -v5.x );
occ += ( k04 - k64 + k62 - k02) * step( 0.0, v0.x );
occ += (-k76 - k37 - k23 - k62) * step( 0.0, -v6.y );
occ += (-k10 - k51 - k45 - k04) * step( 0.0, v0.y );
return occ / 6.2831;
}
//-----------------------------------------------------------------------------------------
mat4 rotationAxisAngle( vec3 v, float angle )
{
float s = sin( angle );
float c = cos( angle );
float ic = 1.0 - c;
return mat4( v.x*v.x*ic + c, v.y*v.x*ic - s*v.z, v.z*v.x*ic + s*v.y, 0.0,
v.x*v.y*ic + s*v.z, v.y*v.y*ic + c, v.z*v.y*ic - s*v.x, 0.0,
v.x*v.z*ic - s*v.y, v.y*v.z*ic + s*v.x, v.z*v.z*ic + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
}
mat4 translate( float x, float y, float z )
{
return mat4( 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0 );
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
#define AA 2
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.4*iTime;
vec3 ro = vec3( 2.5*cos(an), 1.0, 2.5*sin(an) );
vec3 ta = vec3( 0.0, 0.8, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = ( cross(uu,ww));
// animate box
mat4 rot = rotationAxisAngle( normalize(vec3(1.0,1.0,0.0)), iTime );
mat4 tra = translate( 0.0, 1.0, 0.0 );
mat4 txi = tra * rot;
mat4 txx = inverse( txi );
vec3 box = vec3(0.4,0.6,0.8);
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
// raytrace
float tmin = 10000.0;
vec3 nor = vec3(0.0);
vec3 pos = vec3(0.0);
vec2 uv = vec2(0.0);
// raytrace-plane
float oid = 0.0;
float h = (0.0-ro.y)/rd.y;
if( h>0.0 )
{
tmin = h;
nor = vec3(0.0,1.0,0.0);
oid = 1.0;
pos = ro + rd*h;
uv = pos.xz;
}
// raytrace box
vec3 bnor; vec2 buv;
int outFaceID;
vec2 tnf;
if( boxIntersect( ro, rd, txx, txi, box, tnf, bnor, buv, outFaceID) )
{
if( tnf.x>0.0 && tnf.x<tmin )
{
tmin = tnf.x;
nor = bnor;
uv = buv;
oid = 2.0;
}
}
// shading/lighting
vec3 col = vec3(0.9);
if( tmin<100.0 )
{
vec3 lig = normalize(vec3(0.3,0.5,0.8));
pos = ro + tmin*rd;
// material
float occ = 1.0;
vec3 mate = vec3(1.0);
if( oid<1.5 ) // plane
{
mate = vec3(0.6)*(0.8+0.2*checkersGradBox( 2.0*uv ));
// analytic ambient occlusion
occ = 1.0-boxOcclusion( pos, nor, txx, txi, box );
}
else // box
{
mate = vec3(0.5)*(0.8+0.2*checkersGradBox( 5.0*uv ));
// fake ambient occlusion
occ = 0.5 + 0.5*nor.y;
occ *= 0.5 + 0.5*clamp(pos.y,0.0,1.0);
}
// lighting
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
dif *= boxSoftShadow( pos+0.01*nor, lig, txx, box, 4.0 );
col = vec3(0.1,0.2,0.3)*occ + 2.0*dif*vec3(1.0,0.8,0.7);
// material * lighting
col *= mate;
// fog
col = mix( col, vec3(0.9), 1.0-exp( -0.003*tmin*tmin ) );
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
5770,
5838,
5926,
5926,
7986
] | [
[
4547,
4615,
4784,
4821,
5767
],
[
5770,
5838,
5926,
5926,
7986
],
[
8081,
8081,
8128,
8128,
8532
],
[
8534,
8534,
8579,
8579,
8695
],
[
8697,
8773,
8809,
8830,
9053
]
] | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
| float boxOcclusion( in vec3 pos, in vec3 nor, in mat4 txx, in mat4 txi, in vec3 rad )
{ |
vec3 p = (txx*vec4(pos,1.0)).xyz;
vec3 n = (txx*vec4(nor,0.0)).xyz;
// 8 verts
vec3 v0 = normalize( vec3(-1.0,-1.0,-1.0)*rad - p);
vec3 v1 = normalize( vec3( 1.0,-1.0,-1.0)*rad - p);
vec3 v2 = normalize( vec3(-1.0, 1.0,-1.0)*rad - p);
vec3 v3 = normalize( vec3( 1.0, 1.0,-1.0)*rad - p);
vec3 v4 = normalize( vec3(-1.0,-1.0, 1.0)*rad - p);
vec3 v5 = normalize( vec3( 1.0,-1.0, 1.0)*rad - p);
vec3 v6 = normalize( vec3(-1.0, 1.0, 1.0)*rad - p);
vec3 v7 = normalize( vec3( 1.0, 1.0, 1.0)*rad - p);
// 12 edges
float k02 = dot( n, normalize( cross(v2,v0)) ) * acos( clamp(dot(v0,v2),-1.0,1.0) );
float k23 = dot( n, normalize( cross(v3,v2)) ) * acos( clamp(dot(v2,v3),-1.0,1.0) );
float k31 = dot( n, normalize( cross(v1,v3)) ) * acos( clamp(dot(v3,v1),-1.0,1.0) );
float k10 = dot( n, normalize( cross(v0,v1)) ) * acos( clamp(dot(v1,v0),-1.0,1.0) );
float k45 = dot( n, normalize( cross(v5,v4)) ) * acos( clamp(dot(v4,v5),-1.0,1.0) );
float k57 = dot( n, normalize( cross(v7,v5)) ) * acos( clamp(dot(v5,v7),-1.0,1.0) );
float k76 = dot( n, normalize( cross(v6,v7)) ) * acos( clamp(dot(v7,v6),-1.0,1.0) );
float k37 = dot( n, normalize( cross(v7,v3)) ) * acos( clamp(dot(v3,v7),-1.0,1.0) );
float k64 = dot( n, normalize( cross(v4,v6)) ) * acos( clamp(dot(v6,v4),-1.0,1.0) );
float k51 = dot( n, normalize( cross(v1,v5)) ) * acos( clamp(dot(v5,v1),-1.0,1.0) );
float k04 = dot( n, normalize( cross(v4,v0)) ) * acos( clamp(dot(v0,v4),-1.0,1.0) );
float k62 = dot( n, normalize( cross(v2,v6)) ) * acos( clamp(dot(v6,v2),-1.0,1.0) );
// 6 faces
float occ = 0.0;
occ += ( k02 + k23 + k31 + k10) * step( 0.0, v0.z );
occ += ( k45 + k57 + k76 + k64) * step( 0.0, -v4.z );
occ += ( k51 - k31 + k37 - k57) * step( 0.0, -v5.x );
occ += ( k04 - k64 + k62 - k02) * step( 0.0, v0.x );
occ += (-k76 - k37 - k23 - k62) * step( 0.0, -v6.y );
occ += (-k10 - k51 - k45 - k04) * step( 0.0, v0.y );
return occ / 6.2831;
} | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxOcclusion( in vec3 pos, in vec3 nor, in mat4 txx, in mat4 txi, in vec3 rad )
{ | 1 | 4 |
WslGz4 | iq | 2018-12-19T08:01:23 | // The MIT License
// Copyright © 2018 Inigo Quilez
// 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.
// Analytic fake soft shadow for boxes. With a single ray, we can compute a soft shadow.
// It is "plausible", but not accurate. It can probably be optimized further, right now
// I am testing the 12 edges, but generaly you only need to test between 1 and 6.
// Other Box functions:
//
// Intersection: https://www.shadertoy.com/view/ld23DV
// Occlusion: https://www.shadertoy.com/view/4sSXDV
// Occlusion: https://www.shadertoy.com/view/4djXDy
// Density: https://www.shadertoy.com/view/Ml3GR8
// Fake soft shadow: https://www.shadertoy.com/view/WslGz4
// Other Soft Shadow functions:
//
// Sphere: https://www.shadertoy.com/view/4d2XWV
// Ellipsoid: https://www.shadertoy.com/view/llsSzn
// Box: https://www.shadertoy.com/view/WslGz4
// Capsule: https://www.shadertoy.com/view/MlGczG
#define SHADOW_TYPE 2
// SHADOW_TYPE = 2 ---> great soft shadow
// SHADOW_TYPE = 1 ---> super cheap soft shadow
// SHADOW_TYPE = 0 ---> regular hard shadows
#if SHADOW_TYPE==2
float dot2( in vec3 v ) { return dot(v,v); }
float segShadow( in vec3 ro, in vec3 rd, in vec3 pa, float sh )
{
float dm = dot(rd.yz,rd.yz); // dm = 1.0 - rd.x*rd.x
float k1 = (ro.x-pa.x)*dm;
float k2 = (ro.x+pa.x)*dm;
vec2 k5 = (ro.yz+pa.yz)*dm;
float k3 = dot(ro.yz+pa.yz,rd.yz);
vec2 k4 = (pa.yz+pa.yz)*rd.yz;
vec2 k6 = (pa.yz+pa.yz)*dm;
for( int i=0; i<4; i++ )
{
vec2 s = vec2(i&1,i>>1);
float t = dot(s,k4) - k3;
if( t>0.0 )
sh = min(sh,dot2(vec3(clamp(-rd.x*t,k1,k2),k5-k6*s)+rd*t)/(t*t));
}
return sh;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN<tF && tF>0.0) return 0.0;
float sh = 1.0;
sh = segShadow( roo.xyz, rdd.xyz, rad.xyz, sh );
sh = segShadow( roo.yzx, rdd.yzx, rad.yzx, sh );
sh = segShadow( roo.zxy, rdd.zxy, rad.zxy, sh );
sh = clamp(sk*sqrt(sh),0.0,1.0);
return sh*sh*(3.0-2.0*sh);
}
#endif
#if SHADOW_TYPE==1
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
// fake soft shadow
if( tF<0.0) return 1.0;
float sh = clamp(0.3*sk*(tN-tF)/tN,0.0,1.0);
return sh*sh*(3.0-2.0*sh);
}
#endif
#if SHADOW_TYPE==0
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxSoftShadow( in vec3 ro, in vec3 rd, in mat4 txx, in vec3 rad, in float sk )
{
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN>tF || tF<0.0) return 1.0;
return 0.0;
}
#endif
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
bool boxIntersect( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad,
out vec2 outT, out vec3 outNor, out vec2 outST, out int outFaceID )
{
// convert from ray to box space
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
vec3 s = vec3((rdd.x<0.0)?1.0:-1.0,
(rdd.y<0.0)?1.0:-1.0,
(rdd.z<0.0)?1.0:-1.0);
// ray-box intersection in box space
vec3 m = 1.0/rdd;
vec3 t1 = m*(-roo + s*rad);
vec3 t2 = m*(-roo - s*rad);
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return false;
// compute normal (in world space), face and UV
if( t1.x>t1.y && t1.x>t1.z ) { outNor = txi[0].xyz*s.x; outST = roo.yz+rdd.yz*t1.x; outFaceID=(1+int(s.x))/2; /* 0, 1 */ }
else if( t1.y>t1.z ) { outNor = txi[1].xyz*s.y; outST = roo.zx+rdd.zx*t1.y; outFaceID=(5+int(s.y))/2; /* 2, 3 */ }
else { outNor = txi[2].xyz*s.z; outST = roo.xy+rdd.xy*t1.z; outFaceID=(9+int(s.z))/2; /* 4, 5 */ }
outT = vec2(tN,tF);
return true;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxOcclusion( in vec3 pos, in vec3 nor, in mat4 txx, in mat4 txi, in vec3 rad )
{
vec3 p = (txx*vec4(pos,1.0)).xyz;
vec3 n = (txx*vec4(nor,0.0)).xyz;
// 8 verts
vec3 v0 = normalize( vec3(-1.0,-1.0,-1.0)*rad - p);
vec3 v1 = normalize( vec3( 1.0,-1.0,-1.0)*rad - p);
vec3 v2 = normalize( vec3(-1.0, 1.0,-1.0)*rad - p);
vec3 v3 = normalize( vec3( 1.0, 1.0,-1.0)*rad - p);
vec3 v4 = normalize( vec3(-1.0,-1.0, 1.0)*rad - p);
vec3 v5 = normalize( vec3( 1.0,-1.0, 1.0)*rad - p);
vec3 v6 = normalize( vec3(-1.0, 1.0, 1.0)*rad - p);
vec3 v7 = normalize( vec3( 1.0, 1.0, 1.0)*rad - p);
// 12 edges
float k02 = dot( n, normalize( cross(v2,v0)) ) * acos( clamp(dot(v0,v2),-1.0,1.0) );
float k23 = dot( n, normalize( cross(v3,v2)) ) * acos( clamp(dot(v2,v3),-1.0,1.0) );
float k31 = dot( n, normalize( cross(v1,v3)) ) * acos( clamp(dot(v3,v1),-1.0,1.0) );
float k10 = dot( n, normalize( cross(v0,v1)) ) * acos( clamp(dot(v1,v0),-1.0,1.0) );
float k45 = dot( n, normalize( cross(v5,v4)) ) * acos( clamp(dot(v4,v5),-1.0,1.0) );
float k57 = dot( n, normalize( cross(v7,v5)) ) * acos( clamp(dot(v5,v7),-1.0,1.0) );
float k76 = dot( n, normalize( cross(v6,v7)) ) * acos( clamp(dot(v7,v6),-1.0,1.0) );
float k37 = dot( n, normalize( cross(v7,v3)) ) * acos( clamp(dot(v3,v7),-1.0,1.0) );
float k64 = dot( n, normalize( cross(v4,v6)) ) * acos( clamp(dot(v6,v4),-1.0,1.0) );
float k51 = dot( n, normalize( cross(v1,v5)) ) * acos( clamp(dot(v5,v1),-1.0,1.0) );
float k04 = dot( n, normalize( cross(v4,v0)) ) * acos( clamp(dot(v0,v4),-1.0,1.0) );
float k62 = dot( n, normalize( cross(v2,v6)) ) * acos( clamp(dot(v6,v2),-1.0,1.0) );
// 6 faces
float occ = 0.0;
occ += ( k02 + k23 + k31 + k10) * step( 0.0, v0.z );
occ += ( k45 + k57 + k76 + k64) * step( 0.0, -v4.z );
occ += ( k51 - k31 + k37 - k57) * step( 0.0, -v5.x );
occ += ( k04 - k64 + k62 - k02) * step( 0.0, v0.x );
occ += (-k76 - k37 - k23 - k62) * step( 0.0, -v6.y );
occ += (-k10 - k51 - k45 - k04) * step( 0.0, v0.y );
return occ / 6.2831;
}
//-----------------------------------------------------------------------------------------
mat4 rotationAxisAngle( vec3 v, float angle )
{
float s = sin( angle );
float c = cos( angle );
float ic = 1.0 - c;
return mat4( v.x*v.x*ic + c, v.y*v.x*ic - s*v.z, v.z*v.x*ic + s*v.y, 0.0,
v.x*v.y*ic + s*v.z, v.y*v.y*ic + c, v.z*v.y*ic - s*v.x, 0.0,
v.x*v.z*ic - s*v.y, v.y*v.z*ic + s*v.x, v.z*v.z*ic + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
}
mat4 translate( float x, float y, float z )
{
return mat4( 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0 );
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
#define AA 2
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.4*iTime;
vec3 ro = vec3( 2.5*cos(an), 1.0, 2.5*sin(an) );
vec3 ta = vec3( 0.0, 0.8, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = ( cross(uu,ww));
// animate box
mat4 rot = rotationAxisAngle( normalize(vec3(1.0,1.0,0.0)), iTime );
mat4 tra = translate( 0.0, 1.0, 0.0 );
mat4 txi = tra * rot;
mat4 txx = inverse( txi );
vec3 box = vec3(0.4,0.6,0.8);
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
// raytrace
float tmin = 10000.0;
vec3 nor = vec3(0.0);
vec3 pos = vec3(0.0);
vec2 uv = vec2(0.0);
// raytrace-plane
float oid = 0.0;
float h = (0.0-ro.y)/rd.y;
if( h>0.0 )
{
tmin = h;
nor = vec3(0.0,1.0,0.0);
oid = 1.0;
pos = ro + rd*h;
uv = pos.xz;
}
// raytrace box
vec3 bnor; vec2 buv;
int outFaceID;
vec2 tnf;
if( boxIntersect( ro, rd, txx, txi, box, tnf, bnor, buv, outFaceID) )
{
if( tnf.x>0.0 && tnf.x<tmin )
{
tmin = tnf.x;
nor = bnor;
uv = buv;
oid = 2.0;
}
}
// shading/lighting
vec3 col = vec3(0.9);
if( tmin<100.0 )
{
vec3 lig = normalize(vec3(0.3,0.5,0.8));
pos = ro + tmin*rd;
// material
float occ = 1.0;
vec3 mate = vec3(1.0);
if( oid<1.5 ) // plane
{
mate = vec3(0.6)*(0.8+0.2*checkersGradBox( 2.0*uv ));
// analytic ambient occlusion
occ = 1.0-boxOcclusion( pos, nor, txx, txi, box );
}
else // box
{
mate = vec3(0.5)*(0.8+0.2*checkersGradBox( 5.0*uv ));
// fake ambient occlusion
occ = 0.5 + 0.5*nor.y;
occ *= 0.5 + 0.5*clamp(pos.y,0.0,1.0);
}
// lighting
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
dif *= boxSoftShadow( pos+0.01*nor, lig, txx, box, 4.0 );
col = vec3(0.1,0.2,0.3)*occ + 2.0*dif*vec3(1.0,0.8,0.7);
// material * lighting
col *= mate;
// fog
col = mix( col, vec3(0.9), 1.0-exp( -0.003*tmin*tmin ) );
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
8697,
8773,
8809,
8830,
9053
] | [
[
4547,
4615,
4784,
4821,
5767
],
[
5770,
5838,
5926,
5926,
7986
],
[
8081,
8081,
8128,
8128,
8532
],
[
8534,
8534,
8579,
8579,
8695
],
[
8697,
8773,
8809,
8830,
9053
]
] | // http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
| float checkersGradBox( in vec2 p )
{ |
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
} | // http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{ | 19 | 21 |
3d2GRh | iq | 2019-01-23T11:08:33 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// random hash
float hash( in ivec2 q )
{
// You SHOULD replace this by something better. Again, Do Not Use in production.
int n = q.x*131 + q.y*57;
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return float((n>>8)&0x007fffff)/float(0x007fffff);
}
// basic value noise
float noise( in vec2 x, in int p )
{
ivec2 i = ivec2(floor(x));
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
return mix(mix( hash((i+ivec2(0,0))&(p-1)),
hash((i+ivec2(1,0))&(p-1)),f.x),
mix( hash((i+ivec2(0,1))&(p-1)),
hash((i+ivec2(1,1))&(p-1)),f.x),f.y);
}
// fractal noise
float fbm( in vec2 x, in int p )
{
float f = 0.0;
float s = 0.5;
for( int i=0; i<9; i++ )
{
f += s*noise( x, p );
s *= 0.5;
x *= 2.0;
p *= 2;
}
return f;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
bool polar = fract(iTime/6.0)>0.5;
// coords
vec2 q = (polar) ? vec2( 4.0+4.0*atan(p.y,p.x)/3.1415927, length(p) ) : p*8.0;
q += 0.5*iTime;
// fbm
const int pe = 8; // Period. Make it a power of 2
float f = fbm( q, pe );
vec3 col = vec3(f);
// grid
if( !polar )
{
vec2 w = smoothstep(0.0,0.01,abs(mod(8.0*p+float(pe/2),float(pe))/float(pe)-0.5));
col = mix( col, vec3(1.0,0.7,0.0), (1.0-w.x*w.y)*smoothstep( 0.8,0.9,sin(iTime) ) );
}
fragColor = vec4( col, 1.0 );
} | mit | [
1079,
1094,
1120,
1205,
1366
] | [
[
1079,
1094,
1120,
1205,
1366
],
[
1368,
1389,
1425,
1425,
1732
],
[
1734,
1751,
1785,
1785,
1962
],
[
1964,
1964,
2021,
2021,
2627
]
] | // random hash
| float hash( in ivec2 q )
{ |
int n = q.x*131 + q.y*57;
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return float((n>>8)&0x007fffff)/float(0x007fffff);
} | // random hash
float hash( in ivec2 q )
{ | 2 | 2 |
3d2GRh | iq | 2019-01-23T11:08:33 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// random hash
float hash( in ivec2 q )
{
// You SHOULD replace this by something better. Again, Do Not Use in production.
int n = q.x*131 + q.y*57;
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return float((n>>8)&0x007fffff)/float(0x007fffff);
}
// basic value noise
float noise( in vec2 x, in int p )
{
ivec2 i = ivec2(floor(x));
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
return mix(mix( hash((i+ivec2(0,0))&(p-1)),
hash((i+ivec2(1,0))&(p-1)),f.x),
mix( hash((i+ivec2(0,1))&(p-1)),
hash((i+ivec2(1,1))&(p-1)),f.x),f.y);
}
// fractal noise
float fbm( in vec2 x, in int p )
{
float f = 0.0;
float s = 0.5;
for( int i=0; i<9; i++ )
{
f += s*noise( x, p );
s *= 0.5;
x *= 2.0;
p *= 2;
}
return f;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
bool polar = fract(iTime/6.0)>0.5;
// coords
vec2 q = (polar) ? vec2( 4.0+4.0*atan(p.y,p.x)/3.1415927, length(p) ) : p*8.0;
q += 0.5*iTime;
// fbm
const int pe = 8; // Period. Make it a power of 2
float f = fbm( q, pe );
vec3 col = vec3(f);
// grid
if( !polar )
{
vec2 w = smoothstep(0.0,0.01,abs(mod(8.0*p+float(pe/2),float(pe))/float(pe)-0.5));
col = mix( col, vec3(1.0,0.7,0.0), (1.0-w.x*w.y)*smoothstep( 0.8,0.9,sin(iTime) ) );
}
fragColor = vec4( col, 1.0 );
} | mit | [
1368,
1389,
1425,
1425,
1732
] | [
[
1079,
1094,
1120,
1205,
1366
],
[
1368,
1389,
1425,
1425,
1732
],
[
1734,
1751,
1785,
1785,
1962
],
[
1964,
1964,
2021,
2021,
2627
]
] | // basic value noise
| float noise( in vec2 x, in int p )
{ |
ivec2 i = ivec2(floor(x));
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
return mix(mix( hash((i+ivec2(0,0))&(p-1)),
hash((i+ivec2(1,0))&(p-1)),f.x),
mix( hash((i+ivec2(0,1))&(p-1)),
hash((i+ivec2(1,1))&(p-1)),f.x),f.y);
} | // basic value noise
float noise( in vec2 x, in int p )
{ | 1 | 1 |
3d2GRh | iq | 2019-01-23T11:08:33 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// random hash
float hash( in ivec2 q )
{
// You SHOULD replace this by something better. Again, Do Not Use in production.
int n = q.x*131 + q.y*57;
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return float((n>>8)&0x007fffff)/float(0x007fffff);
}
// basic value noise
float noise( in vec2 x, in int p )
{
ivec2 i = ivec2(floor(x));
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
return mix(mix( hash((i+ivec2(0,0))&(p-1)),
hash((i+ivec2(1,0))&(p-1)),f.x),
mix( hash((i+ivec2(0,1))&(p-1)),
hash((i+ivec2(1,1))&(p-1)),f.x),f.y);
}
// fractal noise
float fbm( in vec2 x, in int p )
{
float f = 0.0;
float s = 0.5;
for( int i=0; i<9; i++ )
{
f += s*noise( x, p );
s *= 0.5;
x *= 2.0;
p *= 2;
}
return f;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
bool polar = fract(iTime/6.0)>0.5;
// coords
vec2 q = (polar) ? vec2( 4.0+4.0*atan(p.y,p.x)/3.1415927, length(p) ) : p*8.0;
q += 0.5*iTime;
// fbm
const int pe = 8; // Period. Make it a power of 2
float f = fbm( q, pe );
vec3 col = vec3(f);
// grid
if( !polar )
{
vec2 w = smoothstep(0.0,0.01,abs(mod(8.0*p+float(pe/2),float(pe))/float(pe)-0.5));
col = mix( col, vec3(1.0,0.7,0.0), (1.0-w.x*w.y)*smoothstep( 0.8,0.9,sin(iTime) ) );
}
fragColor = vec4( col, 1.0 );
} | mit | [
1734,
1751,
1785,
1785,
1962
] | [
[
1079,
1094,
1120,
1205,
1366
],
[
1368,
1389,
1425,
1425,
1732
],
[
1734,
1751,
1785,
1785,
1962
],
[
1964,
1964,
2021,
2021,
2627
]
] | // fractal noise
| float fbm( in vec2 x, in int p )
{ |
float f = 0.0;
float s = 0.5;
for( int i=0; i<9; i++ )
{
f += s*noise( x, p );
s *= 0.5;
x *= 2.0;
p *= 2;
}
return f;
} | // fractal noise
float fbm( in vec2 x, in int p )
{ | 1 | 1 |
WdBGW1 | yasuo | 2019-01-28T17:09:52 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// Code is based on: https://www.shadertoy.com/view/MlGcDz
#define matRotateX(rad) mat3(1,0,0,0,cos(rad),-sin(rad),0,sin(rad),cos(rad))
#define matRotateY(rad) mat3(cos(rad),0,-sin(rad),0,1,0,sin(rad),0,cos(rad))
// data from my modeling data.
const int numVertices = 60;
const int numFaces = 42;
const float vertices[60] = float[](
1.013484,-0.004748,-0.170576,1.363493,-0.372823,-1.554794,0.917013,-0.178702,-0.862685,1.288280,-0.178702,-0.862685,1.350874,-0.372823,1.669797,1.010963,-0.004748,0.212469,0.914492,-0.178702,0.904577,1.285759,-0.178702,0.904577,0.581273,-0.024900,0.352901,1.127463,-0.024900,0.352901,1.146371,0.728927,1.492630,0.581273,-0.023149,-0.350895,1.127463,-0.023149,-0.350895,1.146371,0.730678,-1.392582,1.000000,-0.010163,0.363769,1.000000,-0.002439,-0.369747,-1.000000,0.000000,0.006199,0.830098,0.325427,-0.003099,0.862253,-0.223150,0.009298,1.247514,-0.006511,0.004402
);
const int faces[42] = int[](
3,4,2,1,4,3,7,8,6,5,8,7,9,10,11,12,13,14,17,15,18,18,20,16,15,17,19,16,17,18,19,17,16,16,20,19,19,20,15,20,18,15
);
const int numVertices2 = 60;
const int numFaces2 = 69;
const float vertices2[60] = float[](
-0.050644,-1.000000,-0.287185,-0.046908,-1.000000,0.282374,-0.022325,1.000000,-0.603118,0.252637,-0.756947,0.001045,0.086070,-0.554094,0.470331,0.080405,-0.581953,-0.393294,-0.310284,-0.550998,-0.745497,-0.299836,-0.538616,0.847426,0.301236,-0.464652,0.021582,-0.203997,0.661104,-0.847783,0.081116,0.650054,-0.284954,0.155316,0.606239,-0.650704,0.163361,0.764587,-0.622910,-0.191163,0.699229,0.950683,0.172071,0.792447,0.705030,0.085968,0.690296,0.454854,0.166343,0.645718,0.705368,-0.012304,0.995923,0.721267,0.357289,0.557239,0.010229,0.082718,1.115938,0.002954
);
const int faces2[69] = int[](
3,11,13,12,11,9,2,5,8,2,4,5,4,1,6,4,2,1,6,1,7,6,7,10,11,3,19,8,5,14,18,14,15,14,5,9,14,9,17,9,10,12,10,9,6,11,19,9,19,16,9,3,20,19,18,16,19,16,17,9,20,18,19,3,13,10,18,15,16
);
// Triangle intersection. Returns { t, u, v }
// http://iquilezles.org/www/articles/intersectors/intersectors.htm
vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{
vec3 v1v0 = v1 - v0;
vec3 v2v0 = v2 - v0;
vec3 rov0 = ro - v0;
#if 0
// Cramer's rule for solcing p(t) = ro+t·rd = p(u,v) = vo + u·(v1-v0) + v·(v2-v1)
float d = 1.0/determinant(mat3(v1v0, v2v0, -rd ));
float u = d*determinant(mat3(rov0, v2v0, -rd ));
float v = d*determinant(mat3(v1v0, rov0, -rd ));
float t = d*determinant(mat3(v1v0, v2v0, rov0));
#else
// The four determinants above have lots of terms in common. Knowing the changing
// the order of the columns/rows doesn't change the volume/determinant, and that
// the volume is dot(cross(a,b,c)), we can precompute some common terms and reduce
// it all to:
vec3 n = cross( v1v0, v2v0 );
vec3 q = cross( rov0, rd );
float d = 1.0/dot( rd, n );
float u = d*dot( -q, v2v0 );
float v = d*dot( q, v1v0 );
float t = d*dot( -n, rov0 );
#endif
if( u<0.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
return vec3( t, u, v );
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 iBox( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad )
{
// convert from ray to box space
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
// ray-box intersection in box space
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rdd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
// convert to ray space
nor = (txi * vec4(nor,0.0)).xyz;
return vec4( tN, nor );
}
mat4 rotationAxisAngle( vec3 v, float angle )
{
float s = sin( angle );
float c = cos( angle );
float ic = 1.0 - c;
return mat4( v.x*v.x*ic + c, v.y*v.x*ic - s*v.z, v.z*v.x*ic + s*v.y, 0.0,
v.x*v.y*ic + s*v.z, v.y*v.y*ic + c, v.z*v.y*ic - s*v.x, 0.0,
v.x*v.z*ic - s*v.y, v.y*v.z*ic + s*v.x, v.z*v.z*ic + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
}
mat4 translate( float x, float y, float z )
{
return mat4( 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0 );
}
float dBox2d(vec2 p, vec2 b) {
return max(abs(p.x) - b.x, abs(p.y) - b.y);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (-iResolution.xy + 2.0*fragCoord.xy) / iResolution.y;
vec2 prevP = p;
// camera
vec3 ro = vec3(5.0,1.0,0.0);
vec3 ta = vec3( 0.0, 0.8, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
// raytrace
float tmin = 10000.0;
vec3 pos = vec3(0.0);
float oid = 0.0;
// player model
int index = 0;
float xpos = 3.0;
float ypos = 0.3+sin(iTime*0.5)*0.5;
float zpos = 0.0;
vec3 airCraftColor = vec3(0.7);
mat3 rotX = matRotateX(radians(sin(iTime*0.7)*30.0));
for(int i = 0; i<numFaces/3; i++){
int f1 = faces[index];
int f2 = faces[index+1];
int f3 = faces[index+2];
vec3 v0 = vec3(vertices[(f1*3)-3],vertices[((f1*3)-3)+1],vertices[((f1*3)-3)+2]);
vec3 v1 = vec3(vertices[(f2*3)-3],vertices[((f2*3)-3)+1],vertices[((f2*3)-3)+2]);
vec3 v2 = vec3(vertices[(f3*3)-3],vertices[((f3*3)-3)+1],vertices[((f3*3)-3)+2]);
v0.y+=ypos;
v1.y+=ypos;
v2.y+=ypos;
v0.x += 1.0;
v1.x += 1.0;
v2.x += 1.0;
v0 *= rotX;
v1 *= rotX;
v2 *= rotX;
float xspeed = 0.9;
v0.z += sin(iTime*xspeed)*1.5;
v1.z += sin(iTime*xspeed)*1.5;
v2.z += sin(iTime*xspeed)*1.5;
vec3 res = triIntersect( ro, rd, v0, v1, v2);
if( res.x>0.0 && res.x<tmin )
{
tmin = res.x;
oid = 2.0;
airCraftColor = vec3(0.6-(float(i)*0.02));
if(f1 == 9 && f2 == 10 && f3 == 11 || f1 == 12 && f2 == 13 && f3 == 14){
airCraftColor = vec3(0.0,0.0,1.0);
} else if(f1 == 19 && f2 == 20 && f3 == 15){
airCraftColor = mod(iTime,0.2)<0.1?vec3(0.9,0.45,0.2):vec3(0.9,0.45,0.2)*1.5;
} else if(f1 == 16 && f2 == 20 && f3 == 19){
airCraftColor = mod(iTime,0.2)<0.1?vec3(0.9,0.4,0.2):vec3(0.9,0.4,0.2)*1.5;
}
}
index += 3;
}
// boss model
index = 0;
ypos = 1.5+sin(iTime*0.3)*-0.1;
xpos = 1.0+sin(iTime*0.5)*-1.5;
zpos = sin(iTime*1.2)*2.0;
vec3 bossColor = vec3(0.7);
float manimate = sin(iTime*5.0)*0.05;
for(int i = 0; i<numFaces2/3; i++){
int f1 = faces2[index];
int f2 = faces2[index+1];
int f3 = faces2[index+2];
vec3 v0 = vec3(vertices2[(f1*3)-3],vertices2[((f1*3)-3)+1],vertices2[((f1*3)-3)+2]);
vec3 v1 = vec3(vertices2[(f2*3)-3],vertices2[((f2*3)-3)+1],vertices2[((f2*3)-3)+2]);
vec3 v2 = vec3(vertices2[(f3*3)-3],vertices2[((f3*3)-3)+1],vertices2[((f3*3)-3)+2]);
v0.y+=ypos;
v1.y+=ypos;
v2.y+=ypos;
v0.x -= xpos;
v1.x -= xpos;
v2.x -= xpos;
v0.z += zpos;
v1.z += zpos;
v2.z += zpos;
// mouth animation
if(f1 == 9){
v0.y += manimate;
}
if(f2 == 9){
v1.y += manimate;
}
if(f3 == 9){
v2.y += manimate;
}
if(f1 == 4){
v0.y += manimate*-1.0;
}
if(f2 == 4){
v1.y += manimate*-1.0;
}
if(f3 == 4){
v2.y += manimate*-1.0;
}
vec3 res = triIntersect( ro, rd, v0, v1, v2);
if( res.x>0.0 && res.x<tmin )
{
tmin = res.x;
oid = 3.0;
bossColor = vec3(0.7-(float(i)*0.02));
}
index += 3;
}
// enemy bullet
vec3 box = vec3(0.2,0.03,0.2);
vec3 bcolor = vec3(0.8);
mat4 brotX = rotationAxisAngle(vec3(1.0,0.0,0.0),radians(iTime*30.0));
mat4 tra = translate( -xpos+mod(iTime*2.0,3.0)*3.0, 1.0, zpos );
mat4 txi = tra *brotX;
mat4 txx = inverse( txi );
vec4 res = iBox( ro, rd, txx, txi, box);
if( res.x>0.0 && res.x<tmin )
{
tmin = res.x;
oid = 1.0;
bcolor = vec3(0.8-(tmin*0.1));
}
// material/bg
vec3 col = vec3(1.0);
if( tmin<100.0 )
{
// material
vec3 mate = vec3(.0);
if( oid<1.5 ) {
mate = bcolor;
} else if( oid>=1.5 && oid<=2.0 ) {
mate = airCraftColor;
} else if( oid>2.1 && oid<=3.0 ){
mate = bossColor;
}
mate = mate*mate*1.1;
col *= mate;
col = sqrt( col );
} else {
// bg
col = vec3(0.0);
prevP.x+=sin(iTime+p.y)*0.7;
float rbg = (length(prevP+vec2(2.5,0.0))-0.5)+sin(iTime+p.y)*0.2;
col = mix( col, vec3(0.9,0.5,0.0), 1.0-smoothstep(0.15,2.5,abs(rbg)));
float lbg = (length(prevP+vec2(-2.5,0.0))-0.5)+sin(iTime+p.y)*0.2;
col = mix( col, vec3(1.0,0.2,0.2), 1.0-smoothstep(0.15,2.5,abs(lbg)));
}
// UI
p = (fragCoord.xy * 2.0 - iResolution.xy) / min(iResolution.x, iResolution.y);
float ganimate = sin(iTime*1.5)*0.1;
float playerBarBg = dBox2d(p+vec2(1.20, 0.85), vec2(0.3,0.055));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.01,0.011,abs(playerBarBg)));
float playerBar = dBox2d(p+vec2(1.3-ganimate, 0.85), vec2(0.15+ganimate,0.006));
col = mix( col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.029,0.03,abs(playerBar)));
float bossBarBg = dBox2d(p+vec2(-1.20, 0.85), vec2(0.3,0.055));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.01,0.011,abs(bossBarBg)));
float bossBar = dBox2d(p+vec2(-1.2, 0.85), vec2(0.25,0.006));
col = mix( col, vec3(0.6,0.6,1.0), 1.0-smoothstep(0.029,0.03,abs(bossBar)));
// result
fragColor = vec4( col, 1.0 );
} | mit | [
3007,
3121,
3202,
3202,
4169
] | [
[
3007,
3121,
3202,
3202,
4169
],
[
4171,
4239,
4316,
4353,
4871
],
[
4873,
4873,
4920,
4920,
5324
],
[
5326,
5326,
5371,
5371,
5487
],
[
5489,
5489,
5519,
5519,
5566
]
] | // Triangle intersection. Returns { t, u, v }
// http://iquilezles.org/www/articles/intersectors/intersectors.htm
| vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{ |
vec3 v1v0 = v1 - v0;
vec3 v2v0 = v2 - v0;
vec3 rov0 = ro - v0;
#if 0
// Cramer's rule for solcing p(t) = ro+t·rd = p(u,v) = vo + u·(v1-v0) + v·(v2-v1)
float d = 1.0/determinant(mat3(v1v0, v2v0, -rd ));
float u = d*determinant(mat3(rov0, v2v0, -rd ));
float v = d*determinant(mat3(v1v0, rov0, -rd ));
float t = d*determinant(mat3(v1v0, v2v0, rov0));
#else
// The four determinants above have lots of terms in common. Knowing the changing
// the order of the columns/rows doesn't change the volume/determinant, and that
// the volume is dot(cross(a,b,c)), we can precompute some common terms and reduce
// it all to:
vec3 n = cross( v1v0, v2v0 );
vec3 q = cross( rov0, rd );
float d = 1.0/dot( rd, n );
float u = d*dot( -q, v2v0 );
float v = d*dot( q, v1v0 );
float t = d*dot( -n, rov0 );
#endif
if( u<0.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
return vec3( t, u, v );
} | // Triangle intersection. Returns { t, u, v }
// http://iquilezles.org/www/articles/intersectors/intersectors.htm
vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{ | 3 | 5 |
WdBGW1 | yasuo | 2019-01-28T17:09:52 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// Code is based on: https://www.shadertoy.com/view/MlGcDz
#define matRotateX(rad) mat3(1,0,0,0,cos(rad),-sin(rad),0,sin(rad),cos(rad))
#define matRotateY(rad) mat3(cos(rad),0,-sin(rad),0,1,0,sin(rad),0,cos(rad))
// data from my modeling data.
const int numVertices = 60;
const int numFaces = 42;
const float vertices[60] = float[](
1.013484,-0.004748,-0.170576,1.363493,-0.372823,-1.554794,0.917013,-0.178702,-0.862685,1.288280,-0.178702,-0.862685,1.350874,-0.372823,1.669797,1.010963,-0.004748,0.212469,0.914492,-0.178702,0.904577,1.285759,-0.178702,0.904577,0.581273,-0.024900,0.352901,1.127463,-0.024900,0.352901,1.146371,0.728927,1.492630,0.581273,-0.023149,-0.350895,1.127463,-0.023149,-0.350895,1.146371,0.730678,-1.392582,1.000000,-0.010163,0.363769,1.000000,-0.002439,-0.369747,-1.000000,0.000000,0.006199,0.830098,0.325427,-0.003099,0.862253,-0.223150,0.009298,1.247514,-0.006511,0.004402
);
const int faces[42] = int[](
3,4,2,1,4,3,7,8,6,5,8,7,9,10,11,12,13,14,17,15,18,18,20,16,15,17,19,16,17,18,19,17,16,16,20,19,19,20,15,20,18,15
);
const int numVertices2 = 60;
const int numFaces2 = 69;
const float vertices2[60] = float[](
-0.050644,-1.000000,-0.287185,-0.046908,-1.000000,0.282374,-0.022325,1.000000,-0.603118,0.252637,-0.756947,0.001045,0.086070,-0.554094,0.470331,0.080405,-0.581953,-0.393294,-0.310284,-0.550998,-0.745497,-0.299836,-0.538616,0.847426,0.301236,-0.464652,0.021582,-0.203997,0.661104,-0.847783,0.081116,0.650054,-0.284954,0.155316,0.606239,-0.650704,0.163361,0.764587,-0.622910,-0.191163,0.699229,0.950683,0.172071,0.792447,0.705030,0.085968,0.690296,0.454854,0.166343,0.645718,0.705368,-0.012304,0.995923,0.721267,0.357289,0.557239,0.010229,0.082718,1.115938,0.002954
);
const int faces2[69] = int[](
3,11,13,12,11,9,2,5,8,2,4,5,4,1,6,4,2,1,6,1,7,6,7,10,11,3,19,8,5,14,18,14,15,14,5,9,14,9,17,9,10,12,10,9,6,11,19,9,19,16,9,3,20,19,18,16,19,16,17,9,20,18,19,3,13,10,18,15,16
);
// Triangle intersection. Returns { t, u, v }
// http://iquilezles.org/www/articles/intersectors/intersectors.htm
vec3 triIntersect( in vec3 ro, in vec3 rd, in vec3 v0, in vec3 v1, in vec3 v2 )
{
vec3 v1v0 = v1 - v0;
vec3 v2v0 = v2 - v0;
vec3 rov0 = ro - v0;
#if 0
// Cramer's rule for solcing p(t) = ro+t·rd = p(u,v) = vo + u·(v1-v0) + v·(v2-v1)
float d = 1.0/determinant(mat3(v1v0, v2v0, -rd ));
float u = d*determinant(mat3(rov0, v2v0, -rd ));
float v = d*determinant(mat3(v1v0, rov0, -rd ));
float t = d*determinant(mat3(v1v0, v2v0, rov0));
#else
// The four determinants above have lots of terms in common. Knowing the changing
// the order of the columns/rows doesn't change the volume/determinant, and that
// the volume is dot(cross(a,b,c)), we can precompute some common terms and reduce
// it all to:
vec3 n = cross( v1v0, v2v0 );
vec3 q = cross( rov0, rd );
float d = 1.0/dot( rd, n );
float u = d*dot( -q, v2v0 );
float v = d*dot( q, v1v0 );
float t = d*dot( -n, rov0 );
#endif
if( u<0.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
return vec3( t, u, v );
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 iBox( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad )
{
// convert from ray to box space
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
// ray-box intersection in box space
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rdd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
// convert to ray space
nor = (txi * vec4(nor,0.0)).xyz;
return vec4( tN, nor );
}
mat4 rotationAxisAngle( vec3 v, float angle )
{
float s = sin( angle );
float c = cos( angle );
float ic = 1.0 - c;
return mat4( v.x*v.x*ic + c, v.y*v.x*ic - s*v.z, v.z*v.x*ic + s*v.y, 0.0,
v.x*v.y*ic + s*v.z, v.y*v.y*ic + c, v.z*v.y*ic - s*v.x, 0.0,
v.x*v.z*ic - s*v.y, v.y*v.z*ic + s*v.x, v.z*v.z*ic + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
}
mat4 translate( float x, float y, float z )
{
return mat4( 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0 );
}
float dBox2d(vec2 p, vec2 b) {
return max(abs(p.x) - b.x, abs(p.y) - b.y);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (-iResolution.xy + 2.0*fragCoord.xy) / iResolution.y;
vec2 prevP = p;
// camera
vec3 ro = vec3(5.0,1.0,0.0);
vec3 ta = vec3( 0.0, 0.8, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
// raytrace
float tmin = 10000.0;
vec3 pos = vec3(0.0);
float oid = 0.0;
// player model
int index = 0;
float xpos = 3.0;
float ypos = 0.3+sin(iTime*0.5)*0.5;
float zpos = 0.0;
vec3 airCraftColor = vec3(0.7);
mat3 rotX = matRotateX(radians(sin(iTime*0.7)*30.0));
for(int i = 0; i<numFaces/3; i++){
int f1 = faces[index];
int f2 = faces[index+1];
int f3 = faces[index+2];
vec3 v0 = vec3(vertices[(f1*3)-3],vertices[((f1*3)-3)+1],vertices[((f1*3)-3)+2]);
vec3 v1 = vec3(vertices[(f2*3)-3],vertices[((f2*3)-3)+1],vertices[((f2*3)-3)+2]);
vec3 v2 = vec3(vertices[(f3*3)-3],vertices[((f3*3)-3)+1],vertices[((f3*3)-3)+2]);
v0.y+=ypos;
v1.y+=ypos;
v2.y+=ypos;
v0.x += 1.0;
v1.x += 1.0;
v2.x += 1.0;
v0 *= rotX;
v1 *= rotX;
v2 *= rotX;
float xspeed = 0.9;
v0.z += sin(iTime*xspeed)*1.5;
v1.z += sin(iTime*xspeed)*1.5;
v2.z += sin(iTime*xspeed)*1.5;
vec3 res = triIntersect( ro, rd, v0, v1, v2);
if( res.x>0.0 && res.x<tmin )
{
tmin = res.x;
oid = 2.0;
airCraftColor = vec3(0.6-(float(i)*0.02));
if(f1 == 9 && f2 == 10 && f3 == 11 || f1 == 12 && f2 == 13 && f3 == 14){
airCraftColor = vec3(0.0,0.0,1.0);
} else if(f1 == 19 && f2 == 20 && f3 == 15){
airCraftColor = mod(iTime,0.2)<0.1?vec3(0.9,0.45,0.2):vec3(0.9,0.45,0.2)*1.5;
} else if(f1 == 16 && f2 == 20 && f3 == 19){
airCraftColor = mod(iTime,0.2)<0.1?vec3(0.9,0.4,0.2):vec3(0.9,0.4,0.2)*1.5;
}
}
index += 3;
}
// boss model
index = 0;
ypos = 1.5+sin(iTime*0.3)*-0.1;
xpos = 1.0+sin(iTime*0.5)*-1.5;
zpos = sin(iTime*1.2)*2.0;
vec3 bossColor = vec3(0.7);
float manimate = sin(iTime*5.0)*0.05;
for(int i = 0; i<numFaces2/3; i++){
int f1 = faces2[index];
int f2 = faces2[index+1];
int f3 = faces2[index+2];
vec3 v0 = vec3(vertices2[(f1*3)-3],vertices2[((f1*3)-3)+1],vertices2[((f1*3)-3)+2]);
vec3 v1 = vec3(vertices2[(f2*3)-3],vertices2[((f2*3)-3)+1],vertices2[((f2*3)-3)+2]);
vec3 v2 = vec3(vertices2[(f3*3)-3],vertices2[((f3*3)-3)+1],vertices2[((f3*3)-3)+2]);
v0.y+=ypos;
v1.y+=ypos;
v2.y+=ypos;
v0.x -= xpos;
v1.x -= xpos;
v2.x -= xpos;
v0.z += zpos;
v1.z += zpos;
v2.z += zpos;
// mouth animation
if(f1 == 9){
v0.y += manimate;
}
if(f2 == 9){
v1.y += manimate;
}
if(f3 == 9){
v2.y += manimate;
}
if(f1 == 4){
v0.y += manimate*-1.0;
}
if(f2 == 4){
v1.y += manimate*-1.0;
}
if(f3 == 4){
v2.y += manimate*-1.0;
}
vec3 res = triIntersect( ro, rd, v0, v1, v2);
if( res.x>0.0 && res.x<tmin )
{
tmin = res.x;
oid = 3.0;
bossColor = vec3(0.7-(float(i)*0.02));
}
index += 3;
}
// enemy bullet
vec3 box = vec3(0.2,0.03,0.2);
vec3 bcolor = vec3(0.8);
mat4 brotX = rotationAxisAngle(vec3(1.0,0.0,0.0),radians(iTime*30.0));
mat4 tra = translate( -xpos+mod(iTime*2.0,3.0)*3.0, 1.0, zpos );
mat4 txi = tra *brotX;
mat4 txx = inverse( txi );
vec4 res = iBox( ro, rd, txx, txi, box);
if( res.x>0.0 && res.x<tmin )
{
tmin = res.x;
oid = 1.0;
bcolor = vec3(0.8-(tmin*0.1));
}
// material/bg
vec3 col = vec3(1.0);
if( tmin<100.0 )
{
// material
vec3 mate = vec3(.0);
if( oid<1.5 ) {
mate = bcolor;
} else if( oid>=1.5 && oid<=2.0 ) {
mate = airCraftColor;
} else if( oid>2.1 && oid<=3.0 ){
mate = bossColor;
}
mate = mate*mate*1.1;
col *= mate;
col = sqrt( col );
} else {
// bg
col = vec3(0.0);
prevP.x+=sin(iTime+p.y)*0.7;
float rbg = (length(prevP+vec2(2.5,0.0))-0.5)+sin(iTime+p.y)*0.2;
col = mix( col, vec3(0.9,0.5,0.0), 1.0-smoothstep(0.15,2.5,abs(rbg)));
float lbg = (length(prevP+vec2(-2.5,0.0))-0.5)+sin(iTime+p.y)*0.2;
col = mix( col, vec3(1.0,0.2,0.2), 1.0-smoothstep(0.15,2.5,abs(lbg)));
}
// UI
p = (fragCoord.xy * 2.0 - iResolution.xy) / min(iResolution.x, iResolution.y);
float ganimate = sin(iTime*1.5)*0.1;
float playerBarBg = dBox2d(p+vec2(1.20, 0.85), vec2(0.3,0.055));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.01,0.011,abs(playerBarBg)));
float playerBar = dBox2d(p+vec2(1.3-ganimate, 0.85), vec2(0.15+ganimate,0.006));
col = mix( col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.029,0.03,abs(playerBar)));
float bossBarBg = dBox2d(p+vec2(-1.20, 0.85), vec2(0.3,0.055));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.01,0.011,abs(bossBarBg)));
float bossBar = dBox2d(p+vec2(-1.2, 0.85), vec2(0.25,0.006));
col = mix( col, vec3(0.6,0.6,1.0), 1.0-smoothstep(0.029,0.03,abs(bossBar)));
// result
fragColor = vec4( col, 1.0 );
} | mit | [
4171,
4239,
4316,
4353,
4871
] | [
[
3007,
3121,
3202,
3202,
4169
],
[
4171,
4239,
4316,
4353,
4871
],
[
4873,
4873,
4920,
4920,
5324
],
[
5326,
5326,
5371,
5371,
5487
],
[
5489,
5489,
5519,
5519,
5566
]
] | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
| vec4 iBox( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad )
{ |
vec3 rdd = (txx*vec4(rd,0.0)).xyz;
vec3 roo = (txx*vec4(ro,1.0)).xyz;
// ray-box intersection in box space
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rdd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
// convert to ray space
nor = (txi * vec4(nor,0.0)).xyz;
return vec4( tN, nor );
} | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 iBox( in vec3 ro, in vec3 rd, in mat4 txx, in mat4 txi, in vec3 rad )
{ | 5 | 7 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
1111,
2215,
2240,
2240,
2347
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | // The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
| float hash(vec2 p)
{ |
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
} | // The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
2847,
2876,
2907,
2907,
2936
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | // Base distance function
| float ball(vec3 p,float s)
{ |
return length(p)-s;
} | // Base distance function
float ball(vec3 p,float s)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
2945,
2970,
3000,
3000,
3313
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | // Making ball status
| vec4 metaballvalue(int i)
{ |
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
} | // Making ball status
vec4 metaballvalue(int i)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
3317,
3353,
3390,
3390,
3522
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | // Making ball distance function
| float metaballone(vec3 p, int i)
{ |
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
} | // Making ball distance function
float metaballone(vec3 p, int i)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
3527,
3567,
3594,
3594,
3781
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | //Making metaballs distance function
| float metaball(vec3 p)
{ |
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
} | //Making metaballs distance function
float metaball(vec3 p)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
3788,
3819,
3842,
3842,
3981
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | // Making distance function
| float dist(vec3 p)
{ |
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
} | // Making distance function
float dist(vec3 p)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
6190,
6279,
6310,
6310,
6521
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | //Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
| vec3 getnormal( in vec3 p)
{ |
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
} | //Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
6606,
6626,
6674,
6674,
6898
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | // Making shadow
| float softray( vec3 ro, vec3 rd , float hn)
{ |
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
} | // Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{ | 1 | 1 |
3dlSDn | butadiene | 2019-02-25T04:15:29 | // The MIT License
// Copyright © 2019 Butadiene
// 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.
const float _ypos =-0.25;
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
//Making noise
float hash(vec2 p)
{
p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
hash( i + vec2(1.0,1.0) ), u.x), u.y);
}
///////////////////////////////////////////////////////////////////////
float smoothMin(float d1,float d2,float k)
{
return -log(exp(-k*d1)+exp(-k*d2))/k;
}
// Base distance function
float ball(vec3 p,float s)
{
return length(p)-s;
}
// Making ball status
vec4 metaballvalue(int i)
{
float ifloat = float(i);
float kt = 3.*iTime*(0.1+0.01*ifloat);
vec3 ballpos = 0.3*vec3(noise(vec2(ifloat,ifloat)+kt),noise(vec2(ifloat+10.,ifloat*20.)+kt),noise(vec2(ifloat*20.,ifloat+20.)+kt));
float scale = 0.05+0.02*hash(vec2(ifloat,ifloat));
return vec4(ballpos,scale);
}
// Making ball distance function
float metaballone(vec3 p, int i)
{
vec4 value = metaballvalue(i);
vec3 ballpos = p-value.xyz;
float scale =value.w;
return ball(ballpos,scale);
}
//Making metaballs distance function
float metaball(vec3 p)
{
float d1;
float d2 = metaballone(p,0);
for (int i = 1; i < 6; ++i) {
d1 = metaballone(p,i);
d1 = smoothMin(d1,d2,20.);
d2 =d1;
}
return d1;
}
// Making distance function
float dist(vec3 p)
{
float y = p.y;
float d1 =metaball(p);
float d2 = y-(_ypos); //For floor
d1 = smoothMin(d1,d2,20.);
return d1;
}
//enhanced sphere tracing http://erleuchtet.org/~cupe/permanent/enhanced_sphere_tracing.pdf
float raymarch (vec3 ro,vec3 rd)
{
float previousradius = 0.0;
float maxdistance = 3.;
float outside = dist(ro) < 0. ? -1. : +1.;
float pixelradius = 0.01;
float omega = 1.2;
float t =0.0001;
float step = 0.;
float minpixelt =999999999.;
float mint = 0.;
float hit = 0.01;
for (float i = 0.; i < 80.; ++i) {
float radius = outside*dist(ro+rd*t);
bool fail = omega>1. &&step>(abs(radius)+abs(previousradius));
if(fail){
step -= step *omega;
omega =1.0;
}
else{
step = omega * radius;
}
previousradius = radius;
float pixelt = radius/t;
if(!fail&&pixelt<minpixelt){
minpixelt = pixelt;
mint = t;
}
if(!fail&&pixelt<pixelradius||t>maxdistance)
break;
t += step;
}
if ((t > maxdistance || minpixelt > pixelradius)&&(mint>hit)){
return -1.;
}
else{
return mint;
}
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/Xds3zN
//Tetrahedron technique http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 getnormal( in vec3 p)
{
vec2 e = vec2(0.5773,-0.5773)*0.0001;
vec3 nor = normalize( e.xyy*dist(p+e.xyy) + e.yyx*dist(p+e.yyx) + e.yxy*dist(p+e.yxy ) + e.xxx*dist(p+e.xxx));
nor = normalize(vec3(nor));
return nor ;
}
////////////////////////////////////////////////////////////////////////////
// Making shadow
float softray( vec3 ro, vec3 rd , float hn)
{
float t = 0.000001;
float jt = 0.0;
float res = 1.;
for (int i = 0; i < 20; ++i) {
jt = dist(ro+rd*t);
res = min(res,jt*hn/t);
t = t+ clamp(0.02,2.,jt);
}
return clamp(res,0.,1.);
}
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.
// https://www.shadertoy.com/view/ld2GRz
vec4 material(vec3 pos)
{
vec4 ballcol[6]=vec4[6](vec4(0.5,0.,0.,1.),
vec4(0.,0.5,0.,1.),
vec4(0.,0.,0.5,1.),
vec4(0.25,0.25,0,1.),
vec4(0.25,0,0.25,1.),
vec4(0.,0.25,0.25,1.));
vec3 mate = vec3(0,0,0);
float w = 0.01;
// Making ball color
for (int i = 0; i < 6; ++i) {
float x = clamp( (length( metaballvalue(i).xyz - pos )-metaballvalue(i).w)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(ballcol[i].xyz);
w += p;
}
// Making floor color
float x = clamp( (pos.y-_ypos)*10.,0.,1. );
float p = 1.0 - x*x*(3.0-2.0*x);
mate += p*vec3(0.4,0.4,0.4);
w += p;
mate /= w;
return vec4(mate,1);
}
////////////////////////////////////////////////////
//Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord* 2.0 - iResolution.xy) / min(iResolution.x,iResolution.y);
vec2 sc = 2.*(uv-0.5);
vec3 ro = vec3(0.18,0.2,-0.8);
vec3 rd = normalize(vec3(sc,4)-ro);
vec4 baccol = vec4((0.2+uv.y*0.5)*vec3(0.,1.,1.),1.);
float t = raymarch(ro,rd);
vec4 col;
if (t==-1.) {
col = baccol;
}
else{
vec3 pos = ro+rd*t;
col = lighting(pos,ro);
}
fragColor = col;
} | mit | [
8857,
8904,
8940,
8940,
9408
] | [
[
1111,
2215,
2240,
2240,
2347
],
[
2352,
2352,
2381,
2381,
2648
],
[
2742,
2742,
2789,
2789,
2836
],
[
2847,
2876,
2907,
2907,
2936
],
[
2945,
2970,
3000,
3000,
3313
],
[
3317,
3353,
3390,
3390,
3522
],
[
3527,
3567,
3594,
3594,
3781
],
[
3788,
3819,
3842,
3842,
3981
],
[
4084,
4084,
4121,
4121,
5054
],
[
6190,
6279,
6310,
6310,
6521
],
[
6606,
6626,
6674,
6674,
6898
],
[
8037,
8037,
8065,
8065,
8793
],
[
8857,
8904,
8940,
8940,
9408
],
[
9412,
9412,
9469,
9469,
9908
]
] | //Phong reflection model ,Directional light
| vec4 lighting(vec3 pos,vec3 ro)
{ |
vec3 mpos =pos;
vec3 normal =getnormal(mpos);
vec3 viewdir = normalize(pos-ro);
vec3 lightdir = normalize(vec3(0.5,0.5,-0.5));
float sha = softray(mpos,lightdir,3.3);
vec4 Color = material(mpos);
float NdotL = max(0.,dot(normal,lightdir));
vec3 R = -normalize(reflect(lightdir,normal));
float spec =pow(max(dot(R,-viewdir),0.),10.);
vec4 col = sha*(Color* NdotL+vec4(spec,spec,spec,0.));
return col;
} | //Phong reflection model ,Directional light
vec4 lighting(vec3 pos,vec3 ro)
{ | 1 | 1 |
MldfWn | iq | 2019-02-11T07:14:46 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// The distance to a generic ellipsoid can be approximated, but produces
// distorted distance fields that manifest, for example, in wrongly
// rendered soft shadows.
//
// Symmetric ellipsoids (ellipses revolved in 3D) do allow however for an exact
// distance estimation (although it requires solving a cubic, which is expensive)
// and produce beautiful shadows.
//
// Left, a symmetric ellipsoids rendered with the generic ellipsod method,
// producing a too big penumbra.
// Right, same shape rendered with the symmetric solver, producing the
// correct penumbra.
//
// See also https://www.shadertoy.com/view/tdS3DG
#define AA 2 // make this 4 is you have a fast computer
//------------------------------------------------------------------
float sdEllipse( vec2 p, in vec2 ab )
{
p = abs( p ); if( p.x > p.y ){ p=p.yx; ab=ab.yx; }
float l = ab.y*ab.y - ab.x*ab.x;
float m = ab.x*p.x/l;
float n = ab.y*p.y/l;
float m2 = m*m;
float n2 = n*n;
float c = (m2 + n2 - 1.0)/3.0;
float c3 = c*c*c;
float q = c3 + m2*n2*2.0;
float d = c3 + m2*n2;
float g = m + m*n2;
float co;
if( d<0.0 )
{
float h = acos(q/c3)/3.0;
float s = cos(h);
float t = sin(h)*sqrt(3.0);
float rx = sqrt( -c*(s + t + 2.0) + m2 );
float ry = sqrt( -c*(s - t + 2.0) + m2 );
co = ( ry + sign(l)*rx + abs(g)/(rx*ry) - m)/2.0;
}
else
{
float h = 2.0*m*n*sqrt( d );
float s = sign(q+h)*pow( abs(q+h), 1.0/3.0 );
float u = sign(q-h)*pow( abs(q-h), 1.0/3.0 );
float rx = -s - u - c*4.0 + 2.0*m2;
float ry = (s - u)*sqrt(3.0);
float rm = sqrt( rx*rx + ry*ry );
co = (ry/sqrt(rm-rx) + 2.0*g/rm - m)/2.0;
}
float si = sqrt( 1.0 - co*co );
vec2 r = ab * vec2(co,si);
return length(r-p) * sign(p.y-r.y);
}
// generic ellipsoid - approximated distance: https://www.shadertoy.com/view/tdS3DG
float sdEllipsoid( in vec3 p, in vec3 r )
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
// symmetric ellipsoid - EXACT distance
float sdEllipsoidXXZ( in vec3 p, in vec2 r )
{
return sdEllipse( vec2( length(p.xy), p.z ), r );
}
//------------------------------------------------------------------
vec2 map( in vec3 p, int id )
{
// ellipsoid
float d1 = (id==0) ? sdEllipsoid( p, vec3(0.25,0.25,0.02) ) :
sdEllipsoidXXZ( p, vec2(0.25, 0.02) );
// plane
float d2 = p.y+0.25;
return (d1<d2) ? vec2(d1,1.0) : vec2(d2,2.0);
}
vec2 castRay( in vec3 ro, in vec3 rd, int id )
{
float m = 0.0;
float t = 0.0;
const float tmax = 100.0;
for( int i=0; i<64 && t<tmax; i++ )
{
vec2 h = map( ro+rd*t, id );
if( h.x<0.001 ) break;
m = h.y;
t += h.x;
}
return (t<tmax) ? vec2(t,m) : vec2(0.0);
}
float calcSoftshadow( in vec3 ro, in vec3 rd, in int id)
{
float res = 1.0;
float t = 0.01;
for( int i=0; i<256; i++ )
{
float h = map( ro + rd*t, id ).x;
res = min( res, smoothstep(0.0,1.0,8.0*h/t) );
t += clamp( h, 0.005, 0.02 );
if( res<0.001 || t>5.0 ) break;
}
return clamp( res, 0.0, 1.0 );
}
vec3 calcNormal( in vec3 pos, in int id )
{
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy, id ).x +
e.yyx*map( pos + e.yyx, id ).x +
e.yxy*map( pos + e.yxy, id ).x +
e.xxx*map( pos + e.xxx, id ).x );
}
float calcAO( in vec3 pos, in vec3 nor, in int id )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float hr = 0.01 + 0.12*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = map( aopos, id).x;
occ += (hr-dd)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 2.0*occ, 0.0, 1.0 );
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
vec3 render( in vec3 ro, in vec3 rd, int id )
{
vec3 col = vec3(0.1);
vec2 res = castRay(ro,rd, id);
if( res.y>0.5 )
{
float t = res.x;
vec3 pos = ro + t*rd;
vec3 nor;
float occ;
// material
if( res.y>1.5 )
{
nor = vec3(0.0,1.0,0.0);
col = 0.05*vec3(1.0);
occ = 1.0;
col *= 0.8+0.2*checkersGradBox( pos.xz*2.0 );
}
else
{
nor = calcNormal( pos, id );
occ = 0.5+0.5*nor.y;
col = vec3(0.25,0.15,0.05);
}
// lighting
occ *= calcAO( pos, nor, id );
vec3 lig = normalize( vec3(-0.5, 1.0, 0.8) );
vec3 hal = normalize( lig-rd );
float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
dif *= calcSoftshadow( pos, lig, id );
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),32.0)*
dif *
(0.04 + 0.96*pow( clamp(1.0+dot(hal,rd),0.0,1.0), 5.0 ));
vec3 lin = vec3(0.0);
lin += 2.00*dif*vec3(3.30,2.50,2.00);
lin += 0.50*amb*vec3(0.30,0.60,1.50)*occ;
lin += 0.30*bac*vec3(0.40,0.30,0.25)*occ;
col = col*lin;
col += 2.00*spe*vec3(3.30,2.50,2.00);
//col = mix( col, vec3(0.1), 1.0-exp(-0.03*t) );
}
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera
vec3 ro = vec3( 1.0*cos(0.2*iTime), 0.12, 1.0*sin(0.2*iTime) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera-to-world transformation
vec3 cw = normalize(ta-ro);
vec3 cu = normalize( cross(cw,vec3(0.0, 1.0,0.0)) );
vec3 cv = ( cross(cu,cw) );
int id = (fragCoord.x>iResolution.x/2.0) ? 1 : 0;
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 fc = o + vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#else
vec2 fc = vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#endif
vec2 p = (-vec2(iResolution.x/2.0,iResolution.y) + 2.0*fc)/iResolution.y;
// ray direction
vec3 rd = normalize( p.x*cu + p.y*cv + 2.0*cw );
// render
vec3 col = render( ro, rd, id );
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
tot *= smoothstep( 1.0, 2.5, abs(fragCoord.x-iResolution.x/2.0) );
fragColor = vec4( tot, 1.0 );
} | mit | [
2955,
3039,
3083,
3083,
3172
] | [
[
1778,
1847,
1886,
1886,
2951
],
[
2955,
3039,
3083,
3083,
3172
],
[
3174,
3214,
3261,
3261,
3317
],
[
3389,
3389,
3420,
3437,
3670
],
[
3672,
3672,
3720,
3720,
3988
],
[
3991,
3991,
4049,
4049,
4336
],
[
4338,
4338,
4381,
4381,
4605
],
[
4607,
4607,
4660,
4660,
4961
],
[
4965,
5041,
5077,
5098,
5321
],
[
5323,
5323,
5370,
5370,
6862
]
] | // generic ellipsoid - approximated distance: https://www.shadertoy.com/view/tdS3DG
| float sdEllipsoid( in vec3 p, in vec3 r )
{ |
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
} | // generic ellipsoid - approximated distance: https://www.shadertoy.com/view/tdS3DG
float sdEllipsoid( in vec3 p, in vec3 r )
{ | 6 | 7 |
MldfWn | iq | 2019-02-11T07:14:46 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// The distance to a generic ellipsoid can be approximated, but produces
// distorted distance fields that manifest, for example, in wrongly
// rendered soft shadows.
//
// Symmetric ellipsoids (ellipses revolved in 3D) do allow however for an exact
// distance estimation (although it requires solving a cubic, which is expensive)
// and produce beautiful shadows.
//
// Left, a symmetric ellipsoids rendered with the generic ellipsod method,
// producing a too big penumbra.
// Right, same shape rendered with the symmetric solver, producing the
// correct penumbra.
//
// See also https://www.shadertoy.com/view/tdS3DG
#define AA 2 // make this 4 is you have a fast computer
//------------------------------------------------------------------
float sdEllipse( vec2 p, in vec2 ab )
{
p = abs( p ); if( p.x > p.y ){ p=p.yx; ab=ab.yx; }
float l = ab.y*ab.y - ab.x*ab.x;
float m = ab.x*p.x/l;
float n = ab.y*p.y/l;
float m2 = m*m;
float n2 = n*n;
float c = (m2 + n2 - 1.0)/3.0;
float c3 = c*c*c;
float q = c3 + m2*n2*2.0;
float d = c3 + m2*n2;
float g = m + m*n2;
float co;
if( d<0.0 )
{
float h = acos(q/c3)/3.0;
float s = cos(h);
float t = sin(h)*sqrt(3.0);
float rx = sqrt( -c*(s + t + 2.0) + m2 );
float ry = sqrt( -c*(s - t + 2.0) + m2 );
co = ( ry + sign(l)*rx + abs(g)/(rx*ry) - m)/2.0;
}
else
{
float h = 2.0*m*n*sqrt( d );
float s = sign(q+h)*pow( abs(q+h), 1.0/3.0 );
float u = sign(q-h)*pow( abs(q-h), 1.0/3.0 );
float rx = -s - u - c*4.0 + 2.0*m2;
float ry = (s - u)*sqrt(3.0);
float rm = sqrt( rx*rx + ry*ry );
co = (ry/sqrt(rm-rx) + 2.0*g/rm - m)/2.0;
}
float si = sqrt( 1.0 - co*co );
vec2 r = ab * vec2(co,si);
return length(r-p) * sign(p.y-r.y);
}
// generic ellipsoid - approximated distance: https://www.shadertoy.com/view/tdS3DG
float sdEllipsoid( in vec3 p, in vec3 r )
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
// symmetric ellipsoid - EXACT distance
float sdEllipsoidXXZ( in vec3 p, in vec2 r )
{
return sdEllipse( vec2( length(p.xy), p.z ), r );
}
//------------------------------------------------------------------
vec2 map( in vec3 p, int id )
{
// ellipsoid
float d1 = (id==0) ? sdEllipsoid( p, vec3(0.25,0.25,0.02) ) :
sdEllipsoidXXZ( p, vec2(0.25, 0.02) );
// plane
float d2 = p.y+0.25;
return (d1<d2) ? vec2(d1,1.0) : vec2(d2,2.0);
}
vec2 castRay( in vec3 ro, in vec3 rd, int id )
{
float m = 0.0;
float t = 0.0;
const float tmax = 100.0;
for( int i=0; i<64 && t<tmax; i++ )
{
vec2 h = map( ro+rd*t, id );
if( h.x<0.001 ) break;
m = h.y;
t += h.x;
}
return (t<tmax) ? vec2(t,m) : vec2(0.0);
}
float calcSoftshadow( in vec3 ro, in vec3 rd, in int id)
{
float res = 1.0;
float t = 0.01;
for( int i=0; i<256; i++ )
{
float h = map( ro + rd*t, id ).x;
res = min( res, smoothstep(0.0,1.0,8.0*h/t) );
t += clamp( h, 0.005, 0.02 );
if( res<0.001 || t>5.0 ) break;
}
return clamp( res, 0.0, 1.0 );
}
vec3 calcNormal( in vec3 pos, in int id )
{
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy, id ).x +
e.yyx*map( pos + e.yyx, id ).x +
e.yxy*map( pos + e.yxy, id ).x +
e.xxx*map( pos + e.xxx, id ).x );
}
float calcAO( in vec3 pos, in vec3 nor, in int id )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float hr = 0.01 + 0.12*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = map( aopos, id).x;
occ += (hr-dd)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 2.0*occ, 0.0, 1.0 );
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
vec3 render( in vec3 ro, in vec3 rd, int id )
{
vec3 col = vec3(0.1);
vec2 res = castRay(ro,rd, id);
if( res.y>0.5 )
{
float t = res.x;
vec3 pos = ro + t*rd;
vec3 nor;
float occ;
// material
if( res.y>1.5 )
{
nor = vec3(0.0,1.0,0.0);
col = 0.05*vec3(1.0);
occ = 1.0;
col *= 0.8+0.2*checkersGradBox( pos.xz*2.0 );
}
else
{
nor = calcNormal( pos, id );
occ = 0.5+0.5*nor.y;
col = vec3(0.25,0.15,0.05);
}
// lighting
occ *= calcAO( pos, nor, id );
vec3 lig = normalize( vec3(-0.5, 1.0, 0.8) );
vec3 hal = normalize( lig-rd );
float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
dif *= calcSoftshadow( pos, lig, id );
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),32.0)*
dif *
(0.04 + 0.96*pow( clamp(1.0+dot(hal,rd),0.0,1.0), 5.0 ));
vec3 lin = vec3(0.0);
lin += 2.00*dif*vec3(3.30,2.50,2.00);
lin += 0.50*amb*vec3(0.30,0.60,1.50)*occ;
lin += 0.30*bac*vec3(0.40,0.30,0.25)*occ;
col = col*lin;
col += 2.00*spe*vec3(3.30,2.50,2.00);
//col = mix( col, vec3(0.1), 1.0-exp(-0.03*t) );
}
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera
vec3 ro = vec3( 1.0*cos(0.2*iTime), 0.12, 1.0*sin(0.2*iTime) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera-to-world transformation
vec3 cw = normalize(ta-ro);
vec3 cu = normalize( cross(cw,vec3(0.0, 1.0,0.0)) );
vec3 cv = ( cross(cu,cw) );
int id = (fragCoord.x>iResolution.x/2.0) ? 1 : 0;
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 fc = o + vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#else
vec2 fc = vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#endif
vec2 p = (-vec2(iResolution.x/2.0,iResolution.y) + 2.0*fc)/iResolution.y;
// ray direction
vec3 rd = normalize( p.x*cu + p.y*cv + 2.0*cw );
// render
vec3 col = render( ro, rd, id );
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
tot *= smoothstep( 1.0, 2.5, abs(fragCoord.x-iResolution.x/2.0) );
fragColor = vec4( tot, 1.0 );
} | mit | [
3174,
3214,
3261,
3261,
3317
] | [
[
1778,
1847,
1886,
1886,
2951
],
[
2955,
3039,
3083,
3083,
3172
],
[
3174,
3214,
3261,
3261,
3317
],
[
3389,
3389,
3420,
3437,
3670
],
[
3672,
3672,
3720,
3720,
3988
],
[
3991,
3991,
4049,
4049,
4336
],
[
4338,
4338,
4381,
4381,
4605
],
[
4607,
4607,
4660,
4660,
4961
],
[
4965,
5041,
5077,
5098,
5321
],
[
5323,
5323,
5370,
5370,
6862
]
] | // symmetric ellipsoid - EXACT distance
| float sdEllipsoidXXZ( in vec3 p, in vec2 r )
{ |
return sdEllipse( vec2( length(p.xy), p.z ), r );
} | // symmetric ellipsoid - EXACT distance
float sdEllipsoidXXZ( in vec3 p, in vec2 r )
{ | 1 | 1 |
tdS3DG | iq | 2019-02-11T07:14:34 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Computing the exact distance to a generic (non symmetric) ellipsoid
// requires solving a sixth degree equation, which can be difficult.
// Approximating the distance is easier though. This shaders shows one
// such approximation that produces better results than the naive
// distance bound. More info here:
//
// http://iquilezles.org/www/articles/ellipsoids/ellipsoids.htm
//
// Left, naive ellipsoid distance approximation (single square root)
// Right, improved approximation (two square roots).
//
// Note how the improved approximation produces a more accurate intersection
// for the same number of raymarching steps (specially noticeable in the first
// frame of the animation). Note also how the penumbra shadow estimation works
// best with since since it has a more eucliden distance as input.
//
// The technique is based on dividing the bad approximation's distance estimation
// by the length of its gradient to get a first order approximation to the true
// distance (see http://iquilezles.org/www/articles/distance/distance.htm)
// List of other 3D SDFs: https://www.shadertoy.com/playlist/43cXRl
//
// and http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
#define AA 2 // make this 3 is you have a fast computer
//------------------------------------------------------------------
// generic ellipsoid - simple but bad approximated distance
float sdEllipsoid_Bad( in vec3 p, in vec3 r )
{
return (length(p/r)-1.0)*min(min(r.x,r.y),r.z);
}
// generic ellipsoid - improved approximated distance
float sdEllipsoid( in vec3 p, in vec3 r )
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
//------------------------------------------------------------------
vec2 map( in vec3 p, int id )
{
// ellipsoid
float d1 = (id==0) ? sdEllipsoid_Bad( p, vec3(0.18,0.3,0.02) ) :
sdEllipsoid( p, vec3(0.18,0.3,0.02) );
// plane
float d2 = p.y+0.3;
return (d1<d2) ? vec2(d1,1.0) : vec2(d2,2.0);
}
vec2 castRay( in vec3 ro, in vec3 rd, int id )
{
float m = 0.0;
float t = 0.0;
const float tmax = 100.0;
for( int i=0; i<100 && t<tmax; i++ )
{
vec2 h = map( ro+rd*t, id );
if( h.x<0.001 ) break;
m = h.y;
t += h.x;
}
return (t<tmax) ? vec2(t,m) : vec2(0.0);
}
float calcSoftshadow( in vec3 ro, in vec3 rd, in int id)
{
float res = 1.0;
float t = 0.01;
for( int i=0; i<256; i++ )
{
float h = map( ro + rd*t, id ).x;
res = min( res, smoothstep(0.0,1.0,8.0*h/t ));
t += clamp( h, 0.005, 0.02 );
if( res<0.001 || t>5.0 ) break;
}
return clamp( res, 0.0, 1.0 );
}
vec3 calcNormal( in vec3 pos, in int id )
{
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy, id ).x +
e.yyx*map( pos + e.yyx, id ).x +
e.yxy*map( pos + e.yxy, id ).x +
e.xxx*map( pos + e.xxx, id ).x );
}
float calcAO( in vec3 pos, in vec3 nor, in int id )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float hr = 0.01 + 0.12*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = map( aopos, id).x;
occ += (hr-dd)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 2.0*occ, 0.0, 1.0 );
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
vec3 render( in vec3 ro, in vec3 rd, int id )
{
vec3 col = vec3(0.0);
vec2 res = castRay(ro,rd, id);
if( res.y>0.5 )
{
float t = res.x;
vec3 pos = ro + t*rd;
vec3 nor;
float occ;
// material
if( res.y>1.5 )
{
nor = vec3(0.0,1.0,0.0);
col = 0.05*vec3(1.0);
col *= 0.7+0.3*checkersGradBox( pos.xz*2.0 );
occ = 1.0;
}
else
{
nor = calcNormal( pos, id );
occ = 0.5+0.5*nor.y;
col = vec3(0.2);
}
// lighting
occ *= calcAO( pos, nor, id );
vec3 lig = normalize( vec3(-0.5, 1.9, 0.8) );
vec3 hal = normalize( lig-rd );
float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
float sha = calcSoftshadow( pos, lig, id );
sha = sha*sha;
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),32.0)*
dif * sha *
(0.04 + 0.96*pow( clamp(1.0+dot(hal,rd),0.0,1.0), 5.0 ));
//vec3 lin = vec3(0.0);
//lin += 2.00*dif*vec3(3.30,2.50,2.00)*sha;
//lin += 0.50*amb*vec3(0.30,0.60,1.50)*occ;
//lin += 0.30*bac*vec3(0.40,0.30,0.25)*occ;
//col = col*lin;
//col += 2.00*spe*vec3(3.30,2.50,2.00);
col *= 5.0;
col *= vec3(0.2,0.3,0.4)*amb*occ + 1.6*vec3(1.0,0.9,0.75)*dif*sha;
col += vec3(2.8,2.2,1.8)*spe*3.0;
//col = mix( col, vec3(0.1), 1.0-exp(-0.03*t) );
}
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera
vec3 ro = vec3( 1.0*cos(0.2*iTime), 0.12, 1.0*sin(0.2*iTime) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera-to-world transformation
vec3 cw = normalize(ta-ro);
vec3 cu = normalize( cross(cw,vec3(0.0, 1.0,0.0)) );
vec3 cv = ( cross(cu,cw) );
// scene selection
int id = (fragCoord.x>iResolution.x/2.0) ? 1 : 0;
// render
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 fc = o + vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#else
vec2 fc = vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#endif
vec2 p = (-vec2(iResolution.x/2.0,iResolution.y) + 2.0*fc)/iResolution.y;
// ray direction
vec3 rd = normalize( p.x*cu + p.y*cv + 2.0*cw );
// render
vec3 col = render( ro, rd, id );
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// separator
tot *= smoothstep( 1.0, 2.5, abs(fragCoord.x-iResolution.x/2.0) );
fragColor = vec4( tot, 1.0 );
} | mit | [
2405,
2465,
2513,
2513,
2567
] | [
[
2405,
2465,
2513,
2513,
2567
],
[
2570,
2624,
2668,
2668,
2757
],
[
2829,
2829,
2860,
2877,
3109
],
[
3111,
3111,
3159,
3159,
3428
],
[
3431,
3431,
3489,
3489,
3776
],
[
3778,
3778,
3821,
3821,
4045
],
[
4047,
4047,
4100,
4100,
4401
],
[
4404,
4480,
4516,
4537,
4760
],
[
4762,
4762,
4809,
4809,
6501
]
] | // generic ellipsoid - simple but bad approximated distance
| float sdEllipsoid_Bad( in vec3 p, in vec3 r )
{ |
return (length(p/r)-1.0)*min(min(r.x,r.y),r.z);
} | // generic ellipsoid - simple but bad approximated distance
float sdEllipsoid_Bad( in vec3 p, in vec3 r )
{ | 2 | 2 |
tdS3DG | iq | 2019-02-11T07:14:34 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Computing the exact distance to a generic (non symmetric) ellipsoid
// requires solving a sixth degree equation, which can be difficult.
// Approximating the distance is easier though. This shaders shows one
// such approximation that produces better results than the naive
// distance bound. More info here:
//
// http://iquilezles.org/www/articles/ellipsoids/ellipsoids.htm
//
// Left, naive ellipsoid distance approximation (single square root)
// Right, improved approximation (two square roots).
//
// Note how the improved approximation produces a more accurate intersection
// for the same number of raymarching steps (specially noticeable in the first
// frame of the animation). Note also how the penumbra shadow estimation works
// best with since since it has a more eucliden distance as input.
//
// The technique is based on dividing the bad approximation's distance estimation
// by the length of its gradient to get a first order approximation to the true
// distance (see http://iquilezles.org/www/articles/distance/distance.htm)
// List of other 3D SDFs: https://www.shadertoy.com/playlist/43cXRl
//
// and http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
#define AA 2 // make this 3 is you have a fast computer
//------------------------------------------------------------------
// generic ellipsoid - simple but bad approximated distance
float sdEllipsoid_Bad( in vec3 p, in vec3 r )
{
return (length(p/r)-1.0)*min(min(r.x,r.y),r.z);
}
// generic ellipsoid - improved approximated distance
float sdEllipsoid( in vec3 p, in vec3 r )
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
//------------------------------------------------------------------
vec2 map( in vec3 p, int id )
{
// ellipsoid
float d1 = (id==0) ? sdEllipsoid_Bad( p, vec3(0.18,0.3,0.02) ) :
sdEllipsoid( p, vec3(0.18,0.3,0.02) );
// plane
float d2 = p.y+0.3;
return (d1<d2) ? vec2(d1,1.0) : vec2(d2,2.0);
}
vec2 castRay( in vec3 ro, in vec3 rd, int id )
{
float m = 0.0;
float t = 0.0;
const float tmax = 100.0;
for( int i=0; i<100 && t<tmax; i++ )
{
vec2 h = map( ro+rd*t, id );
if( h.x<0.001 ) break;
m = h.y;
t += h.x;
}
return (t<tmax) ? vec2(t,m) : vec2(0.0);
}
float calcSoftshadow( in vec3 ro, in vec3 rd, in int id)
{
float res = 1.0;
float t = 0.01;
for( int i=0; i<256; i++ )
{
float h = map( ro + rd*t, id ).x;
res = min( res, smoothstep(0.0,1.0,8.0*h/t ));
t += clamp( h, 0.005, 0.02 );
if( res<0.001 || t>5.0 ) break;
}
return clamp( res, 0.0, 1.0 );
}
vec3 calcNormal( in vec3 pos, in int id )
{
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy, id ).x +
e.yyx*map( pos + e.yyx, id ).x +
e.yxy*map( pos + e.yxy, id ).x +
e.xxx*map( pos + e.xxx, id ).x );
}
float calcAO( in vec3 pos, in vec3 nor, in int id )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float hr = 0.01 + 0.12*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = map( aopos, id).x;
occ += (hr-dd)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 2.0*occ, 0.0, 1.0 );
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
vec3 render( in vec3 ro, in vec3 rd, int id )
{
vec3 col = vec3(0.0);
vec2 res = castRay(ro,rd, id);
if( res.y>0.5 )
{
float t = res.x;
vec3 pos = ro + t*rd;
vec3 nor;
float occ;
// material
if( res.y>1.5 )
{
nor = vec3(0.0,1.0,0.0);
col = 0.05*vec3(1.0);
col *= 0.7+0.3*checkersGradBox( pos.xz*2.0 );
occ = 1.0;
}
else
{
nor = calcNormal( pos, id );
occ = 0.5+0.5*nor.y;
col = vec3(0.2);
}
// lighting
occ *= calcAO( pos, nor, id );
vec3 lig = normalize( vec3(-0.5, 1.9, 0.8) );
vec3 hal = normalize( lig-rd );
float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
float sha = calcSoftshadow( pos, lig, id );
sha = sha*sha;
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),32.0)*
dif * sha *
(0.04 + 0.96*pow( clamp(1.0+dot(hal,rd),0.0,1.0), 5.0 ));
//vec3 lin = vec3(0.0);
//lin += 2.00*dif*vec3(3.30,2.50,2.00)*sha;
//lin += 0.50*amb*vec3(0.30,0.60,1.50)*occ;
//lin += 0.30*bac*vec3(0.40,0.30,0.25)*occ;
//col = col*lin;
//col += 2.00*spe*vec3(3.30,2.50,2.00);
col *= 5.0;
col *= vec3(0.2,0.3,0.4)*amb*occ + 1.6*vec3(1.0,0.9,0.75)*dif*sha;
col += vec3(2.8,2.2,1.8)*spe*3.0;
//col = mix( col, vec3(0.1), 1.0-exp(-0.03*t) );
}
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera
vec3 ro = vec3( 1.0*cos(0.2*iTime), 0.12, 1.0*sin(0.2*iTime) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera-to-world transformation
vec3 cw = normalize(ta-ro);
vec3 cu = normalize( cross(cw,vec3(0.0, 1.0,0.0)) );
vec3 cv = ( cross(cu,cw) );
// scene selection
int id = (fragCoord.x>iResolution.x/2.0) ? 1 : 0;
// render
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 fc = o + vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#else
vec2 fc = vec2( mod(fragCoord.x,iResolution.x/2.0), fragCoord.y);
#endif
vec2 p = (-vec2(iResolution.x/2.0,iResolution.y) + 2.0*fc)/iResolution.y;
// ray direction
vec3 rd = normalize( p.x*cu + p.y*cv + 2.0*cw );
// render
vec3 col = render( ro, rd, id );
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// separator
tot *= smoothstep( 1.0, 2.5, abs(fragCoord.x-iResolution.x/2.0) );
fragColor = vec4( tot, 1.0 );
} | mit | [
2570,
2624,
2668,
2668,
2757
] | [
[
2405,
2465,
2513,
2513,
2567
],
[
2570,
2624,
2668,
2668,
2757
],
[
2829,
2829,
2860,
2877,
3109
],
[
3111,
3111,
3159,
3159,
3428
],
[
3431,
3431,
3489,
3489,
3776
],
[
3778,
3778,
3821,
3821,
4045
],
[
4047,
4047,
4100,
4100,
4401
],
[
4404,
4480,
4516,
4537,
4760
],
[
4762,
4762,
4809,
4809,
6501
]
] | // generic ellipsoid - improved approximated distance
| float sdEllipsoid( in vec3 p, in vec3 r )
{ |
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
} | // generic ellipsoid - improved approximated distance
float sdEllipsoid( in vec3 p, in vec3 r )
{ | 6 | 7 |
3lsGD4 | tpfto | 2019-04-27T10:27:51 | // The MIT License
// Copyright © 2019 J. M.
// 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.
// Basic domain coloring plot (https://en.wikipedia.org/wiki/Domain_coloring)
// of the Lambert W function w = W(z) (https://en.wikipedia.org/wiki/Lambert_W_function)
#define PI 3.14159265359
#define EE 2.71828182846
#define SCALE 5.0 // plot scale
#define SPACING 0.0625 // grid line spacing
// from Fabrice Neyret,
#define cmul(A,B) ( mat2( A, -(A).y, (A).x ) * (B) )
#define cinv(Z) ( vec2( (Z).x, -(Z).y ) / dot(Z, Z) )
#define cdiv(A,B) cmul( A, cinv(B) )
// Schlick bias function, from http://dept-info.labri.u-bordeaux.fr/~schlick/DOC/gem2.ps.gz
float bias( float a, float x )
{
return x/((1.0/a - 2.0) * (1.0 - x) + 1.0);
}
// biased sawtooth
float my_saw( float x, float p )
{
float xs = mod(x, 1.0);
float xh = clamp(xs, 0.0, p);
return 0.5 + 0.5 * bias(0.95, xh) * (1.0 - smoothstep(p, 1.0, xs));
}
// modified version of Inigo Quilez's method at https://www.shadertoy.com/view/MsS3Wc
// using "rational smoothstep" from https://tpfto.wordpress.com/2019/03/28/on-a-rational-variant-of-smoothstep/
vec3 smooth_hue( float h )
{
vec3 rgb = clamp( abs(mod(6.0 * h + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
return rgb * rgb * rgb/(1.0 - 3.0 * rgb * (1.0 - rgb));
}
// modified DLMF coloring, adapted from https://www.shadertoy.com/view/WtXGWN
vec3 smooth_dlmf( float h )
{
vec3 rgb = clamp( vec3(1., -1., -1.) * abs((vec3(8., 4., 8.) * mod(h, 1.0) - vec3(4.5, 1.5, 5.5))) + vec3(-1.5, 1.5, 2.5), 0.0, 1.0 );
return rgb * rgb * rgb/(1.0 - 3.0 * rgb * (1.0 - rgb));
}
// complex square root, from Numerical Recipes
vec2 sqrtz( in vec2 z )
{
float ar = abs(z.x);
float ai = abs(z.y);
float w = (dot(z, z) == 0.0) ? 0.0 : ( (ar >= ai) ? (sqrt(ar) * sqrt(0.5 * (1.0 + sqrt(1.0 + (ai * ai)/(ar * ar))))) : (sqrt(ai) * sqrt(0.5 * (ar/ai + sqrt(1.0 + (ar * ar)/(ai * ai))))));
return ((w == 0.0) ? vec2(0.0) : ((z.x >= 0.0 ? vec2(w, 0.5 * z.y/w) : vec2(0.5*ai/w, ((z.y >= 0.0) ? w : -w)))));
}
// complex logarithm
vec2 logz( in vec2 z )
{
return vec2(log(length(z)), atan(z.y, z.x));
}
// Lambert W function, Winitzki's approximation (https://doi.org/10.1007/3-540-44839-X_82)
vec2 LambertW( in vec2 z )
{
vec2 v = sqrtz(2.0 * EE * z + 2.0);
vec2 w = 2.0 * logz(vec2(1.0, 0.0) + 0.8842 * v);
return cdiv(w - logz(vec2(1.0, 0.0) + 0.9294 * logz(vec2(1.0, 0.0) + 0.5106 * v)) - vec2(1.213, 0.0), vec2(1.0, 0.0) + cinv(w + vec2(4.69483568, 0.0)));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 aspect = iResolution.xy / iResolution.y;
vec2 z = ( fragCoord.xy / iResolution.y ) - 0.5 * aspect;
z *= SCALE;
vec2 w = LambertW(z);
float ph = atan(w.y, w.x);
float lm = log(0.0001 + length(w));
vec3 c = vec3(1.0);
c = smooth_dlmf(0.5 * (ph / PI));
// uncomment for HSV version
// c = smooth_hue(0.5 * (ph / PI));
c *= mix(1.0, my_saw((0.5 * (lm/PI))/SPACING, 0.95) * my_saw((0.5 * (ph / PI))/SPACING, 0.95), 0.5 + 0.5 * cos(iTime));
fragColor = vec4(c, 1.0);
} | mit | [
1537,
1629,
1661,
1661,
1711
] | [
[
1537,
1629,
1661,
1661,
1711
],
[
1713,
1732,
1766,
1766,
1902
],
[
1904,
2102,
2130,
2130,
2283
],
[
2285,
2363,
2392,
2392,
2590
],
[
2640,
2640,
2665,
2665,
3031
],
[
3055,
3055,
3079,
3079,
3127
],
[
3221,
3221,
3249,
3249,
3503
],
[
3505,
3505,
3562,
3562,
4090
]
] | // Schlick bias function, from http://dept-info.labri.u-bordeaux.fr/~schlick/DOC/gem2.ps.gz
| float bias( float a, float x )
{ |
return x/((1.0/a - 2.0) * (1.0 - x) + 1.0);
} | // Schlick bias function, from http://dept-info.labri.u-bordeaux.fr/~schlick/DOC/gem2.ps.gz
float bias( float a, float x )
{ | 6 | 6 |
3lsGD4 | tpfto | 2019-04-27T10:27:51 | // The MIT License
// Copyright © 2019 J. M.
// 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.
// Basic domain coloring plot (https://en.wikipedia.org/wiki/Domain_coloring)
// of the Lambert W function w = W(z) (https://en.wikipedia.org/wiki/Lambert_W_function)
#define PI 3.14159265359
#define EE 2.71828182846
#define SCALE 5.0 // plot scale
#define SPACING 0.0625 // grid line spacing
// from Fabrice Neyret,
#define cmul(A,B) ( mat2( A, -(A).y, (A).x ) * (B) )
#define cinv(Z) ( vec2( (Z).x, -(Z).y ) / dot(Z, Z) )
#define cdiv(A,B) cmul( A, cinv(B) )
// Schlick bias function, from http://dept-info.labri.u-bordeaux.fr/~schlick/DOC/gem2.ps.gz
float bias( float a, float x )
{
return x/((1.0/a - 2.0) * (1.0 - x) + 1.0);
}
// biased sawtooth
float my_saw( float x, float p )
{
float xs = mod(x, 1.0);
float xh = clamp(xs, 0.0, p);
return 0.5 + 0.5 * bias(0.95, xh) * (1.0 - smoothstep(p, 1.0, xs));
}
// modified version of Inigo Quilez's method at https://www.shadertoy.com/view/MsS3Wc
// using "rational smoothstep" from https://tpfto.wordpress.com/2019/03/28/on-a-rational-variant-of-smoothstep/
vec3 smooth_hue( float h )
{
vec3 rgb = clamp( abs(mod(6.0 * h + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
return rgb * rgb * rgb/(1.0 - 3.0 * rgb * (1.0 - rgb));
}
// modified DLMF coloring, adapted from https://www.shadertoy.com/view/WtXGWN
vec3 smooth_dlmf( float h )
{
vec3 rgb = clamp( vec3(1., -1., -1.) * abs((vec3(8., 4., 8.) * mod(h, 1.0) - vec3(4.5, 1.5, 5.5))) + vec3(-1.5, 1.5, 2.5), 0.0, 1.0 );
return rgb * rgb * rgb/(1.0 - 3.0 * rgb * (1.0 - rgb));
}
// complex square root, from Numerical Recipes
vec2 sqrtz( in vec2 z )
{
float ar = abs(z.x);
float ai = abs(z.y);
float w = (dot(z, z) == 0.0) ? 0.0 : ( (ar >= ai) ? (sqrt(ar) * sqrt(0.5 * (1.0 + sqrt(1.0 + (ai * ai)/(ar * ar))))) : (sqrt(ai) * sqrt(0.5 * (ar/ai + sqrt(1.0 + (ar * ar)/(ai * ai))))));
return ((w == 0.0) ? vec2(0.0) : ((z.x >= 0.0 ? vec2(w, 0.5 * z.y/w) : vec2(0.5*ai/w, ((z.y >= 0.0) ? w : -w)))));
}
// complex logarithm
vec2 logz( in vec2 z )
{
return vec2(log(length(z)), atan(z.y, z.x));
}
// Lambert W function, Winitzki's approximation (https://doi.org/10.1007/3-540-44839-X_82)
vec2 LambertW( in vec2 z )
{
vec2 v = sqrtz(2.0 * EE * z + 2.0);
vec2 w = 2.0 * logz(vec2(1.0, 0.0) + 0.8842 * v);
return cdiv(w - logz(vec2(1.0, 0.0) + 0.9294 * logz(vec2(1.0, 0.0) + 0.5106 * v)) - vec2(1.213, 0.0), vec2(1.0, 0.0) + cinv(w + vec2(4.69483568, 0.0)));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 aspect = iResolution.xy / iResolution.y;
vec2 z = ( fragCoord.xy / iResolution.y ) - 0.5 * aspect;
z *= SCALE;
vec2 w = LambertW(z);
float ph = atan(w.y, w.x);
float lm = log(0.0001 + length(w));
vec3 c = vec3(1.0);
c = smooth_dlmf(0.5 * (ph / PI));
// uncomment for HSV version
// c = smooth_hue(0.5 * (ph / PI));
c *= mix(1.0, my_saw((0.5 * (lm/PI))/SPACING, 0.95) * my_saw((0.5 * (ph / PI))/SPACING, 0.95), 0.5 + 0.5 * cos(iTime));
fragColor = vec4(c, 1.0);
} | mit | [
1713,
1732,
1766,
1766,
1902
] | [
[
1537,
1629,
1661,
1661,
1711
],
[
1713,
1732,
1766,
1766,
1902
],
[
1904,
2102,
2130,
2130,
2283
],
[
2285,
2363,
2392,
2392,
2590
],
[
2640,
2640,
2665,
2665,
3031
],
[
3055,
3055,
3079,
3079,
3127
],
[
3221,
3221,
3249,
3249,
3503
],
[
3505,
3505,
3562,
3562,
4090
]
] | // biased sawtooth
| float my_saw( float x, float p )
{ |
float xs = mod(x, 1.0);
float xh = clamp(xs, 0.0, p);
return 0.5 + 0.5 * bias(0.95, xh) * (1.0 - smoothstep(p, 1.0, xs));
} | // biased sawtooth
float my_saw( float x, float p )
{ | 6 | 6 |
3lsGD4 | tpfto | 2019-04-27T10:27:51 | // The MIT License
// Copyright © 2019 J. M.
// 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.
// Basic domain coloring plot (https://en.wikipedia.org/wiki/Domain_coloring)
// of the Lambert W function w = W(z) (https://en.wikipedia.org/wiki/Lambert_W_function)
#define PI 3.14159265359
#define EE 2.71828182846
#define SCALE 5.0 // plot scale
#define SPACING 0.0625 // grid line spacing
// from Fabrice Neyret,
#define cmul(A,B) ( mat2( A, -(A).y, (A).x ) * (B) )
#define cinv(Z) ( vec2( (Z).x, -(Z).y ) / dot(Z, Z) )
#define cdiv(A,B) cmul( A, cinv(B) )
// Schlick bias function, from http://dept-info.labri.u-bordeaux.fr/~schlick/DOC/gem2.ps.gz
float bias( float a, float x )
{
return x/((1.0/a - 2.0) * (1.0 - x) + 1.0);
}
// biased sawtooth
float my_saw( float x, float p )
{
float xs = mod(x, 1.0);
float xh = clamp(xs, 0.0, p);
return 0.5 + 0.5 * bias(0.95, xh) * (1.0 - smoothstep(p, 1.0, xs));
}
// modified version of Inigo Quilez's method at https://www.shadertoy.com/view/MsS3Wc
// using "rational smoothstep" from https://tpfto.wordpress.com/2019/03/28/on-a-rational-variant-of-smoothstep/
vec3 smooth_hue( float h )
{
vec3 rgb = clamp( abs(mod(6.0 * h + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
return rgb * rgb * rgb/(1.0 - 3.0 * rgb * (1.0 - rgb));
}
// modified DLMF coloring, adapted from https://www.shadertoy.com/view/WtXGWN
vec3 smooth_dlmf( float h )
{
vec3 rgb = clamp( vec3(1., -1., -1.) * abs((vec3(8., 4., 8.) * mod(h, 1.0) - vec3(4.5, 1.5, 5.5))) + vec3(-1.5, 1.5, 2.5), 0.0, 1.0 );
return rgb * rgb * rgb/(1.0 - 3.0 * rgb * (1.0 - rgb));
}
// complex square root, from Numerical Recipes
vec2 sqrtz( in vec2 z )
{
float ar = abs(z.x);
float ai = abs(z.y);
float w = (dot(z, z) == 0.0) ? 0.0 : ( (ar >= ai) ? (sqrt(ar) * sqrt(0.5 * (1.0 + sqrt(1.0 + (ai * ai)/(ar * ar))))) : (sqrt(ai) * sqrt(0.5 * (ar/ai + sqrt(1.0 + (ar * ar)/(ai * ai))))));
return ((w == 0.0) ? vec2(0.0) : ((z.x >= 0.0 ? vec2(w, 0.5 * z.y/w) : vec2(0.5*ai/w, ((z.y >= 0.0) ? w : -w)))));
}
// complex logarithm
vec2 logz( in vec2 z )
{
return vec2(log(length(z)), atan(z.y, z.x));
}
// Lambert W function, Winitzki's approximation (https://doi.org/10.1007/3-540-44839-X_82)
vec2 LambertW( in vec2 z )
{
vec2 v = sqrtz(2.0 * EE * z + 2.0);
vec2 w = 2.0 * logz(vec2(1.0, 0.0) + 0.8842 * v);
return cdiv(w - logz(vec2(1.0, 0.0) + 0.9294 * logz(vec2(1.0, 0.0) + 0.5106 * v)) - vec2(1.213, 0.0), vec2(1.0, 0.0) + cinv(w + vec2(4.69483568, 0.0)));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 aspect = iResolution.xy / iResolution.y;
vec2 z = ( fragCoord.xy / iResolution.y ) - 0.5 * aspect;
z *= SCALE;
vec2 w = LambertW(z);
float ph = atan(w.y, w.x);
float lm = log(0.0001 + length(w));
vec3 c = vec3(1.0);
c = smooth_dlmf(0.5 * (ph / PI));
// uncomment for HSV version
// c = smooth_hue(0.5 * (ph / PI));
c *= mix(1.0, my_saw((0.5 * (lm/PI))/SPACING, 0.95) * my_saw((0.5 * (ph / PI))/SPACING, 0.95), 0.5 + 0.5 * cos(iTime));
fragColor = vec4(c, 1.0);
} | mit | [
2285,
2363,
2392,
2392,
2590
] | [
[
1537,
1629,
1661,
1661,
1711
],
[
1713,
1732,
1766,
1766,
1902
],
[
1904,
2102,
2130,
2130,
2283
],
[
2285,
2363,
2392,
2392,
2590
],
[
2640,
2640,
2665,
2665,
3031
],
[
3055,
3055,
3079,
3079,
3127
],
[
3221,
3221,
3249,
3249,
3503
],
[
3505,
3505,
3562,
3562,
4090
]
] | // modified DLMF coloring, adapted from https://www.shadertoy.com/view/WtXGWN
| vec3 smooth_dlmf( float h )
{ |
vec3 rgb = clamp( vec3(1., -1., -1.) * abs((vec3(8., 4., 8.) * mod(h, 1.0) - vec3(4.5, 1.5, 5.5))) + vec3(-1.5, 1.5, 2.5), 0.0, 1.0 );
return rgb * rgb * rgb/(1.0 - 3.0 * rgb * (1.0 - rgb));
} | // modified DLMF coloring, adapted from https://www.shadertoy.com/view/WtXGWN
vec3 smooth_dlmf( float h )
{ | 10 | 10 |
3s2SRV | iq | 2019-04-04T11:39:08 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Bounding box of a capsule
// Other capsule functions:
//
// Capsule intersection: https://www.shadertoy.com/view/Xt3SzX
// Capsule bounding box: https://www.shadertoy.com/view/3s2SRV
// Capsule distance: https://www.shadertoy.com/view/Xds3zN
// Capsule occlusion: https://www.shadertoy.com/view/llGyzG
// Other bounding box functions:
//
// Disk - 3D BBox : https://www.shadertoy.com/view/ll3Xzf
// Cylinder - 3D BBox : https://www.shadertoy.com/view/MtcXRf
// Ellipse - 3D BBox : https://www.shadertoy.com/view/Xtjczw
// Cone - 3D BBox : https://www.shadertoy.com/view/WdjSRK
// Capsule - 3D Bbox : https://www.shadertoy.com/view/3s2SRV
// Cubic Bezier - 2D BBox : https://www.shadertoy.com/view/XdVBWd
// Quadratic Bezier - 3D BBox : https://www.shadertoy.com/view/ldj3Wh
// Quadratic Bezier - 2D BBox : https://www.shadertoy.com/view/lsyfWc
#define AA 3
struct bound3
{
vec3 mMin;
vec3 mMax;
};
//---------------------------------------------------------------------------------------
// bounding box for a capsule
//---------------------------------------------------------------------------------------
bound3 CapsuleAABB( in vec3 pa, in vec3 pb, in float ra )
{
vec3 a = pb - pa;
return bound3( min( pa - ra, pb - ra ),
max( pa + ra, pb + ra ) );
}
// http://www.iquilezles.org/www/articles/intersectors/intersectors.htm
float iCapsule( in vec3 ro, in vec3 rd, in vec3 pa, in vec3 pb, in float r )
{
vec3 ba = pb - pa;
vec3 oa = ro - pa;
float baba = dot(ba,ba);
float bard = dot(ba,rd);
float baoa = dot(ba,oa);
float rdoa = dot(rd,oa);
float oaoa = dot(oa,oa);
float a = baba - bard*bard;
float b = baba*rdoa - baoa*bard;
float c = baba*oaoa - baoa*baoa - r*r*baba;
float h = b*b - a*c;
if( h>=0.0 )
{
float t = (-b-sqrt(h))/a;
float y = baoa + t*bard;
// body
if( y>0.0 && y<baba ) return t;
// caps
vec3 oc = (y<=0.0) ? oa : ro - pb;
b = dot(rd,oc);
c = dot(oc,oc) - r*r;
h = b*b - c;
if( h>0.0 )
{
return -b - sqrt(h);
}
}
return -1.0;
}
// compute normal
vec3 capNormal( in vec3 pos, in vec3 a, in vec3 b, in float r )
{
vec3 ba = b - a;
vec3 pa = pos - a;
float h = clamp(dot(pa,ba)/dot(ba,ba),0.0,1.0);
return (pa - h*ba)/r;
}
// ray-box intersection
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
vec3 m = 1.0/rd;
vec3 n = m*(ro-cen);
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
}
float hash1( in vec2 p )
{
return fract(sin(dot(p, vec2(12.9898, 78.233)))*43758.5453);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// camera position
vec3 ro = vec3( -0.5, 0.4, 1.5 );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// cylidner animation
vec3 c_a = 0.2 + 0.3*sin(iTime*vec3(1.11,1.27,1.47)+vec3(2.0,5.0,6.0));
vec3 c_b = -0.2 + 0.3*sin(iTime*vec3(1.23,1.41,1.07)+vec3(0.0,1.0,3.0));
float c_ra = 0.3 + 0.2*sin(iTime*1.3+0.5);
// render
vec3 col = vec3(0.4)*(1.0-0.3*length(p));
// raytrace
float t = iCapsule( ro, rd, c_a, c_b, c_ra );
float tmin = 1e10;
if( t>0.0 )
{
tmin = t;
// shading/lighting
vec3 pos = ro + t*rd;
vec3 nor = capNormal( pos, c_a, c_b, c_ra );
float dif = clamp( dot(nor,vec3(0.5,0.7,0.2)), 0.0, 1.0 );
float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0));
col = sqrt( vec3(0.2,0.3,0.4)*amb + vec3(0.8,0.7,0.5)*dif );
col *= vec3(1.0,0.75,0.3);
}
// compute bounding box of cylinder
bound3 bbox = CapsuleAABB( c_a, c_b, c_ra );
// raytrace bounding box
vec3 bcen = 0.5*(bbox.mMin+bbox.mMax);
vec3 brad = 0.5*(bbox.mMax-bbox.mMin);
vec2 tbox = iBox( ro, rd, bcen, brad );
if( tbox.x>0.0 )
{
// back face
if( tbox.y < tmin )
{
vec3 pos = ro + rd*tbox.y;
vec3 e = smoothstep( brad-0.03, brad-0.02, abs(pos-bcen) );
float al = 1.0 - (1.0-e.x*e.y)*(1.0-e.y*e.z)*(1.0-e.z*e.x);
col = mix( col, vec3(0.0), 0.25 + 0.75*al );
}
// front face
if( tbox.x < tmin )
{
vec3 pos = ro + rd*tbox.x;
vec3 e = smoothstep( brad-0.03, brad-0.02, abs(pos-bcen) );
float al = 1.0 - (1.0-e.x*e.y)*(1.0-e.y*e.z)*(1.0-e.z*e.x);
col = mix( col, vec3(0.0), 0.15 + 0.85*al );
}
}
// no gamma required here, it's done in line 118
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// dithering
tot += ((hash1(fragCoord.xy)+hash1(fragCoord.yx+13.1))/2.0 - 0.5)/256.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
2452,
2524,
2602,
2602,
3333
] | [
[
2062,
2272,
2331,
2331,
2450
],
[
2452,
2524,
2602,
2602,
3333
],
[
3335,
3353,
3418,
3418,
3544
],
[
3547,
3571,
3635,
3635,
3915
],
[
3918,
3918,
3944,
3944,
4011
]
] | // http://www.iquilezles.org/www/articles/intersectors/intersectors.htm
| float iCapsule( in vec3 ro, in vec3 rd, in vec3 pa, in vec3 pb, in float r )
{ |
vec3 ba = pb - pa;
vec3 oa = ro - pa;
float baba = dot(ba,ba);
float bard = dot(ba,rd);
float baoa = dot(ba,oa);
float rdoa = dot(rd,oa);
float oaoa = dot(oa,oa);
float a = baba - bard*bard;
float b = baba*rdoa - baoa*bard;
float c = baba*oaoa - baoa*baoa - r*r*baba;
float h = b*b - a*c;
if( h>=0.0 )
{
float t = (-b-sqrt(h))/a;
float y = baoa + t*bard;
// body
if( y>0.0 && y<baba ) return t;
// caps
vec3 oc = (y<=0.0) ? oa : ro - pb;
b = dot(rd,oc);
c = dot(oc,oc) - r*r;
h = b*b - c;
if( h>0.0 )
{
return -b - sqrt(h);
}
}
return -1.0;
} | // http://www.iquilezles.org/www/articles/intersectors/intersectors.htm
float iCapsule( in vec3 ro, in vec3 rd, in vec3 pa, in vec3 pb, in float r )
{ | 3 | 3 |
tsSXzK | iq | 2019-04-04T06:43:51 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Euclidean distance to a capped cone. Uses only two square roots instead of
// three like the naive implementation.
//
//
// Other cone functions:
//
// Cone bbox: https://www.shadertoy.com/view/WdjSRK
// Cone distance: https://www.shadertoy.com/view/tsSXzK
// Cone intersection: https://www.shadertoy.com/view/llcfRf
//
//
// List of other 3D SDFs: https://www.shadertoy.com/playlist/43cXRl
//
// and http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
// http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{
float rba = rb-ra;
float baba = dot(b-a,b-a);
float papa = dot(p-a,p-a);
float paba = dot(p-a,b-a)/baba;
float x = sqrt( papa - paba*paba*baba );
float cax = max(0.0,x-((paba<0.5)?ra:rb));
float cay = abs(paba-0.5)-0.5;
float k = rba*rba + baba;
float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 );
float cbx = x-ra - f*rba;
float cby = paba - f;
float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(cax*cax + cay*cay*baba,
cbx*cbx + cby*cby*baba) );
}
float map( in vec3 pos )
{
return sdCone(pos, vec3(-0.15,-0.2,-0.1), vec3(0.2,0.2,0.1), 0.4, 0.1 );
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps ) +
e.yyx*map( pos + e.yyx*eps ) +
e.yxy*map( pos + e.yxy*eps ) +
e.xxx*map( pos + e.xxx*eps ) );
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.5*(iTime-10.0);
vec3 ro = vec3( 1.0*cos(an), 0.4, 1.0*sin(an) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// raymarch
const float tmax = 5.0;
float t = 0.0;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
float h = map(pos);
if( h<0.0001 || t>tmax ) break;
t += h;
}
// shading/lighting
vec3 col = vec3(0.0);
if( t<tmax )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal(pos);
float dif = clamp( dot(nor,vec3(0.57703)), 0.0, 1.0 );
float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0));
col = vec3(0.2,0.3,0.4)*amb + vec3(0.8,0.7,0.5)*dif;
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
1563,
1633,
1691,
1691,
2251
] | [
[
1563,
1633,
1691,
1691,
2251
],
[
2254,
2254,
2280,
2280,
2359
],
[
2361,
2425,
2457,
2457,
2696
]
] | // http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
| float sdCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{ |
float rba = rb-ra;
float baba = dot(b-a,b-a);
float papa = dot(p-a,p-a);
float paba = dot(p-a,b-a)/baba;
float x = sqrt( papa - paba*paba*baba );
float cax = max(0.0,x-((paba<0.5)?ra:rb));
float cay = abs(paba-0.5)-0.5;
float k = rba*rba + baba;
float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 );
float cbx = x-ra - f*rba;
float cby = paba - f;
float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(cax*cax + cay*cay*baba,
cbx*cbx + cby*cby*baba) );
} | // http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{ | 5 | 5 |
MlGczG | iq | 2019-05-16T12:05:45 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Fake/approximated soft shadow for a capsule.
// Other capsule functions:
//
// Capsule intersection: https://www.shadertoy.com/view/Xt3SzX
// Capsule bounding box: https://www.shadertoy.com/view/3s2SRV
// Capsule distance: https://www.shadertoy.com/view/Xds3zN
// Capsule occlusion: https://www.shadertoy.com/view/llGyzG
// Other soft-shadow functions:
//
// Sphere: https://www.shadertoy.com/view/4d2XWV
// Ellipsoid: https://www.shadertoy.com/view/llsSzn
// Box: https://www.shadertoy.com/view/WslGz4
// Capsule: https://www.shadertoy.com/view/MlGczG
#define AA 3
//================================================================
// fakesoft shadow occlusion
float capShadow( in vec3 ro, in vec3 rd, in vec3 a, in vec3 b, in float r, in float k )
{
vec3 ba = b - a;
vec3 oa = ro - a;
// closest distance between ray and segment
#if 1
// naive way to solve the 2x2 system of equations
float oad = dot( oa, rd );
float dba = dot( rd, ba );
float baba = dot( ba, ba );
float oaba = dot( oa, ba );
vec2 th = vec2( -oad*baba + dba*oaba, oaba - oad*dba ) / (baba - dba*dba);
#else
// fizzer's way to solve the 2x2 system of equations
vec3 th = inverse(mat3(-rd,ba,cross(rd,ba))) * oa;
#endif
th.x = max( th.x, 0.0001 );
th.y = clamp( th.y, 0.0, 1.0 );
vec3 p = a + ba*th.y;
vec3 q = ro + rd*th.x;
float d = length( p-q )-r;
// fake shadow
float s = clamp( k*d/th.x+0.5, 0.0, 1.0 );
return s*s*(3.0-2.0*s);
}
//================================================================
// intersect capsule
float capIntersect( in vec3 ro, in vec3 rd, in vec3 pa, in vec3 pb, in float r )
{
vec3 ba = pb - pa;
vec3 oa = ro - pa;
float baba = dot(ba,ba);
float bard = dot(ba,rd);
float baoa = dot(ba,oa);
float rdoa = dot(rd,oa);
float oaoa = dot(oa,oa);
float a = baba - bard*bard;
float b = baba*rdoa - baoa*bard;
float c = baba*oaoa - baoa*baoa - r*r*baba;
float h = b*b - a*c;
if( h>=0.0 )
{
float t = (-b-sqrt(h))/a;
float y = baoa + t*bard;
// body
if( y>0.0 && y<baba ) return t;
// caps
vec3 oc = (y<=0.0) ? oa : ro - pb;
b = dot(rd,oc);
c = dot(oc,oc) - r*r;
h = b*b - c;
if( h>0.0 )
{
return -b - sqrt(h);
}
}
return -1.0;
}
// compute normal
vec3 capNormal( in vec3 pos, in vec3 a, in vec3 b, in float r )
{
vec3 ba = b - a;
vec3 pa = pos - a;
float h = clamp(dot(pa,ba)/dot(ba,ba),0.0,1.0);
return (pa - h*ba)/r;
}
// fake occlusion
float capOcclusion( in vec3 p, in vec3 n, in vec3 a, in vec3 b, in float r )
{
vec3 ba = b - a, pa = p - a;
float h = clamp(dot(pa,ba)/dot(ba,ba),0.0,1.0);
vec3 d = pa - h*ba;
float l = length(d);
float o = 1.0 - max(0.0,dot(-d,n))*r*r/(l*l*l);
return sqrt(o*o*o);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.5*iTime;
vec3 ro = vec3( 1.0*cos(an), 0.4, 1.0*sin(an) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
vec3 tot = vec3(0.0);
#if AA>1
#define ZERO min(iFrame,0)
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
const vec3 capA = vec3(-0.3,-0.1,-0.1);
const vec3 capB = vec3(0.3,0.1,0.4);
const float capR = 0.2;
vec3 col = vec3(0.0);
const vec3 lig = normalize(vec3(-0.8,0.8,0.2));
float tmin = 1e20;
float sha = 1.0;
float occ = 1.0;
vec3 nor;
// plane (floor)
{
float t = (-0.3-ro.y)/rd.y;
if( t>0.0 && t<tmin )
{
tmin = t;
vec3 pos = ro + t*rd;
nor = vec3(0.0,1.0,0.0);
// fake soft shadow!
sha = capShadow( pos+0.001*nor, lig, capA, capB, capR, 4.0 );
// fake occlusion
occ = capOcclusion( pos, nor, capA, capB, capR );
}
}
// capsule
{
float t = capIntersect( ro, rd, capA, capB, capR );
if( t>0.0 && t<tmin )
{
tmin = t;
vec3 pos = ro + t*rd;
nor = capNormal(pos, capA, capB, capR );
occ = 0.5 + 0.5*nor.y;
sha = 1.0;
}
}
// lighting
if( tmin<1e19 )
{
float dif = clamp( dot(nor,lig), 0.0, 1.0 )*sha;
float amb = 1.0*occ;
col = vec3(0.2,0.3,0.4)*amb;
col += vec3(0.7,0.6,0.5)*dif*0.8;
}
tot += sqrt( col );
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
1739,
1768,
1857,
1857,
2579
] | [
[
1739,
1768,
1857,
1857,
2579
],
[
2582,
2670,
2752,
2752,
3483
],
[
3485,
3503,
3568,
3568,
3694
],
[
3697,
3715,
3793,
3793,
4007
]
] | // fakesoft shadow occlusion
| float capShadow( in vec3 ro, in vec3 rd, in vec3 a, in vec3 b, in float r, in float k )
{ |
vec3 ba = b - a;
vec3 oa = ro - a;
// closest distance between ray and segment
#if 1
// naive way to solve the 2x2 system of equations
float oad = dot( oa, rd );
float dba = dot( rd, ba );
float baba = dot( ba, ba );
float oaba = dot( oa, ba );
vec2 th = vec2( -oad*baba + dba*oaba, oaba - oad*dba ) / (baba - dba*dba);
#else
// fizzer's way to solve the 2x2 system of equations
vec3 th = inverse(mat3(-rd,ba,cross(rd,ba))) * oa;
#endif
th.x = max( th.x, 0.0001 );
th.y = clamp( th.y, 0.0, 1.0 );
vec3 p = a + ba*th.y;
vec3 q = ro + rd*th.x;
float d = length( p-q )-r;
// fake shadow
float s = clamp( k*d/th.x+0.5, 0.0, 1.0 );
return s*s*(3.0-2.0*s);
} | // fakesoft shadow occlusion
float capShadow( in vec3 ro, in vec3 rd, in vec3 a, in vec3 b, in float r, in float k )
{ | 1 | 1 |
Wt2GRh | eliemichel | 2019-05-23T01:27:45 | // Released under the MIT licence
// Copyright (c) 2019 - Alt144 (Élie Michel)
// Study around the snake() function, to build uv-space shaped like a snake,
// where each section is half a circle.
#define PI 3.141593
float pbeat(float t, float p)
{
return pow(1. - fract(t), p);
}
mat2 rot(float t)
{
float s = sin(t);
float c = cos(t);
return mat2(c, s, -s, c);
}
float fill(float d)
{
return smoothstep(.01, .0, d);
}
float sat(float x) { return clamp(x, 0., 1.); }
float triangle(vec2 uv, float radius)
{
return min(
fill(abs(uv.y) + (uv.x - radius) * tan(PI/6.)),
fill(-uv.x - radius)
);
}
float pattern(vec2 uv)
{
vec2 udx = floor(uv*5.);
uv.y += mod(udx.x, 2.)/5. * 0.5;
udx = floor(uv*5.);
vec2 guv = fract(uv*5.)-.5;
guv = rot(0.) * guv;
return triangle(guv, mix(0.25, 0.45, pbeat(iTime + udx.x*.01, 10.0)));
}
float lstep(float a, float b, float x)
{
return (x - a) / (b - a);
}
vec2 ring(vec2 uv, float innerRadius, float outerRadius)
{
float a = atan(uv.y, uv.x);
float r = length(uv);
uv = vec2(a / PI, sat(smoothstep(innerRadius, outerRadius, r)));
return uv;
}
/**
* uv: us-space to deform
* rad1: Radius of the top arcs
* rad2: Radius of the bottom arcs
* th: Thickness of the snake
*/
vec2 snake(vec2 uv, float rad1, float rad2, float th)
{
float radsum = rad1 + th + rad2;
vec2 uv0 = uv;
vec2 uv2 = uv;
uv.x = mod(uv.x - radsum, 2. * radsum) - radsum;
uv2.x = mod(uv2.x, 2. * radsum) - radsum;
uv = ring(uv, rad1, rad1 + th);
uv2 = ring(uv2 * vec2(-1.,1.), rad2, rad2 + th);
float mid = (rad2+th/2.)/radsum;
uv2.x = mix(0.0, mid, lstep(-1.0, 0.0, uv2.x));
uv.x = mix(mid, 1.0, lstep(0.0, 1.0, uv.x));
uv2.y = 1. - uv2.y;
vec2 uv3 = mix(uv, uv2, step(0., -uv0.y));
uv = mix(uv, uv2, step(0., -uv0.y));
return uv;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord.xy * 2. - iResolution.xy)/iResolution.x;
vec2 uv0 = uv;
float rad = 0.3;
float th = 0.1;
uv = snake(uv, rad - th, rad, th);
uv.x *= 4./5.;
float ss = 0.05;
float t = mix(0., 1., fract(iTime * .5));
float test = step(t,uv.x) * step(uv.x, t+ss);
test *= step(.1, uv.y) * step(uv.y, 0.9);
vec2 tuv = uv - vec2(iTime*0.1,0.);
vec3 col = vec3(0., pattern(tuv), test);
col = vec3(1.,1.,.9);
col = mix(col, vec3(.95, 0.15, 0.1), fill((abs(fract(tuv.x*5.-.05)-0.5)-0.15)*2.0));
col = mix(col, vec3(.9, 0.7, 0.2), fill((abs(fract(tuv.x*5.+.05)-0.5)-0.15)*2.0));
col = mix(col, vec3(.05, 0.35, 0.9), fill((abs(fract(tuv.x*5.)-0.5)-0.15)*2.0));
col = mix(col, vec3(.1), pattern(tuv * vec2(15.,1.)) * step(.001, tuv.y) * step(tuv.y, .999));
float o = 0.07;
uv = snake(uv0, rad-th - o, rad + o, th);
col = mix(col, vec3(.1), fill(max(.45 - uv.y, uv.y - .55) * 0.35));
o = -o;
uv = snake(uv0, rad-th - o, rad + o, th);
col = mix(col, vec3(.1), fill(max(.45 - uv.y, uv.y - .55) * 0.35));
o -= 0.01;
uv = snake(uv0, rad-th - o, rad + o, th);
//col = mix(col, vec3(.1), fill((.55 - uv.y) * 0.35));
fragColor = vec4(col, 1.0);
}
| mit | [
1170,
1301,
1356,
1356,
1897
] | [
[
219,
219,
250,
250,
286
],
[
288,
288,
307,
307,
383
],
[
385,
385,
406,
406,
443
],
[
445,
445,
465,
465,
492
],
[
494,
494,
533,
533,
640
],
[
642,
642,
666,
666,
890
],
[
892,
892,
932,
932,
964
],
[
966,
966,
1024,
1024,
1168
],
[
1170,
1301,
1356,
1356,
1897
],
[
1899,
1899,
1956,
1956,
3241
]
] | /**
* uv: us-space to deform
* rad1: Radius of the top arcs
* rad2: Radius of the bottom arcs
* th: Thickness of the snake
*/
| vec2 snake(vec2 uv, float rad1, float rad2, float th)
{ |
float radsum = rad1 + th + rad2;
vec2 uv0 = uv;
vec2 uv2 = uv;
uv.x = mod(uv.x - radsum, 2. * radsum) - radsum;
uv2.x = mod(uv2.x, 2. * radsum) - radsum;
uv = ring(uv, rad1, rad1 + th);
uv2 = ring(uv2 * vec2(-1.,1.), rad2, rad2 + th);
float mid = (rad2+th/2.)/radsum;
uv2.x = mix(0.0, mid, lstep(-1.0, 0.0, uv2.x));
uv.x = mix(mid, 1.0, lstep(0.0, 1.0, uv.x));
uv2.y = 1. - uv2.y;
vec2 uv3 = mix(uv, uv2, step(0., -uv0.y));
uv = mix(uv, uv2, step(0., -uv0.y));
return uv;
} | /**
* uv: us-space to deform
* rad1: Radius of the top arcs
* rad2: Radius of the bottom arcs
* th: Thickness of the snake
*/
vec2 snake(vec2 uv, float rad1, float rad2, float th)
{ | 1 | 1 |
3l23RK | iq | 2019-06-11T14:16:47 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
// and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
// c is the sin/cos of the angle. r is the radius
float sdPie( in vec2 p, in vec2 c, in float r )
{
p.x = abs(p.x);
float l = length(p) - r;
float m = length(p - c*clamp(dot(p,c),0.0,r) );
return max(l,m*sign(c.y*p.x-c.x*p.y));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// normalized pixel coordinates
vec2 p = (fragCoord*2.0-iResolution.xy)/iResolution.y;
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
// animation
float t = 3.14*(0.5+0.5*cos(iTime*0.52));
vec2 w = vec2(0.50,0.25)*(0.5+0.5*cos(iTime*vec2(1.1,1.3)+vec2(0.0,2.0)));
// distance
float d = sdPie(p,vec2(sin(t),cos(t)), 0.5);
// coloring
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-2.0*abs(d));
col *= 0.8 + 0.2*cos(128.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );
if( iMouse.z>0.001 )
{
d = sdPie(m,vec2(sin(t),cos(t)), 0.5);
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025));
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015));
}
fragColor = vec4(col, 1.0);
} | mit | [
1234,
1284,
1333,
1333,
1476
] | [
[
1234,
1284,
1333,
1333,
1476
],
[
1478,
1478,
1535,
1571,
2424
]
] | // c is the sin/cos of the angle. r is the radius
| float sdPie( in vec2 p, in vec2 c, in float r )
{ |
p.x = abs(p.x);
float l = length(p) - r;
float m = length(p - c*clamp(dot(p,c),0.0,r) );
return max(l,m*sign(c.y*p.x-c.x*p.y));
} | // c is the sin/cos of the angle. r is the radius
float sdPie( in vec2 p, in vec2 c, in float r )
{ | 3 | 11 |
3tj3DW | iq | 2019-06-03T23:28:36 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Analytic intersection of a sphere f(x,y,z)=x^4+y^4+z^4-r^4=0. I Specialized
// the generic quartic equation solver for the only geometrically valid cases,
// reduced all canceling complex operations, and optimized coefficients.
// List of ray-surface intersectors at https://www.shadertoy.com/playlist/l3dXRf
// and http://iquilezles.org/www/articles/intersectors/intersectors.htm
// f(x,y,z) = x^4 + y^4 + z^4 - ra^4
float iSphere4( in vec3 ro, in vec3 rd, in float ra )
{
// -----------------------------
// solve quartic equation
// -----------------------------
float r2 = ra*ra;
vec3 d2 = rd*rd; vec3 d3 = d2*rd;
vec3 o2 = ro*ro; vec3 o3 = o2*ro;
float ka = 1.0/dot(d2,d2);
float k3 = ka* dot(ro,d3);
float k2 = ka* dot(o2,d2);
float k1 = ka* dot(o3,rd);
float k0 = ka*(dot(o2,o2) - r2*r2);
// -----------------------------
// solve cubic
// -----------------------------
float c2 = k2 - k3*k3;
float c1 = k1 + 2.0*k3*k3*k3 - 3.0*k3*k2;
float c0 = k0 - 3.0*k3*k3*k3*k3 + 6.0*k3*k3*k2 - 4.0*k3*k1;
float p = c2*c2 + c0/3.0;
float q = c2*c2*c2 - c2*c0 + c1*c1;
float h = q*q - p*p*p;
// -----------------------------
// skip the case of three real solutions for the cubic, which involves four
// complex solutions for the quartic, since we know this objcet is convex
// -----------------------------
if( h<0.0 ) return -1.0;
// one real solution, two complex (conjugated)
float sh = sqrt(h);
float s = sign(q+sh)*pow(abs(q+sh),1.0/3.0); // cuberoot
float t = sign(q-sh)*pow(abs(q-sh),1.0/3.0); // cuberoot
vec2 w = vec2( s+t,s-t );
// -----------------------------
// the quartic will have two real solutions and two complex solutions.
// we only want the real ones
// -----------------------------
#if 1
vec2 v = vec2( w.x+c2*4.0, w.y*sqrt(3.0) )*0.5;
float r = length(v);
return -abs(v.y)/sqrt(r+v.x) - c1/r - k3;
#else
float r = sqrt( c2*c2 + w.x*w.x + 2.0*w.x*c2 - c0 );
return -sqrt( 3.0*w.y*w.y/(4.0*r+w.x*2.0+c2*8.0)) - c1/r - k3;
#endif
}
// df/dx,df/dy,df/dx for f(x,y,z) = x^4 + y^4 + z^4 - ra^4
vec3 nSphere4( in vec3 pos )
{
return normalize( pos*pos*pos );
}
#define AA 2
#define ZERO min(iFrame,0)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.5*iTime;
vec3 ro = vec3( 3.1*cos(an), 1.4, 3.1*sin(an) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
// raytrace
float t = iSphere4( ro, rd, 1.0 );
// shading/lighting
vec3 col = vec3(0.08)*(1.0-0.3*length(p)) + 0.02*rd.y;
if( t>0.0 && t<100.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = nSphere4( pos );
vec3 lig = normalize(vec3(0.7,0.6,0.3));
vec3 hal = normalize(-rd+lig);
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
float amb = clamp( 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0)), 0.0, 1.0 );
#if 0
col = vec3(0.8);
#else
const float fr = 3.14159*7.5;
vec3 uvw = pow(abs(nor),vec3(1.0/3.0));
//vec3 uvw = pos;
col = vec3(0.5);
float w = pow(1.0+dot(nor,rd),3.0);
col += 0.4*smoothstep(-0.01,0.01,cos(uvw.x*fr*0.5)*cos(uvw.y*fr*0.5)*cos(uvw.z*fr*0.5));
col *= 1.0*smoothstep(-1.0,-0.98+0.2*w,cos(uvw.x*fr))
*smoothstep(-1.0,-0.98+0.2*w,cos(uvw.y*fr))
*smoothstep(-1.0,-0.98+0.2*w,cos(uvw.z*fr));
#endif
col *= vec3(0.2,0.3,0.4)*amb + vec3(1.0,0.9,0.7)*dif;
col += 0.4*pow(clamp(dot(hal,nor),0.0,1.0),12.0)*dif;
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// dither to remove banding in the background
tot += fract(sin(fragCoord.x*vec3(13,1,11)+fragCoord.y*vec3(1,7,5))*158.391832)/255.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
3227,
3286,
3316,
3316,
3355
] | [
[
1466,
1503,
1558,
1662,
3225
],
[
3227,
3286,
3316,
3316,
3355
]
] | // df/dx,df/dy,df/dx for f(x,y,z) = x^4 + y^4 + z^4 - ra^4
| vec3 nSphere4( in vec3 pos )
{ |
return normalize( pos*pos*pos );
} | // df/dx,df/dy,df/dx for f(x,y,z) = x^4 + y^4 + z^4 - ra^4
vec3 nSphere4( in vec3 pos )
{ | 1 | 1 |
wl23RK | iq | 2019-06-12T02:32:30 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Distance to a sector of a circle
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
// sca is the sin/cos of the orientation
// scb is the sin/cos of the aperture
float sdArc( in vec2 p, in vec2 sca, in vec2 scb, in float ra, in float rb )
{
p *= mat2(sca.x,sca.y,-sca.y,sca.x);
p.x = abs(p.x);
float k = (scb.y*p.x>scb.x*p.y) ? dot(p.xy,scb) : length(p);
return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// normalized pixel coordinates
vec2 p = (fragCoord*2.0-iResolution.xy)/iResolution.y;
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
// animation
float time = iTime;
float ta = 3.14*(0.5+0.5*cos(time*0.52+2.0));
float tb = 3.14*(0.5+0.5*cos(time*0.31+2.0));
float rb = 0.15*(0.5+0.5*cos(time*0.41+3.0));
// distance
float d = sdArc(p,vec2(sin(ta),cos(ta)),vec2(sin(tb),cos(tb)), 0.7, rb);
// coloring
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-2.0*abs(d));
col *= 0.8 + 0.2*cos(128.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );
if( iMouse.z>0.001 )
{
d = sdArc(m,vec2(sin(ta),cos(ta)),vec2(sin(tb),cos(tb)), 0.7, rb);
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025));
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015));
}
fragColor = vec4(col, 1.0);
} | mit | [
1274,
1353,
1431,
1431,
1612
] | [
[
1274,
1353,
1431,
1431,
1612
],
[
1614,
1614,
1671,
1707,
2653
]
] | // sca is the sin/cos of the orientation
// scb is the sin/cos of the aperture
| float sdArc( in vec2 p, in vec2 sca, in vec2 scb, in float ra, in float rb )
{ |
p *= mat2(sca.x,sca.y,-sca.y,sca.x);
p.x = abs(p.x);
float k = (scb.y*p.x>scb.x*p.y) ? dot(p.xy,scb) : length(p);
return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
} | // sca is the sin/cos of the orientation
// scb is the sin/cos of the aperture
float sdArc( in vec2 p, in vec2 sca, in vec2 scb, in float ra, in float rb )
{ | 1 | 4 |
wlXSD7 | iq | 2019-07-15T08:58:25 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Euclidean distance to chain link. After elongating a torus with the technique
// in this article https://www.shadertoy.com/view/Ml3fWj, you can simplify a few
// things, producing a branch free and compact sdLink() with a minimal number of
// square roots and operations.
// List of other 3D SDFs: https://www.shadertoy.com/playlist/43cXRl
//
// and http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdLink( in vec3 p, in float le, in float r1, in float r2 )
{
vec3 q = vec3( p.x, max(abs(p.y)-le,0.0), p.z );
return length(vec2(length(q.xy)-r1,q.z)) - r2;
}
float map( in vec3 pos )
{
// animate
pos.y += 0.5*iTime;
// paramteres
const float le = 0.13, r1 = 0.2, r2 = 0.09;
// make a chain out of sdLink's
vec3 a = pos; a.y = fract(a.y )-0.5;
vec3 b = pos; b.y = fract(b.y+0.5)-0.5;
// evaluate two links
return min(sdLink(a.xyz,le,r1,r2),
sdLink(b.zyx,le,r1,r2));
}
/*
// build the chain directly, it saves one of four square roots
// over using sdLinks()
float sdChain( in vec3 pos, in float le, in float r1, in float r2 )
{
float ya = max(abs(fract(pos.y )-0.5)-le,0.0);
float yb = max(abs(fract(pos.y+0.5)-0.5)-le,0.0);
float la = ya*ya - 2.0*r1*sqrt(pos.x*pos.x+ya*ya);
float lb = yb*yb - 2.0*r1*sqrt(pos.z*pos.z+yb*yb);
return sqrt(dot(pos.xz,pos.xz) + r1*r1 + min(la,lb)) - r2;
}
*/
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps ) +
e.yyx*map( pos + e.yyx*eps ) +
e.yxy*map( pos + e.yxy*eps ) +
e.xxx*map( pos + e.xxx*eps ) );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
{
float res = 1.0;
float t = mint;
for( int i=0; i<16; i++ )
{
float h = map( ro + rd*t );
res = min( res, 8.0*h/t );
t += clamp( h, 0.02, 0.10 );
if( res<0.005 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
float calcOcclusion( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float hr = 0.01 + 0.15*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = map( aopos );
occ += -(dd-hr)*sca;
sca *= 0.95;
}
return clamp( 1.0 - occ*1.5, 0.0, 1.0 );
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.7*iTime;
vec3 ro = vec3( 1.0*cos(an), 0.2, 1.0*sin(an) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = ( cross(uu,ww));
// render
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// raymarch
const float tmax = 5.0;
float t = 0.0;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
float h = map(pos);
if( h<0.0001 || t>tmax ) break;
t += h;
}
// shading/lighting
vec3 col = vec3(0.0);
if( t<tmax )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal(pos);
vec3 lig = normalize(vec3(0.6,0.2,0.4));
vec3 hal = normalize(lig-rd);
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
float occ = calcOcclusion( pos, nor );
if( dif>0.001 ) dif *= calcSoftshadow( pos, lig, 0.01, 1.0 );
float spe = pow(clamp(dot(nor,hal),0.0,1.0),16.0)*dif*(0.04+0.96*pow(clamp(1.0-dot(hal,-rd),0.0,1.0),5.0));
float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0));
col = vec3(0.5,1.0,1.2)*amb*occ;
col += vec3(2.8,2.2,1.8)*dif;
col *= 0.2;
col += vec3(2.8,2.2,1.8)*spe*3.0;
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
2847,
2909,
2987,
2987,
3250
] | [
[
1503,
1503,
1569,
1569,
1675
],
[
1677,
1677,
1703,
1718,
2050
],
[
2506,
2570,
2602,
2602,
2841
],
[
2847,
2909,
2987,
2987,
3250
],
[
3252,
3252,
3301,
3301,
3594
]
] | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
| float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
{ |
float res = 1.0;
float t = mint;
for( int i=0; i<16; i++ )
{
float h = map( ro + rd*t );
res = min( res, 8.0*h/t );
t += clamp( h, 0.02, 0.10 );
if( res<0.005 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
} | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
{ | 1 | 60 |
WtB3Wt | iq | 2019-07-19T06:36:25 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// I raymarched a 3D slice of a 4D rounded box. The 3D slice (plane) that
// cuts the 4D box is animated over time, and the cube itself is rotating
// in 4D space. Note this is NOT 4D raymarching, it is 3D raymarching (of
// a 3D slice of a 4D world).
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // Set AA to 1 if your machine is too slow
#endif
float sdBox( in vec4 p, in vec4 b )
{
vec4 d = abs(p) - b;
return min( max(max(d.x,d.y),max(d.z,d.w)),0.0) + length(max(d,0.0));
}
mat4x4 q2m( in vec4 q )
{
return mat4x4( q.x, -q.y, -q.z, -q.w,
q.y, q.x, -q.w, q.z,
q.z, q.w, q.x, -q.y,
q.w, -q.z, q.y, q.x );
}
float map( in vec3 pos, float time )
{
// take a 3D slice
vec4 p = vec4(pos,0.5*sin(time*0.513));
// rotate 3D point into 4D
vec4 q1 = normalize( cos( 0.2*time*vec4(1.0,1.7,1.1,1.5) + vec4(0.0,1.0,5.0,4.0) ) );
vec4 q2 = normalize( cos( 0.2*time*vec4(1.9,1.7,1.4,1.3) + vec4(3.0,2.0,6.0,5.0) ) );
p = q2m(q2)*p*q2m(q1);
// 4D box
return sdBox( p, vec4(0.8,0.5,0.7,0.2) )- 0.03;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos, in float time )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.00025;
return normalize( e.xyy*map( pos + e.xyy*eps, time ) +
e.yyx*map( pos + e.yyx*eps, time ) +
e.yxy*map( pos + e.yxy*eps, time ) +
e.xxx*map( pos + e.xxx*eps, time ) );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax, float time )
{
float res = 1.0;
float t = mint;
for( int i=0; i<128; i++ )
{
float h = map( ro + rd*t, time );
res = min( res, 16.0*h/t );
t += clamp( h, 0.01, 0.25 );
if( res<0.001 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
vec2 intersect( in vec3 ro, in vec3 rd, in float time )
{
vec2 res = vec2(1e20,-1.0);
// plane
{
float t = (-1.0-ro.y)/rd.y;
if( t>0.0 ) res = vec2(t,1.0);
}
{
// box
float tmax = min(6.0,res.x);
float t = 0.4;
for( int i=0; i<128; i++ )
{
vec3 pos = ro + t*rd;
float h = map(pos, time);
if( h<0.001 || t>tmax ) break;
t += h;
}
if( t<tmax && t<res.x ) res = vec2(t,2.0);
}
return res;
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy )
{
// filter kernel
vec2 w = abs(dpdx)+abs(dpdy) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float di = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
float time = iTime - 0.5*(1.0/24.0)*(float(m*AA+n)+di)/float(AA*AA-1);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float time = iTime;
#endif
// create view ray
vec3 ro = vec3(-0.5,0.0,2.5);
vec3 rd = normalize( vec3(p,-1.8) );
// ray differentials
vec2 px = (-iResolution.xy+2.0*(fragCoord.xy+vec2(1.0,0.0)))/iResolution.y;
vec2 py = (-iResolution.xy+2.0*(fragCoord.xy+vec2(0.0,1.0)))/iResolution.y;
vec3 rdx = normalize( vec3(px,-1.8) );
vec3 rdy = normalize( vec3(py,-1.8) );
// raymarch
vec2 tm = intersect( ro, rd, time );
vec3 col = vec3(0.6,0.75,0.85) - 0.97*rd.y;
if( tm.y>0.0 )
{
// shading/lighting
vec3 pos = ro + tm.x*rd;
vec3 nor = (tm.y<1.5)?vec3(0.0,1.0,0.0):calcNormal(pos,time);
vec3 lig = normalize(vec3(0.8,0.4,0.6));
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
vec3 hal = normalize(lig-rd);
float sha = calcSoftshadow( pos+0.001*nor, lig, 0.001, 4.0, time );
float amb = 0.6 + 0.4*nor.y;
float bou = clamp(-nor.y,0.0,1.0);
float spe = clamp(dot(nor,hal),0.0,1.0);
col = 3.5*vec3(1.00,0.80,0.60)*dif*sha;
col += 4.0*vec3(0.12,0.18,0.24)*amb;
col += 2.0*vec3(0.30,0.20,0.10)*bou;
if( pos.y<-.99 )
{
// project pixel footprint into the plane
vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y);
vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y);
float f = checkersGradBox( 2.0*pos.xz, 2.0*dpdx.xz, 2.0*dpdy.xz );
col *= 0.2 + f*vec3(0.05);
}
else
{
col *= 0.25;
}
col += 0.2*pow(spe,8.0)*dif*sha;
col = mix( col, vec3(0.6,0.7,0.8), 1.0-exp(-0.001*tm.x*tm.x) );
}
// gamma
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
tot = pow( tot, vec3(0.45) );
tot = clamp(tot,0.0,1.0);
tot = tot*tot*(3.0-2.0*tot);
fragColor = vec4( tot, 1.0 );
} | mit | [
2194,
2258,
2305,
2305,
2569
] | [
[
1439,
1439,
1476,
1476,
1577
],
[
1579,
1579,
1604,
1604,
1775
],
[
1777,
1777,
1815,
1838,
2192
],
[
2194,
2258,
2305,
2305,
2569
],
[
2571,
2633,
2723,
2723,
2994
],
[
2996,
2996,
3053,
3053,
3490
],
[
3492,
3568,
3632,
3653,
3886
]
] | // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
| vec3 calcNormal( in vec3 pos, in float time )
{ |
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.00025;
return normalize( e.xyy*map( pos + e.xyy*eps, time ) +
e.yyx*map( pos + e.yyx*eps, time ) +
e.yxy*map( pos + e.yxy*eps, time ) +
e.xxx*map( pos + e.xxx*eps, time ) );
} | // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos, in float time )
{ | 1 | 6 |
WtB3Wt | iq | 2019-07-19T06:36:25 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// I raymarched a 3D slice of a 4D rounded box. The 3D slice (plane) that
// cuts the 4D box is animated over time, and the cube itself is rotating
// in 4D space. Note this is NOT 4D raymarching, it is 3D raymarching (of
// a 3D slice of a 4D world).
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // Set AA to 1 if your machine is too slow
#endif
float sdBox( in vec4 p, in vec4 b )
{
vec4 d = abs(p) - b;
return min( max(max(d.x,d.y),max(d.z,d.w)),0.0) + length(max(d,0.0));
}
mat4x4 q2m( in vec4 q )
{
return mat4x4( q.x, -q.y, -q.z, -q.w,
q.y, q.x, -q.w, q.z,
q.z, q.w, q.x, -q.y,
q.w, -q.z, q.y, q.x );
}
float map( in vec3 pos, float time )
{
// take a 3D slice
vec4 p = vec4(pos,0.5*sin(time*0.513));
// rotate 3D point into 4D
vec4 q1 = normalize( cos( 0.2*time*vec4(1.0,1.7,1.1,1.5) + vec4(0.0,1.0,5.0,4.0) ) );
vec4 q2 = normalize( cos( 0.2*time*vec4(1.9,1.7,1.4,1.3) + vec4(3.0,2.0,6.0,5.0) ) );
p = q2m(q2)*p*q2m(q1);
// 4D box
return sdBox( p, vec4(0.8,0.5,0.7,0.2) )- 0.03;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos, in float time )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.00025;
return normalize( e.xyy*map( pos + e.xyy*eps, time ) +
e.yyx*map( pos + e.yyx*eps, time ) +
e.yxy*map( pos + e.yxy*eps, time ) +
e.xxx*map( pos + e.xxx*eps, time ) );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax, float time )
{
float res = 1.0;
float t = mint;
for( int i=0; i<128; i++ )
{
float h = map( ro + rd*t, time );
res = min( res, 16.0*h/t );
t += clamp( h, 0.01, 0.25 );
if( res<0.001 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
vec2 intersect( in vec3 ro, in vec3 rd, in float time )
{
vec2 res = vec2(1e20,-1.0);
// plane
{
float t = (-1.0-ro.y)/rd.y;
if( t>0.0 ) res = vec2(t,1.0);
}
{
// box
float tmax = min(6.0,res.x);
float t = 0.4;
for( int i=0; i<128; i++ )
{
vec3 pos = ro + t*rd;
float h = map(pos, time);
if( h<0.001 || t>tmax ) break;
t += h;
}
if( t<tmax && t<res.x ) res = vec2(t,2.0);
}
return res;
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy )
{
// filter kernel
vec2 w = abs(dpdx)+abs(dpdy) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float di = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
float time = iTime - 0.5*(1.0/24.0)*(float(m*AA+n)+di)/float(AA*AA-1);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float time = iTime;
#endif
// create view ray
vec3 ro = vec3(-0.5,0.0,2.5);
vec3 rd = normalize( vec3(p,-1.8) );
// ray differentials
vec2 px = (-iResolution.xy+2.0*(fragCoord.xy+vec2(1.0,0.0)))/iResolution.y;
vec2 py = (-iResolution.xy+2.0*(fragCoord.xy+vec2(0.0,1.0)))/iResolution.y;
vec3 rdx = normalize( vec3(px,-1.8) );
vec3 rdy = normalize( vec3(py,-1.8) );
// raymarch
vec2 tm = intersect( ro, rd, time );
vec3 col = vec3(0.6,0.75,0.85) - 0.97*rd.y;
if( tm.y>0.0 )
{
// shading/lighting
vec3 pos = ro + tm.x*rd;
vec3 nor = (tm.y<1.5)?vec3(0.0,1.0,0.0):calcNormal(pos,time);
vec3 lig = normalize(vec3(0.8,0.4,0.6));
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
vec3 hal = normalize(lig-rd);
float sha = calcSoftshadow( pos+0.001*nor, lig, 0.001, 4.0, time );
float amb = 0.6 + 0.4*nor.y;
float bou = clamp(-nor.y,0.0,1.0);
float spe = clamp(dot(nor,hal),0.0,1.0);
col = 3.5*vec3(1.00,0.80,0.60)*dif*sha;
col += 4.0*vec3(0.12,0.18,0.24)*amb;
col += 2.0*vec3(0.30,0.20,0.10)*bou;
if( pos.y<-.99 )
{
// project pixel footprint into the plane
vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y);
vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y);
float f = checkersGradBox( 2.0*pos.xz, 2.0*dpdx.xz, 2.0*dpdy.xz );
col *= 0.2 + f*vec3(0.05);
}
else
{
col *= 0.25;
}
col += 0.2*pow(spe,8.0)*dif*sha;
col = mix( col, vec3(0.6,0.7,0.8), 1.0-exp(-0.001*tm.x*tm.x) );
}
// gamma
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
tot = pow( tot, vec3(0.45) );
tot = clamp(tot,0.0,1.0);
tot = tot*tot*(3.0-2.0*tot);
fragColor = vec4( tot, 1.0 );
} | mit | [
2571,
2633,
2723,
2723,
2994
] | [
[
1439,
1439,
1476,
1476,
1577
],
[
1579,
1579,
1604,
1604,
1775
],
[
1777,
1777,
1815,
1838,
2192
],
[
2194,
2258,
2305,
2305,
2569
],
[
2571,
2633,
2723,
2723,
2994
],
[
2996,
2996,
3053,
3053,
3490
],
[
3492,
3568,
3632,
3653,
3886
]
] | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
| float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax, float time )
{ |
float res = 1.0;
float t = mint;
for( int i=0; i<128; i++ )
{
float h = map( ro + rd*t, time );
res = min( res, 16.0*h/t );
t += clamp( h, 0.01, 0.25 );
if( res<0.001 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
} | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax, float time )
{ | 1 | 1 |
WtB3Wt | iq | 2019-07-19T06:36:25 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// I raymarched a 3D slice of a 4D rounded box. The 3D slice (plane) that
// cuts the 4D box is animated over time, and the cube itself is rotating
// in 4D space. Note this is NOT 4D raymarching, it is 3D raymarching (of
// a 3D slice of a 4D world).
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // Set AA to 1 if your machine is too slow
#endif
float sdBox( in vec4 p, in vec4 b )
{
vec4 d = abs(p) - b;
return min( max(max(d.x,d.y),max(d.z,d.w)),0.0) + length(max(d,0.0));
}
mat4x4 q2m( in vec4 q )
{
return mat4x4( q.x, -q.y, -q.z, -q.w,
q.y, q.x, -q.w, q.z,
q.z, q.w, q.x, -q.y,
q.w, -q.z, q.y, q.x );
}
float map( in vec3 pos, float time )
{
// take a 3D slice
vec4 p = vec4(pos,0.5*sin(time*0.513));
// rotate 3D point into 4D
vec4 q1 = normalize( cos( 0.2*time*vec4(1.0,1.7,1.1,1.5) + vec4(0.0,1.0,5.0,4.0) ) );
vec4 q2 = normalize( cos( 0.2*time*vec4(1.9,1.7,1.4,1.3) + vec4(3.0,2.0,6.0,5.0) ) );
p = q2m(q2)*p*q2m(q1);
// 4D box
return sdBox( p, vec4(0.8,0.5,0.7,0.2) )- 0.03;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos, in float time )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.00025;
return normalize( e.xyy*map( pos + e.xyy*eps, time ) +
e.yyx*map( pos + e.yyx*eps, time ) +
e.yxy*map( pos + e.yxy*eps, time ) +
e.xxx*map( pos + e.xxx*eps, time ) );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax, float time )
{
float res = 1.0;
float t = mint;
for( int i=0; i<128; i++ )
{
float h = map( ro + rd*t, time );
res = min( res, 16.0*h/t );
t += clamp( h, 0.01, 0.25 );
if( res<0.001 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
vec2 intersect( in vec3 ro, in vec3 rd, in float time )
{
vec2 res = vec2(1e20,-1.0);
// plane
{
float t = (-1.0-ro.y)/rd.y;
if( t>0.0 ) res = vec2(t,1.0);
}
{
// box
float tmax = min(6.0,res.x);
float t = 0.4;
for( int i=0; i<128; i++ )
{
vec3 pos = ro + t*rd;
float h = map(pos, time);
if( h<0.001 || t>tmax ) break;
t += h;
}
if( t<tmax && t<res.x ) res = vec2(t,2.0);
}
return res;
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy )
{
// filter kernel
vec2 w = abs(dpdx)+abs(dpdy) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float di = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
float time = iTime - 0.5*(1.0/24.0)*(float(m*AA+n)+di)/float(AA*AA-1);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float time = iTime;
#endif
// create view ray
vec3 ro = vec3(-0.5,0.0,2.5);
vec3 rd = normalize( vec3(p,-1.8) );
// ray differentials
vec2 px = (-iResolution.xy+2.0*(fragCoord.xy+vec2(1.0,0.0)))/iResolution.y;
vec2 py = (-iResolution.xy+2.0*(fragCoord.xy+vec2(0.0,1.0)))/iResolution.y;
vec3 rdx = normalize( vec3(px,-1.8) );
vec3 rdy = normalize( vec3(py,-1.8) );
// raymarch
vec2 tm = intersect( ro, rd, time );
vec3 col = vec3(0.6,0.75,0.85) - 0.97*rd.y;
if( tm.y>0.0 )
{
// shading/lighting
vec3 pos = ro + tm.x*rd;
vec3 nor = (tm.y<1.5)?vec3(0.0,1.0,0.0):calcNormal(pos,time);
vec3 lig = normalize(vec3(0.8,0.4,0.6));
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
vec3 hal = normalize(lig-rd);
float sha = calcSoftshadow( pos+0.001*nor, lig, 0.001, 4.0, time );
float amb = 0.6 + 0.4*nor.y;
float bou = clamp(-nor.y,0.0,1.0);
float spe = clamp(dot(nor,hal),0.0,1.0);
col = 3.5*vec3(1.00,0.80,0.60)*dif*sha;
col += 4.0*vec3(0.12,0.18,0.24)*amb;
col += 2.0*vec3(0.30,0.20,0.10)*bou;
if( pos.y<-.99 )
{
// project pixel footprint into the plane
vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y);
vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y);
float f = checkersGradBox( 2.0*pos.xz, 2.0*dpdx.xz, 2.0*dpdy.xz );
col *= 0.2 + f*vec3(0.05);
}
else
{
col *= 0.25;
}
col += 0.2*pow(spe,8.0)*dif*sha;
col = mix( col, vec3(0.6,0.7,0.8), 1.0-exp(-0.001*tm.x*tm.x) );
}
// gamma
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
tot = pow( tot, vec3(0.45) );
tot = clamp(tot,0.0,1.0);
tot = tot*tot*(3.0-2.0*tot);
fragColor = vec4( tot, 1.0 );
} | mit | [
3492,
3568,
3632,
3653,
3886
] | [
[
1439,
1439,
1476,
1476,
1577
],
[
1579,
1579,
1604,
1604,
1775
],
[
1777,
1777,
1815,
1838,
2192
],
[
2194,
2258,
2305,
2305,
2569
],
[
2571,
2633,
2723,
2723,
2994
],
[
2996,
2996,
3053,
3053,
3490
],
[
3492,
3568,
3632,
3653,
3886
]
] | // http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
| float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy )
{ |
vec2 w = abs(dpdx)+abs(dpdy) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
} | // http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy )
{ | 15 | 15 |
ttBXRG | iq | 2019-08-19T20:19:09 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Staircase function y=f(x)
//
// k>1 : flat horizontals
// k<1 : flat verticals
//
// The inverse function x=f^-1(x) is just the function itselft
// with parameter 1/k instead of k.
float staircase( in float x, in float k )
{
float i = floor(x);
float f = fract(x);
float a = 0.5*pow(2.0*((f<0.5)?f:1.0-f), k);
f = (f<0.5)?a:1.0-a;
return i+f;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// coordinates
float dx = 3.0/iResolution.y;
vec2 p = fragCoord.xy*dx;
// animate staircase curve
float k = pow( 2.0, 4.0*sin(3.1415927*iTime));
// background
vec3 col = vec3( 0.2 + 0.02*mod(floor(p.x)+floor(p.y),2.0) );
// draw curve y=f(x)
float y = staircase( p.x, k );
col = mix( col, vec3(1.0,0.8,0.3), 1.0-smoothstep(0.0, 2.0*dx, abs(p.y-y) ) );
// draw curve x=f^-1(x)
float x = staircase( p.y, 1.0/k );
col = mix( col, vec3(1.0,0.8,0.3), 1.0-smoothstep(0.0, 2.0*dx, abs(p.x-x) ) );
fragColor = vec4( col, 1.0 );
} | mit | [
1080,
1264,
1307,
1307,
1457
] | [
[
1080,
1264,
1307,
1307,
1457
],
[
1459,
1459,
1516,
1540,
2121
]
] | // Staircase function y=f(x)
//
// k>1 : flat horizontals
// k<1 : flat verticals
//
// The inverse function x=f^-1(x) is just the function itselft
// with parameter 1/k instead of k.
| float staircase( in float x, in float k )
{ |
float i = floor(x);
float f = fract(x);
float a = 0.5*pow(2.0*((f<0.5)?f:1.0-f), k);
f = (f<0.5)?a:1.0-a;
return i+f;
} | // Staircase function y=f(x)
//
// k>1 : flat horizontals
// k<1 : flat verticals
//
// The inverse function x=f^-1(x) is just the function itselft
// with parameter 1/k instead of k.
float staircase( in float x, in float k )
{ | 2 | 3 |
wlBXWK | skythedragon | 2019-08-29T17:22:10 | /*
MIT License
Copyright (c) 2019 - 2021 Dimas "Dimev", "Skythedragon" Leenman
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.
Update 1 (25-9-2019): added 2 lines to prevent mie from shining through objects inside the atmosphere
Update 2 (2-10-2019): made use of HW_PERFORMANCE to improve performance on mobile (reduces number of samples), also added a sun
Update 3 (5-10-2019): added a license
Update 4 (28-11-2019): atmosphere now correctly blocks light from the scene passing through, and added an ambient scattering term
Update 5 (28-11-2019): mouse drag now changes the time of day
Update 6 (28-11-2019): atmosphere now doesn't use the ray sphere intersect function, meaning it's only one function
Update 7 (22-12-2019): Compacted the mie and rayleigh parts into a single vec2 + added a basic skylight
Update 8 (15-5-2020): Added ozone absorption (Can also be used as absorption in general)
Update 9 (6-5-2021): Changed the ozone distribution from 1 / cosh(x) to 1 / (x^2 + 1), and removed the clamp, better integration is planned
Update 10 (6-5-2021): Changed the integrator to be a bit better, but it might have broken it a bit as well (and it's not 100% done yet)
Update 11 (18-5-2021): Changed the integrator again, to fix it, because apparently it got worse since last update
Update 12 (19-5-2021): Found a slight issue at certain view angles backwards, fixed with a simple max
Update 13 (Planned): Change the integration again, according to seb hillaire: transmittance + total instead of optical depth and total
See Enscape clouds, this hopefully improves the quality
Scattering works by calculating how much light is scattered to the camera on a certain path/
This implementation does that by taking a number of samples across that path to check the amount of light that reaches the path
and it calculates the color of this light from the effects of scattering.
There are two types of scattering, rayleigh and mie
rayleigh is caused by small particles (molecules) and scatters certain colors better than others (causing a blue sky on earth)
mie is caused by bigger particles (like water droplets), and scatters all colors equally, but only in a certain direction.
Mie scattering causes the red sky during the sunset, because it scatters the remaining red light
To know where the ray starts and ends, we need to calculate where the ray enters and exits the atmosphere
We do this using a ray-sphere intersect
The scattering code is based on https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky
with some modifications to allow moving the planet, as well as objects inside the atmosphere, correct light absorbsion
from objects in the scene and an ambient scattering term tp light up the dark side a bit if needed
the camera also moves up and down, and the sun rotates around the planet as well
Note: Because rayleigh is a long word to type, I use ray instead on most variable names
the same goes for position (which becomes pos), direction (which becomes dir) and optical (becomes opt)
*/
// first, lets define some constants to use (planet radius, position, and scattering coefficients)
#define PLANET_POS vec3(0.0) /* the position of the planet */
#define PLANET_RADIUS 6371e3 /* radius of the planet */
#define ATMOS_RADIUS 6471e3 /* radius of the atmosphere */
// scattering coeffs
#define RAY_BETA vec3(5.5e-6, 13.0e-6, 22.4e-6) /* rayleigh, affects the color of the sky */
#define MIE_BETA vec3(21e-6) /* mie, affects the color of the blob around the sun */
#define AMBIENT_BETA vec3(0.0) /* ambient, affects the scattering color when there is no lighting from the sun */
#define ABSORPTION_BETA vec3(2.04e-5, 4.97e-5, 1.95e-6) /* what color gets absorbed by the atmosphere (Due to things like ozone) */
#define G 0.7 /* mie scattering direction, or how big the blob around the sun is */
// and the heights (how far to go up before the scattering has no effect)
#define HEIGHT_RAY 8e3 /* rayleigh height */
#define HEIGHT_MIE 1.2e3 /* and mie */
#define HEIGHT_ABSORPTION 30e3 /* at what height the absorption is at it's maximum */
#define ABSORPTION_FALLOFF 4e3 /* how much the absorption decreases the further away it gets from the maximum height */
// and the steps (more looks better, but is slower)
// the primary step has the most effect on looks
#if HW_PERFORMANCE==0
// edit these if you are on mobile
#define PRIMARY_STEPS 12
#define LIGHT_STEPS 4
# else
// and these on desktop
#define PRIMARY_STEPS 32 /* primary steps, affects quality the most */
#define LIGHT_STEPS 8 /* light steps, how much steps in the light direction are taken */
#endif
// camera mode, 0 is on the ground, 1 is in space, 2 is moving, 3 is moving from ground to space
#define CAMERA_MODE 2
/*
Next we'll define the main scattering function.
This traces a ray from start to end and takes a certain amount of samples along this ray, in order to calculate the color.
For every sample, we'll also trace a ray in the direction of the light,
because the color that reaches the sample also changes due to scattering
*/
vec3 calculate_scattering(
vec3 start, // the start of the ray (the camera position)
vec3 dir, // the direction of the ray (the camera vector)
float max_dist, // the maximum distance the ray can travel (because something is in the way, like an object)
vec3 scene_color, // the color of the scene
vec3 light_dir, // the direction of the light
vec3 light_intensity, // how bright the light is, affects the brightness of the atmosphere
vec3 planet_position, // the position of the planet
float planet_radius, // the radius of the planet
float atmo_radius, // the radius of the atmosphere
vec3 beta_ray, // the amount rayleigh scattering scatters the colors (for earth: causes the blue atmosphere)
vec3 beta_mie, // the amount mie scattering scatters colors
vec3 beta_absorption, // how much air is absorbed
vec3 beta_ambient, // the amount of scattering that always occurs, cna help make the back side of the atmosphere a bit brighter
float g, // the direction mie scatters the light in (like a cone). closer to -1 means more towards a single direction
float height_ray, // how high do you have to go before there is no rayleigh scattering?
float height_mie, // the same, but for mie
float height_absorption, // the height at which the most absorption happens
float absorption_falloff, // how fast the absorption falls off from the absorption height
int steps_i, // the amount of steps along the 'primary' ray, more looks better but slower
int steps_l // the amount of steps along the light ray, more looks better but slower
) {
// add an offset to the camera position, so that the atmosphere is in the correct position
start -= planet_position;
// calculate the start and end position of the ray, as a distance along the ray
// we do this with a ray sphere intersect
float a = dot(dir, dir);
float b = 2.0 * dot(dir, start);
float c = dot(start, start) - (atmo_radius * atmo_radius);
float d = (b * b) - 4.0 * a * c;
// stop early if there is no intersect
if (d < 0.0) return scene_color;
// calculate the ray length
vec2 ray_length = vec2(
max((-b - sqrt(d)) / (2.0 * a), 0.0),
min((-b + sqrt(d)) / (2.0 * a), max_dist)
);
// if the ray did not hit the atmosphere, return a black color
if (ray_length.x > ray_length.y) return scene_color;
// prevent the mie glow from appearing if there's an object in front of the camera
bool allow_mie = max_dist > ray_length.y;
// make sure the ray is no longer than allowed
ray_length.y = min(ray_length.y, max_dist);
ray_length.x = max(ray_length.x, 0.0);
// get the step size of the ray
float step_size_i = (ray_length.y - ray_length.x) / float(steps_i);
// next, set how far we are along the ray, so we can calculate the position of the sample
// if the camera is outside the atmosphere, the ray should start at the edge of the atmosphere
// if it's inside, it should start at the position of the camera
// the min statement makes sure of that
float ray_pos_i = ray_length.x + step_size_i * 0.5;
// these are the values we use to gather all the scattered light
vec3 total_ray = vec3(0.0); // for rayleigh
vec3 total_mie = vec3(0.0); // for mie
// initialize the optical depth. This is used to calculate how much air was in the ray
vec3 opt_i = vec3(0.0);
// we define the density early, as this helps doing integration
// usually we would do riemans summing, which is just the squares under the integral area
// this is a bit innefficient, and we can make it better by also taking the extra triangle at the top of the square into account
// the starting value is a bit inaccurate, but it should make it better overall
vec3 prev_density = vec3(0.0);
// also init the scale height, avoids some vec2's later on
vec2 scale_height = vec2(height_ray, height_mie);
// Calculate the Rayleigh and Mie phases.
// This is the color that will be scattered for this ray
// mu, mumu and gg are used quite a lot in the calculation, so to speed it up, precalculate them
float mu = dot(dir, light_dir);
float mumu = mu * mu;
float gg = g * g;
float phase_ray = 3.0 / (50.2654824574 /* (16 * pi) */) * (1.0 + mumu);
float phase_mie = allow_mie ? 3.0 / (25.1327412287 /* (8 * pi) */) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg)) : 0.0;
// now we need to sample the 'primary' ray. this ray gathers the light that gets scattered onto it
for (int i = 0; i < steps_i; ++i) {
// calculate where we are along this ray
vec3 pos_i = start + dir * ray_pos_i;
// and how high we are above the surface
float height_i = length(pos_i) - planet_radius;
// now calculate the density of the particles (both for rayleigh and mie)
vec3 density = vec3(exp(-height_i / scale_height), 0.0);
// and the absorption density. this is for ozone, which scales together with the rayleigh,
// but absorbs the most at a specific height, so use the sech function for a nice curve falloff for this height
// clamp it to avoid it going out of bounds. This prevents weird black spheres on the night side
float denom = (height_absorption - height_i) / absorption_falloff;
density.z = (1.0 / (denom * denom + 1.0)) * density.x;
// multiply it by the step size here
// we are going to use the density later on as well
density *= step_size_i;
// Add these densities to the optical depth, so that we know how many particles are on this ray.
// max here is needed to prevent opt_i from potentially becoming negative
opt_i += max(density + (prev_density - density) * 0.5, 0.0);
// and update the previous density
prev_density = density;
// Calculate the step size of the light ray.
// again with a ray sphere intersect
// a, b, c and d are already defined
a = dot(light_dir, light_dir);
b = 2.0 * dot(light_dir, pos_i);
c = dot(pos_i, pos_i) - (atmo_radius * atmo_radius);
d = (b * b) - 4.0 * a * c;
// no early stopping, this one should always be inside the atmosphere
// calculate the ray length
float step_size_l = (-b + sqrt(d)) / (2.0 * a * float(steps_l));
// and the position along this ray
// this time we are sure the ray is in the atmosphere, so set it to 0
float ray_pos_l = step_size_l * 0.5;
// and the optical depth of this ray
vec3 opt_l = vec3(0.0);
// again, use the prev density for better integration
vec3 prev_density_l = vec3(0.0);
// now sample the light ray
// this is similar to what we did before
for (int l = 0; l < steps_l; ++l) {
// calculate where we are along this ray
vec3 pos_l = pos_i + light_dir * ray_pos_l;
// the heigth of the position
float height_l = length(pos_l) - planet_radius;
// calculate the particle density, and add it
// this is a bit verbose
// first, set the density for ray and mie
vec3 density_l = vec3(exp(-height_l / scale_height), 0.0);
// then, the absorption
float denom = (height_absorption - height_l) / absorption_falloff;
density_l.z = (1.0 / (denom * denom + 1.0)) * density_l.x;
// multiply the density by the step size
density_l *= step_size_l;
// and add it to the total optical depth
opt_l += max(density_l + (prev_density_l - density_l) * 0.5, 0.0);
// and update the previous density
prev_density_l = density_l;
// and increment where we are along the light ray.
ray_pos_l += step_size_l;
}
// Now we need to calculate the attenuation
// this is essentially how much light reaches the current sample point due to scattering
vec3 attn = exp(-beta_ray * (opt_i.x + opt_l.x) - beta_mie * (opt_i.y + opt_l.y) - beta_absorption * (opt_i.z + opt_l.z));
// accumulate the scattered light (how much will be scattered towards the camera)
total_ray += density.x * attn;
total_mie += density.y * attn;
// and increment the position on this ray
ray_pos_i += step_size_i;
}
// calculate how much light can pass through the atmosphere
vec3 opacity = exp(-(beta_mie * opt_i.y + beta_ray * opt_i.x + beta_absorption * opt_i.z));
// calculate and return the final color
return (
phase_ray * beta_ray * total_ray // rayleigh color
+ phase_mie * beta_mie * total_mie // mie
+ opt_i.x * beta_ambient // and ambient
) * light_intensity + scene_color * opacity; // now make sure the background is rendered correctly
}
/*
A ray-sphere intersect
This was previously used in the atmosphere as well, but it's only used for the planet intersect now, since the atmosphere has this
ray sphere intersect built in
*/
vec2 ray_sphere_intersect(
vec3 start, // starting position of the ray
vec3 dir, // the direction of the ray
float radius // and the sphere radius
) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(dir, dir);
float b = 2.0 * dot(dir, start);
float c = dot(start, start) - (radius * radius);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
/*
To make the planet we're rendering look nicer, we implemented a skylight function here
Essentially it just takes a sample of the atmosphere in the direction of the surface normal
*/
vec3 skylight(vec3 sample_pos, vec3 surface_normal, vec3 light_dir, vec3 background_col) {
// slightly bend the surface normal towards the light direction
surface_normal = normalize(mix(surface_normal, light_dir, 0.6));
// and sample the atmosphere
return calculate_scattering(
sample_pos, // the position of the camera
surface_normal, // the camera vector (ray direction of this pixel)
3.0 * ATMOS_RADIUS, // max dist, since nothing will stop the ray here, just use some arbitrary value
background_col, // scene color, just the background color here
light_dir, // light direction
vec3(40.0), // light intensity, 40 looks nice
PLANET_POS, // position of the planet
PLANET_RADIUS, // radius of the planet in meters
ATMOS_RADIUS, // radius of the atmosphere in meters
RAY_BETA, // Rayleigh scattering coefficient
MIE_BETA, // Mie scattering coefficient
ABSORPTION_BETA, // Absorbtion coefficient
AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
G, // Mie preferred scattering direction
HEIGHT_RAY, // Rayleigh scale height
HEIGHT_MIE, // Mie scale height
HEIGHT_ABSORPTION, // the height at which the most absorption happens
ABSORPTION_FALLOFF, // how fast the absorption falls off from the absorption height
LIGHT_STEPS, // steps in the ray direction
LIGHT_STEPS // steps in the light direction
);
}
/*
The following function returns the scene color and depth
(the color of the pixel without the atmosphere, and the distance to the surface that is visible on that pixel)
in this case, the function renders a green sphere on the place where the planet should be
color is in .xyz, distance in .w
I won't explain too much about how this works, since that's not the aim of this shader
*/
vec4 render_scene(vec3 pos, vec3 dir, vec3 light_dir) {
// the color to use, w is the scene depth
vec4 color = vec4(0.0, 0.0, 0.0, 1e12);
// add a sun, if the angle between the ray direction and the light direction is small enough, color the pixels white
color.xyz = vec3(dot(dir, light_dir) > 0.9998 ? 3.0 : 0.0);
// get where the ray intersects the planet
vec2 planet_intersect = ray_sphere_intersect(pos - PLANET_POS, dir, PLANET_RADIUS);
// if the ray hit the planet, set the max distance to that ray
if (0.0 < planet_intersect.y) {
color.w = max(planet_intersect.x, 0.0);
// sample position, where the pixel is
vec3 sample_pos = pos + (dir * planet_intersect.x) - PLANET_POS;
// and the surface normal
vec3 surface_normal = normalize(sample_pos);
// get the color of the sphere
color.xyz = vec3(0.0, 0.25, 0.05);
// get wether this point is shadowed, + how much light scatters towards the camera according to the lommel-seelinger law
vec3 N = surface_normal;
vec3 V = -dir;
vec3 L = light_dir;
float dotNV = max(1e-6, dot(N, V));
float dotNL = max(1e-6, dot(N, L));
float shadow = dotNL / (dotNL + dotNV);
// apply the shadow
color.xyz *= shadow;
// apply skylight
color.xyz += clamp(skylight(sample_pos, surface_normal, light_dir, vec3(0.0)) * vec3(0.0, 0.25, 0.05), 0.0, 1.0);
}
return color;
}
/*
next, we need a way to do something with the scattering function
to do something with it we need the camera vector (which is the ray direction) of the current pixel
this function calculates it
*/
vec3 get_camera_vector(vec3 resolution, vec2 coord) {
vec2 uv = coord.xy / resolution.xy - vec2(0.5);
uv.x *= resolution.x / resolution.y;
return normalize(vec3(uv.x, uv.y, -1.0));
}
/*
Finally, draw the atmosphere to screen
we first get the camera vector and position, as well as the light dir
*/
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the camera vector
vec3 camera_vector = get_camera_vector(iResolution, fragCoord);
// get the camera position, switch based on the defines
#if CAMERA_MODE==0
vec3 camera_position = vec3(0.0, PLANET_RADIUS + 100.0, 0.0);
#endif
#if CAMERA_MODE==1
vec3 camera_position = vec3(0.0, ATMOS_RADIUS , ATMOS_RADIUS);
#endif
#if CAMERA_MODE==2
vec3 camera_position = vec3(0.0, ATMOS_RADIUS + (-cos(iTime / 2.0) * (ATMOS_RADIUS - PLANET_RADIUS - 1.0)), 0.0);
#endif
#if CAMERA_MODE==3
float offset = (1.0 - cos(iTime / 2.0)) * ATMOS_RADIUS;
vec3 camera_position = vec3(0.0, PLANET_RADIUS + 1.0, offset);
#endif
// get the light direction
// also base this on the mouse position, that way the time of day can be changed with the mouse
vec3 light_dir = iMouse.y == 0.0 ?
normalize(vec3(0.0, cos(-iTime/8.0), sin(-iTime/8.0))) :
normalize(vec3(0.0, cos(iMouse.y * -5.0 / iResolution.y), sin(iMouse.y * -5.0 / iResolution.y)));
// get the scene color and depth, color is in xyz, depth in w
// replace this with something better if you are using this shader for something else
vec4 scene = render_scene(camera_position, camera_vector, light_dir);
// the color of this pixel
vec3 col = vec3(0.0);//scene.xyz;
// get the atmosphere color
col += calculate_scattering(
camera_position, // the position of the camera
camera_vector, // the camera vector (ray direction of this pixel)
scene.w, // max dist, essentially the scene depth
scene.xyz, // scene color, the color of the current pixel being rendered
light_dir, // light direction
vec3(40.0), // light intensity, 40 looks nice
PLANET_POS, // position of the planet
PLANET_RADIUS, // radius of the planet in meters
ATMOS_RADIUS, // radius of the atmosphere in meters
RAY_BETA, // Rayleigh scattering coefficient
MIE_BETA, // Mie scattering coefficient
ABSORPTION_BETA, // Absorbtion coefficient
AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
G, // Mie preferred scattering direction
HEIGHT_RAY, // Rayleigh scale height
HEIGHT_MIE, // Mie scale height
HEIGHT_ABSORPTION, // the height at which the most absorption happens
ABSORPTION_FALLOFF, // how fast the absorption falls off from the absorption height
PRIMARY_STEPS, // steps in the ray direction
LIGHT_STEPS // steps in the light direction
);
// apply exposure, removing this makes the brighter colors look ugly
// you can play around with removing this
col = 1.0 - exp(-col);
// Output to screen
fragColor = vec4(col, 1.0);
} | mit | [
16068,
16254,
16344,
16413,
17996
] | [
[
5752,
6075,
7734,
7829,
15295
],
[
15488,
15488,
15650,
15787,
16066
],
[
16068,
16254,
16344,
16413,
17996
],
[
17998,
18385,
18440,
18491,
19945
],
[
19947,
20147,
20200,
20200,
20347
],
[
20349,
20465,
20520,
20554,
23518
]
] | /*
To make the planet we're rendering look nicer, we implemented a skylight function here
Essentially it just takes a sample of the atmosphere in the direction of the surface normal
*/
| vec3 skylight(vec3 sample_pos, vec3 surface_normal, vec3 light_dir, vec3 background_col) { |
surface_normal = normalize(mix(surface_normal, light_dir, 0.6));
// and sample the atmosphere
return calculate_scattering(
sample_pos, // the position of the camera
surface_normal, // the camera vector (ray direction of this pixel)
3.0 * ATMOS_RADIUS, // max dist, since nothing will stop the ray here, just use some arbitrary value
background_col, // scene color, just the background color here
light_dir, // light direction
vec3(40.0), // light intensity, 40 looks nice
PLANET_POS, // position of the planet
PLANET_RADIUS, // radius of the planet in meters
ATMOS_RADIUS, // radius of the atmosphere in meters
RAY_BETA, // Rayleigh scattering coefficient
MIE_BETA, // Mie scattering coefficient
ABSORPTION_BETA, // Absorbtion coefficient
AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
G, // Mie preferred scattering direction
HEIGHT_RAY, // Rayleigh scale height
HEIGHT_MIE, // Mie scale height
HEIGHT_ABSORPTION, // the height at which the most absorption happens
ABSORPTION_FALLOFF, // how fast the absorption falls off from the absorption height
LIGHT_STEPS, // steps in the ray direction
LIGHT_STEPS // steps in the light direction
);
} | /*
To make the planet we're rendering look nicer, we implemented a skylight function here
Essentially it just takes a sample of the atmosphere in the direction of the surface normal
*/
vec3 skylight(vec3 sample_pos, vec3 surface_normal, vec3 light_dir, vec3 background_col) { | 1 | 1 |
wlBXWK | skythedragon | 2019-08-29T17:22:10 | /*
MIT License
Copyright (c) 2019 - 2021 Dimas "Dimev", "Skythedragon" Leenman
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.
Update 1 (25-9-2019): added 2 lines to prevent mie from shining through objects inside the atmosphere
Update 2 (2-10-2019): made use of HW_PERFORMANCE to improve performance on mobile (reduces number of samples), also added a sun
Update 3 (5-10-2019): added a license
Update 4 (28-11-2019): atmosphere now correctly blocks light from the scene passing through, and added an ambient scattering term
Update 5 (28-11-2019): mouse drag now changes the time of day
Update 6 (28-11-2019): atmosphere now doesn't use the ray sphere intersect function, meaning it's only one function
Update 7 (22-12-2019): Compacted the mie and rayleigh parts into a single vec2 + added a basic skylight
Update 8 (15-5-2020): Added ozone absorption (Can also be used as absorption in general)
Update 9 (6-5-2021): Changed the ozone distribution from 1 / cosh(x) to 1 / (x^2 + 1), and removed the clamp, better integration is planned
Update 10 (6-5-2021): Changed the integrator to be a bit better, but it might have broken it a bit as well (and it's not 100% done yet)
Update 11 (18-5-2021): Changed the integrator again, to fix it, because apparently it got worse since last update
Update 12 (19-5-2021): Found a slight issue at certain view angles backwards, fixed with a simple max
Update 13 (Planned): Change the integration again, according to seb hillaire: transmittance + total instead of optical depth and total
See Enscape clouds, this hopefully improves the quality
Scattering works by calculating how much light is scattered to the camera on a certain path/
This implementation does that by taking a number of samples across that path to check the amount of light that reaches the path
and it calculates the color of this light from the effects of scattering.
There are two types of scattering, rayleigh and mie
rayleigh is caused by small particles (molecules) and scatters certain colors better than others (causing a blue sky on earth)
mie is caused by bigger particles (like water droplets), and scatters all colors equally, but only in a certain direction.
Mie scattering causes the red sky during the sunset, because it scatters the remaining red light
To know where the ray starts and ends, we need to calculate where the ray enters and exits the atmosphere
We do this using a ray-sphere intersect
The scattering code is based on https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky
with some modifications to allow moving the planet, as well as objects inside the atmosphere, correct light absorbsion
from objects in the scene and an ambient scattering term tp light up the dark side a bit if needed
the camera also moves up and down, and the sun rotates around the planet as well
Note: Because rayleigh is a long word to type, I use ray instead on most variable names
the same goes for position (which becomes pos), direction (which becomes dir) and optical (becomes opt)
*/
// first, lets define some constants to use (planet radius, position, and scattering coefficients)
#define PLANET_POS vec3(0.0) /* the position of the planet */
#define PLANET_RADIUS 6371e3 /* radius of the planet */
#define ATMOS_RADIUS 6471e3 /* radius of the atmosphere */
// scattering coeffs
#define RAY_BETA vec3(5.5e-6, 13.0e-6, 22.4e-6) /* rayleigh, affects the color of the sky */
#define MIE_BETA vec3(21e-6) /* mie, affects the color of the blob around the sun */
#define AMBIENT_BETA vec3(0.0) /* ambient, affects the scattering color when there is no lighting from the sun */
#define ABSORPTION_BETA vec3(2.04e-5, 4.97e-5, 1.95e-6) /* what color gets absorbed by the atmosphere (Due to things like ozone) */
#define G 0.7 /* mie scattering direction, or how big the blob around the sun is */
// and the heights (how far to go up before the scattering has no effect)
#define HEIGHT_RAY 8e3 /* rayleigh height */
#define HEIGHT_MIE 1.2e3 /* and mie */
#define HEIGHT_ABSORPTION 30e3 /* at what height the absorption is at it's maximum */
#define ABSORPTION_FALLOFF 4e3 /* how much the absorption decreases the further away it gets from the maximum height */
// and the steps (more looks better, but is slower)
// the primary step has the most effect on looks
#if HW_PERFORMANCE==0
// edit these if you are on mobile
#define PRIMARY_STEPS 12
#define LIGHT_STEPS 4
# else
// and these on desktop
#define PRIMARY_STEPS 32 /* primary steps, affects quality the most */
#define LIGHT_STEPS 8 /* light steps, how much steps in the light direction are taken */
#endif
// camera mode, 0 is on the ground, 1 is in space, 2 is moving, 3 is moving from ground to space
#define CAMERA_MODE 2
/*
Next we'll define the main scattering function.
This traces a ray from start to end and takes a certain amount of samples along this ray, in order to calculate the color.
For every sample, we'll also trace a ray in the direction of the light,
because the color that reaches the sample also changes due to scattering
*/
vec3 calculate_scattering(
vec3 start, // the start of the ray (the camera position)
vec3 dir, // the direction of the ray (the camera vector)
float max_dist, // the maximum distance the ray can travel (because something is in the way, like an object)
vec3 scene_color, // the color of the scene
vec3 light_dir, // the direction of the light
vec3 light_intensity, // how bright the light is, affects the brightness of the atmosphere
vec3 planet_position, // the position of the planet
float planet_radius, // the radius of the planet
float atmo_radius, // the radius of the atmosphere
vec3 beta_ray, // the amount rayleigh scattering scatters the colors (for earth: causes the blue atmosphere)
vec3 beta_mie, // the amount mie scattering scatters colors
vec3 beta_absorption, // how much air is absorbed
vec3 beta_ambient, // the amount of scattering that always occurs, cna help make the back side of the atmosphere a bit brighter
float g, // the direction mie scatters the light in (like a cone). closer to -1 means more towards a single direction
float height_ray, // how high do you have to go before there is no rayleigh scattering?
float height_mie, // the same, but for mie
float height_absorption, // the height at which the most absorption happens
float absorption_falloff, // how fast the absorption falls off from the absorption height
int steps_i, // the amount of steps along the 'primary' ray, more looks better but slower
int steps_l // the amount of steps along the light ray, more looks better but slower
) {
// add an offset to the camera position, so that the atmosphere is in the correct position
start -= planet_position;
// calculate the start and end position of the ray, as a distance along the ray
// we do this with a ray sphere intersect
float a = dot(dir, dir);
float b = 2.0 * dot(dir, start);
float c = dot(start, start) - (atmo_radius * atmo_radius);
float d = (b * b) - 4.0 * a * c;
// stop early if there is no intersect
if (d < 0.0) return scene_color;
// calculate the ray length
vec2 ray_length = vec2(
max((-b - sqrt(d)) / (2.0 * a), 0.0),
min((-b + sqrt(d)) / (2.0 * a), max_dist)
);
// if the ray did not hit the atmosphere, return a black color
if (ray_length.x > ray_length.y) return scene_color;
// prevent the mie glow from appearing if there's an object in front of the camera
bool allow_mie = max_dist > ray_length.y;
// make sure the ray is no longer than allowed
ray_length.y = min(ray_length.y, max_dist);
ray_length.x = max(ray_length.x, 0.0);
// get the step size of the ray
float step_size_i = (ray_length.y - ray_length.x) / float(steps_i);
// next, set how far we are along the ray, so we can calculate the position of the sample
// if the camera is outside the atmosphere, the ray should start at the edge of the atmosphere
// if it's inside, it should start at the position of the camera
// the min statement makes sure of that
float ray_pos_i = ray_length.x + step_size_i * 0.5;
// these are the values we use to gather all the scattered light
vec3 total_ray = vec3(0.0); // for rayleigh
vec3 total_mie = vec3(0.0); // for mie
// initialize the optical depth. This is used to calculate how much air was in the ray
vec3 opt_i = vec3(0.0);
// we define the density early, as this helps doing integration
// usually we would do riemans summing, which is just the squares under the integral area
// this is a bit innefficient, and we can make it better by also taking the extra triangle at the top of the square into account
// the starting value is a bit inaccurate, but it should make it better overall
vec3 prev_density = vec3(0.0);
// also init the scale height, avoids some vec2's later on
vec2 scale_height = vec2(height_ray, height_mie);
// Calculate the Rayleigh and Mie phases.
// This is the color that will be scattered for this ray
// mu, mumu and gg are used quite a lot in the calculation, so to speed it up, precalculate them
float mu = dot(dir, light_dir);
float mumu = mu * mu;
float gg = g * g;
float phase_ray = 3.0 / (50.2654824574 /* (16 * pi) */) * (1.0 + mumu);
float phase_mie = allow_mie ? 3.0 / (25.1327412287 /* (8 * pi) */) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg)) : 0.0;
// now we need to sample the 'primary' ray. this ray gathers the light that gets scattered onto it
for (int i = 0; i < steps_i; ++i) {
// calculate where we are along this ray
vec3 pos_i = start + dir * ray_pos_i;
// and how high we are above the surface
float height_i = length(pos_i) - planet_radius;
// now calculate the density of the particles (both for rayleigh and mie)
vec3 density = vec3(exp(-height_i / scale_height), 0.0);
// and the absorption density. this is for ozone, which scales together with the rayleigh,
// but absorbs the most at a specific height, so use the sech function for a nice curve falloff for this height
// clamp it to avoid it going out of bounds. This prevents weird black spheres on the night side
float denom = (height_absorption - height_i) / absorption_falloff;
density.z = (1.0 / (denom * denom + 1.0)) * density.x;
// multiply it by the step size here
// we are going to use the density later on as well
density *= step_size_i;
// Add these densities to the optical depth, so that we know how many particles are on this ray.
// max here is needed to prevent opt_i from potentially becoming negative
opt_i += max(density + (prev_density - density) * 0.5, 0.0);
// and update the previous density
prev_density = density;
// Calculate the step size of the light ray.
// again with a ray sphere intersect
// a, b, c and d are already defined
a = dot(light_dir, light_dir);
b = 2.0 * dot(light_dir, pos_i);
c = dot(pos_i, pos_i) - (atmo_radius * atmo_radius);
d = (b * b) - 4.0 * a * c;
// no early stopping, this one should always be inside the atmosphere
// calculate the ray length
float step_size_l = (-b + sqrt(d)) / (2.0 * a * float(steps_l));
// and the position along this ray
// this time we are sure the ray is in the atmosphere, so set it to 0
float ray_pos_l = step_size_l * 0.5;
// and the optical depth of this ray
vec3 opt_l = vec3(0.0);
// again, use the prev density for better integration
vec3 prev_density_l = vec3(0.0);
// now sample the light ray
// this is similar to what we did before
for (int l = 0; l < steps_l; ++l) {
// calculate where we are along this ray
vec3 pos_l = pos_i + light_dir * ray_pos_l;
// the heigth of the position
float height_l = length(pos_l) - planet_radius;
// calculate the particle density, and add it
// this is a bit verbose
// first, set the density for ray and mie
vec3 density_l = vec3(exp(-height_l / scale_height), 0.0);
// then, the absorption
float denom = (height_absorption - height_l) / absorption_falloff;
density_l.z = (1.0 / (denom * denom + 1.0)) * density_l.x;
// multiply the density by the step size
density_l *= step_size_l;
// and add it to the total optical depth
opt_l += max(density_l + (prev_density_l - density_l) * 0.5, 0.0);
// and update the previous density
prev_density_l = density_l;
// and increment where we are along the light ray.
ray_pos_l += step_size_l;
}
// Now we need to calculate the attenuation
// this is essentially how much light reaches the current sample point due to scattering
vec3 attn = exp(-beta_ray * (opt_i.x + opt_l.x) - beta_mie * (opt_i.y + opt_l.y) - beta_absorption * (opt_i.z + opt_l.z));
// accumulate the scattered light (how much will be scattered towards the camera)
total_ray += density.x * attn;
total_mie += density.y * attn;
// and increment the position on this ray
ray_pos_i += step_size_i;
}
// calculate how much light can pass through the atmosphere
vec3 opacity = exp(-(beta_mie * opt_i.y + beta_ray * opt_i.x + beta_absorption * opt_i.z));
// calculate and return the final color
return (
phase_ray * beta_ray * total_ray // rayleigh color
+ phase_mie * beta_mie * total_mie // mie
+ opt_i.x * beta_ambient // and ambient
) * light_intensity + scene_color * opacity; // now make sure the background is rendered correctly
}
/*
A ray-sphere intersect
This was previously used in the atmosphere as well, but it's only used for the planet intersect now, since the atmosphere has this
ray sphere intersect built in
*/
vec2 ray_sphere_intersect(
vec3 start, // starting position of the ray
vec3 dir, // the direction of the ray
float radius // and the sphere radius
) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(dir, dir);
float b = 2.0 * dot(dir, start);
float c = dot(start, start) - (radius * radius);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
/*
To make the planet we're rendering look nicer, we implemented a skylight function here
Essentially it just takes a sample of the atmosphere in the direction of the surface normal
*/
vec3 skylight(vec3 sample_pos, vec3 surface_normal, vec3 light_dir, vec3 background_col) {
// slightly bend the surface normal towards the light direction
surface_normal = normalize(mix(surface_normal, light_dir, 0.6));
// and sample the atmosphere
return calculate_scattering(
sample_pos, // the position of the camera
surface_normal, // the camera vector (ray direction of this pixel)
3.0 * ATMOS_RADIUS, // max dist, since nothing will stop the ray here, just use some arbitrary value
background_col, // scene color, just the background color here
light_dir, // light direction
vec3(40.0), // light intensity, 40 looks nice
PLANET_POS, // position of the planet
PLANET_RADIUS, // radius of the planet in meters
ATMOS_RADIUS, // radius of the atmosphere in meters
RAY_BETA, // Rayleigh scattering coefficient
MIE_BETA, // Mie scattering coefficient
ABSORPTION_BETA, // Absorbtion coefficient
AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
G, // Mie preferred scattering direction
HEIGHT_RAY, // Rayleigh scale height
HEIGHT_MIE, // Mie scale height
HEIGHT_ABSORPTION, // the height at which the most absorption happens
ABSORPTION_FALLOFF, // how fast the absorption falls off from the absorption height
LIGHT_STEPS, // steps in the ray direction
LIGHT_STEPS // steps in the light direction
);
}
/*
The following function returns the scene color and depth
(the color of the pixel without the atmosphere, and the distance to the surface that is visible on that pixel)
in this case, the function renders a green sphere on the place where the planet should be
color is in .xyz, distance in .w
I won't explain too much about how this works, since that's not the aim of this shader
*/
vec4 render_scene(vec3 pos, vec3 dir, vec3 light_dir) {
// the color to use, w is the scene depth
vec4 color = vec4(0.0, 0.0, 0.0, 1e12);
// add a sun, if the angle between the ray direction and the light direction is small enough, color the pixels white
color.xyz = vec3(dot(dir, light_dir) > 0.9998 ? 3.0 : 0.0);
// get where the ray intersects the planet
vec2 planet_intersect = ray_sphere_intersect(pos - PLANET_POS, dir, PLANET_RADIUS);
// if the ray hit the planet, set the max distance to that ray
if (0.0 < planet_intersect.y) {
color.w = max(planet_intersect.x, 0.0);
// sample position, where the pixel is
vec3 sample_pos = pos + (dir * planet_intersect.x) - PLANET_POS;
// and the surface normal
vec3 surface_normal = normalize(sample_pos);
// get the color of the sphere
color.xyz = vec3(0.0, 0.25, 0.05);
// get wether this point is shadowed, + how much light scatters towards the camera according to the lommel-seelinger law
vec3 N = surface_normal;
vec3 V = -dir;
vec3 L = light_dir;
float dotNV = max(1e-6, dot(N, V));
float dotNL = max(1e-6, dot(N, L));
float shadow = dotNL / (dotNL + dotNV);
// apply the shadow
color.xyz *= shadow;
// apply skylight
color.xyz += clamp(skylight(sample_pos, surface_normal, light_dir, vec3(0.0)) * vec3(0.0, 0.25, 0.05), 0.0, 1.0);
}
return color;
}
/*
next, we need a way to do something with the scattering function
to do something with it we need the camera vector (which is the ray direction) of the current pixel
this function calculates it
*/
vec3 get_camera_vector(vec3 resolution, vec2 coord) {
vec2 uv = coord.xy / resolution.xy - vec2(0.5);
uv.x *= resolution.x / resolution.y;
return normalize(vec3(uv.x, uv.y, -1.0));
}
/*
Finally, draw the atmosphere to screen
we first get the camera vector and position, as well as the light dir
*/
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the camera vector
vec3 camera_vector = get_camera_vector(iResolution, fragCoord);
// get the camera position, switch based on the defines
#if CAMERA_MODE==0
vec3 camera_position = vec3(0.0, PLANET_RADIUS + 100.0, 0.0);
#endif
#if CAMERA_MODE==1
vec3 camera_position = vec3(0.0, ATMOS_RADIUS , ATMOS_RADIUS);
#endif
#if CAMERA_MODE==2
vec3 camera_position = vec3(0.0, ATMOS_RADIUS + (-cos(iTime / 2.0) * (ATMOS_RADIUS - PLANET_RADIUS - 1.0)), 0.0);
#endif
#if CAMERA_MODE==3
float offset = (1.0 - cos(iTime / 2.0)) * ATMOS_RADIUS;
vec3 camera_position = vec3(0.0, PLANET_RADIUS + 1.0, offset);
#endif
// get the light direction
// also base this on the mouse position, that way the time of day can be changed with the mouse
vec3 light_dir = iMouse.y == 0.0 ?
normalize(vec3(0.0, cos(-iTime/8.0), sin(-iTime/8.0))) :
normalize(vec3(0.0, cos(iMouse.y * -5.0 / iResolution.y), sin(iMouse.y * -5.0 / iResolution.y)));
// get the scene color and depth, color is in xyz, depth in w
// replace this with something better if you are using this shader for something else
vec4 scene = render_scene(camera_position, camera_vector, light_dir);
// the color of this pixel
vec3 col = vec3(0.0);//scene.xyz;
// get the atmosphere color
col += calculate_scattering(
camera_position, // the position of the camera
camera_vector, // the camera vector (ray direction of this pixel)
scene.w, // max dist, essentially the scene depth
scene.xyz, // scene color, the color of the current pixel being rendered
light_dir, // light direction
vec3(40.0), // light intensity, 40 looks nice
PLANET_POS, // position of the planet
PLANET_RADIUS, // radius of the planet in meters
ATMOS_RADIUS, // radius of the atmosphere in meters
RAY_BETA, // Rayleigh scattering coefficient
MIE_BETA, // Mie scattering coefficient
ABSORPTION_BETA, // Absorbtion coefficient
AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
G, // Mie preferred scattering direction
HEIGHT_RAY, // Rayleigh scale height
HEIGHT_MIE, // Mie scale height
HEIGHT_ABSORPTION, // the height at which the most absorption happens
ABSORPTION_FALLOFF, // how fast the absorption falls off from the absorption height
PRIMARY_STEPS, // steps in the ray direction
LIGHT_STEPS // steps in the light direction
);
// apply exposure, removing this makes the brighter colors look ugly
// you can play around with removing this
col = 1.0 - exp(-col);
// Output to screen
fragColor = vec4(col, 1.0);
} | mit | [
17998,
18385,
18440,
18491,
19945
] | [
[
5752,
6075,
7734,
7829,
15295
],
[
15488,
15488,
15650,
15787,
16066
],
[
16068,
16254,
16344,
16413,
17996
],
[
17998,
18385,
18440,
18491,
19945
],
[
19947,
20147,
20200,
20200,
20347
],
[
20349,
20465,
20520,
20554,
23518
]
] | /*
The following function returns the scene color and depth
(the color of the pixel without the atmosphere, and the distance to the surface that is visible on that pixel)
in this case, the function renders a green sphere on the place where the planet should be
color is in .xyz, distance in .w
I won't explain too much about how this works, since that's not the aim of this shader
*/
| vec4 render_scene(vec3 pos, vec3 dir, vec3 light_dir) { |
vec4 color = vec4(0.0, 0.0, 0.0, 1e12);
// add a sun, if the angle between the ray direction and the light direction is small enough, color the pixels white
color.xyz = vec3(dot(dir, light_dir) > 0.9998 ? 3.0 : 0.0);
// get where the ray intersects the planet
vec2 planet_intersect = ray_sphere_intersect(pos - PLANET_POS, dir, PLANET_RADIUS);
// if the ray hit the planet, set the max distance to that ray
if (0.0 < planet_intersect.y) {
color.w = max(planet_intersect.x, 0.0);
// sample position, where the pixel is
vec3 sample_pos = pos + (dir * planet_intersect.x) - PLANET_POS;
// and the surface normal
vec3 surface_normal = normalize(sample_pos);
// get the color of the sphere
color.xyz = vec3(0.0, 0.25, 0.05);
// get wether this point is shadowed, + how much light scatters towards the camera according to the lommel-seelinger law
vec3 N = surface_normal;
vec3 V = -dir;
vec3 L = light_dir;
float dotNV = max(1e-6, dot(N, V));
float dotNL = max(1e-6, dot(N, L));
float shadow = dotNL / (dotNL + dotNV);
// apply the shadow
color.xyz *= shadow;
// apply skylight
color.xyz += clamp(skylight(sample_pos, surface_normal, light_dir, vec3(0.0)) * vec3(0.0, 0.25, 0.05), 0.0, 1.0);
}
return color;
} | /*
The following function returns the scene color and depth
(the color of the pixel without the atmosphere, and the distance to the surface that is visible on that pixel)
in this case, the function renders a green sphere on the place where the planet should be
color is in .xyz, distance in .w
I won't explain too much about how this works, since that's not the aim of this shader
*/
vec4 render_scene(vec3 pos, vec3 dir, vec3 light_dir) { | 1 | 1 |
wlBXWK | skythedragon | 2019-08-29T17:22:10 | /*
MIT License
Copyright (c) 2019 - 2021 Dimas "Dimev", "Skythedragon" Leenman
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.
Update 1 (25-9-2019): added 2 lines to prevent mie from shining through objects inside the atmosphere
Update 2 (2-10-2019): made use of HW_PERFORMANCE to improve performance on mobile (reduces number of samples), also added a sun
Update 3 (5-10-2019): added a license
Update 4 (28-11-2019): atmosphere now correctly blocks light from the scene passing through, and added an ambient scattering term
Update 5 (28-11-2019): mouse drag now changes the time of day
Update 6 (28-11-2019): atmosphere now doesn't use the ray sphere intersect function, meaning it's only one function
Update 7 (22-12-2019): Compacted the mie and rayleigh parts into a single vec2 + added a basic skylight
Update 8 (15-5-2020): Added ozone absorption (Can also be used as absorption in general)
Update 9 (6-5-2021): Changed the ozone distribution from 1 / cosh(x) to 1 / (x^2 + 1), and removed the clamp, better integration is planned
Update 10 (6-5-2021): Changed the integrator to be a bit better, but it might have broken it a bit as well (and it's not 100% done yet)
Update 11 (18-5-2021): Changed the integrator again, to fix it, because apparently it got worse since last update
Update 12 (19-5-2021): Found a slight issue at certain view angles backwards, fixed with a simple max
Update 13 (Planned): Change the integration again, according to seb hillaire: transmittance + total instead of optical depth and total
See Enscape clouds, this hopefully improves the quality
Scattering works by calculating how much light is scattered to the camera on a certain path/
This implementation does that by taking a number of samples across that path to check the amount of light that reaches the path
and it calculates the color of this light from the effects of scattering.
There are two types of scattering, rayleigh and mie
rayleigh is caused by small particles (molecules) and scatters certain colors better than others (causing a blue sky on earth)
mie is caused by bigger particles (like water droplets), and scatters all colors equally, but only in a certain direction.
Mie scattering causes the red sky during the sunset, because it scatters the remaining red light
To know where the ray starts and ends, we need to calculate where the ray enters and exits the atmosphere
We do this using a ray-sphere intersect
The scattering code is based on https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky
with some modifications to allow moving the planet, as well as objects inside the atmosphere, correct light absorbsion
from objects in the scene and an ambient scattering term tp light up the dark side a bit if needed
the camera also moves up and down, and the sun rotates around the planet as well
Note: Because rayleigh is a long word to type, I use ray instead on most variable names
the same goes for position (which becomes pos), direction (which becomes dir) and optical (becomes opt)
*/
// first, lets define some constants to use (planet radius, position, and scattering coefficients)
#define PLANET_POS vec3(0.0) /* the position of the planet */
#define PLANET_RADIUS 6371e3 /* radius of the planet */
#define ATMOS_RADIUS 6471e3 /* radius of the atmosphere */
// scattering coeffs
#define RAY_BETA vec3(5.5e-6, 13.0e-6, 22.4e-6) /* rayleigh, affects the color of the sky */
#define MIE_BETA vec3(21e-6) /* mie, affects the color of the blob around the sun */
#define AMBIENT_BETA vec3(0.0) /* ambient, affects the scattering color when there is no lighting from the sun */
#define ABSORPTION_BETA vec3(2.04e-5, 4.97e-5, 1.95e-6) /* what color gets absorbed by the atmosphere (Due to things like ozone) */
#define G 0.7 /* mie scattering direction, or how big the blob around the sun is */
// and the heights (how far to go up before the scattering has no effect)
#define HEIGHT_RAY 8e3 /* rayleigh height */
#define HEIGHT_MIE 1.2e3 /* and mie */
#define HEIGHT_ABSORPTION 30e3 /* at what height the absorption is at it's maximum */
#define ABSORPTION_FALLOFF 4e3 /* how much the absorption decreases the further away it gets from the maximum height */
// and the steps (more looks better, but is slower)
// the primary step has the most effect on looks
#if HW_PERFORMANCE==0
// edit these if you are on mobile
#define PRIMARY_STEPS 12
#define LIGHT_STEPS 4
# else
// and these on desktop
#define PRIMARY_STEPS 32 /* primary steps, affects quality the most */
#define LIGHT_STEPS 8 /* light steps, how much steps in the light direction are taken */
#endif
// camera mode, 0 is on the ground, 1 is in space, 2 is moving, 3 is moving from ground to space
#define CAMERA_MODE 2
/*
Next we'll define the main scattering function.
This traces a ray from start to end and takes a certain amount of samples along this ray, in order to calculate the color.
For every sample, we'll also trace a ray in the direction of the light,
because the color that reaches the sample also changes due to scattering
*/
vec3 calculate_scattering(
vec3 start, // the start of the ray (the camera position)
vec3 dir, // the direction of the ray (the camera vector)
float max_dist, // the maximum distance the ray can travel (because something is in the way, like an object)
vec3 scene_color, // the color of the scene
vec3 light_dir, // the direction of the light
vec3 light_intensity, // how bright the light is, affects the brightness of the atmosphere
vec3 planet_position, // the position of the planet
float planet_radius, // the radius of the planet
float atmo_radius, // the radius of the atmosphere
vec3 beta_ray, // the amount rayleigh scattering scatters the colors (for earth: causes the blue atmosphere)
vec3 beta_mie, // the amount mie scattering scatters colors
vec3 beta_absorption, // how much air is absorbed
vec3 beta_ambient, // the amount of scattering that always occurs, cna help make the back side of the atmosphere a bit brighter
float g, // the direction mie scatters the light in (like a cone). closer to -1 means more towards a single direction
float height_ray, // how high do you have to go before there is no rayleigh scattering?
float height_mie, // the same, but for mie
float height_absorption, // the height at which the most absorption happens
float absorption_falloff, // how fast the absorption falls off from the absorption height
int steps_i, // the amount of steps along the 'primary' ray, more looks better but slower
int steps_l // the amount of steps along the light ray, more looks better but slower
) {
// add an offset to the camera position, so that the atmosphere is in the correct position
start -= planet_position;
// calculate the start and end position of the ray, as a distance along the ray
// we do this with a ray sphere intersect
float a = dot(dir, dir);
float b = 2.0 * dot(dir, start);
float c = dot(start, start) - (atmo_radius * atmo_radius);
float d = (b * b) - 4.0 * a * c;
// stop early if there is no intersect
if (d < 0.0) return scene_color;
// calculate the ray length
vec2 ray_length = vec2(
max((-b - sqrt(d)) / (2.0 * a), 0.0),
min((-b + sqrt(d)) / (2.0 * a), max_dist)
);
// if the ray did not hit the atmosphere, return a black color
if (ray_length.x > ray_length.y) return scene_color;
// prevent the mie glow from appearing if there's an object in front of the camera
bool allow_mie = max_dist > ray_length.y;
// make sure the ray is no longer than allowed
ray_length.y = min(ray_length.y, max_dist);
ray_length.x = max(ray_length.x, 0.0);
// get the step size of the ray
float step_size_i = (ray_length.y - ray_length.x) / float(steps_i);
// next, set how far we are along the ray, so we can calculate the position of the sample
// if the camera is outside the atmosphere, the ray should start at the edge of the atmosphere
// if it's inside, it should start at the position of the camera
// the min statement makes sure of that
float ray_pos_i = ray_length.x + step_size_i * 0.5;
// these are the values we use to gather all the scattered light
vec3 total_ray = vec3(0.0); // for rayleigh
vec3 total_mie = vec3(0.0); // for mie
// initialize the optical depth. This is used to calculate how much air was in the ray
vec3 opt_i = vec3(0.0);
// we define the density early, as this helps doing integration
// usually we would do riemans summing, which is just the squares under the integral area
// this is a bit innefficient, and we can make it better by also taking the extra triangle at the top of the square into account
// the starting value is a bit inaccurate, but it should make it better overall
vec3 prev_density = vec3(0.0);
// also init the scale height, avoids some vec2's later on
vec2 scale_height = vec2(height_ray, height_mie);
// Calculate the Rayleigh and Mie phases.
// This is the color that will be scattered for this ray
// mu, mumu and gg are used quite a lot in the calculation, so to speed it up, precalculate them
float mu = dot(dir, light_dir);
float mumu = mu * mu;
float gg = g * g;
float phase_ray = 3.0 / (50.2654824574 /* (16 * pi) */) * (1.0 + mumu);
float phase_mie = allow_mie ? 3.0 / (25.1327412287 /* (8 * pi) */) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg)) : 0.0;
// now we need to sample the 'primary' ray. this ray gathers the light that gets scattered onto it
for (int i = 0; i < steps_i; ++i) {
// calculate where we are along this ray
vec3 pos_i = start + dir * ray_pos_i;
// and how high we are above the surface
float height_i = length(pos_i) - planet_radius;
// now calculate the density of the particles (both for rayleigh and mie)
vec3 density = vec3(exp(-height_i / scale_height), 0.0);
// and the absorption density. this is for ozone, which scales together with the rayleigh,
// but absorbs the most at a specific height, so use the sech function for a nice curve falloff for this height
// clamp it to avoid it going out of bounds. This prevents weird black spheres on the night side
float denom = (height_absorption - height_i) / absorption_falloff;
density.z = (1.0 / (denom * denom + 1.0)) * density.x;
// multiply it by the step size here
// we are going to use the density later on as well
density *= step_size_i;
// Add these densities to the optical depth, so that we know how many particles are on this ray.
// max here is needed to prevent opt_i from potentially becoming negative
opt_i += max(density + (prev_density - density) * 0.5, 0.0);
// and update the previous density
prev_density = density;
// Calculate the step size of the light ray.
// again with a ray sphere intersect
// a, b, c and d are already defined
a = dot(light_dir, light_dir);
b = 2.0 * dot(light_dir, pos_i);
c = dot(pos_i, pos_i) - (atmo_radius * atmo_radius);
d = (b * b) - 4.0 * a * c;
// no early stopping, this one should always be inside the atmosphere
// calculate the ray length
float step_size_l = (-b + sqrt(d)) / (2.0 * a * float(steps_l));
// and the position along this ray
// this time we are sure the ray is in the atmosphere, so set it to 0
float ray_pos_l = step_size_l * 0.5;
// and the optical depth of this ray
vec3 opt_l = vec3(0.0);
// again, use the prev density for better integration
vec3 prev_density_l = vec3(0.0);
// now sample the light ray
// this is similar to what we did before
for (int l = 0; l < steps_l; ++l) {
// calculate where we are along this ray
vec3 pos_l = pos_i + light_dir * ray_pos_l;
// the heigth of the position
float height_l = length(pos_l) - planet_radius;
// calculate the particle density, and add it
// this is a bit verbose
// first, set the density for ray and mie
vec3 density_l = vec3(exp(-height_l / scale_height), 0.0);
// then, the absorption
float denom = (height_absorption - height_l) / absorption_falloff;
density_l.z = (1.0 / (denom * denom + 1.0)) * density_l.x;
// multiply the density by the step size
density_l *= step_size_l;
// and add it to the total optical depth
opt_l += max(density_l + (prev_density_l - density_l) * 0.5, 0.0);
// and update the previous density
prev_density_l = density_l;
// and increment where we are along the light ray.
ray_pos_l += step_size_l;
}
// Now we need to calculate the attenuation
// this is essentially how much light reaches the current sample point due to scattering
vec3 attn = exp(-beta_ray * (opt_i.x + opt_l.x) - beta_mie * (opt_i.y + opt_l.y) - beta_absorption * (opt_i.z + opt_l.z));
// accumulate the scattered light (how much will be scattered towards the camera)
total_ray += density.x * attn;
total_mie += density.y * attn;
// and increment the position on this ray
ray_pos_i += step_size_i;
}
// calculate how much light can pass through the atmosphere
vec3 opacity = exp(-(beta_mie * opt_i.y + beta_ray * opt_i.x + beta_absorption * opt_i.z));
// calculate and return the final color
return (
phase_ray * beta_ray * total_ray // rayleigh color
+ phase_mie * beta_mie * total_mie // mie
+ opt_i.x * beta_ambient // and ambient
) * light_intensity + scene_color * opacity; // now make sure the background is rendered correctly
}
/*
A ray-sphere intersect
This was previously used in the atmosphere as well, but it's only used for the planet intersect now, since the atmosphere has this
ray sphere intersect built in
*/
vec2 ray_sphere_intersect(
vec3 start, // starting position of the ray
vec3 dir, // the direction of the ray
float radius // and the sphere radius
) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(dir, dir);
float b = 2.0 * dot(dir, start);
float c = dot(start, start) - (radius * radius);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
/*
To make the planet we're rendering look nicer, we implemented a skylight function here
Essentially it just takes a sample of the atmosphere in the direction of the surface normal
*/
vec3 skylight(vec3 sample_pos, vec3 surface_normal, vec3 light_dir, vec3 background_col) {
// slightly bend the surface normal towards the light direction
surface_normal = normalize(mix(surface_normal, light_dir, 0.6));
// and sample the atmosphere
return calculate_scattering(
sample_pos, // the position of the camera
surface_normal, // the camera vector (ray direction of this pixel)
3.0 * ATMOS_RADIUS, // max dist, since nothing will stop the ray here, just use some arbitrary value
background_col, // scene color, just the background color here
light_dir, // light direction
vec3(40.0), // light intensity, 40 looks nice
PLANET_POS, // position of the planet
PLANET_RADIUS, // radius of the planet in meters
ATMOS_RADIUS, // radius of the atmosphere in meters
RAY_BETA, // Rayleigh scattering coefficient
MIE_BETA, // Mie scattering coefficient
ABSORPTION_BETA, // Absorbtion coefficient
AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
G, // Mie preferred scattering direction
HEIGHT_RAY, // Rayleigh scale height
HEIGHT_MIE, // Mie scale height
HEIGHT_ABSORPTION, // the height at which the most absorption happens
ABSORPTION_FALLOFF, // how fast the absorption falls off from the absorption height
LIGHT_STEPS, // steps in the ray direction
LIGHT_STEPS // steps in the light direction
);
}
/*
The following function returns the scene color and depth
(the color of the pixel without the atmosphere, and the distance to the surface that is visible on that pixel)
in this case, the function renders a green sphere on the place where the planet should be
color is in .xyz, distance in .w
I won't explain too much about how this works, since that's not the aim of this shader
*/
vec4 render_scene(vec3 pos, vec3 dir, vec3 light_dir) {
// the color to use, w is the scene depth
vec4 color = vec4(0.0, 0.0, 0.0, 1e12);
// add a sun, if the angle between the ray direction and the light direction is small enough, color the pixels white
color.xyz = vec3(dot(dir, light_dir) > 0.9998 ? 3.0 : 0.0);
// get where the ray intersects the planet
vec2 planet_intersect = ray_sphere_intersect(pos - PLANET_POS, dir, PLANET_RADIUS);
// if the ray hit the planet, set the max distance to that ray
if (0.0 < planet_intersect.y) {
color.w = max(planet_intersect.x, 0.0);
// sample position, where the pixel is
vec3 sample_pos = pos + (dir * planet_intersect.x) - PLANET_POS;
// and the surface normal
vec3 surface_normal = normalize(sample_pos);
// get the color of the sphere
color.xyz = vec3(0.0, 0.25, 0.05);
// get wether this point is shadowed, + how much light scatters towards the camera according to the lommel-seelinger law
vec3 N = surface_normal;
vec3 V = -dir;
vec3 L = light_dir;
float dotNV = max(1e-6, dot(N, V));
float dotNL = max(1e-6, dot(N, L));
float shadow = dotNL / (dotNL + dotNV);
// apply the shadow
color.xyz *= shadow;
// apply skylight
color.xyz += clamp(skylight(sample_pos, surface_normal, light_dir, vec3(0.0)) * vec3(0.0, 0.25, 0.05), 0.0, 1.0);
}
return color;
}
/*
next, we need a way to do something with the scattering function
to do something with it we need the camera vector (which is the ray direction) of the current pixel
this function calculates it
*/
vec3 get_camera_vector(vec3 resolution, vec2 coord) {
vec2 uv = coord.xy / resolution.xy - vec2(0.5);
uv.x *= resolution.x / resolution.y;
return normalize(vec3(uv.x, uv.y, -1.0));
}
/*
Finally, draw the atmosphere to screen
we first get the camera vector and position, as well as the light dir
*/
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the camera vector
vec3 camera_vector = get_camera_vector(iResolution, fragCoord);
// get the camera position, switch based on the defines
#if CAMERA_MODE==0
vec3 camera_position = vec3(0.0, PLANET_RADIUS + 100.0, 0.0);
#endif
#if CAMERA_MODE==1
vec3 camera_position = vec3(0.0, ATMOS_RADIUS , ATMOS_RADIUS);
#endif
#if CAMERA_MODE==2
vec3 camera_position = vec3(0.0, ATMOS_RADIUS + (-cos(iTime / 2.0) * (ATMOS_RADIUS - PLANET_RADIUS - 1.0)), 0.0);
#endif
#if CAMERA_MODE==3
float offset = (1.0 - cos(iTime / 2.0)) * ATMOS_RADIUS;
vec3 camera_position = vec3(0.0, PLANET_RADIUS + 1.0, offset);
#endif
// get the light direction
// also base this on the mouse position, that way the time of day can be changed with the mouse
vec3 light_dir = iMouse.y == 0.0 ?
normalize(vec3(0.0, cos(-iTime/8.0), sin(-iTime/8.0))) :
normalize(vec3(0.0, cos(iMouse.y * -5.0 / iResolution.y), sin(iMouse.y * -5.0 / iResolution.y)));
// get the scene color and depth, color is in xyz, depth in w
// replace this with something better if you are using this shader for something else
vec4 scene = render_scene(camera_position, camera_vector, light_dir);
// the color of this pixel
vec3 col = vec3(0.0);//scene.xyz;
// get the atmosphere color
col += calculate_scattering(
camera_position, // the position of the camera
camera_vector, // the camera vector (ray direction of this pixel)
scene.w, // max dist, essentially the scene depth
scene.xyz, // scene color, the color of the current pixel being rendered
light_dir, // light direction
vec3(40.0), // light intensity, 40 looks nice
PLANET_POS, // position of the planet
PLANET_RADIUS, // radius of the planet in meters
ATMOS_RADIUS, // radius of the atmosphere in meters
RAY_BETA, // Rayleigh scattering coefficient
MIE_BETA, // Mie scattering coefficient
ABSORPTION_BETA, // Absorbtion coefficient
AMBIENT_BETA, // ambient scattering, turned off for now. This causes the air to glow a bit when no light reaches it
G, // Mie preferred scattering direction
HEIGHT_RAY, // Rayleigh scale height
HEIGHT_MIE, // Mie scale height
HEIGHT_ABSORPTION, // the height at which the most absorption happens
ABSORPTION_FALLOFF, // how fast the absorption falls off from the absorption height
PRIMARY_STEPS, // steps in the ray direction
LIGHT_STEPS // steps in the light direction
);
// apply exposure, removing this makes the brighter colors look ugly
// you can play around with removing this
col = 1.0 - exp(-col);
// Output to screen
fragColor = vec4(col, 1.0);
} | mit | [
19947,
20147,
20200,
20200,
20347
] | [
[
5752,
6075,
7734,
7829,
15295
],
[
15488,
15488,
15650,
15787,
16066
],
[
16068,
16254,
16344,
16413,
17996
],
[
17998,
18385,
18440,
18491,
19945
],
[
19947,
20147,
20200,
20200,
20347
],
[
20349,
20465,
20520,
20554,
23518
]
] | /*
next, we need a way to do something with the scattering function
to do something with it we need the camera vector (which is the ray direction) of the current pixel
this function calculates it
*/
| vec3 get_camera_vector(vec3 resolution, vec2 coord) { |
vec2 uv = coord.xy / resolution.xy - vec2(0.5);
uv.x *= resolution.x / resolution.y;
return normalize(vec3(uv.x, uv.y, -1.0));
} | /*
next, we need a way to do something with the scattering function
to do something with it we need the camera vector (which is the ray direction) of the current pixel
this function calculates it
*/
vec3 get_camera_vector(vec3 resolution, vec2 coord) { | 1 | 1 |
wtjSDW | iq | 2019-08-16T07:15:19 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Exact distance to a cone that intersects a sphere, or a
// "solid angle". Beware doing the max() of a cone and a
// sphere won't produce an exact euclidean distance.
// Based on sdPie(): https://www.shadertoy.com/view/3l23RK
// List of other 3D SDFs: https://www.shadertoy.com/playlist/43cXRl
//
// and http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
// c is the sin/cos of the desired cone angle
float sdSolidAngle(vec3 p, vec2 c, float ra)
{
vec2 q = vec2( length(p.xz), p.y );
float l = length(q) - ra;
float m = length(q - c*clamp(dot(q,c),0.0,ra) );
return max(l,m*sign(c.y*q.x-c.x*q.y));
}
float map( in vec3 pos )
{
pos.y += 0.4;
pos.xy = (mat2(4,3,-3,4)/5.0)*pos.xy;
return sdSolidAngle(pos, vec2(3,4)/5.0, 0.7 );
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps ) +
e.yyx*map( pos + e.yyx*eps ) +
e.yxy*map( pos + e.yxy*eps ) +
e.xxx*map( pos + e.xxx*eps ) );
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.5*iTime;
vec3 ro = vec3( 1.0*cos(an), 0.4, 1.0*sin(an) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// raymarch
const float tmax = 5.0;
float t = 0.0;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
float h = map(pos);
if( h<0.0001 || t>tmax ) break;
t += h;
}
// shading/lighting
vec3 col = vec3(0.0);
if( t<tmax )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal(pos);
float dif = clamp( dot(nor,vec3(0.57703)), 0.0, 1.0 );
float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0));
col = vec3(0.2,0.3,0.4)*amb + vec3(0.8,0.7,0.5)*dif;
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
1455,
1501,
1547,
1547,
1717
] | [
[
1455,
1501,
1547,
1547,
1717
],
[
1719,
1719,
1745,
1745,
1863
],
[
1865,
1929,
1961,
1961,
2200
]
] | // c is the sin/cos of the desired cone angle
| float sdSolidAngle(vec3 p, vec2 c, float ra)
{ |
vec2 q = vec2( length(p.xz), p.y );
float l = length(q) - ra;
float m = length(q - c*clamp(dot(q,c),0.0,ra) );
return max(l,m*sign(c.y*q.x-c.x*q.y));
} | // c is the sin/cos of the desired cone angle
float sdSolidAngle(vec3 p, vec2 c, float ra)
{ | 1 | 6 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
2998,
3074,
3134,
3134,
3508
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Get surface color given a material number
// plus ray intersection data.
| vec3 get_material_color(int material, cast_ray_result ray) { |
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
} | // Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
3510,
3628,
3675,
3675,
3895
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
| float get_material_reflectivity(int material) { |
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
} | // Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
3897,
3939,
3984,
3984,
4117
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Get index of refraction of a material.
| float get_material_refraction(int material) { |
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
} | // Get index of refraction of a material.
float get_material_refraction(int material) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
4119,
4209,
4251,
4251,
4384
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
| float get_material_opacity(int material) { |
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
} | // Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
4386,
4541,
4629,
4629,
5005
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
| vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) { |
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
} | // Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
5007,
5235,
5300,
5300,
5393
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
| vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) { |
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
} | // Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
5395,
5579,
5642,
5642,
5846
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
| mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) { |
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
} | // Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
5848,
6015,
6075,
6075,
7284
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
| cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) { |
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
} | // Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) { | 1 | 1 |
Wdd3zs | pineapplemachine | 2019-09-22T07:35:58 | // This shader was written by Sophie Kirschner.
// It is released under a CC0 public domain license.
// Maximum number of reflections/refractions per fragment
// Higher numbers look better but are more demanding
#define MAX_PATHS 8
// Color of the "void" - the space behind the scene
#define VOID_COLOR vec3(0.45, 0.65, 0.8)
// Set to 1.0 for a nice typical viewing experience.
// Set higher to reduce the FOV and produce a zoomed-in effect.
// Set lower to increase FOV and produce a fisheye effect.
#define CAMERA_ZOOM 1.0
// The scene is illuminated by a directional light and
// an ambient light; their parameters are defined here.
#define LIGHT_DIRECTION normalize(vec3(-4.7, -4.2, 9.5))
#define AMBIENT_LIGHT 0.3
// Name the various recognized material numbers.
#define MATERIAL_NONE 0
#define MATERIAL_RED_LIGHTER 1
#define MATERIAL_RED_DARKER 2
#define MATERIAL_GREEN_LIGHTER 3
#define MATERIAL_GREEN_DARKER 4
#define MATERIAL_PLANE 5
// Number of vertices in the scene's vertices[] array
#define NUM_VERTICES 17
// Number of triangles in the scene's triangles[] array
#define NUM_TRIANGLES 14
// Struct returned by the cast_ray function
struct cast_ray_result {
// Identify the triangle within the triangles[] array
// that the ray intersected
int tri_index;
// UV describing where on the triangle the intersection occurred
vec2 uv;
// Distance to the intersected point on the triangle
float dist;
// 3D position of the ray/triangle intersection
vec3 intersection;
};
// Describe the position of verticies in 3D space
const vec3 vertices[NUM_VERTICES] = vec3[NUM_VERTICES](
// Big Transparent Pyramid
vec3(+0.0, +2.0, +0.0),
vec3(+1.0, +0.0, +1.0),
vec3(-1.0, +0.0, +1.0),
vec3(+1.0, +0.0, -1.0),
vec3(-1.0, +0.0, -1.0),
// Small Green Pyramid
vec3(+2.0, +1.0, +2.0),
vec3(+2.5, +0.0, +2.5),
vec3(+1.5, +0.0, +2.5),
vec3(+2.5, +0.0, +1.5),
vec3(+1.5, +0.0, +1.5),
// Plane
vec3(-4.0, +0.0, -4.0),
vec3(-4.0, +0.0, +4.0),
vec3(+4.0, +0.0, -4.0),
vec3(+4.0, +0.0, +4.0),
vec3(+4.0, +1.0, -4.0),
vec3(+4.0, +1.0, +4.0),
vec3(-4.0, +1.0, +4.0)
);
// Describe triangles by identifying their verticies.
// The w component describes the triangle's material.
const ivec4 triangles[NUM_TRIANGLES] = ivec4[NUM_TRIANGLES](
// Big Transparent Pyramid
ivec4(0, 2, 1, MATERIAL_RED_LIGHTER),
ivec4(0, 4, 2, MATERIAL_RED_DARKER),
ivec4(0, 3, 4, MATERIAL_RED_LIGHTER),
ivec4(0, 1, 3, MATERIAL_RED_DARKER),
// Small Green Pyramid
ivec4(5, 7, 6, MATERIAL_GREEN_DARKER),
ivec4(5, 9, 7, MATERIAL_GREEN_LIGHTER),
ivec4(5, 8, 9, MATERIAL_GREEN_DARKER),
ivec4(5, 6, 8, MATERIAL_GREEN_LIGHTER),
// Plane
ivec4(10, 11, 12, MATERIAL_PLANE),
ivec4(13, 12, 11, MATERIAL_PLANE),
ivec4(14, 12, 13, MATERIAL_PLANE),
ivec4(15, 14, 13, MATERIAL_PLANE),
ivec4(15, 13, 16, MATERIAL_PLANE),
ivec4(13, 11, 16, MATERIAL_PLANE)
);
// Get surface color given a material number
// plus ray intersection data.
vec3 get_material_color(int material, cast_ray_result ray) {
return (
material == MATERIAL_RED_LIGHTER ? vec3(1.0, 0.8, 0.8) :
material == MATERIAL_RED_DARKER ? vec3(1.0, 0.4, 0.4) :
material == MATERIAL_GREEN_LIGHTER ? vec3(0.1, 0.8, 0.3) :
material == MATERIAL_GREEN_DARKER ? vec3(0.0, 0.6, 0.1) :
material == MATERIAL_PLANE ? vec3(0.25 + ray.uv * 0.5, 0.75) :
VOID_COLOR
);
}
// Get reflectivity of a material.
// 0.0 is not reflective at all.
// 1.0 is maximally reflective, a perfect mirror.
float get_material_reflectivity(int material) {
return (
material == MATERIAL_NONE ? 0.0 :
material == MATERIAL_PLANE ? 0.8 :
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
0.125
);
}
// Get index of refraction of a material.
float get_material_refraction(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 1.25 :
material == MATERIAL_RED_DARKER ? 1.25 :
0.0
);
}
// Get opacity of a material.
// 0.0 is totally transparent.
// 1.0 is completely opaque.
float get_material_opacity(int material) {
return (
material == MATERIAL_RED_LIGHTER ? 0.25 :
material == MATERIAL_RED_DARKER ? 0.25 :
1.0
);
}
// Determine whether a line intersects a triangle.
// Returns a vector whose components are: (intersected?, U, V)
// https://www.shadertoy.com/view/MlGcDz
vec3 line_intersects_tri(vec3 line_a, vec3 line_b, vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 v1v0 = tri_b - tri_a;
vec3 v2v0 = tri_c - tri_a;
vec3 rov0 = line_a - tri_a;
vec3 n = cross(v1v0, v2v0);
vec3 q = cross(rov0, line_b);
float d = 1.0 / dot(line_b, n);
float u = d * dot(-q, v2v0);
float v = d * dot(q, v1v0);
float t = d * dot(-n, rov0);
if(u < 0.0 || v < 0.0 || (u + v) > 1.0) t = -1.0;
return vec3(t, u, v);
}
// Given the three points of a triangle in clockwise order,
// compute the surface normal of that triangle.
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal#targetText=A%20surface%20normal%20for%20a,winding).
vec3 get_tri_surface_normal(vec3 tri_a, vec3 tri_b, vec3 tri_c) {
vec3 u = tri_b - tri_a;
vec3 v = tri_c - tri_a;
return normalize(cross(u, v));
}
// Generate a look-at rotation matrix based on a camera
// position and view target.
// https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function
mat3 look_at_matrix(vec3 camera_position, vec3 camera_target) {
vec3 forward = normalize(camera_target - camera_position);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
vec3 up = cross(forward, right);
return mat3(right, up, forward);
}
// Check all triangles in the scene for intersection with a ray.
// Return information about the intersection nearest to ray_origin,
// if there was any intersection.
cast_ray_result cast_ray(vec3 ray_origin, vec3 ray_target) {
int result_tri_index = -1;
vec2 result_uv = vec2(0.0);
float result_dist = 1e18;
vec3 result_intersection = vec3(0.0);
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5 &&
tri_distance < result_dist
) {
result_tri_index = tri_index;
result_uv = intersection.yz;
result_dist = tri_distance;
result_intersection = intersection_point;
}
}
return cast_ray_result(
result_tri_index,
result_uv,
result_dist,
result_intersection
);
}
// Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) {
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
}
// Trace the path of a ray, i.e. from the camera position to a
// ray direction depending on a fragment's position in the render.
// The function will incorporate up to MAX_PATHS reflections and
// refractions in the final sample color.
vec3 sample_ray(vec3 ray_origin, vec3 ray_target) {
// Initialize the ray queue -
// list of ray paths that should contribute to this sample
// It will initially contain only the input sample
int next_path_index = 1;
vec3 queued_ray_origin[MAX_PATHS];
vec3 queued_ray_target[MAX_PATHS];
float queued_ray_weight[MAX_PATHS];
float color_accumulator_weight = 0.0;
vec3 color_accumulator = vec3(0.0);
queued_ray_origin[0] = ray_origin;
queued_ray_target[0] = ray_target;
queued_ray_weight[0] = 1.0;
// Enumerate rays in the queue
for(int path_index = 0; path_index < MAX_PATHS; path_index++) {
// Check for queue exhaustion
if(path_index >= next_path_index) {
break;
}
// Ignore rays with a very small contribution to the overall
// sample result
float this_ray_weight = queued_ray_weight[path_index];
if(this_ray_weight < 0.02) {
continue;
}
// Time to trace the ray
vec3 this_ray_origin = queued_ray_origin[path_index];
vec3 this_ray_target = queued_ray_target[path_index];
cast_ray_result this_ray = cast_ray(
this_ray_origin, this_ray_target
);
vec3 tri_normal = get_tri_surface_normal(
vertices[triangles[this_ray.tri_index].x],
vertices[triangles[this_ray.tri_index].y],
vertices[triangles[this_ray.tri_index].z]
);
// Get material properties for the intersected triangle
int material = (this_ray.tri_index >= 0 ?
triangles[this_ray.tri_index].w : MATERIAL_NONE
);
vec3 material_color = get_material_color(material, this_ray);
float material_reflectivity = get_material_reflectivity(material);
float material_opacity = get_material_opacity(material);
// Calculate diffuse directional lighting with shadows
float shadow_amount = cast_shadow_ray(
this_ray.intersection, -LIGHT_DIRECTION
);
float diffuse_light_intensity = AMBIENT_LIGHT + max(0.0,
(1.0 - AMBIENT_LIGHT) * 2.0 * dot(tri_normal, -LIGHT_DIRECTION)
);
float light_intensity = clamp(
AMBIENT_LIGHT + diffuse_light_intensity - shadow_amount,
AMBIENT_LIGHT, 1.0
);
// Cast a reflection ray
// http://paulbourke.net/geometry/reflected/
// https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html
if(material_reflectivity > 1e-3) {
vec3 reflected_ray_target = this_ray_target - (
2.0 * tri_normal * dot(this_ray_target, tri_normal)
);
queued_ray_weight[next_path_index] = (
this_ray_weight * material_reflectivity
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = reflected_ray_target;
next_path_index++;
}
// Cast a refracted ray for transparent surfaces
if(material_opacity < (1.0 - 1e-3)) {
// Compute the refracted ray direction
// https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading/reflection-refraction-fresnel
float refraction = get_material_refraction(material);
float iof_before = 1.0;
float iof_after = refraction;
float cos_incidence = dot(tri_normal, this_ray_target);
vec3 refraction_normal = tri_normal;
if(cos_incidence < 0.0) {
cos_incidence = -cos_incidence;
}
else {
iof_before = refraction;
iof_after = 1.0;
refraction_normal = -refraction_normal;
}
float eta = iof_before / iof_after;
float k = 1.0 - eta * eta * (1.0 - cos_incidence * cos_incidence);
vec3 refracted_ray_target = (
k < 0.0 ? this_ray_target :
eta * this_ray_target + (eta * cos_incidence - sqrt(k)) * refraction_normal
);
// Add refraced ray to the queue
queued_ray_weight[next_path_index] = (
this_ray_weight * (1.0 - material_opacity)
);
queued_ray_origin[next_path_index] = this_ray.intersection;
queued_ray_target[next_path_index] = refracted_ray_target;
next_path_index++;
}
// Determine the color sampled at the end of this ray
// and add it to the accumulator
vec3 this_sample_color = light_intensity * material_color;
float this_sample_weight = max(0.0, (
this_ray_weight * material_opacity * (1.0 + light_intensity)
));
color_accumulator = (
color_accumulator * color_accumulator_weight +
this_sample_color * this_sample_weight
) / (
color_accumulator_weight + this_sample_weight
);
color_accumulator_weight += this_sample_weight;
}
// No more paths to trace! Return the final sample color.
return color_accumulator;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Describe the position of the camera in the scene
float t = 1.5 + 0.75 * iTime;
vec3 camera_position = vec3(6.0 * cos(t), 3.5, 6.0 * sin(t));
vec3 camera_target = vec3(0.0, 0.0, 0.0);
mat3 camera_rot_matrix = look_at_matrix(
camera_position, camera_target
);
// Determine the direction of the ray
// Rays toward the center of the view travel in a more
// directly forward direction; rays toward the edges of
// the view travel at more of an angle to the camera.
// This produces a nice field-of-view effect.
vec2 ray_coord = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
vec3 forward_ray_direction = normalize(
vec3(ray_coord.x, ray_coord.y, CAMERA_ZOOM)
);
vec3 ray_direction = camera_rot_matrix * forward_ray_direction;
// Calculate the color of the fragment at this location.
vec3 sample_color = sample_ray(camera_position, ray_direction);
fragColor = vec4(sample_color, 1.0);
}
| cc0-1.0 | [
7286,
7453,
7510,
7510,
8476
] | [
[
2998,
3074,
3134,
3134,
3508
],
[
3510,
3628,
3675,
3675,
3895
],
[
3897,
3939,
3984,
3984,
4117
],
[
4119,
4209,
4251,
4251,
4384
],
[
4386,
4541,
4629,
4629,
5005
],
[
5007,
5235,
5300,
5300,
5393
],
[
5395,
5579,
5642,
5642,
5846
],
[
5848,
6015,
6075,
6075,
7284
],
[
7286,
7453,
7510,
7510,
8476
],
[
8478,
8715,
8766,
8918,
13848
],
[
13850,
13850,
13905,
13961,
14886
]
] | // Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
| float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) { |
float shadow_amount = 0.0;
for(int tri_index = 0; tri_index < NUM_TRIANGLES; tri_index++) {
vec3 tri_a = vertices[triangles[tri_index].x];
vec3 tri_b = vertices[triangles[tri_index].y];
vec3 tri_c = vertices[triangles[tri_index].z];
vec3 intersection = line_intersects_tri(
ray_origin, ray_target, tri_a, tri_b, tri_c
);
vec3 intersection_point = (
(tri_b * intersection.y) +
(tri_c * intersection.z) +
(tri_a * (1.0 - intersection.y - intersection.z))
);
float tri_distance = length(
ray_origin - intersection_point
);
if(intersection.x > 0.0 &&
tri_distance > 1e-5
) {
shadow_amount += (
get_material_opacity(triangles[tri_index].w)
);
if(shadow_amount >= 1.0) {
break;
}
}
}
return shadow_amount;
} | // Cast a ray from a surface toward the scene's directional light
// source and determine how much in shadow the surface is.
// Considers opacity, but not refraction.
float cast_shadow_ray(vec3 ray_origin, vec3 ray_target) { | 1 | 1 |
Wt2XzK | iq | 2019-09-13T08:10:46 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader shows how to control the velocity of an
// object that is being animated with a sin() function.
//
// When animating something like angle=sin(v*t), with t being
// the time (iTime for example), w is only the (angular)
// velocity of the object if v is a constant. Hence, doing
// v(t) = smoothstep(0,1,t) for exampe will result in an
// acceleration and a deceleration, which is probably not
// intenteded.
//
// To get the desired behaviour you need to define your v(t),
// integrate it to get w(t), then plug it into the sin:
//
// v(t) = smoothste(0,1,t) if t<1
// v(t) = 1 if t>=1
//
// Then you integrate that to get
//
// w(t) = t*t*t*(1.0 - t/2.0) if t<1
// w(t) = t-0.5 if t>=1
//
// Try using the incorrect method below and restarting the
// shader in order to see the difference:
// 0: incorrect method: sin(v(t)*t)
// 1: correct method: sin(w(t))
#define METHOD 1
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 iBox( in vec3 ro, in vec3 rd, in mat4 tx, in vec3 rad )
{
vec3 rdd = (tx*vec4(rd,0.0)).xyz;
vec3 roo = (tx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rdd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
return vec4( tN, nor );
}
mat4 rotationAxisAngle( vec3 v, float angle )
{
float s = sin( angle );
float c = cos( angle );
float ic = 1.0 - c;
return mat4( v.x*v.x*ic + c, v.y*v.x*ic - s*v.z, v.z*v.x*ic + s*v.y, 0.0,
v.x*v.y*ic + s*v.z, v.y*v.y*ic + c, v.z*v.y*ic - s*v.x, 0.0,
v.x*v.z*ic - s*v.y, v.y*v.z*ic + s*v.x, v.z*v.z*ic + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy) / iResolution.y;
vec3 ro = vec3( 0.0, 0.0, 2.2 );
vec3 rd = normalize( vec3(p.xy,-1.8) );
#if METHOD==0
// WRONG
float animation = smoothstep(0.0,1.0,iTime)*iTime;
#else
// CORRECT
float animation = (iTime<1.0) ?
iTime*iTime*iTime*(1.0 - iTime/2.0)
:
iTime-0.5;
#endif
mat4 txi = rotationAxisAngle( normalize(vec3(1.0,1.0,0.0)), 0.5*animation-1.0 );
vec3 col = vec3(0.1);
const vec3 box = vec3(0.4,0.6,0.8) ;
vec4 res = iBox( ro, rd, txi, box);
if( res.x>0.0 )
{
vec3 onor = res.yzw;
vec3 wpos = ro + res.x*rd;
mat4 txx = inverse( txi );
vec3 opos = (txi*vec4(wpos,1.0)).xyz;
vec3 wnor = (txx*vec4(onor,0.0)).xyz;
col = vec3(1.0,0.5,0.1)*(0.5 + 0.5*wnor.y);
col *= 1.0 - (1.0-abs(onor.x))*smoothstep( box.x-0.04, box.x-0.02, abs(opos.x) );
col *= 1.0 - (1.0-abs(onor.y))*smoothstep( box.y-0.04, box.y-0.02, abs(opos.y) );
col *= 1.0 - (1.0-abs(onor.z))*smoothstep( box.z-0.04, box.z-0.02, abs(opos.z) );
}
col = sqrt( col );
fragColor = vec4( col, 1.0 );
} | mit | [
2023,
2091,
2154,
2154,
2559
] | [
[
2023,
2091,
2154,
2154,
2559
],
[
2561,
2561,
2608,
2608,
3011
],
[
3013,
3013,
3070,
3070,
4240
]
] | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
| vec4 iBox( in vec3 ro, in vec3 rd, in mat4 tx, in vec3 rad )
{ |
vec3 rdd = (tx*vec4(rd,0.0)).xyz;
vec3 roo = (tx*vec4(ro,1.0)).xyz;
vec3 m = 1.0/rdd;
vec3 n = m*roo;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rdd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
return vec4( tN, nor );
} | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 iBox( in vec3 ro, in vec3 rd, in mat4 tx, in vec3 rad )
{ | 1 | 1 |
3sd3Rs | iq | 2019-10-07T04:24:59 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This is a 1D noise that uses a single random number/hash
// per invocation, instead of two like regular value and gradient
// noises do. This means no neighboring information is needed
// in order to preserve continuity, and therefore it's much faster
// than both Value and Gradient Noise.
//
// In fact, this is a hybrid between the two - it has zeros at
// integer locations like Gradient noise, but the gradients/
// derivatives are constant, +-1. That way each cycle's boundaries
// are fixed and continuity is preserved always. Then, the single
// per cycle random number K controls the value of the signal's
// peak, a bit like in a Value Noise. A quartic function is used
// to interpolate the whole curve inside the cycle.
//
// p(0)=0, p'(0)=1, p(1)=0, p'(1)=-1, p(1/2)=k, p'(1/2)=0
//
// results in
//
// p(x)=x·(x-1)·((16k-4)·x·(x-1)-1)
//
// The yellow curve shows this new Basic Noise, superimposed on
// top of a regular Gradient Noise in dark grey.
float hash( uint n );
// Basic noise
float bnoise( in float x )
{
// setup
float i = floor(x);
float f = fract(x);
float s = sign(fract(x/2.0)-0.5);
// use some hash to create a random value k in [0..1] from i
//float k = hash(uint(i));
//float k = 0.5+0.5*sin(i);
float k = fract(i*.1731);
// quartic polynomial
return s*f*(f-1.0)*((16.0*k-4.0)*f*(f-1.0)-1.0);
}
// Traditional gradient noise
float gnoise( in float p )
{
uint i = uint(floor(p));
float f = fract(p);
float u = f*f*(3.0-2.0*f);
float g0 = hash(i+0u)*2.0-1.0;
float g1 = hash(i+1u)*2.0-1.0;
return 2.4*mix( g0*(f-0.0), g1*(f-1.0), u);
}
////////////////////////////////////
float fbm( in float x )
{
float n = 0.0;
float s = 1.0;
for( int i=0; i<9; i++ )
{
n += s*bnoise(x);
s *= 0.5;
x *= 2.0;
x += 0.131;
}
return n;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
float px = 1.0/iResolution.y;
vec2 p = fragCoord*px;
vec3 col = vec3( 0.0 );
col = mix( col, vec3(0.7), 1.0 - smoothstep( 0.0, 2.0*px, abs( p.y - 0.75 ) ) );
col = mix( col, vec3(0.7), 1.0 - smoothstep( 0.0, 2.0*px, abs( p.y - 0.25 ) ) );
p.x += iTime*0.1;
{
float y = 0.75+0.25*gnoise( 6.0*p.x );
col = mix( col, vec3(0.3,0.3,0.3), 1.0 - smoothstep( 0.0, 4.0*px, abs(p.y-y) ) );
}
{
float y = 0.75+0.25*bnoise( 6.0*p.x );
col = mix( col, vec3(1.0,1.0,0.0), 1.0 - smoothstep( 0.0, 4.0*px, abs(p.y-y) ) );
}
{
float y = 0.25+0.15*fbm( 2.0*p.x );
col = mix( col, vec3(1.0,0.6,0.2), 1.0 - smoothstep( 0.0, 4.0*px, abs(p.y-y) ) );
}
fragColor = vec4( col, 1.0 );
}
float hash( uint n )
{ // integer hash copied from Hugo Elias
n = (n<<13U)^n;
n = n*(n*n*15731U+789221U)+1376312589U;
return float(n&uvec3(0x0fffffffU))/float(0x0fffffff);
}
| mit | [
2074,
2089,
2117,
2134,
2461
] | [
[
2074,
2089,
2117,
2134,
2461
],
[
2463,
2493,
2521,
2521,
2724
],
[
2764,
2764,
2789,
2789,
2970
],
[
2973,
2973,
3030,
3030,
3778
],
[
3780,
3780,
3803,
3844,
3966
]
] | // Basic noise
| float bnoise( in float x )
{ |
float i = floor(x);
float f = fract(x);
float s = sign(fract(x/2.0)-0.5);
// use some hash to create a random value k in [0..1] from i
//float k = hash(uint(i));
//float k = 0.5+0.5*sin(i);
float k = fract(i*.1731);
// quartic polynomial
return s*f*(f-1.0)*((16.0*k-4.0)*f*(f-1.0)-1.0);
} | // Basic noise
float bnoise( in float x )
{ | 2 | 4 |
3sd3Rs | iq | 2019-10-07T04:24:59 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This is a 1D noise that uses a single random number/hash
// per invocation, instead of two like regular value and gradient
// noises do. This means no neighboring information is needed
// in order to preserve continuity, and therefore it's much faster
// than both Value and Gradient Noise.
//
// In fact, this is a hybrid between the two - it has zeros at
// integer locations like Gradient noise, but the gradients/
// derivatives are constant, +-1. That way each cycle's boundaries
// are fixed and continuity is preserved always. Then, the single
// per cycle random number K controls the value of the signal's
// peak, a bit like in a Value Noise. A quartic function is used
// to interpolate the whole curve inside the cycle.
//
// p(0)=0, p'(0)=1, p(1)=0, p'(1)=-1, p(1/2)=k, p'(1/2)=0
//
// results in
//
// p(x)=x·(x-1)·((16k-4)·x·(x-1)-1)
//
// The yellow curve shows this new Basic Noise, superimposed on
// top of a regular Gradient Noise in dark grey.
float hash( uint n );
// Basic noise
float bnoise( in float x )
{
// setup
float i = floor(x);
float f = fract(x);
float s = sign(fract(x/2.0)-0.5);
// use some hash to create a random value k in [0..1] from i
//float k = hash(uint(i));
//float k = 0.5+0.5*sin(i);
float k = fract(i*.1731);
// quartic polynomial
return s*f*(f-1.0)*((16.0*k-4.0)*f*(f-1.0)-1.0);
}
// Traditional gradient noise
float gnoise( in float p )
{
uint i = uint(floor(p));
float f = fract(p);
float u = f*f*(3.0-2.0*f);
float g0 = hash(i+0u)*2.0-1.0;
float g1 = hash(i+1u)*2.0-1.0;
return 2.4*mix( g0*(f-0.0), g1*(f-1.0), u);
}
////////////////////////////////////
float fbm( in float x )
{
float n = 0.0;
float s = 1.0;
for( int i=0; i<9; i++ )
{
n += s*bnoise(x);
s *= 0.5;
x *= 2.0;
x += 0.131;
}
return n;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
float px = 1.0/iResolution.y;
vec2 p = fragCoord*px;
vec3 col = vec3( 0.0 );
col = mix( col, vec3(0.7), 1.0 - smoothstep( 0.0, 2.0*px, abs( p.y - 0.75 ) ) );
col = mix( col, vec3(0.7), 1.0 - smoothstep( 0.0, 2.0*px, abs( p.y - 0.25 ) ) );
p.x += iTime*0.1;
{
float y = 0.75+0.25*gnoise( 6.0*p.x );
col = mix( col, vec3(0.3,0.3,0.3), 1.0 - smoothstep( 0.0, 4.0*px, abs(p.y-y) ) );
}
{
float y = 0.75+0.25*bnoise( 6.0*p.x );
col = mix( col, vec3(1.0,1.0,0.0), 1.0 - smoothstep( 0.0, 4.0*px, abs(p.y-y) ) );
}
{
float y = 0.25+0.15*fbm( 2.0*p.x );
col = mix( col, vec3(1.0,0.6,0.2), 1.0 - smoothstep( 0.0, 4.0*px, abs(p.y-y) ) );
}
fragColor = vec4( col, 1.0 );
}
float hash( uint n )
{ // integer hash copied from Hugo Elias
n = (n<<13U)^n;
n = n*(n*n*15731U+789221U)+1376312589U;
return float(n&uvec3(0x0fffffffU))/float(0x0fffffff);
}
| mit | [
2463,
2493,
2521,
2521,
2724
] | [
[
2074,
2089,
2117,
2134,
2461
],
[
2463,
2493,
2521,
2521,
2724
],
[
2764,
2764,
2789,
2789,
2970
],
[
2973,
2973,
3030,
3030,
3778
],
[
3780,
3780,
3803,
3844,
3966
]
] | // Traditional gradient noise
| float gnoise( in float p )
{ |
uint i = uint(floor(p));
float f = fract(p);
float u = f*f*(3.0-2.0*f);
float g0 = hash(i+0u)*2.0-1.0;
float g1 = hash(i+1u)*2.0-1.0;
return 2.4*mix( g0*(f-0.0), g1*(f-1.0), u);
} | // Traditional gradient noise
float gnoise( in float p )
{ | 1 | 2 |
WsV3zz | iq | 2019-10-01T00:21:04 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Shows the anisotropic self-similarity of Brownian Motion
// on the left and the isotropic self-similarity of Fractional
// Brownian Motion if gain=0.5.
//
// On the left, a clasical Brownian Motion is generated, which
// is a process with Hurst Exponet H=1/2 (uncorrelated deltas,
// no memory). Such curve has a fractal dimension of 1.5 and
// needs a vertical scaling factor of sqrt(x) when it's scaled
// by x horizontally. It's power spectrum decays as f^-2
//
// On the right, a Fractional Brownian Motion with H=1
// which means a gain G of 0.5. It's a long memory curve
// with possitively correlated increments, has a fractal
// of 1, and is naturally istropicaly self-similar (non-
// distorted zoom). Because of that, it's what we use to
// mimic mountains. It's power spectrum decays as f^-3
//
// More info: http://iquilezles.org/www/articles/fbm/fbm.htm
// integer hash copied from Hugo Elias
float hash( int n )
{
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return -1.0+2.0*float( n & ivec3(0x0fffffff))/float(0x0fffffff);
}
// gradient noise
float gnoise( in float p )
{
int i = int(floor(p));
float f = fract(p);
float u = f*f*(3.0-2.0*f);
return mix( hash(i+0)*(f-0.0),
hash(i+1)*(f-1.0), u);
}
// fbm
float fbm( in float x, in float G )
{
x += 26.06;
float n = 0.0;
float s = 1.0;
float a = 0.0;
float f = 1.0;
for( int i=0; i<16; i++ )
{
n += s*gnoise(x*f);
a += s;
s *= G;
f *= 2.0;
x += 0.31;
}
return n;
}
vec3 anim( in vec2 p, float time )
{
vec3 col = vec3(0.0);
//float ani = fract(time/4.0);
float ani = smoothstep(0.0,1.0,fract(time/4.0));
float zoom = pow( 2.0, 6.0*ani );
if( p.x<0.0 )
{
vec2 q = vec2(p.x*0.5 + 0.5,p.y);
float G = 0.707107;
float comp = zoom;
float comp2 = sqrt(comp);
if( q.y<0.0 )
{
float y = -0.5+0.5*comp2*(fbm(0.8*q.x/comp, G ));
y += zoom*0.004;
col = mix( col, vec3(1.0,1.0,0.5).zyx, 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
else
{
float y = 0.5+0.5*fbm(0.8*q.x, G );
col = mix( col, vec3(1.0,0.5,0.0).zyx, (1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y)));
}
}
else
{
vec2 q = vec2(p.x*0.5 - 0.5,p.y);
float G = 0.5;
float comp = zoom;
float comp2 = comp;
if( p.y<0.0 )
{
float y = -0.5+0.9*comp2*(fbm(1.0*q.x/comp, G ));
y += zoom*0.004;
col = mix( col, vec3(1.0,1.0,0.5), 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
else
{
float y = 0.5+0.9*fbm(1.0*q.x, G );
col = mix( col, vec3(1.0,0.5,0.0), 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
}
col *= smoothstep(0.01,0.02,abs(p.x) );
col *= smoothstep(0.01,0.02,abs(p.y) );
return col;
}
#define AA 5
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 col = vec3(0.0);
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
vec2 o = vec2(m,n)/float(AA);
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
float time = iTime + 0.5*(1.0/24.0)*(float(m*AA+n)+d)/float(AA*AA);
col += anim(p,iTime);
}
col /= float(AA*AA);
fragColor = vec4( col, 1.0 );
}
| mit | [
1950,
1989,
2011,
2011,
2153
] | [
[
1950,
1989,
2011,
2011,
2153
],
[
2155,
2173,
2201,
2201,
2359
],
[
2361,
2368,
2405,
2405,
2660
],
[
2662,
2662,
2698,
2698,
4074
]
] | // integer hash copied from Hugo Elias
| float hash( int n )
{ |
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return -1.0+2.0*float( n & ivec3(0x0fffffff))/float(0x0fffffff);
} | // integer hash copied from Hugo Elias
float hash( int n )
{ | 1 | 1 |
WsV3zz | iq | 2019-10-01T00:21:04 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Shows the anisotropic self-similarity of Brownian Motion
// on the left and the isotropic self-similarity of Fractional
// Brownian Motion if gain=0.5.
//
// On the left, a clasical Brownian Motion is generated, which
// is a process with Hurst Exponet H=1/2 (uncorrelated deltas,
// no memory). Such curve has a fractal dimension of 1.5 and
// needs a vertical scaling factor of sqrt(x) when it's scaled
// by x horizontally. It's power spectrum decays as f^-2
//
// On the right, a Fractional Brownian Motion with H=1
// which means a gain G of 0.5. It's a long memory curve
// with possitively correlated increments, has a fractal
// of 1, and is naturally istropicaly self-similar (non-
// distorted zoom). Because of that, it's what we use to
// mimic mountains. It's power spectrum decays as f^-3
//
// More info: http://iquilezles.org/www/articles/fbm/fbm.htm
// integer hash copied from Hugo Elias
float hash( int n )
{
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return -1.0+2.0*float( n & ivec3(0x0fffffff))/float(0x0fffffff);
}
// gradient noise
float gnoise( in float p )
{
int i = int(floor(p));
float f = fract(p);
float u = f*f*(3.0-2.0*f);
return mix( hash(i+0)*(f-0.0),
hash(i+1)*(f-1.0), u);
}
// fbm
float fbm( in float x, in float G )
{
x += 26.06;
float n = 0.0;
float s = 1.0;
float a = 0.0;
float f = 1.0;
for( int i=0; i<16; i++ )
{
n += s*gnoise(x*f);
a += s;
s *= G;
f *= 2.0;
x += 0.31;
}
return n;
}
vec3 anim( in vec2 p, float time )
{
vec3 col = vec3(0.0);
//float ani = fract(time/4.0);
float ani = smoothstep(0.0,1.0,fract(time/4.0));
float zoom = pow( 2.0, 6.0*ani );
if( p.x<0.0 )
{
vec2 q = vec2(p.x*0.5 + 0.5,p.y);
float G = 0.707107;
float comp = zoom;
float comp2 = sqrt(comp);
if( q.y<0.0 )
{
float y = -0.5+0.5*comp2*(fbm(0.8*q.x/comp, G ));
y += zoom*0.004;
col = mix( col, vec3(1.0,1.0,0.5).zyx, 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
else
{
float y = 0.5+0.5*fbm(0.8*q.x, G );
col = mix( col, vec3(1.0,0.5,0.0).zyx, (1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y)));
}
}
else
{
vec2 q = vec2(p.x*0.5 - 0.5,p.y);
float G = 0.5;
float comp = zoom;
float comp2 = comp;
if( p.y<0.0 )
{
float y = -0.5+0.9*comp2*(fbm(1.0*q.x/comp, G ));
y += zoom*0.004;
col = mix( col, vec3(1.0,1.0,0.5), 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
else
{
float y = 0.5+0.9*fbm(1.0*q.x, G );
col = mix( col, vec3(1.0,0.5,0.0), 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
}
col *= smoothstep(0.01,0.02,abs(p.x) );
col *= smoothstep(0.01,0.02,abs(p.y) );
return col;
}
#define AA 5
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 col = vec3(0.0);
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
vec2 o = vec2(m,n)/float(AA);
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
float time = iTime + 0.5*(1.0/24.0)*(float(m*AA+n)+d)/float(AA*AA);
col += anim(p,iTime);
}
col /= float(AA*AA);
fragColor = vec4( col, 1.0 );
}
| mit | [
2155,
2173,
2201,
2201,
2359
] | [
[
1950,
1989,
2011,
2011,
2153
],
[
2155,
2173,
2201,
2201,
2359
],
[
2361,
2368,
2405,
2405,
2660
],
[
2662,
2662,
2698,
2698,
4074
]
] | // gradient noise
| float gnoise( in float p )
{ |
int i = int(floor(p));
float f = fract(p);
float u = f*f*(3.0-2.0*f);
return mix( hash(i+0)*(f-0.0),
hash(i+1)*(f-1.0), u);
} | // gradient noise
float gnoise( in float p )
{ | 1 | 2 |
WsV3zz | iq | 2019-10-01T00:21:04 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Shows the anisotropic self-similarity of Brownian Motion
// on the left and the isotropic self-similarity of Fractional
// Brownian Motion if gain=0.5.
//
// On the left, a clasical Brownian Motion is generated, which
// is a process with Hurst Exponet H=1/2 (uncorrelated deltas,
// no memory). Such curve has a fractal dimension of 1.5 and
// needs a vertical scaling factor of sqrt(x) when it's scaled
// by x horizontally. It's power spectrum decays as f^-2
//
// On the right, a Fractional Brownian Motion with H=1
// which means a gain G of 0.5. It's a long memory curve
// with possitively correlated increments, has a fractal
// of 1, and is naturally istropicaly self-similar (non-
// distorted zoom). Because of that, it's what we use to
// mimic mountains. It's power spectrum decays as f^-3
//
// More info: http://iquilezles.org/www/articles/fbm/fbm.htm
// integer hash copied from Hugo Elias
float hash( int n )
{
n = (n << 13) ^ n;
n = n * (n * n * 15731 + 789221) + 1376312589;
return -1.0+2.0*float( n & ivec3(0x0fffffff))/float(0x0fffffff);
}
// gradient noise
float gnoise( in float p )
{
int i = int(floor(p));
float f = fract(p);
float u = f*f*(3.0-2.0*f);
return mix( hash(i+0)*(f-0.0),
hash(i+1)*(f-1.0), u);
}
// fbm
float fbm( in float x, in float G )
{
x += 26.06;
float n = 0.0;
float s = 1.0;
float a = 0.0;
float f = 1.0;
for( int i=0; i<16; i++ )
{
n += s*gnoise(x*f);
a += s;
s *= G;
f *= 2.0;
x += 0.31;
}
return n;
}
vec3 anim( in vec2 p, float time )
{
vec3 col = vec3(0.0);
//float ani = fract(time/4.0);
float ani = smoothstep(0.0,1.0,fract(time/4.0));
float zoom = pow( 2.0, 6.0*ani );
if( p.x<0.0 )
{
vec2 q = vec2(p.x*0.5 + 0.5,p.y);
float G = 0.707107;
float comp = zoom;
float comp2 = sqrt(comp);
if( q.y<0.0 )
{
float y = -0.5+0.5*comp2*(fbm(0.8*q.x/comp, G ));
y += zoom*0.004;
col = mix( col, vec3(1.0,1.0,0.5).zyx, 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
else
{
float y = 0.5+0.5*fbm(0.8*q.x, G );
col = mix( col, vec3(1.0,0.5,0.0).zyx, (1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y)));
}
}
else
{
vec2 q = vec2(p.x*0.5 - 0.5,p.y);
float G = 0.5;
float comp = zoom;
float comp2 = comp;
if( p.y<0.0 )
{
float y = -0.5+0.9*comp2*(fbm(1.0*q.x/comp, G ));
y += zoom*0.004;
col = mix( col, vec3(1.0,1.0,0.5), 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
else
{
float y = 0.5+0.9*fbm(1.0*q.x, G );
col = mix( col, vec3(1.0,0.5,0.0), 1.0-smoothstep( 0.0, 12.0/iResolution.x,q.y-y));
}
}
col *= smoothstep(0.01,0.02,abs(p.x) );
col *= smoothstep(0.01,0.02,abs(p.y) );
return col;
}
#define AA 5
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 col = vec3(0.0);
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
vec2 o = vec2(m,n)/float(AA);
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
float time = iTime + 0.5*(1.0/24.0)*(float(m*AA+n)+d)/float(AA*AA);
col += anim(p,iTime);
}
col /= float(AA*AA);
fragColor = vec4( col, 1.0 );
}
| mit | [
2361,
2368,
2405,
2405,
2660
] | [
[
1950,
1989,
2011,
2011,
2153
],
[
2155,
2173,
2201,
2201,
2359
],
[
2361,
2368,
2405,
2405,
2660
],
[
2662,
2662,
2698,
2698,
4074
]
] | // fbm
| float fbm( in float x, in float G )
{ |
x += 26.06;
float n = 0.0;
float s = 1.0;
float a = 0.0;
float f = 1.0;
for( int i=0; i<16; i++ )
{
n += s*gnoise(x*f);
a += s;
s *= G;
f *= 2.0;
x += 0.31;
}
return n;
} | // fbm
float fbm( in float x, in float G )
{ | 1 | 2 |
tdKSRR | iq | 2019-11-09T03:06:28 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// Something I toyed with a decade ago was mapping all possible
// quadratic polynomials to the surface of a sphere:
//
// http://www.iquilezles.org/blog/?p=1089
//
// See also this video: https://www.youtube.com/watch?v=JJYVqviE2Uk
//
// Basically, all possible quadratic equations (parabolas) can be
// mapped to the surface of a sphere. p(x)=ax²+bx+c becomes a point
// (a,b,c) in 3D space, and since all quadratics of the form
// (k·a, k·b, k·c) has the same solutions, all space can be
// collapsed into a unit sphere through vector normalization.
//
// In blue are complex solutions.
// In yellow are real solutions with different signs
// In white are real solutions with same signs
//
// Similar idea, but for cubic equations:
// https://www.shadertoy.com/view/tsVSRR
//-------------------------------------------------------
// For a point in the sphere's surface p, return a color based on
// the solutions of the associate quadratic polynomial
vec3 getColor( in vec3 p )
{
// rotate the solution space (the sphere mapping)
float an = 0.5*iTime;
float si = sin(an), co = cos(an);
p.xz = mat2(co,-si,si,co)*p.xz;
vec3 col = vec3(0.0);
float m = 11.0;
// solve quadratic
float h = p.y*p.y - 4.0*p.x*p.z;
if( h<0.0 )
{
// Complex solution. Make it blue
col = vec3(0.0,0.5,1.0);
//m = -h;
float f = sqrt(abs(p.z/p.x));
m = 1.0*min(f,1.0/f);
}
else
{
// Real solution. Yellow if same sign and white if not
float t1 = (-p.y-sqrt(h))*0.5/p.x;
float t2 = (-p.y+sqrt(h))*0.5/p.x;
col = (t1*t2>0.0) ? vec3(1.0,0.9,0.8) : vec3(1.0,0.5,0.1);
m = 16.0*abs(min(min(abs( t1),abs( t2)),
min(abs(1.0/t1),abs(1.0/t2))));
}
// shade
col *= clamp(log(1.0+m),0.0,1.0);
col *= clamp(log(1.0+16.0*abs(h)),0.0,1.0);
// discriminant isolines
col *= 0.7 + 0.3*smoothstep(-0.1,0.1,sin(abs(24.0*h)));
#if 0
//if( abs(p.x-p.z)<0.01 ) col = vec3(1,0,0);
//if( abs(h-2.0)<0.01 ) col = vec3(1,0,0);
if( abs(h+2.0)<0.01 ) col = vec3(1,0,0);
if( abs(p.x-0.0)<0.01 ) col = vec3(1,0,0);
//if( abs(p.y-0.0)<0.01 ) col = vec3(0,1,0);
if( abs(p.z-0.0)<0.01 ) col = vec3(0,0,1);
if( length(p-vec3( 1, 0, 1)/sqrt(2.0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(-1, 0,-1)/sqrt(2.0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3( 1, 0, 0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(-1, 0, 0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3( 0, 0, 1))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3( 0, 0,-1))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(1, 2,1)/sqrt(6.0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(1,-2,1)/sqrt(6.0))<0.05 ) col = vec3(0,0,0);
#endif
return col;
}
//-------------------------------------------------------
float sphIntersect( in vec3 ro, in vec3 rd, in vec4 sph )
{
vec3 oc = ro - sph.xyz;
float b = dot( oc, rd );
float c = dot( oc, oc ) - sph.w*sph.w;
float h = b*b - c;
if( h<0.0 ) return -1.0;
return -b - sqrt( h );
}
float sphSoftShadow( in vec3 ro, in vec3 rd, in vec4 sph, in float k )
{
vec3 oc = ro - sph.xyz;
float b = dot( oc, rd );
float c = dot( oc, oc ) - sph.w*sph.w;
float h = b*b - c;
return (b>0.0) ? step(-0.0001,c) : smoothstep( 0.0, 1.0, h*k/b );
}
float sphOcclusion( in vec3 pos, in vec3 nor, in vec4 sph )
{
vec3 r = sph.xyz - pos;
float l = length(r);
return dot(nor,r)*(sph.w*sph.w)/(l*l*l);
}
vec3 sphNormal( in vec3 pos, in vec4 sph )
{
return normalize(pos-sph.xyz);
}
float iPlane( in vec3 ro, in vec3 rd )
{
return (-1.0 - ro.y)/rd.y;
}
//=====================================================
vec3 plot2D( in vec2 px )
{
vec2 p = px/iResolution.xy;
#if 1
p.x -= 0.5;
vec2 a = p.yx*vec2(3.141593, 6.283185);
vec3 q = vec3( cos(a.x),
sin(a.x)*cos(a.y),
sin(a.x)*sin(a.y) );
#else
p.y = -0.5 + p.y;
vec2 a = p*vec2(6.283185,3.141593);
vec3 q = vec3( cos(a.y)*cos(a.x),
sin(a.y),
cos(a.y)*sin(a.x) );
#endif
//if( length(q-vec3(0,1,0))<0.1 ) return vec3(1,0,0);
return getColor(q);
}
//=====================================================
vec3 plot3D( in vec2 px )
{
vec2 p = (-iResolution.xy + 2.0*px)/iResolution.y;
// camera
vec3 ro = vec3(0.0, 0.0, 3.0 );
vec3 rd = normalize( vec3(p,-2.0) );
// sphere
vec4 sph = vec4( 0.0, 0.0, 0.0, 1.0 );
vec3 col = vec3(0.0);
// intersect geometry
float tmin = 1e10;
vec3 nor;
float occ = 1.0;
vec3 mate = vec3(1.0);
// plane/floor
float t1 = iPlane( ro, rd );
if( t1>0.0 )
{
tmin = t1;
vec3 pos = ro + t1*rd;
nor = vec3(0.0,1.0,0.0);
occ = 1.0-sphOcclusion( pos, nor, sph );
mate = vec3(0.2);
}
// sphere
float t2 = sphIntersect( ro, rd, sph );
if( t2>0.0 && t2<tmin )
{
tmin = t2;
vec3 pos = ro + t2*rd;
nor = sphNormal( pos, sph );
occ = 0.5 + 0.5*nor.y;
mate = getColor(nor);
}
// apply color and lighting
if( tmin<1000.0 )
{
vec3 pos = ro + tmin*rd;
vec3 lig = normalize( vec3(0.6,0.3,0.4) );
float sha = sphSoftShadow( pos, lig, sph, 2.0 );
vec3 lin = vec3(1.5)*clamp(dot(nor,lig),0.0,1.0)*sha;
lin += 0.5*occ;
lin += 0.5*occ*pow(clamp(1.0+dot(nor,rd),0.0,1.0),3.0);
col = mate*lin;
// fog
col *= exp( -0.05*tmin );
}
return col;
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // make this 2 or 3 for antialiasing
#endif
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 p = fragCoord + vec2(float(m),float(n))/float(AA)-0.5;
#else
vec2 p = fragCoord;
#endif
// draw stuff
vec3 col = (sin(0.7*iTime)<-0.5) ? plot2D(p) : plot3D(p);
// gamma correction
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
// output color
fragColor = vec4( tot, 1.0 );
} | mit | [
1918,
2039,
2067,
2121,
3943
] | [
[
1918,
2039,
2067,
2121,
3943
],
[
4004,
4004,
4063,
4063,
4226
],
[
4228,
4228,
4300,
4300,
4495
],
[
4513,
4513,
4574,
4574,
4675
],
[
4677,
4677,
4721,
4721,
4758
],
[
4760,
4760,
4800,
4800,
4833
],
[
4892,
4892,
4919,
4919,
5425
],
[
5484,
5484,
5511,
5511,
6794
]
] | // For a point in the sphere's surface p, return a color based on
// the solutions of the associate quadratic polynomial
| vec3 getColor( in vec3 p )
{ |
float an = 0.5*iTime;
float si = sin(an), co = cos(an);
p.xz = mat2(co,-si,si,co)*p.xz;
vec3 col = vec3(0.0);
float m = 11.0;
// solve quadratic
float h = p.y*p.y - 4.0*p.x*p.z;
if( h<0.0 )
{
// Complex solution. Make it blue
col = vec3(0.0,0.5,1.0);
//m = -h;
float f = sqrt(abs(p.z/p.x));
m = 1.0*min(f,1.0/f);
}
else
{
// Real solution. Yellow if same sign and white if not
float t1 = (-p.y-sqrt(h))*0.5/p.x;
float t2 = (-p.y+sqrt(h))*0.5/p.x;
col = (t1*t2>0.0) ? vec3(1.0,0.9,0.8) : vec3(1.0,0.5,0.1);
m = 16.0*abs(min(min(abs( t1),abs( t2)),
min(abs(1.0/t1),abs(1.0/t2))));
}
// shade
col *= clamp(log(1.0+m),0.0,1.0);
col *= clamp(log(1.0+16.0*abs(h)),0.0,1.0);
// discriminant isolines
col *= 0.7 + 0.3*smoothstep(-0.1,0.1,sin(abs(24.0*h)));
#if 0
//if( abs(p.x-p.z)<0.01 ) col = vec3(1,0,0);
//if( abs(h-2.0)<0.01 ) col = vec3(1,0,0);
if( abs(h+2.0)<0.01 ) col = vec3(1,0,0);
if( abs(p.x-0.0)<0.01 ) col = vec3(1,0,0);
//if( abs(p.y-0.0)<0.01 ) col = vec3(0,1,0);
if( abs(p.z-0.0)<0.01 ) col = vec3(0,0,1);
if( length(p-vec3( 1, 0, 1)/sqrt(2.0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(-1, 0,-1)/sqrt(2.0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3( 1, 0, 0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(-1, 0, 0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3( 0, 0, 1))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3( 0, 0,-1))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(1, 2,1)/sqrt(6.0))<0.05 ) col = vec3(0,0,0);
if( length(p-vec3(1,-2,1)/sqrt(6.0))<0.05 ) col = vec3(0,0,0);
#endif
return col;
} | // For a point in the sphere's surface p, return a color based on
// the solutions of the associate quadratic polynomial
vec3 getColor( in vec3 p )
{ | 1 | 1 |
tsVSRR | iq | 2019-11-09T03:06:40 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
//-------------------------------------------------------
// For a point p in the unit box, return a color
// based on the solutions of the associated cubic
// polynomial. The point p is interprested as the
// 3 polar angles of a 4D point in a unit sphere,
// and that 4D point maps to a unique cubic
// polynomials a,b,c,d coefficients.
vec3 getColor( in vec3 pb ) // p is in -1..1
{
vec3 col = vec3(0.0);
float m = 0.0;
//-------------------------------------------------------
// convert from box space to cannoincal 0..1
pb = 0.5 + 0.498*pb/vec3(1.0,1.0,2.0);
pb += vec3(0.5,0.5,0.5);
// convert to 4D polar coordinates with radius=1
pb *= vec3(3.141593,3.141593,6.283185);
// convert to cartesian 4D
vec4 ps = vec4( cos(pb.x),
sin(pb.x)*cos(pb.y),
sin(pb.x)*sin(pb.y)*cos(pb.z),
sin(pb.x)*sin(pb.y)*sin(pb.z) );
//-----------------------------
// make lead coefficient=1
ps /= ps.x;
#if 1
//-----------------------------
// compute depressed cubic t^3 + pt + q = 0
float p = (3.0*ps.z - ps.y*ps.y)/3.0;
float q = (2.0*ps.y*ps.y*ps.y - 9.0*ps.y*ps.z + 27.0*ps.w)/27.0;
// discriminant
float h = -4.0*p*p*p - 27.0*q*q;
#else
// discriminant
float h = 18.0*ps.y*ps.z*ps.w - 4.0*ps.y*ps.y*ps.y*ps.w + ps.y*ps.y*ps.z*ps.z - 4.0*ps.z*ps.z*ps.z - 27.0*ps.w*ps.w;
#endif
if( h<0.0 )
{
// 1 real, 2 complex roots. Make it blue, and shade it
// based on modulo of the roots
m = -h;//length(vec2(-p.y,sqrt(-h)))*0.5/abs(p.x);
col = vec3(0.0,0.5,1.0);
}
else
{
// 3 real roots. Make it yellow if possitive and
// purple is negative, and shader it based on size
// of the first root
m = h;//1.0;//(-p.y-sqrt(h))*0.5/p.x;
col = vec3(1.0,0.5,0.0);// : vec3(1.0,0.0,0.5);
}
// discriminant --> geometric mean of root differences
h = pow(abs(h),1.0/6.0);
col *= 0.7 + 0.3*smoothstep(-0.1,0.1,sin(abs(12.0*h))); // discriminant isolines
//col *= h;
return col;
}
//-------------------------------------------------------
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 boxIntersect( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
ro -= cen;
// ray-box intersection in box space
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
return vec4( tN, nor );
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxShadow( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
ro -= cen;
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return -1.0;
return tN;
}
float sdBox( in vec2 p, in vec2 b )
{
vec2 q = abs(p) - b;
return min(max(q.x,q.y),0.0) + length(max(q,0.0));
}
float iPlane( in vec3 ro, in vec3 rd )
{
return (0.0 - ro.y)/rd.y;
}
//=====================================================
vec3 plot3D( in vec2 px )
{
vec2 p = (-iResolution.xy + 2.0*px)/iResolution.y;
// camera
// camera movement
float an = 0.2*iTime;
vec3 ro = vec3( 4.0*cos(an), 4.0, 4.0*sin(an) );
vec3 ta = vec3( 0.0, 0.5, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.2*ww );
// sphere
//vec3 box = vec3( 1.0 );
float h = 0.5+0.49995*sin(1.0*iTime);
//h = 1.0;
vec3 box_cen = vec3(0.0,h,0.0);
vec3 box_rad = vec3(1.0,h,2.0);
vec3 col = vec3(0.0);
// intersect geometry
float tmin = 1e10;
vec3 nor;
float occ = 1.0;
vec3 mate = vec3(1.0);
// plane/floor
float t1 = iPlane( ro, rd );
if( t1>0.0 )
{
tmin = t1;
vec3 pos = ro + t1*rd;
nor = vec3(0.0,1.0,0.0);
occ = 1.0;//-sphOcclusion( pos, nor, sph );
float d = sdBox( pos.xz, box_rad.xz );
occ = 0.2 + 0.8*clamp(1.0 - 1.0/(1.0+d*d),0.0,1.0);
mate = vec3(0.2);
}
// box
vec4 t2 = boxIntersect( ro, rd, box_cen, box_rad );
if( t2.x>0.0 && t2.x<tmin )
{
tmin = t2.x;
vec3 pos = ro + t2.x*rd;
nor = t2.yzw;
occ = 0.2+0.8*clamp(pos.y/2.0,0.0,1.0);
mate = getColor(pos);
// wireframe
mate *= 1.0 - (1.0-abs(nor.x))*smoothstep( box_rad.x-0.04, box_rad.x-0.02, abs(pos.x-box_cen.x) );
mate *= 1.0 - (1.0-abs(nor.y))*smoothstep( box_rad.y-0.04, box_rad.y-0.02, abs(pos.y-box_cen.y) );
mate *= 1.0 - (1.0-abs(nor.z))*smoothstep( box_rad.z-0.04, box_rad.z-0.02, abs(pos.z-box_cen.z) );
}
// apply color and lighting
if( tmin<1000.0 )
{
vec3 pos = ro + tmin*rd;
vec3 lig = normalize( vec3(0.6,0.2,0.4) );
float sha = step( boxShadow( pos+0.01*nor, lig, box_cen, box_rad ), 0.0 );
vec3 lin = vec3(0.0);
lin += vec3(1.5)*clamp(dot(nor,lig),0.0,1.0)*sha;
lin += 0.5*occ;
//lin += 0.5*occ*pow(clamp(1.0+dot(nor,rd),0.0,1.0),3.0);
col = mate*lin;
//col = vec3(occ);
//col = mate;
// fog
col *= exp( -0.05*tmin );
}
return col;
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // make this 2 or 3 for antialiasing
#endif
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 p = fragCoord + vec2(float(m),float(n))/float(AA)-0.5;
#else
vec2 p = fragCoord;
#endif
vec3 col = plot3D(p);
// gamma correction
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
1136,
1417,
1463,
1463,
3245
] | [
[
1136,
1417,
1463,
1463,
3245
],
[
3306,
3374,
3446,
3446,
3844
],
[
3847,
3915,
3985,
3985,
4256
],
[
4258,
4258,
4296,
4296,
4378
],
[
4380,
4380,
4420,
4420,
4452
],
[
4511,
4511,
4538,
4538,
6832
]
] | // For a point p in the unit box, return a color
// based on the solutions of the associated cubic
// polynomial. The point p is interprested as the
// 3 polar angles of a 4D point in a unit sphere,
// and that 4D point maps to a unique cubic
// polynomials a,b,c,d coefficients.
| vec3 getColor( in vec3 pb ) // p is in -1..1
{ |
vec3 col = vec3(0.0);
float m = 0.0;
//-------------------------------------------------------
// convert from box space to cannoincal 0..1
pb = 0.5 + 0.498*pb/vec3(1.0,1.0,2.0);
pb += vec3(0.5,0.5,0.5);
// convert to 4D polar coordinates with radius=1
pb *= vec3(3.141593,3.141593,6.283185);
// convert to cartesian 4D
vec4 ps = vec4( cos(pb.x),
sin(pb.x)*cos(pb.y),
sin(pb.x)*sin(pb.y)*cos(pb.z),
sin(pb.x)*sin(pb.y)*sin(pb.z) );
//-----------------------------
// make lead coefficient=1
ps /= ps.x;
#if 1
//-----------------------------
// compute depressed cubic t^3 + pt + q = 0
float p = (3.0*ps.z - ps.y*ps.y)/3.0;
float q = (2.0*ps.y*ps.y*ps.y - 9.0*ps.y*ps.z + 27.0*ps.w)/27.0;
// discriminant
float h = -4.0*p*p*p - 27.0*q*q;
#else
// discriminant
float h = 18.0*ps.y*ps.z*ps.w - 4.0*ps.y*ps.y*ps.y*ps.w + ps.y*ps.y*ps.z*ps.z - 4.0*ps.z*ps.z*ps.z - 27.0*ps.w*ps.w;
#endif
if( h<0.0 )
{
// 1 real, 2 complex roots. Make it blue, and shade it
// based on modulo of the roots
m = -h;//length(vec2(-p.y,sqrt(-h)))*0.5/abs(p.x);
col = vec3(0.0,0.5,1.0);
}
else
{
// 3 real roots. Make it yellow if possitive and
// purple is negative, and shader it based on size
// of the first root
m = h;//1.0;//(-p.y-sqrt(h))*0.5/p.x;
col = vec3(1.0,0.5,0.0);// : vec3(1.0,0.0,0.5);
}
// discriminant --> geometric mean of root differences
h = pow(abs(h),1.0/6.0);
col *= 0.7 + 0.3*smoothstep(-0.1,0.1,sin(abs(12.0*h))); // discriminant isolines
//col *= h;
return col;
} | // For a point p in the unit box, return a color
// based on the solutions of the associated cubic
// polynomial. The point p is interprested as the
// 3 polar angles of a 4D point in a unit sphere,
// and that 4D point maps to a unique cubic
// polynomials a,b,c,d coefficients.
vec3 getColor( in vec3 pb ) // p is in -1..1
{ | 1 | 1 |
tsVSRR | iq | 2019-11-09T03:06:40 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
//-------------------------------------------------------
// For a point p in the unit box, return a color
// based on the solutions of the associated cubic
// polynomial. The point p is interprested as the
// 3 polar angles of a 4D point in a unit sphere,
// and that 4D point maps to a unique cubic
// polynomials a,b,c,d coefficients.
vec3 getColor( in vec3 pb ) // p is in -1..1
{
vec3 col = vec3(0.0);
float m = 0.0;
//-------------------------------------------------------
// convert from box space to cannoincal 0..1
pb = 0.5 + 0.498*pb/vec3(1.0,1.0,2.0);
pb += vec3(0.5,0.5,0.5);
// convert to 4D polar coordinates with radius=1
pb *= vec3(3.141593,3.141593,6.283185);
// convert to cartesian 4D
vec4 ps = vec4( cos(pb.x),
sin(pb.x)*cos(pb.y),
sin(pb.x)*sin(pb.y)*cos(pb.z),
sin(pb.x)*sin(pb.y)*sin(pb.z) );
//-----------------------------
// make lead coefficient=1
ps /= ps.x;
#if 1
//-----------------------------
// compute depressed cubic t^3 + pt + q = 0
float p = (3.0*ps.z - ps.y*ps.y)/3.0;
float q = (2.0*ps.y*ps.y*ps.y - 9.0*ps.y*ps.z + 27.0*ps.w)/27.0;
// discriminant
float h = -4.0*p*p*p - 27.0*q*q;
#else
// discriminant
float h = 18.0*ps.y*ps.z*ps.w - 4.0*ps.y*ps.y*ps.y*ps.w + ps.y*ps.y*ps.z*ps.z - 4.0*ps.z*ps.z*ps.z - 27.0*ps.w*ps.w;
#endif
if( h<0.0 )
{
// 1 real, 2 complex roots. Make it blue, and shade it
// based on modulo of the roots
m = -h;//length(vec2(-p.y,sqrt(-h)))*0.5/abs(p.x);
col = vec3(0.0,0.5,1.0);
}
else
{
// 3 real roots. Make it yellow if possitive and
// purple is negative, and shader it based on size
// of the first root
m = h;//1.0;//(-p.y-sqrt(h))*0.5/p.x;
col = vec3(1.0,0.5,0.0);// : vec3(1.0,0.0,0.5);
}
// discriminant --> geometric mean of root differences
h = pow(abs(h),1.0/6.0);
col *= 0.7 + 0.3*smoothstep(-0.1,0.1,sin(abs(12.0*h))); // discriminant isolines
//col *= h;
return col;
}
//-------------------------------------------------------
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 boxIntersect( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
ro -= cen;
// ray-box intersection in box space
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
return vec4( tN, nor );
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxShadow( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
ro -= cen;
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return -1.0;
return tN;
}
float sdBox( in vec2 p, in vec2 b )
{
vec2 q = abs(p) - b;
return min(max(q.x,q.y),0.0) + length(max(q,0.0));
}
float iPlane( in vec3 ro, in vec3 rd )
{
return (0.0 - ro.y)/rd.y;
}
//=====================================================
vec3 plot3D( in vec2 px )
{
vec2 p = (-iResolution.xy + 2.0*px)/iResolution.y;
// camera
// camera movement
float an = 0.2*iTime;
vec3 ro = vec3( 4.0*cos(an), 4.0, 4.0*sin(an) );
vec3 ta = vec3( 0.0, 0.5, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.2*ww );
// sphere
//vec3 box = vec3( 1.0 );
float h = 0.5+0.49995*sin(1.0*iTime);
//h = 1.0;
vec3 box_cen = vec3(0.0,h,0.0);
vec3 box_rad = vec3(1.0,h,2.0);
vec3 col = vec3(0.0);
// intersect geometry
float tmin = 1e10;
vec3 nor;
float occ = 1.0;
vec3 mate = vec3(1.0);
// plane/floor
float t1 = iPlane( ro, rd );
if( t1>0.0 )
{
tmin = t1;
vec3 pos = ro + t1*rd;
nor = vec3(0.0,1.0,0.0);
occ = 1.0;//-sphOcclusion( pos, nor, sph );
float d = sdBox( pos.xz, box_rad.xz );
occ = 0.2 + 0.8*clamp(1.0 - 1.0/(1.0+d*d),0.0,1.0);
mate = vec3(0.2);
}
// box
vec4 t2 = boxIntersect( ro, rd, box_cen, box_rad );
if( t2.x>0.0 && t2.x<tmin )
{
tmin = t2.x;
vec3 pos = ro + t2.x*rd;
nor = t2.yzw;
occ = 0.2+0.8*clamp(pos.y/2.0,0.0,1.0);
mate = getColor(pos);
// wireframe
mate *= 1.0 - (1.0-abs(nor.x))*smoothstep( box_rad.x-0.04, box_rad.x-0.02, abs(pos.x-box_cen.x) );
mate *= 1.0 - (1.0-abs(nor.y))*smoothstep( box_rad.y-0.04, box_rad.y-0.02, abs(pos.y-box_cen.y) );
mate *= 1.0 - (1.0-abs(nor.z))*smoothstep( box_rad.z-0.04, box_rad.z-0.02, abs(pos.z-box_cen.z) );
}
// apply color and lighting
if( tmin<1000.0 )
{
vec3 pos = ro + tmin*rd;
vec3 lig = normalize( vec3(0.6,0.2,0.4) );
float sha = step( boxShadow( pos+0.01*nor, lig, box_cen, box_rad ), 0.0 );
vec3 lin = vec3(0.0);
lin += vec3(1.5)*clamp(dot(nor,lig),0.0,1.0)*sha;
lin += 0.5*occ;
//lin += 0.5*occ*pow(clamp(1.0+dot(nor,rd),0.0,1.0),3.0);
col = mate*lin;
//col = vec3(occ);
//col = mate;
// fog
col *= exp( -0.05*tmin );
}
return col;
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // make this 2 or 3 for antialiasing
#endif
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 p = fragCoord + vec2(float(m),float(n))/float(AA)-0.5;
#else
vec2 p = fragCoord;
#endif
vec3 col = plot3D(p);
// gamma correction
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
3306,
3374,
3446,
3446,
3844
] | [
[
1136,
1417,
1463,
1463,
3245
],
[
3306,
3374,
3446,
3446,
3844
],
[
3847,
3915,
3985,
3985,
4256
],
[
4258,
4258,
4296,
4296,
4378
],
[
4380,
4380,
4420,
4420,
4452
],
[
4511,
4511,
4538,
4538,
6832
]
] | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
| vec4 boxIntersect( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{ |
ro -= cen;
// ray-box intersection in box space
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
return vec4( tN, nor );
} | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 boxIntersect( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{ | 1 | 1 |
tsVSRR | iq | 2019-11-09T03:06:40 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
//-------------------------------------------------------
// For a point p in the unit box, return a color
// based on the solutions of the associated cubic
// polynomial. The point p is interprested as the
// 3 polar angles of a 4D point in a unit sphere,
// and that 4D point maps to a unique cubic
// polynomials a,b,c,d coefficients.
vec3 getColor( in vec3 pb ) // p is in -1..1
{
vec3 col = vec3(0.0);
float m = 0.0;
//-------------------------------------------------------
// convert from box space to cannoincal 0..1
pb = 0.5 + 0.498*pb/vec3(1.0,1.0,2.0);
pb += vec3(0.5,0.5,0.5);
// convert to 4D polar coordinates with radius=1
pb *= vec3(3.141593,3.141593,6.283185);
// convert to cartesian 4D
vec4 ps = vec4( cos(pb.x),
sin(pb.x)*cos(pb.y),
sin(pb.x)*sin(pb.y)*cos(pb.z),
sin(pb.x)*sin(pb.y)*sin(pb.z) );
//-----------------------------
// make lead coefficient=1
ps /= ps.x;
#if 1
//-----------------------------
// compute depressed cubic t^3 + pt + q = 0
float p = (3.0*ps.z - ps.y*ps.y)/3.0;
float q = (2.0*ps.y*ps.y*ps.y - 9.0*ps.y*ps.z + 27.0*ps.w)/27.0;
// discriminant
float h = -4.0*p*p*p - 27.0*q*q;
#else
// discriminant
float h = 18.0*ps.y*ps.z*ps.w - 4.0*ps.y*ps.y*ps.y*ps.w + ps.y*ps.y*ps.z*ps.z - 4.0*ps.z*ps.z*ps.z - 27.0*ps.w*ps.w;
#endif
if( h<0.0 )
{
// 1 real, 2 complex roots. Make it blue, and shade it
// based on modulo of the roots
m = -h;//length(vec2(-p.y,sqrt(-h)))*0.5/abs(p.x);
col = vec3(0.0,0.5,1.0);
}
else
{
// 3 real roots. Make it yellow if possitive and
// purple is negative, and shader it based on size
// of the first root
m = h;//1.0;//(-p.y-sqrt(h))*0.5/p.x;
col = vec3(1.0,0.5,0.0);// : vec3(1.0,0.0,0.5);
}
// discriminant --> geometric mean of root differences
h = pow(abs(h),1.0/6.0);
col *= 0.7 + 0.3*smoothstep(-0.1,0.1,sin(abs(12.0*h))); // discriminant isolines
//col *= h;
return col;
}
//-------------------------------------------------------
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec4 boxIntersect( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
ro -= cen;
// ray-box intersection in box space
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec4(-1.0);
vec3 nor = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz);
return vec4( tN, nor );
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxShadow( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{
ro -= cen;
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return -1.0;
return tN;
}
float sdBox( in vec2 p, in vec2 b )
{
vec2 q = abs(p) - b;
return min(max(q.x,q.y),0.0) + length(max(q,0.0));
}
float iPlane( in vec3 ro, in vec3 rd )
{
return (0.0 - ro.y)/rd.y;
}
//=====================================================
vec3 plot3D( in vec2 px )
{
vec2 p = (-iResolution.xy + 2.0*px)/iResolution.y;
// camera
// camera movement
float an = 0.2*iTime;
vec3 ro = vec3( 4.0*cos(an), 4.0, 4.0*sin(an) );
vec3 ta = vec3( 0.0, 0.5, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.2*ww );
// sphere
//vec3 box = vec3( 1.0 );
float h = 0.5+0.49995*sin(1.0*iTime);
//h = 1.0;
vec3 box_cen = vec3(0.0,h,0.0);
vec3 box_rad = vec3(1.0,h,2.0);
vec3 col = vec3(0.0);
// intersect geometry
float tmin = 1e10;
vec3 nor;
float occ = 1.0;
vec3 mate = vec3(1.0);
// plane/floor
float t1 = iPlane( ro, rd );
if( t1>0.0 )
{
tmin = t1;
vec3 pos = ro + t1*rd;
nor = vec3(0.0,1.0,0.0);
occ = 1.0;//-sphOcclusion( pos, nor, sph );
float d = sdBox( pos.xz, box_rad.xz );
occ = 0.2 + 0.8*clamp(1.0 - 1.0/(1.0+d*d),0.0,1.0);
mate = vec3(0.2);
}
// box
vec4 t2 = boxIntersect( ro, rd, box_cen, box_rad );
if( t2.x>0.0 && t2.x<tmin )
{
tmin = t2.x;
vec3 pos = ro + t2.x*rd;
nor = t2.yzw;
occ = 0.2+0.8*clamp(pos.y/2.0,0.0,1.0);
mate = getColor(pos);
// wireframe
mate *= 1.0 - (1.0-abs(nor.x))*smoothstep( box_rad.x-0.04, box_rad.x-0.02, abs(pos.x-box_cen.x) );
mate *= 1.0 - (1.0-abs(nor.y))*smoothstep( box_rad.y-0.04, box_rad.y-0.02, abs(pos.y-box_cen.y) );
mate *= 1.0 - (1.0-abs(nor.z))*smoothstep( box_rad.z-0.04, box_rad.z-0.02, abs(pos.z-box_cen.z) );
}
// apply color and lighting
if( tmin<1000.0 )
{
vec3 pos = ro + tmin*rd;
vec3 lig = normalize( vec3(0.6,0.2,0.4) );
float sha = step( boxShadow( pos+0.01*nor, lig, box_cen, box_rad ), 0.0 );
vec3 lin = vec3(0.0);
lin += vec3(1.5)*clamp(dot(nor,lig),0.0,1.0)*sha;
lin += 0.5*occ;
//lin += 0.5*occ*pow(clamp(1.0+dot(nor,rd),0.0,1.0),3.0);
col = mate*lin;
//col = vec3(occ);
//col = mate;
// fog
col *= exp( -0.05*tmin );
}
return col;
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // make this 2 or 3 for antialiasing
#endif
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 p = fragCoord + vec2(float(m),float(n))/float(AA)-0.5;
#else
vec2 p = fragCoord;
#endif
vec3 col = plot3D(p);
// gamma correction
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
3847,
3915,
3985,
3985,
4256
] | [
[
1136,
1417,
1463,
1463,
3245
],
[
3306,
3374,
3446,
3446,
3844
],
[
3847,
3915,
3985,
3985,
4256
],
[
4258,
4258,
4296,
4296,
4378
],
[
4380,
4380,
4420,
4420,
4452
],
[
4511,
4511,
4538,
4538,
6832
]
] | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
| float boxShadow( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{ |
ro -= cen;
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return -1.0;
return tN;
} | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
float boxShadow( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad )
{ | 1 | 1 |
WdVXRm | blackle | 2019-11-13T18:46:25 | //CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/
//To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work.
#define FK(k) floatBitsToInt(cos(k))^floatBitsToInt(k)
float hash1(float k) {
int x = FK(k);int y = FK(cos(k));
return float((x*x-y)*(y*y+x)-x)/2.14e9;
}
vec3 hash3(float k) {
float r1 = hash1(k);
float r2 = hash1(r1);
float r3 = hash1(r2);
return vec3(r1, r2, r3);
}
//rotate P around axis AX by angle RO
vec3 rotate(vec3 p, vec3 ax, float ro) {
return mix(dot(p,ax)*ax, p, cos(ro)) + sin(ro)*cross(ax,p);
}
vec3 sphericalCoordinates(vec2 p) {
float phi = acos(p.x);
float theta = p.y*3.1415;
return vec3(cos(phi), sin(phi)*sin(theta), sin(phi)*cos(theta));
}
vec3 domainRepetition(vec3 p, vec3 scale) {
return (fract(p/scale)-0.5)*scale;
}
vec4 component(vec3 p, vec3 offset, vec3 rotation) {
vec3 axis = sphericalCoordinates(rotation.xy);
float angle = rotation.z * 3.1415;
p = rotate(p, axis, angle);
p = domainRepetition(p + offset, vec3(1));
vec3 normal = rotate(normalize(p), axis, -angle);
return vec4(length(p)-0.48, normal);
}
vec4 scene(vec3 p) {
vec4 accum = vec4(0.);
float iters = 5.;
for (float i = 0.; i < iters; i++) {
vec3 off = hash3(i);
vec3 rot = hash3(hash1(i));
accum += component(p, off, rot);
}
return accum/sqrt(iters*1.5)-0.1;
}
float phong(vec3 norm, vec3 light) {
return abs(dot(norm, light));
}
vec3 shade(vec3 p, vec3 norm, vec3 cam) {
float d1 = length(sin(p)*0.5+0.5)/sqrt(3.);
float d2 = length(sin(norm)*0.5+0.5)/sqrt(3.);
return sqrt(phong(norm, cam)*( d1*vec3(0.8,0.2,0.1) + (1.-d2)*vec3(0.3,0.6,0.9) ));
}
void castRay(vec3 cam, inout vec3 p, inout vec4 dist) {
float sgn = 1.;
for (int i = 0; i < 100; i++) {
dist = scene(p);
if (i == 0) sgn = sign(dist.x);
if (abs(dist.x) < 0.001) return;
p += cam*dist.x*sgn;
}
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = (fragCoord-0.5*iResolution.xy)/iResolution.y;
fragColor = vec4(0.0);
vec3 cam = normalize(vec3(0.5, uv));
vec3 init = vec3(iTime,0.,0.);
vec3 p = init;
vec4 dist; vec3 norm;
castRay(cam, p, dist);
norm = normalize(dist.yzw);
vec3 col1 = shade(p, norm, cam);
float pdist = distance(p, init);
float transparency = pow(1./(pdist+1.),8.);
float fog1 = pow(exp(-pdist*0.5)/exp(0.),0.5);
vec3 col2 = col1;
if (transparency > 0.02) {
p+=cam*0.1;
init = p;
castRay(cam, p, dist);
norm = normalize(dist.yzw);
col2 = shade(p, norm, cam);
}
float pdist2 = distance(p, init);
float fog2 = pow(exp(-pdist2*0.5)/exp(0.),0.5);
fragColor.xyz = mix(col1*fog1, col2*fog2, transparency);
} | cc0-1.0 | [
488,
526,
566,
566,
632
] | [
[
249,
249,
271,
271,
355
],
[
357,
357,
378,
378,
486
],
[
488,
526,
566,
566,
632
],
[
634,
634,
669,
669,
797
],
[
799,
799,
842,
842,
883
],
[
885,
885,
937,
937,
1204
],
[
1206,
1206,
1226,
1226,
1459
],
[
1461,
1461,
1497,
1497,
1533
],
[
1535,
1535,
1576,
1576,
1766
],
[
1768,
1768,
1823,
1823,
2022
],
[
2024,
2024,
2081,
2131,
2934
]
] | //rotate P around axis AX by angle RO
| vec3 rotate(vec3 p, vec3 ax, float ro) { |
return mix(dot(p,ax)*ax, p, cos(ro)) + sin(ro)*cross(ax,p);
} | //rotate P around axis AX by angle RO
vec3 rotate(vec3 p, vec3 ax, float ro) { | 1 | 1 |
WdyXRD | iq | 2019-11-11T20:42:12 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader is what I believe the most accurate fake soft shadow implementation
// to date, in that it reproduces correct inner and outer penumbra sizes casting
// a single ray. It matches the ground truth pretty well for different light
// source sizes.
//
// Code is in line 119
//------------------------------------------------------------------
float sdPlane( vec3 p )
{
return p.y;
}
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
float sdSphere( vec3 p, float r )
{
return length(p) - r;
}
float sdCylinder(vec3 p, float h, float r)
{
vec2 q = vec2( length(p.xz)-r, abs(p.y-h*0.5)-h*0.5 );
return min( max(q.x,q.y),0.0) + length(max(q,0.0));
}
bool shadowBox( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad, in float tmax )
{
vec3 m = 1.0/rd;
vec3 n = m*(ro-cen);
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return false;
return tN>0.0 && tN<tmax;
}
bool shadowSphere( in vec3 ro, in vec3 rd, in vec3 cen, in float rad, in float tmax )
{
vec3 oc = ro - cen;
float b = dot( oc, rd );
float c = dot( oc, oc ) - rad*rad;
float h = b*b - c;
if( h<0.0 ) return false;
float t = -b - sqrt( h );
return t>0.0 && t<tmax;
}
bool shadowCylinder( in vec3 ro, in vec3 rd, in float he, float ra, in float tmax )
{
float he2 = he*he;
float k2 = 1.0 - rd.y*rd.y;
float k1 = dot(ro,rd) - ro.y*rd.y;
float k0 = dot(ro,ro) - ro.y*ro.y - ra*ra;
float h = k1*k1 - k2*k0;
if( h<0.0 ) return false;
h = sqrt(h);
float t = (-k1-h)/k2;
// body
float y = ro.y + t*rd.y;
if( y>0.0 && y<he )
{
return t>0.0 && t<tmax;
}
// caps
t = ( ((y<0.0) ? 0.0 : he) - ro.y)/rd.y;
if( abs(k1+k2*t)<h )
{
return t>0.0 && t<tmax;
}
return false;
}
//------------------------------------------------------------------
float map( in vec3 pos )
{
vec3 p2 = vec3( mod(pos.x+1.0,3.0)-1.0, pos.yz );
vec3 p3 = vec3( mod(pos.x+2.0,3.0)-1.0, pos.yz );
vec3 p4 = vec3( mod(pos.x+3.0,3.0)-1.0, pos.yz );
float d1 = sdPlane( pos-vec3(0.0,0.00,0.0) );
float d2 = sdSphere( p2-vec3(0.0,0.30,0.0), 0.4 );
float d3 = sdBox( p3-vec3(0.0,0.25,0.0), vec3(0.2,0.5,0.2) );
float d4 = sdCylinder( p4-vec3(0.0,0.0,0.0), 0.8,0.3 );
return min(min(d1,d2),min(d3,d4));
}
//------------------------------------------------------------------
//
// Approximated soft shadows, based on
//
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
//
// and
//
// https://www.shadertoy.com/view/tscSRS
//
// and further fixed and improved
//
float apprSoftShadow(vec3 ro, vec3 rd, float mint, float tmax, float w)
{
float t = mint;
float res = 1.0;
for( int i=0; i<256; i++ )
{
float h = map(ro + t*rd);
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
}
// montecarlo based shadow, for ground truth comparison
float seed; float rand(void) { return fract(sin(seed++)*768.475278); }
float realSoftShadow( in vec3 ro, in vec3 rd, in float tmin, in float tmax, float w )
{
vec3 uu = normalize(cross(rd,vec3(0,1,0)));
vec3 vv = normalize(cross(rd,uu));
float tot = 0.0;
const int num = 32; // cast 32 rays
for( int j=0; j<num; j++ )
{
// uniform distribution on an disk
float ra = sqrt(rand());
float an = 6.283185*rand();
vec3 jrd = rd + w*ra*(uu*cos(an)+vv*sin(an));
// raycast
float res = 1.0;
for( int i=0; i<7; i++ ) // 7 objects
{
int k = i % 3;
bool sha = false;
if(k==0) sha = shadowBox( ro, jrd, vec3(-4.0 + float(i),0.25,0.0), vec3(0.2,0.5,0.2), tmax);
else if(k==1) sha = shadowSphere(ro, jrd, vec3(-4.0 + float(i),0.3,0.0), 0.4, tmax);
else sha = shadowCylinder( ro - vec3(-4.0 + float(i),0.0,0.0), jrd, 0.8, 0.3, tmax);
if( sha ) { res=0.0; break; }
}
tot += res;
}
return tot/float(num);
}
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy ) +
e.yyx*map( pos + e.yyx ) +
e.yxy*map( pos + e.yxy ) +
e.xxx*map( pos + e.xxx ) );
}
float castRay( in vec3 ro, in vec3 rd )
{
float tmin = 1.0;
float tmax = 20.0;
#if 1
// bounding volume
float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) tmax = min( tmax, tp1 );
float tp2 = (1.0-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.0 ) tmin = max( tmin, tp2 );
else tmax = min( tmax, tp2 ); }
#endif
float t = tmin;
for( int i=0; i<128; i++ )
{
float precis = 0.0005*t;
float res = map( ro+rd*t );
if( res<precis || t>tmax ) break;
t += res;
}
if( t>tmax ) t=-1.0;
return t;
}
float calcAO( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float h = 0.001 + 0.15*float(i)/4.0;
float d = map( pos + h*nor );
occ += (h-d)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 1.5*occ, 0.0, 1.0 );
}
vec3 render( in vec3 ro, in vec3 rd, in int technique, in float lightSize)
{
vec3 col = vec3(0.0);
float t = castRay(ro,rd);
if( t>-0.5 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
// material
vec3 mate = vec3(0.3);
// key light
vec3 lig = normalize( vec3(-0.1, 0.3, 0.6) );
vec3 hal = normalize( lig-rd );
float sha = (technique==0) ? realSoftShadow(pos, lig, 0.01, 3.0, lightSize )
: apprSoftShadow(pos, lig, 0.01, 3.0, lightSize );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 ) * sha;
float spe = pow(clamp(dot(nor,hal),0.0,1.0),16.0)*
dif *
(0.04+0.96*pow(clamp(1.0+dot(hal,rd),0.0,1.0),5.0));
col = mate * 4.0*dif*vec3(1.00,0.70,0.5);
col += 9.0*spe*vec3(0.90,0.80,1.0);
// ambient light
float occ = (pos.y>0.01) ? 1.0 : calcAO( pos, nor );
float amb = 0.5 + 0.5*nor.y;
col += mate*amb*occ*vec3(0.05,0.1,0.15);
// fog
col *= exp( -0.0008*t*t*t );
}
return col;
}
mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
vec3 cw = normalize(ta-ro);
vec3 cp = vec3(sin(cr), cos(cr),0.0);
vec3 cu = normalize( cross(cw,cp) );
vec3 cv = normalize( cross(cu,cw) );
return mat3( cu, cv, cw );
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // make this 2 or 3 for antialiasing
#endif
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera
float an = 12.0 - sin(0.1*iTime);
vec3 ro = vec3( 3.0*cos(0.1*an), 1.0, -3.0*sin(0.1*an) );
vec3 ta = vec3( 0.0, -0.4, 0.0 );
// camera-to-world transformation
mat3 ca = setCamera( ro, ta, 0.0 );
seed = sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)*sin(iTime);
// animation
int technique = (fract(iTime/3.0)>0.5) ? 1 : 0;
float lightSize = 0.05 + 0.04*sin(0.7*iTime);
// render
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// ray direction
vec3 rd = ca * normalize( vec3(p.xy,2.0) );
// render
vec3 col = render( ro, rd, technique, lightSize);
// gain
col = 1.8*col/(1.0+dot(col,vec3(0.333)));
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
3622,
3892,
3965,
3965,
4295
] | [
[
1435,
1435,
1460,
1460,
1475
],
[
1477,
1477,
1508,
1508,
1599
],
[
1601,
1601,
1636,
1636,
1664
],
[
1666,
1666,
1710,
1710,
1827
],
[
1829,
1829,
1913,
1913,
2185
],
[
2187,
2187,
2274,
2274,
2461
],
[
2463,
2463,
2548,
2548,
3070
],
[
3142,
3142,
3168,
3168,
3620
],
[
3622,
3892,
3965,
3965,
4295
],
[
4298,
4366,
4384,
4384,
4424
],
[
4426,
4426,
4513,
4513,
5492
],
[
5494,
5494,
5526,
5526,
5726
],
[
5728,
5728,
5769,
5769,
6339
],
[
6341,
6341,
6383,
6383,
6644
],
[
6646,
6646,
6722,
6722,
7809
],
[
7811,
7811,
7863,
7863,
8040
]
] | //------------------------------------------------------------------
//
// Approximated soft shadows, based on
//
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
//
// and
//
// https://www.shadertoy.com/view/tscSRS
//
// and further fixed and improved
//
| float apprSoftShadow(vec3 ro, vec3 rd, float mint, float tmax, float w)
{ |
float t = mint;
float res = 1.0;
for( int i=0; i<256; i++ )
{
float h = map(ro + t*rd);
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
} | //------------------------------------------------------------------
//
// Approximated soft shadows, based on
//
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
//
// and
//
// https://www.shadertoy.com/view/tscSRS
//
// and further fixed and improved
//
float apprSoftShadow(vec3 ro, vec3 rd, float mint, float tmax, float w)
{ | 1 | 3 |
WdyXRD | iq | 2019-11-11T20:42:12 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader is what I believe the most accurate fake soft shadow implementation
// to date, in that it reproduces correct inner and outer penumbra sizes casting
// a single ray. It matches the ground truth pretty well for different light
// source sizes.
//
// Code is in line 119
//------------------------------------------------------------------
float sdPlane( vec3 p )
{
return p.y;
}
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
float sdSphere( vec3 p, float r )
{
return length(p) - r;
}
float sdCylinder(vec3 p, float h, float r)
{
vec2 q = vec2( length(p.xz)-r, abs(p.y-h*0.5)-h*0.5 );
return min( max(q.x,q.y),0.0) + length(max(q,0.0));
}
bool shadowBox( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad, in float tmax )
{
vec3 m = 1.0/rd;
vec3 n = m*(ro-cen);
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return false;
return tN>0.0 && tN<tmax;
}
bool shadowSphere( in vec3 ro, in vec3 rd, in vec3 cen, in float rad, in float tmax )
{
vec3 oc = ro - cen;
float b = dot( oc, rd );
float c = dot( oc, oc ) - rad*rad;
float h = b*b - c;
if( h<0.0 ) return false;
float t = -b - sqrt( h );
return t>0.0 && t<tmax;
}
bool shadowCylinder( in vec3 ro, in vec3 rd, in float he, float ra, in float tmax )
{
float he2 = he*he;
float k2 = 1.0 - rd.y*rd.y;
float k1 = dot(ro,rd) - ro.y*rd.y;
float k0 = dot(ro,ro) - ro.y*ro.y - ra*ra;
float h = k1*k1 - k2*k0;
if( h<0.0 ) return false;
h = sqrt(h);
float t = (-k1-h)/k2;
// body
float y = ro.y + t*rd.y;
if( y>0.0 && y<he )
{
return t>0.0 && t<tmax;
}
// caps
t = ( ((y<0.0) ? 0.0 : he) - ro.y)/rd.y;
if( abs(k1+k2*t)<h )
{
return t>0.0 && t<tmax;
}
return false;
}
//------------------------------------------------------------------
float map( in vec3 pos )
{
vec3 p2 = vec3( mod(pos.x+1.0,3.0)-1.0, pos.yz );
vec3 p3 = vec3( mod(pos.x+2.0,3.0)-1.0, pos.yz );
vec3 p4 = vec3( mod(pos.x+3.0,3.0)-1.0, pos.yz );
float d1 = sdPlane( pos-vec3(0.0,0.00,0.0) );
float d2 = sdSphere( p2-vec3(0.0,0.30,0.0), 0.4 );
float d3 = sdBox( p3-vec3(0.0,0.25,0.0), vec3(0.2,0.5,0.2) );
float d4 = sdCylinder( p4-vec3(0.0,0.0,0.0), 0.8,0.3 );
return min(min(d1,d2),min(d3,d4));
}
//------------------------------------------------------------------
//
// Approximated soft shadows, based on
//
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
//
// and
//
// https://www.shadertoy.com/view/tscSRS
//
// and further fixed and improved
//
float apprSoftShadow(vec3 ro, vec3 rd, float mint, float tmax, float w)
{
float t = mint;
float res = 1.0;
for( int i=0; i<256; i++ )
{
float h = map(ro + t*rd);
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
}
// montecarlo based shadow, for ground truth comparison
float seed; float rand(void) { return fract(sin(seed++)*768.475278); }
float realSoftShadow( in vec3 ro, in vec3 rd, in float tmin, in float tmax, float w )
{
vec3 uu = normalize(cross(rd,vec3(0,1,0)));
vec3 vv = normalize(cross(rd,uu));
float tot = 0.0;
const int num = 32; // cast 32 rays
for( int j=0; j<num; j++ )
{
// uniform distribution on an disk
float ra = sqrt(rand());
float an = 6.283185*rand();
vec3 jrd = rd + w*ra*(uu*cos(an)+vv*sin(an));
// raycast
float res = 1.0;
for( int i=0; i<7; i++ ) // 7 objects
{
int k = i % 3;
bool sha = false;
if(k==0) sha = shadowBox( ro, jrd, vec3(-4.0 + float(i),0.25,0.0), vec3(0.2,0.5,0.2), tmax);
else if(k==1) sha = shadowSphere(ro, jrd, vec3(-4.0 + float(i),0.3,0.0), 0.4, tmax);
else sha = shadowCylinder( ro - vec3(-4.0 + float(i),0.0,0.0), jrd, 0.8, 0.3, tmax);
if( sha ) { res=0.0; break; }
}
tot += res;
}
return tot/float(num);
}
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy ) +
e.yyx*map( pos + e.yyx ) +
e.yxy*map( pos + e.yxy ) +
e.xxx*map( pos + e.xxx ) );
}
float castRay( in vec3 ro, in vec3 rd )
{
float tmin = 1.0;
float tmax = 20.0;
#if 1
// bounding volume
float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) tmax = min( tmax, tp1 );
float tp2 = (1.0-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.0 ) tmin = max( tmin, tp2 );
else tmax = min( tmax, tp2 ); }
#endif
float t = tmin;
for( int i=0; i<128; i++ )
{
float precis = 0.0005*t;
float res = map( ro+rd*t );
if( res<precis || t>tmax ) break;
t += res;
}
if( t>tmax ) t=-1.0;
return t;
}
float calcAO( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float h = 0.001 + 0.15*float(i)/4.0;
float d = map( pos + h*nor );
occ += (h-d)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 1.5*occ, 0.0, 1.0 );
}
vec3 render( in vec3 ro, in vec3 rd, in int technique, in float lightSize)
{
vec3 col = vec3(0.0);
float t = castRay(ro,rd);
if( t>-0.5 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
// material
vec3 mate = vec3(0.3);
// key light
vec3 lig = normalize( vec3(-0.1, 0.3, 0.6) );
vec3 hal = normalize( lig-rd );
float sha = (technique==0) ? realSoftShadow(pos, lig, 0.01, 3.0, lightSize )
: apprSoftShadow(pos, lig, 0.01, 3.0, lightSize );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 ) * sha;
float spe = pow(clamp(dot(nor,hal),0.0,1.0),16.0)*
dif *
(0.04+0.96*pow(clamp(1.0+dot(hal,rd),0.0,1.0),5.0));
col = mate * 4.0*dif*vec3(1.00,0.70,0.5);
col += 9.0*spe*vec3(0.90,0.80,1.0);
// ambient light
float occ = (pos.y>0.01) ? 1.0 : calcAO( pos, nor );
float amb = 0.5 + 0.5*nor.y;
col += mate*amb*occ*vec3(0.05,0.1,0.15);
// fog
col *= exp( -0.0008*t*t*t );
}
return col;
}
mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
vec3 cw = normalize(ta-ro);
vec3 cp = vec3(sin(cr), cos(cr),0.0);
vec3 cu = normalize( cross(cw,cp) );
vec3 cv = normalize( cross(cu,cw) );
return mat3( cu, cv, cw );
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // make this 2 or 3 for antialiasing
#endif
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera
float an = 12.0 - sin(0.1*iTime);
vec3 ro = vec3( 3.0*cos(0.1*an), 1.0, -3.0*sin(0.1*an) );
vec3 ta = vec3( 0.0, -0.4, 0.0 );
// camera-to-world transformation
mat3 ca = setCamera( ro, ta, 0.0 );
seed = sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)*sin(iTime);
// animation
int technique = (fract(iTime/3.0)>0.5) ? 1 : 0;
float lightSize = 0.05 + 0.04*sin(0.7*iTime);
// render
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// ray direction
vec3 rd = ca * normalize( vec3(p.xy,2.0) );
// render
vec3 col = render( ro, rd, technique, lightSize);
// gain
col = 1.8*col/(1.0+dot(col,vec3(0.333)));
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
4298,
4366,
4384,
4384,
4424
] | [
[
1435,
1435,
1460,
1460,
1475
],
[
1477,
1477,
1508,
1508,
1599
],
[
1601,
1601,
1636,
1636,
1664
],
[
1666,
1666,
1710,
1710,
1827
],
[
1829,
1829,
1913,
1913,
2185
],
[
2187,
2187,
2274,
2274,
2461
],
[
2463,
2463,
2548,
2548,
3070
],
[
3142,
3142,
3168,
3168,
3620
],
[
3622,
3892,
3965,
3965,
4295
],
[
4298,
4366,
4384,
4384,
4424
],
[
4426,
4426,
4513,
4513,
5492
],
[
5494,
5494,
5526,
5526,
5726
],
[
5728,
5728,
5769,
5769,
6339
],
[
6341,
6341,
6383,
6383,
6644
],
[
6646,
6646,
6722,
6722,
7809
],
[
7811,
7811,
7863,
7863,
8040
]
] | // montecarlo based shadow, for ground truth comparison
float seed; | float rand(void) { | return fract(sin(seed++)*768.475278); } | // montecarlo based shadow, for ground truth comparison
float seed; float rand(void) { | 1 | 1 |
Ws3XWl | iq | 2019-11-13T23:45:13 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader uses a a grid of spheres to carve out fractal detail from
// a solid block. Unlike naive SDF disaplcemente by a traditional fBM,
// this shader produces a field that is a valid SDF, so there's no need
// to reduce the raymarcher's step size to get artifact free visuals.
//
// The article that explains this technique can be found here:
//
// https://iquilezles.org/www/articles/fbmsdf/fbmsdf.htm
//
// A additive synthesis example of this technique, here:
//
// https://www.shadertoy.com/view/3dGSWR
// 0 = lattice
// 1 = simplex
#define NOISE 0
// please, do not use in real projects - replace this by something better
float hash(vec3 p)
{
p = 17.0*fract( p*0.3183099+vec3(.11,.17,.13) );
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}
// http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
// http://iquilezles.org/www/articles/smin/smin.htm
float smax( float a, float b, float k )
{
float h = max(k-abs(a-b),0.0);
return max(a, b) + h*h*0.25/k;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
}
//---------------------------------------------------------------
// A random SDF - it places spheres of random sizes in a grid
//---------------------------------------------------------------
float sdBase( in vec3 p )
{
#if NOISE==0
vec3 i = floor(p);
vec3 f = fract(p);
#define RAD(r) ((r)*(r)*0.7)
#define SPH(i,f,c) length(f-c)-RAD(hash(i+c))
return min(min(min(SPH(i,f,vec3(0,0,0)),
SPH(i,f,vec3(0,0,1))),
min(SPH(i,f,vec3(0,1,0)),
SPH(i,f,vec3(0,1,1)))),
min(min(SPH(i,f,vec3(1,0,0)),
SPH(i,f,vec3(1,0,1))),
min(SPH(i,f,vec3(1,1,0)),
SPH(i,f,vec3(1,1,1)))));
#else
const float K1 = 0.333333333;
const float K2 = 0.166666667;
vec3 i = floor(p + (p.x + p.y + p.z) * K1);
vec3 d0 = p - (i - (i.x + i.y + i.z) * K2);
vec3 e = step(d0.yzx, d0);
vec3 i1 = e*(1.0-e.zxy);
vec3 i2 = 1.0-e.zxy*(1.0-e);
vec3 d1 = d0 - (i1 - 1.0*K2);
vec3 d2 = d0 - (i2 - 2.0*K2);
vec3 d3 = d0 - (1.0 - 3.0*K2);
float r0 = hash( i+0.0 );
float r1 = hash( i+i1 );
float r2 = hash( i+i2 );
float r3 = hash( i+1.0 );
#define SPH(d,r) length(d)-r*r*0.55
return min( min(SPH(d0,r0),
SPH(d1,r1)),
min(SPH(d2,r2),
SPH(d3,r3)));
#endif
}
//---------------------------------------------------------------
// subtractive fbm
//---------------------------------------------------------------
vec2 sdFbm( in vec3 p, float d )
{
const mat3 m = mat3( 0.00, 0.80, 0.60,
-0.80, 0.36, -0.48,
-0.60, -0.48, 0.64 );
float t = 0.0;
float s = 1.0;
for( int i=0; i<7; i++ )
{
float n = s*sdBase(p);
d = smax( d, -n, 0.2*s );
t += d;
p = 2.0*m*p;
s = 0.5*s;
}
return vec2(d,t);
}
vec2 map( in vec3 p )
{
// box
float d = sdBox( p, vec3(1.0) );
// fbm
vec2 dt = sdFbm( p+0.5, d );
dt.y = 1.0+dt.y*2.0; dt.y = dt.y*dt.y;
return dt;
}
const float precis = 0.0005;
vec2 raycast( in vec3 ro, in vec3 rd )
{
vec2 res = vec2(-1.0);
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return res;
// raymarch
float t = dis.x;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
vec2 h = map( pos );
res.x = t;
res.y = h.y;
if( h.x<precis || t>dis.y ) break;
t += h.x;
}
if( t>dis.y ) res = vec2(-1.0);
return res;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773*precis;
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftShadow(vec3 ro, vec3 rd, float tmin, float tmax, float w)
{
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return 1.0;
tmin = max(tmin,dis.x);
tmax = min(tmax,dis.y);
float t = tmin;
float res = 1.0;
for( int i=0; i<128; i++ )
{
float h = map(ro + t*rd).x;
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 1 // make this 2 or 3 for antialiasing
#endif
#define ZERO min(iFrame,0)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// camera anim
float an = -0.1*iTime;
vec3 ro = 4.0*vec3( cos(an), 0.4, sin(an) );
vec3 ta = vec3( 0.0, -0.35, 0.0 );
// camera matrix
vec3 cw = normalize( ta-ro );
vec3 cu = normalize( cross(cw,vec3(0.0,1.0,0.0)) );
vec3 cv = normalize( cross(cu,cw) );
vec3 rd = normalize( p.x*cu + p.y*cv + 2.7*cw );
// render
vec3 col = vec3(0.01);
vec2 tm = raycast( ro, rd );
float t = tm.x;
if( t>0.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
float occ = tm.y*tm.y;
// material
vec3 mate = mix( vec3(0.6,0.3,0.1), vec3(1), tm.y )*0.7;
// key light
{
const vec3 lig = normalize(vec3(1.0,0.5,0.6));
float dif = dot(lig,nor);
if( dif>0.0 ) dif *= calcSoftShadow(pos+nor*0.001,lig,0.001,10.0,0.003);
dif = clamp(dif,0.0,1.0);
vec3 hal = normalize(lig-rd);
float spe = clamp(dot(hal,nor),0.0,1.0);
spe = pow(spe,4.0)*dif*(0.04+0.96*pow(max(1.0-dot(hal,lig),0.0),5.0));
col = vec3(0.0);
col += mate*1.5*vec3(1.30,0.85,0.75)*dif;
col += 9.0*spe;
}
// ambient light
{
col += mate*0.2*vec3(0.40,0.45,0.60)*occ*(0.6+0.4*nor.y);
}
}
// tonemap
col = col*1.7/(1.0+col);
// gamma
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// vignetting
vec2 q = fragCoord/iResolution.xy;
tot *= 0.7 + 0.3*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.2);
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor=vec4(tot,1.0);
} | mit | [
1655,
1729,
1751,
1751,
1854
] | [
[
1655,
1729,
1751,
1751,
1854
],
[
1856,
1926,
1957,
1957,
2048
],
[
2050,
2102,
2143,
2143,
2215
],
[
2217,
2285,
2336,
2336,
2604
],
[
2801,
2801,
2828,
2828,
4022
],
[
4024,
4175,
4209,
4209,
4570
],
[
4572,
4572,
4595,
4606,
4754
],
[
4786,
4786,
4826,
4826,
5225
],
[
5227,
5291,
5323,
5323,
5531
],
[
5533,
5595,
5668,
5695,
6167
]
] | // please, do not use in real projects - replace this by something better
| float hash(vec3 p)
{ |
p = 17.0*fract( p*0.3183099+vec3(.11,.17,.13) );
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
} | // please, do not use in real projects - replace this by something better
float hash(vec3 p)
{ | 2 | 2 |
Ws3XWl | iq | 2019-11-13T23:45:13 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader uses a a grid of spheres to carve out fractal detail from
// a solid block. Unlike naive SDF disaplcemente by a traditional fBM,
// this shader produces a field that is a valid SDF, so there's no need
// to reduce the raymarcher's step size to get artifact free visuals.
//
// The article that explains this technique can be found here:
//
// https://iquilezles.org/www/articles/fbmsdf/fbmsdf.htm
//
// A additive synthesis example of this technique, here:
//
// https://www.shadertoy.com/view/3dGSWR
// 0 = lattice
// 1 = simplex
#define NOISE 0
// please, do not use in real projects - replace this by something better
float hash(vec3 p)
{
p = 17.0*fract( p*0.3183099+vec3(.11,.17,.13) );
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}
// http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
// http://iquilezles.org/www/articles/smin/smin.htm
float smax( float a, float b, float k )
{
float h = max(k-abs(a-b),0.0);
return max(a, b) + h*h*0.25/k;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
}
//---------------------------------------------------------------
// A random SDF - it places spheres of random sizes in a grid
//---------------------------------------------------------------
float sdBase( in vec3 p )
{
#if NOISE==0
vec3 i = floor(p);
vec3 f = fract(p);
#define RAD(r) ((r)*(r)*0.7)
#define SPH(i,f,c) length(f-c)-RAD(hash(i+c))
return min(min(min(SPH(i,f,vec3(0,0,0)),
SPH(i,f,vec3(0,0,1))),
min(SPH(i,f,vec3(0,1,0)),
SPH(i,f,vec3(0,1,1)))),
min(min(SPH(i,f,vec3(1,0,0)),
SPH(i,f,vec3(1,0,1))),
min(SPH(i,f,vec3(1,1,0)),
SPH(i,f,vec3(1,1,1)))));
#else
const float K1 = 0.333333333;
const float K2 = 0.166666667;
vec3 i = floor(p + (p.x + p.y + p.z) * K1);
vec3 d0 = p - (i - (i.x + i.y + i.z) * K2);
vec3 e = step(d0.yzx, d0);
vec3 i1 = e*(1.0-e.zxy);
vec3 i2 = 1.0-e.zxy*(1.0-e);
vec3 d1 = d0 - (i1 - 1.0*K2);
vec3 d2 = d0 - (i2 - 2.0*K2);
vec3 d3 = d0 - (1.0 - 3.0*K2);
float r0 = hash( i+0.0 );
float r1 = hash( i+i1 );
float r2 = hash( i+i2 );
float r3 = hash( i+1.0 );
#define SPH(d,r) length(d)-r*r*0.55
return min( min(SPH(d0,r0),
SPH(d1,r1)),
min(SPH(d2,r2),
SPH(d3,r3)));
#endif
}
//---------------------------------------------------------------
// subtractive fbm
//---------------------------------------------------------------
vec2 sdFbm( in vec3 p, float d )
{
const mat3 m = mat3( 0.00, 0.80, 0.60,
-0.80, 0.36, -0.48,
-0.60, -0.48, 0.64 );
float t = 0.0;
float s = 1.0;
for( int i=0; i<7; i++ )
{
float n = s*sdBase(p);
d = smax( d, -n, 0.2*s );
t += d;
p = 2.0*m*p;
s = 0.5*s;
}
return vec2(d,t);
}
vec2 map( in vec3 p )
{
// box
float d = sdBox( p, vec3(1.0) );
// fbm
vec2 dt = sdFbm( p+0.5, d );
dt.y = 1.0+dt.y*2.0; dt.y = dt.y*dt.y;
return dt;
}
const float precis = 0.0005;
vec2 raycast( in vec3 ro, in vec3 rd )
{
vec2 res = vec2(-1.0);
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return res;
// raymarch
float t = dis.x;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
vec2 h = map( pos );
res.x = t;
res.y = h.y;
if( h.x<precis || t>dis.y ) break;
t += h.x;
}
if( t>dis.y ) res = vec2(-1.0);
return res;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773*precis;
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftShadow(vec3 ro, vec3 rd, float tmin, float tmax, float w)
{
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return 1.0;
tmin = max(tmin,dis.x);
tmax = min(tmax,dis.y);
float t = tmin;
float res = 1.0;
for( int i=0; i<128; i++ )
{
float h = map(ro + t*rd).x;
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 1 // make this 2 or 3 for antialiasing
#endif
#define ZERO min(iFrame,0)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// camera anim
float an = -0.1*iTime;
vec3 ro = 4.0*vec3( cos(an), 0.4, sin(an) );
vec3 ta = vec3( 0.0, -0.35, 0.0 );
// camera matrix
vec3 cw = normalize( ta-ro );
vec3 cu = normalize( cross(cw,vec3(0.0,1.0,0.0)) );
vec3 cv = normalize( cross(cu,cw) );
vec3 rd = normalize( p.x*cu + p.y*cv + 2.7*cw );
// render
vec3 col = vec3(0.01);
vec2 tm = raycast( ro, rd );
float t = tm.x;
if( t>0.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
float occ = tm.y*tm.y;
// material
vec3 mate = mix( vec3(0.6,0.3,0.1), vec3(1), tm.y )*0.7;
// key light
{
const vec3 lig = normalize(vec3(1.0,0.5,0.6));
float dif = dot(lig,nor);
if( dif>0.0 ) dif *= calcSoftShadow(pos+nor*0.001,lig,0.001,10.0,0.003);
dif = clamp(dif,0.0,1.0);
vec3 hal = normalize(lig-rd);
float spe = clamp(dot(hal,nor),0.0,1.0);
spe = pow(spe,4.0)*dif*(0.04+0.96*pow(max(1.0-dot(hal,lig),0.0),5.0));
col = vec3(0.0);
col += mate*1.5*vec3(1.30,0.85,0.75)*dif;
col += 9.0*spe;
}
// ambient light
{
col += mate*0.2*vec3(0.40,0.45,0.60)*occ*(0.6+0.4*nor.y);
}
}
// tonemap
col = col*1.7/(1.0+col);
// gamma
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// vignetting
vec2 q = fragCoord/iResolution.xy;
tot *= 0.7 + 0.3*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.2);
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor=vec4(tot,1.0);
} | mit | [
1856,
1926,
1957,
1957,
2048
] | [
[
1655,
1729,
1751,
1751,
1854
],
[
1856,
1926,
1957,
1957,
2048
],
[
2050,
2102,
2143,
2143,
2215
],
[
2217,
2285,
2336,
2336,
2604
],
[
2801,
2801,
2828,
2828,
4022
],
[
4024,
4175,
4209,
4209,
4570
],
[
4572,
4572,
4595,
4606,
4754
],
[
4786,
4786,
4826,
4826,
5225
],
[
5227,
5291,
5323,
5323,
5531
],
[
5533,
5595,
5668,
5695,
6167
]
] | // http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
| float sdBox( vec3 p, vec3 b )
{ |
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
} | // http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBox( vec3 p, vec3 b )
{ | 61 | 579 |
Ws3XWl | iq | 2019-11-13T23:45:13 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader uses a a grid of spheres to carve out fractal detail from
// a solid block. Unlike naive SDF disaplcemente by a traditional fBM,
// this shader produces a field that is a valid SDF, so there's no need
// to reduce the raymarcher's step size to get artifact free visuals.
//
// The article that explains this technique can be found here:
//
// https://iquilezles.org/www/articles/fbmsdf/fbmsdf.htm
//
// A additive synthesis example of this technique, here:
//
// https://www.shadertoy.com/view/3dGSWR
// 0 = lattice
// 1 = simplex
#define NOISE 0
// please, do not use in real projects - replace this by something better
float hash(vec3 p)
{
p = 17.0*fract( p*0.3183099+vec3(.11,.17,.13) );
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}
// http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
// http://iquilezles.org/www/articles/smin/smin.htm
float smax( float a, float b, float k )
{
float h = max(k-abs(a-b),0.0);
return max(a, b) + h*h*0.25/k;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
}
//---------------------------------------------------------------
// A random SDF - it places spheres of random sizes in a grid
//---------------------------------------------------------------
float sdBase( in vec3 p )
{
#if NOISE==0
vec3 i = floor(p);
vec3 f = fract(p);
#define RAD(r) ((r)*(r)*0.7)
#define SPH(i,f,c) length(f-c)-RAD(hash(i+c))
return min(min(min(SPH(i,f,vec3(0,0,0)),
SPH(i,f,vec3(0,0,1))),
min(SPH(i,f,vec3(0,1,0)),
SPH(i,f,vec3(0,1,1)))),
min(min(SPH(i,f,vec3(1,0,0)),
SPH(i,f,vec3(1,0,1))),
min(SPH(i,f,vec3(1,1,0)),
SPH(i,f,vec3(1,1,1)))));
#else
const float K1 = 0.333333333;
const float K2 = 0.166666667;
vec3 i = floor(p + (p.x + p.y + p.z) * K1);
vec3 d0 = p - (i - (i.x + i.y + i.z) * K2);
vec3 e = step(d0.yzx, d0);
vec3 i1 = e*(1.0-e.zxy);
vec3 i2 = 1.0-e.zxy*(1.0-e);
vec3 d1 = d0 - (i1 - 1.0*K2);
vec3 d2 = d0 - (i2 - 2.0*K2);
vec3 d3 = d0 - (1.0 - 3.0*K2);
float r0 = hash( i+0.0 );
float r1 = hash( i+i1 );
float r2 = hash( i+i2 );
float r3 = hash( i+1.0 );
#define SPH(d,r) length(d)-r*r*0.55
return min( min(SPH(d0,r0),
SPH(d1,r1)),
min(SPH(d2,r2),
SPH(d3,r3)));
#endif
}
//---------------------------------------------------------------
// subtractive fbm
//---------------------------------------------------------------
vec2 sdFbm( in vec3 p, float d )
{
const mat3 m = mat3( 0.00, 0.80, 0.60,
-0.80, 0.36, -0.48,
-0.60, -0.48, 0.64 );
float t = 0.0;
float s = 1.0;
for( int i=0; i<7; i++ )
{
float n = s*sdBase(p);
d = smax( d, -n, 0.2*s );
t += d;
p = 2.0*m*p;
s = 0.5*s;
}
return vec2(d,t);
}
vec2 map( in vec3 p )
{
// box
float d = sdBox( p, vec3(1.0) );
// fbm
vec2 dt = sdFbm( p+0.5, d );
dt.y = 1.0+dt.y*2.0; dt.y = dt.y*dt.y;
return dt;
}
const float precis = 0.0005;
vec2 raycast( in vec3 ro, in vec3 rd )
{
vec2 res = vec2(-1.0);
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return res;
// raymarch
float t = dis.x;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
vec2 h = map( pos );
res.x = t;
res.y = h.y;
if( h.x<precis || t>dis.y ) break;
t += h.x;
}
if( t>dis.y ) res = vec2(-1.0);
return res;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773*precis;
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftShadow(vec3 ro, vec3 rd, float tmin, float tmax, float w)
{
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return 1.0;
tmin = max(tmin,dis.x);
tmax = min(tmax,dis.y);
float t = tmin;
float res = 1.0;
for( int i=0; i<128; i++ )
{
float h = map(ro + t*rd).x;
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 1 // make this 2 or 3 for antialiasing
#endif
#define ZERO min(iFrame,0)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// camera anim
float an = -0.1*iTime;
vec3 ro = 4.0*vec3( cos(an), 0.4, sin(an) );
vec3 ta = vec3( 0.0, -0.35, 0.0 );
// camera matrix
vec3 cw = normalize( ta-ro );
vec3 cu = normalize( cross(cw,vec3(0.0,1.0,0.0)) );
vec3 cv = normalize( cross(cu,cw) );
vec3 rd = normalize( p.x*cu + p.y*cv + 2.7*cw );
// render
vec3 col = vec3(0.01);
vec2 tm = raycast( ro, rd );
float t = tm.x;
if( t>0.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
float occ = tm.y*tm.y;
// material
vec3 mate = mix( vec3(0.6,0.3,0.1), vec3(1), tm.y )*0.7;
// key light
{
const vec3 lig = normalize(vec3(1.0,0.5,0.6));
float dif = dot(lig,nor);
if( dif>0.0 ) dif *= calcSoftShadow(pos+nor*0.001,lig,0.001,10.0,0.003);
dif = clamp(dif,0.0,1.0);
vec3 hal = normalize(lig-rd);
float spe = clamp(dot(hal,nor),0.0,1.0);
spe = pow(spe,4.0)*dif*(0.04+0.96*pow(max(1.0-dot(hal,lig),0.0),5.0));
col = vec3(0.0);
col += mate*1.5*vec3(1.30,0.85,0.75)*dif;
col += 9.0*spe;
}
// ambient light
{
col += mate*0.2*vec3(0.40,0.45,0.60)*occ*(0.6+0.4*nor.y);
}
}
// tonemap
col = col*1.7/(1.0+col);
// gamma
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// vignetting
vec2 q = fragCoord/iResolution.xy;
tot *= 0.7 + 0.3*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.2);
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor=vec4(tot,1.0);
} | mit | [
2050,
2102,
2143,
2143,
2215
] | [
[
1655,
1729,
1751,
1751,
1854
],
[
1856,
1926,
1957,
1957,
2048
],
[
2050,
2102,
2143,
2143,
2215
],
[
2217,
2285,
2336,
2336,
2604
],
[
2801,
2801,
2828,
2828,
4022
],
[
4024,
4175,
4209,
4209,
4570
],
[
4572,
4572,
4595,
4606,
4754
],
[
4786,
4786,
4826,
4826,
5225
],
[
5227,
5291,
5323,
5323,
5531
],
[
5533,
5595,
5668,
5695,
6167
]
] | // http://iquilezles.org/www/articles/smin/smin.htm
| float smax( float a, float b, float k )
{ |
float h = max(k-abs(a-b),0.0);
return max(a, b) + h*h*0.25/k;
} | // http://iquilezles.org/www/articles/smin/smin.htm
float smax( float a, float b, float k )
{ | 21 | 42 |
Ws3XWl | iq | 2019-11-13T23:45:13 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader uses a a grid of spheres to carve out fractal detail from
// a solid block. Unlike naive SDF disaplcemente by a traditional fBM,
// this shader produces a field that is a valid SDF, so there's no need
// to reduce the raymarcher's step size to get artifact free visuals.
//
// The article that explains this technique can be found here:
//
// https://iquilezles.org/www/articles/fbmsdf/fbmsdf.htm
//
// A additive synthesis example of this technique, here:
//
// https://www.shadertoy.com/view/3dGSWR
// 0 = lattice
// 1 = simplex
#define NOISE 0
// please, do not use in real projects - replace this by something better
float hash(vec3 p)
{
p = 17.0*fract( p*0.3183099+vec3(.11,.17,.13) );
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}
// http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
// http://iquilezles.org/www/articles/smin/smin.htm
float smax( float a, float b, float k )
{
float h = max(k-abs(a-b),0.0);
return max(a, b) + h*h*0.25/k;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
}
//---------------------------------------------------------------
// A random SDF - it places spheres of random sizes in a grid
//---------------------------------------------------------------
float sdBase( in vec3 p )
{
#if NOISE==0
vec3 i = floor(p);
vec3 f = fract(p);
#define RAD(r) ((r)*(r)*0.7)
#define SPH(i,f,c) length(f-c)-RAD(hash(i+c))
return min(min(min(SPH(i,f,vec3(0,0,0)),
SPH(i,f,vec3(0,0,1))),
min(SPH(i,f,vec3(0,1,0)),
SPH(i,f,vec3(0,1,1)))),
min(min(SPH(i,f,vec3(1,0,0)),
SPH(i,f,vec3(1,0,1))),
min(SPH(i,f,vec3(1,1,0)),
SPH(i,f,vec3(1,1,1)))));
#else
const float K1 = 0.333333333;
const float K2 = 0.166666667;
vec3 i = floor(p + (p.x + p.y + p.z) * K1);
vec3 d0 = p - (i - (i.x + i.y + i.z) * K2);
vec3 e = step(d0.yzx, d0);
vec3 i1 = e*(1.0-e.zxy);
vec3 i2 = 1.0-e.zxy*(1.0-e);
vec3 d1 = d0 - (i1 - 1.0*K2);
vec3 d2 = d0 - (i2 - 2.0*K2);
vec3 d3 = d0 - (1.0 - 3.0*K2);
float r0 = hash( i+0.0 );
float r1 = hash( i+i1 );
float r2 = hash( i+i2 );
float r3 = hash( i+1.0 );
#define SPH(d,r) length(d)-r*r*0.55
return min( min(SPH(d0,r0),
SPH(d1,r1)),
min(SPH(d2,r2),
SPH(d3,r3)));
#endif
}
//---------------------------------------------------------------
// subtractive fbm
//---------------------------------------------------------------
vec2 sdFbm( in vec3 p, float d )
{
const mat3 m = mat3( 0.00, 0.80, 0.60,
-0.80, 0.36, -0.48,
-0.60, -0.48, 0.64 );
float t = 0.0;
float s = 1.0;
for( int i=0; i<7; i++ )
{
float n = s*sdBase(p);
d = smax( d, -n, 0.2*s );
t += d;
p = 2.0*m*p;
s = 0.5*s;
}
return vec2(d,t);
}
vec2 map( in vec3 p )
{
// box
float d = sdBox( p, vec3(1.0) );
// fbm
vec2 dt = sdFbm( p+0.5, d );
dt.y = 1.0+dt.y*2.0; dt.y = dt.y*dt.y;
return dt;
}
const float precis = 0.0005;
vec2 raycast( in vec3 ro, in vec3 rd )
{
vec2 res = vec2(-1.0);
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return res;
// raymarch
float t = dis.x;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
vec2 h = map( pos );
res.x = t;
res.y = h.y;
if( h.x<precis || t>dis.y ) break;
t += h.x;
}
if( t>dis.y ) res = vec2(-1.0);
return res;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773*precis;
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftShadow(vec3 ro, vec3 rd, float tmin, float tmax, float w)
{
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return 1.0;
tmin = max(tmin,dis.x);
tmax = min(tmax,dis.y);
float t = tmin;
float res = 1.0;
for( int i=0; i<128; i++ )
{
float h = map(ro + t*rd).x;
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 1 // make this 2 or 3 for antialiasing
#endif
#define ZERO min(iFrame,0)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// camera anim
float an = -0.1*iTime;
vec3 ro = 4.0*vec3( cos(an), 0.4, sin(an) );
vec3 ta = vec3( 0.0, -0.35, 0.0 );
// camera matrix
vec3 cw = normalize( ta-ro );
vec3 cu = normalize( cross(cw,vec3(0.0,1.0,0.0)) );
vec3 cv = normalize( cross(cu,cw) );
vec3 rd = normalize( p.x*cu + p.y*cv + 2.7*cw );
// render
vec3 col = vec3(0.01);
vec2 tm = raycast( ro, rd );
float t = tm.x;
if( t>0.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
float occ = tm.y*tm.y;
// material
vec3 mate = mix( vec3(0.6,0.3,0.1), vec3(1), tm.y )*0.7;
// key light
{
const vec3 lig = normalize(vec3(1.0,0.5,0.6));
float dif = dot(lig,nor);
if( dif>0.0 ) dif *= calcSoftShadow(pos+nor*0.001,lig,0.001,10.0,0.003);
dif = clamp(dif,0.0,1.0);
vec3 hal = normalize(lig-rd);
float spe = clamp(dot(hal,nor),0.0,1.0);
spe = pow(spe,4.0)*dif*(0.04+0.96*pow(max(1.0-dot(hal,lig),0.0),5.0));
col = vec3(0.0);
col += mate*1.5*vec3(1.30,0.85,0.75)*dif;
col += 9.0*spe;
}
// ambient light
{
col += mate*0.2*vec3(0.40,0.45,0.60)*occ*(0.6+0.4*nor.y);
}
}
// tonemap
col = col*1.7/(1.0+col);
// gamma
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// vignetting
vec2 q = fragCoord/iResolution.xy;
tot *= 0.7 + 0.3*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.2);
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor=vec4(tot,1.0);
} | mit | [
2217,
2285,
2336,
2336,
2604
] | [
[
1655,
1729,
1751,
1751,
1854
],
[
1856,
1926,
1957,
1957,
2048
],
[
2050,
2102,
2143,
2143,
2215
],
[
2217,
2285,
2336,
2336,
2604
],
[
2801,
2801,
2828,
2828,
4022
],
[
4024,
4175,
4209,
4209,
4570
],
[
4572,
4572,
4595,
4606,
4754
],
[
4786,
4786,
4826,
4826,
5225
],
[
5227,
5291,
5323,
5323,
5531
],
[
5533,
5595,
5668,
5695,
6167
]
] | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
| vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{ |
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
} | // http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{ | 4 | 24 |
Ws3XWl | iq | 2019-11-13T23:45:13 | // The MIT License
// Copyright © 2019 Inigo Quilez
// 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.
// This shader uses a a grid of spheres to carve out fractal detail from
// a solid block. Unlike naive SDF disaplcemente by a traditional fBM,
// this shader produces a field that is a valid SDF, so there's no need
// to reduce the raymarcher's step size to get artifact free visuals.
//
// The article that explains this technique can be found here:
//
// https://iquilezles.org/www/articles/fbmsdf/fbmsdf.htm
//
// A additive synthesis example of this technique, here:
//
// https://www.shadertoy.com/view/3dGSWR
// 0 = lattice
// 1 = simplex
#define NOISE 0
// please, do not use in real projects - replace this by something better
float hash(vec3 p)
{
p = 17.0*fract( p*0.3183099+vec3(.11,.17,.13) );
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}
// http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
// http://iquilezles.org/www/articles/smin/smin.htm
float smax( float a, float b, float k )
{
float h = max(k-abs(a-b),0.0);
return max(a, b) + h*h*0.25/k;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max( max( t1.x, t1.y ), t1.z );
float tF = min( min( t2.x, t2.y ), t2.z );
if( tN > tF || tF < 0.0) return vec2(-1.0);
return vec2( tN, tF );
}
//---------------------------------------------------------------
// A random SDF - it places spheres of random sizes in a grid
//---------------------------------------------------------------
float sdBase( in vec3 p )
{
#if NOISE==0
vec3 i = floor(p);
vec3 f = fract(p);
#define RAD(r) ((r)*(r)*0.7)
#define SPH(i,f,c) length(f-c)-RAD(hash(i+c))
return min(min(min(SPH(i,f,vec3(0,0,0)),
SPH(i,f,vec3(0,0,1))),
min(SPH(i,f,vec3(0,1,0)),
SPH(i,f,vec3(0,1,1)))),
min(min(SPH(i,f,vec3(1,0,0)),
SPH(i,f,vec3(1,0,1))),
min(SPH(i,f,vec3(1,1,0)),
SPH(i,f,vec3(1,1,1)))));
#else
const float K1 = 0.333333333;
const float K2 = 0.166666667;
vec3 i = floor(p + (p.x + p.y + p.z) * K1);
vec3 d0 = p - (i - (i.x + i.y + i.z) * K2);
vec3 e = step(d0.yzx, d0);
vec3 i1 = e*(1.0-e.zxy);
vec3 i2 = 1.0-e.zxy*(1.0-e);
vec3 d1 = d0 - (i1 - 1.0*K2);
vec3 d2 = d0 - (i2 - 2.0*K2);
vec3 d3 = d0 - (1.0 - 3.0*K2);
float r0 = hash( i+0.0 );
float r1 = hash( i+i1 );
float r2 = hash( i+i2 );
float r3 = hash( i+1.0 );
#define SPH(d,r) length(d)-r*r*0.55
return min( min(SPH(d0,r0),
SPH(d1,r1)),
min(SPH(d2,r2),
SPH(d3,r3)));
#endif
}
//---------------------------------------------------------------
// subtractive fbm
//---------------------------------------------------------------
vec2 sdFbm( in vec3 p, float d )
{
const mat3 m = mat3( 0.00, 0.80, 0.60,
-0.80, 0.36, -0.48,
-0.60, -0.48, 0.64 );
float t = 0.0;
float s = 1.0;
for( int i=0; i<7; i++ )
{
float n = s*sdBase(p);
d = smax( d, -n, 0.2*s );
t += d;
p = 2.0*m*p;
s = 0.5*s;
}
return vec2(d,t);
}
vec2 map( in vec3 p )
{
// box
float d = sdBox( p, vec3(1.0) );
// fbm
vec2 dt = sdFbm( p+0.5, d );
dt.y = 1.0+dt.y*2.0; dt.y = dt.y*dt.y;
return dt;
}
const float precis = 0.0005;
vec2 raycast( in vec3 ro, in vec3 rd )
{
vec2 res = vec2(-1.0);
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return res;
// raymarch
float t = dis.x;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
vec2 h = map( pos );
res.x = t;
res.y = h.y;
if( h.x<precis || t>dis.y ) break;
t += h.x;
}
if( t>dis.y ) res = vec2(-1.0);
return res;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773*precis;
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x );
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftShadow(vec3 ro, vec3 rd, float tmin, float tmax, float w)
{
// bounding volume
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return 1.0;
tmin = max(tmin,dis.x);
tmax = min(tmax,dis.y);
float t = tmin;
float res = 1.0;
for( int i=0; i<128; i++ )
{
float h = map(ro + t*rd).x;
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
}
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 1 // make this 2 or 3 for antialiasing
#endif
#define ZERO min(iFrame,0)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
float d = 0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// camera anim
float an = -0.1*iTime;
vec3 ro = 4.0*vec3( cos(an), 0.4, sin(an) );
vec3 ta = vec3( 0.0, -0.35, 0.0 );
// camera matrix
vec3 cw = normalize( ta-ro );
vec3 cu = normalize( cross(cw,vec3(0.0,1.0,0.0)) );
vec3 cv = normalize( cross(cu,cw) );
vec3 rd = normalize( p.x*cu + p.y*cv + 2.7*cw );
// render
vec3 col = vec3(0.01);
vec2 tm = raycast( ro, rd );
float t = tm.x;
if( t>0.0 )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
float occ = tm.y*tm.y;
// material
vec3 mate = mix( vec3(0.6,0.3,0.1), vec3(1), tm.y )*0.7;
// key light
{
const vec3 lig = normalize(vec3(1.0,0.5,0.6));
float dif = dot(lig,nor);
if( dif>0.0 ) dif *= calcSoftShadow(pos+nor*0.001,lig,0.001,10.0,0.003);
dif = clamp(dif,0.0,1.0);
vec3 hal = normalize(lig-rd);
float spe = clamp(dot(hal,nor),0.0,1.0);
spe = pow(spe,4.0)*dif*(0.04+0.96*pow(max(1.0-dot(hal,lig),0.0),5.0));
col = vec3(0.0);
col += mate*1.5*vec3(1.30,0.85,0.75)*dif;
col += 9.0*spe;
}
// ambient light
{
col += mate*0.2*vec3(0.40,0.45,0.60)*occ*(0.6+0.4*nor.y);
}
}
// tonemap
col = col*1.7/(1.0+col);
// gamma
col = pow(col,vec3(0.4545));
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// vignetting
vec2 q = fragCoord/iResolution.xy;
tot *= 0.7 + 0.3*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.2);
// cheap dithering
tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor=vec4(tot,1.0);
} | mit | [
5533,
5595,
5668,
5695,
6167
] | [
[
1655,
1729,
1751,
1751,
1854
],
[
1856,
1926,
1957,
1957,
2048
],
[
2050,
2102,
2143,
2143,
2215
],
[
2217,
2285,
2336,
2336,
2604
],
[
2801,
2801,
2828,
2828,
4022
],
[
4024,
4175,
4209,
4209,
4570
],
[
4572,
4572,
4595,
4606,
4754
],
[
4786,
4786,
4826,
4826,
5225
],
[
5227,
5291,
5323,
5323,
5531
],
[
5533,
5595,
5668,
5695,
6167
]
] | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
| float calcSoftShadow(vec3 ro, vec3 rd, float tmin, float tmax, float w)
{ |
vec2 dis = iBox( ro, rd, vec3(1.0) ) ;
if( dis.y<0.0 ) return 1.0;
tmin = max(tmin,dis.x);
tmax = min(tmax,dis.y);
float t = tmin;
float res = 1.0;
for( int i=0; i<128; i++ )
{
float h = map(ro + t*rd).x;
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res<-1.0 || t>tmax ) break;
}
res = max(res,-1.0); // clamp to [-1,1]
return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep
} | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftShadow(vec3 ro, vec3 rd, float tmin, float tmax, float w)
{ | 1 | 1 |
Wl33RN | Codax | 2019-12-11T04:07:01 | // The MIT License
// Copyright © 2019 Miguel "Codax" Nieves
// Twitter: @GameDevMig
// 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.
#define PI 3.14159265359
//Inigo Quilez's Palette Function
//https://www.shadertoy.com/view/ll2GD3
vec3 pal( in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d )
{
return a + b*cos( 6.28318*(c*t+d) );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
//How many stripes to show (even works best)
float stripes = 16.0;
//Wave Scroll Speed
float speed = 0.35;
//Animated height of each wave from -1 to 1
float amplitude = sin(iTime * 2.0);
//float amplitude = (2.0 * abs( (2.0 * fract(iTime * 0.5))-1.0 ) - 1.0); //Triangle Wave
amplitude *= 0.68;//0.78;
uv.y = uv.y * stripes;
uv.x = uv.x * 4.0;
float waveID = round(uv.y);
//Current Wave but cap the parts would overlap
vec2 waveUV = uv;
waveUV.x += iTime * speed * (2.0 * step(1.0,mod(waveID,2.0)) - 1.);
waveUV.y += max(-0.5, min(0.5, sin(waveUV.x * PI * 2.0) * amplitude));
//Use next line only if amplitude is between -0.5 and 0.5
//waveUV.y += sin(waveUV.x * PI * 2.0) * amplitude;
//Hold on to the current Stripe value
float midWave = waveUV.y;
//Calculate the Value from the Stripe Above
float upperWave = uv.x + ( iTime * speed * (2.0 * step(1.0,mod(waveID + 1.0,2.0)) - 1.));
upperWave = sin(upperWave * PI * 2.0) * amplitude;
upperWave *= 1.0- step(0.5, fract(uv.y));
/*if (fract(uv.y) >= 0.5) //Optimized Out
{
upperWave = 0.0;
}*/
upperWave += fract(uv.y);
upperWave = step(1.0, upperWave);
//Calculate the Value from the Strip Below
float lowerWave = uv.x + ( iTime * speed * (2.0 * step(1.0,mod(waveID - 1.0,2.0)) - 1.));
lowerWave = sin(lowerWave * PI * 2.0) * amplitude;
lowerWave *= step(0.5, fract(uv.y));
/*if (fract(uv.y) < 0.5) //Optimized Out
{
lowerWave = 0.0;
}
*/
lowerWave += fract(uv.y);
lowerWave = step(0.00, lowerWave);
//Mix and Overlap
midWave *= upperWave; //Use the Upperwave to first mask the mid wave
upperWave *= step(0.000,midWave - waveID); //Then overlap the upperwave by the midwave
waveUV.y *= (1.0 - upperWave); //Mask out the upper Wave
waveUV.y += upperWave * (uv.y + 1.0); //Add in the upper
waveUV.y *= lowerWave; //Mask out the lower wave from the
waveUV.y += (1.0 - lowerWave) * (uv.y - 1.0); //Put in the overlap from the lower wave
//Create a color ID from 0.0 - 1.0
float colorID = floor( waveUV.y ) / stripes;
float ct = iTime * 0.1;
//vec3 col = pal( colorID, vec3(0.5,0.5,0.5),vec3(0.5,0.5,0.5),vec3(2.0,1.0,1.0),vec3(0.4 * sin(ct*1.23),0.40 * cos(ct*2.14),0.80 * sin(-ct)) );
vec3 col = pal( colorID, vec3(0.5,0.5,1.0),vec3(0.5,0.5,1.0),vec3(2.0, 1.0, 0.0),vec3(0.2+sin(ct*1.23), 0.2+cos(ct*2.14), 0.1 + sin(-ct)));
//Darken the bottom few waves
col *= smoothstep(1.0, 0.6, 1.0 - colorID);
// Wave Debug
//col = vec3(colorID);// * fract(uv.y));
// Output to screen
fragColor = vec4(col,1.0);
} | mit | [
1138,
1212,
1280,
1280,
1323
] | [
[
1138,
1212,
1280,
1280,
1323
],
[
1325,
1325,
1382,
1432,
4255
]
] | //Inigo Quilez's Palette Function
//https://www.shadertoy.com/view/ll2GD3
| vec3 pal( in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d )
{ |
return a + b*cos( 6.28318*(c*t+d) );
} | //Inigo Quilez's Palette Function
//https://www.shadertoy.com/view/ll2GD3
vec3 pal( in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d )
{ | 68 | 75 |
tlVGDc | iq | 2020-01-27T12:22:54 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Euclidean distance to rhombus with thickness and rounding.
// List of other 3D SDFs: https://www.shadertoy.com/playlist/43cXRl
//
// and http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float ndot(vec2 a, vec2 b ) { return a.x*b.x - a.y*b.y; }
// la,lb=semi axis, h=height, ra=corner
float sdRhombus(vec3 p, float la, float lb, float h, float ra)
{
p = abs(p);
vec2 b = vec2(la,lb);
float f = clamp( (ndot(b,b-2.0*p.xz))/dot(b,b), -1.0, 1.0 );
vec2 q = vec2(length(p.xz-0.5*b*vec2(1.0-f,1.0+f))*sign(p.x*b.y+p.z*b.x-b.x*b.y)-ra, p.y-h);
return min(max(q.x,q.y),0.0) + length(max(q,0.0));
}
float map( in vec3 pos )
{
return sdRhombus(pos, 0.6, 0.2, 0.02, 0.02 ) - 0.01;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps ) +
e.yyx*map( pos + e.yyx*eps ) +
e.yxy*map( pos + e.yxy*eps ) +
e.xxx*map( pos + e.xxx*eps ) );
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.5*(iTime-10.0);
vec3 ro = vec3( 1.0*cos(an), 0.4, 1.0*sin(an) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// raymarch
const float tmax = 5.0;
float t = 0.0;
for( int i=0; i<256; i++ )
{
vec3 pos = ro + t*rd;
float h = map(pos);
if( h<0.0001 || t>tmax ) break;
t += h;
}
// shading/lighting
vec3 col = vec3(0.0);
if( t<tmax )
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal(pos);
float dif = clamp( dot(nor,vec3(0.57703)), 0.0, 1.0 );
float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0));
col = vec3(0.2,0.3,0.4)*amb + vec3(0.8,0.7,0.5)*dif;
}
// gamma
col = sqrt( col );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
1347,
1387,
1451,
1451,
1709
] | [
[
1288,
1288,
1317,
1317,
1345
],
[
1347,
1387,
1451,
1451,
1709
],
[
1712,
1712,
1738,
1738,
1797
],
[
1799,
1863,
1895,
1895,
2134
]
] | // la,lb=semi axis, h=height, ra=corner
| float sdRhombus(vec3 p, float la, float lb, float h, float ra)
{ |
p = abs(p);
vec2 b = vec2(la,lb);
float f = clamp( (ndot(b,b-2.0*p.xz))/dot(b,b), -1.0, 1.0 );
vec2 q = vec2(length(p.xz-0.5*b*vec2(1.0-f,1.0+f))*sign(p.x*b.y+p.z*b.x-b.x*b.y)-ra, p.y-h);
return min(max(q.x,q.y),0.0) + length(max(q,0.0));
} | // la,lb=semi axis, h=height, ra=corner
float sdRhombus(vec3 p, float la, float lb, float h, float ra)
{ | 8 | 12 |
3ttSWX | iq | 2020-02-16T12:20:24 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Space parametrization of a segment, or can maybe also call it
// "capsule" coordinates.
//
// It shows a global coordinate system were things stretch at the
// sides BUT coodinates are global and physical, and also a local
// coordinate system that is a regular grid full of circles).
// List of all shape parametrizations: https://www.shadertoy.com/playlist/XclfRs
float dot2( in vec2 v ) { return dot(v,v); }
float cro(in vec2 a, in vec2 b) { return a.x*b.y-a.y*b.x; }
// x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paSegment( in vec2 p, vec2 a, vec2 b, float r, float band )
{
vec2 ba = b-a;
vec2 pa = p-a;
vec2 pb = p-b;
float f = dot(pa,ba)/dot(ba,ba);
float h = clamp(f, 0.0, 1.0 );
float d = length(pa-h*ba) - r;
float ra = band*round(d/band);
float lba = length(ba);
float l = 0.0;
if( f<0.0 )
{
l = (r+ra)*atan(dot(pa,ba),cro(pa,ba));
}
else if( f>1.0 )
{
l = lba + (r+ra)*atan(dot(pb,ba),cro(pb,ba));
}
else
{
if( cro(pa,ba)<0.0 )
{
l = lba*2.0 + 3.1415927*(r+ra) - f*lba;
}
else
{
l = f*lba;
}
}
return vec4( d-ra, l, lba*2.0+6.283185*(r+ra), d );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// normalized pixel coordinates
vec2 uv = (fragCoord*2.0-iResolution.xy)/iResolution.y;
// animate segment
float id = floor((iTime+0.1)/5.0);
vec2 pa = 0.7*cos( vec2(3,2) + id*1.0);
vec2 pb = 0.7*cos( vec2(6,1) + id*1.7);
float ra = 0.15 + 0.05*sin(id*0.2);
const float bandSize = 0.15;
// distance
vec4 b = paSegment(uv,pa,pb,ra,bandSize);
// base color
float d = b.w;
vec3 col = vec3(1.0,0.68,0.35) + vec3(-0.35,0.15,0.6)*step(d,0.0);
col *= 1.0 - 0.6*exp(-64.0*abs(d));
col *= 1.0-smoothstep(0.47,0.50,abs(fract(d/bandSize)-0.5));
col *= 0.9 + 0.2*smoothstep(0.26,0.24,abs(fract(0.5*d/bandSize+0.25)-0.5));
col += smoothstep(0.004, 0.002, abs(d));
if( d>-bandSize*0.5 )
{
vec2 q = b.xy;
// optional - ensure periodicity, but break physicallity
q.y *= floor(b.z/bandSize)*(bandSize/b.z);
// animate circles
q.y -= iTime*0.1;
// draw circles
vec2 uv = fract(q/bandSize+0.5)-0.5;
float l = length(uv);
col *= 0.1 + 0.9*smoothstep(0.01,0.04,abs(l-0.35));
col *= 0.1 + 0.9*smoothstep(0.10,0.11,l);
}
fragColor = vec4(col, 1.0);
} | mit | [
1556,
1660,
1727,
1727,
2402
] | [
[
1450,
1450,
1475,
1475,
1494
],
[
1495,
1495,
1528,
1528,
1554
],
[
1556,
1660,
1727,
1727,
2402
],
[
2405,
2405,
2462,
2498,
3664
]
] | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
| vec4 paSegment( in vec2 p, vec2 a, vec2 b, float r, float band )
{ |
vec2 ba = b-a;
vec2 pa = p-a;
vec2 pb = p-b;
float f = dot(pa,ba)/dot(ba,ba);
float h = clamp(f, 0.0, 1.0 );
float d = length(pa-h*ba) - r;
float ra = band*round(d/band);
float lba = length(ba);
float l = 0.0;
if( f<0.0 )
{
l = (r+ra)*atan(dot(pa,ba),cro(pa,ba));
}
else if( f>1.0 )
{
l = lba + (r+ra)*atan(dot(pb,ba),cro(pb,ba));
}
else
{
if( cro(pa,ba)<0.0 )
{
l = lba*2.0 + 3.1415927*(r+ra) - f*lba;
}
else
{
l = f*lba;
}
}
return vec4( d-ra, l, lba*2.0+6.283185*(r+ra), d );
} | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paSegment( in vec2 p, vec2 a, vec2 b, float r, float band )
{ | 1 | 1 |
tldSRj | iq | 2020-02-10T21:16:53 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// A wave based noise, similar to gabor and all its variants but much
// simplified, originially explored in shadertoy by user robobo1221
// in this shader: https://www.shadertoy.com/view/wttSRj
//
// It is comparable in speed to traditional gradient noise (if the
// architecture supports fast sin/cos, like GPUs do anyways), but
// slower than value noise of course. The advantage is that it's
// infinitely derivable. It can also be easily animated by moving
// the waves over time or rotating the gradients, which is fun.
//
// But the main advantage is that it can generate a wromy look, as
// in robobo1221's original shader, by changing the kF constant in
// line 45 to be closer to 6
// Value Noise 2D, Derivatives: https://www.shadertoy.com/view/4dXBRH
// Gradient Noise 2D, Derivatives: https://www.shadertoy.com/view/XdXBRH
// Value Noise 3D, Derivatives: https://www.shadertoy.com/view/XsXfRH
// Gradient Noise 3D, Derivatives: https://www.shadertoy.com/view/4dffRH
// Value Noise 2D : https://www.shadertoy.com/view/lsf3WH
// Value Noise 3D : https://www.shadertoy.com/view/4sfGzS
// Gradient Noise 2D : https://www.shadertoy.com/view/XdXGW8
// Gradient Noise 3D : https://www.shadertoy.com/view/Xsl3Dl
// Simplex Noise 2D : https://www.shadertoy.com/view/Msf3WH
// Wave Noise 2D : https://www.shadertoy.com/view/tldSRj
// You should replace this hash by one that you like and meets
// your needs. This one is here just as example and should not
// be used in production.
vec2 g( vec2 n ) { return sin(n.x*n.y*vec2(12,17)+vec2(1,2)); }
//vec2 g( vec2 n ) { return sin(n.x*n.y+vec2(0,1.571)); } // if you want the gradients to lay on a circle
float noise(vec2 p)
{
const float kF = 2.0; // make 6 to see worms
vec2 i = floor(p);
vec2 f = fract(p);
f = f*f*(3.0-2.0*f);
return mix(mix(sin(kF*dot(p,g(i+vec2(0,0)))),
sin(kF*dot(p,g(i+vec2(1,0)))),f.x),
mix(sin(kF*dot(p,g(i+vec2(0,1)))),
sin(kF*dot(p,g(i+vec2(1,1)))),f.x),f.y);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.xy;
vec2 uv = p*vec2(iResolution.x/iResolution.y,1.0);
float f = 0.0;
// left: noise
if( p.x<0.6 )
{
f = noise( 24.0*uv + iTime*4.0 );
}
// right: fractal noise (4 octaves)
else
{
uv *= 8.0;
uv += iTime*4.0/3.0;
const mat2 m = mat2( 1.6, 1.2, -1.2, 1.6 );
f = 0.5000*noise( uv ); uv = m*uv;
f += 0.2500*noise( uv ); uv = m*uv;
f += 0.1250*noise( uv ); uv = m*uv;
f += 0.0625*noise( uv ); uv = m*uv;
}
f = 0.5 + 0.5*f;
f *= smoothstep( 0.0, 0.005, abs(p.x-0.6) );
fragColor = vec4( f, f, f, 1.0 );
} | mit | [
2510,
2662,
2680,
2680,
2725
] | [
[
2510,
2662,
2680,
2680,
2725
],
[
2833,
2833,
2854,
2854,
3194
],
[
3196,
3196,
3253,
3253,
3857
]
] | // You should replace this hash by one that you like and meets
// your needs. This one is here just as example and should not
// be used in production.
| vec2 g( vec2 n ) { | return sin(n.x*n.y*vec2(12,17)+vec2(1,2)); } | // You should replace this hash by one that you like and meets
// your needs. This one is here just as example and should not
// be used in production.
vec2 g( vec2 n ) { | 2 | 3 |
ttcXWX | iq | 2020-02-16T12:20:31 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Space parametrization of a rounded box. Or could also be called
// "rounded box" coordinates.
// List of all shape parametrizations: https://www.shadertoy.com/playlist/XclfRs
float msign( in float x ) { return (x<0.0)?-1.0:1.0; }
// x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paBox( in vec2 p,
in vec2 b, in float r,
in float s )
{
vec2 q = abs(p)-b;
float l = b.x+b.y + 1.570796*r;
float k1 = min(max(q.x,q.y),0.0) + length(max(q,0.0))-r;
float k2 = ((q.x>0.0)?atan(q.y,q.x):1.570796);
float k3 = 3.0 + 2.0*msign(min(p.x,-p.y)) - msign(p.x);
float k4 = msign(p.x*p.y);
float k5 = r*k2+max(-q.x,0.0);
float ra = s*round(k1/s);
float l2 = l + 1.570796*ra;
return vec4(k1-ra,
k3*l2+k4*(b.y+((q.y>0.0)?k5+k2*ra:q.y)),
4.0*l2,
k1);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// pixel coordinates
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.x;
// animate segment
float id = floor((iTime+0.1)/3.0);
vec2 si = vec2(0.35,0.15) + 0.1*cos( vec2(1,2) + id*vec2(3,1) );
float ra = 0.15 + 0.05*sin(id*0.2);
// distance and parametrization
const float band = 0.1;
vec4 b = paBox( p, si, ra, band );
// base color
float d = b.w;
vec3 col = vec3(1.0,0.68,0.35) + vec3(-0.35,0.15,0.6)*step(d,0.0);
col *= 1.0 - 0.6*exp(-64.0*abs(d));
col *= 1.0-smoothstep(0.47,0.50,abs(fract(d/band)-0.5));
col *= 0.9 + 0.2*smoothstep(0.26,0.24,abs(fract(0.5*d/band+0.25)-0.5));
col += smoothstep(0.004, 0.002, abs(d));
// circles
if( d>-band*0.5 )
{
vec2 q = b.xy;
q.y *= floor(b.z/band)*(band/b.z); // optional - ensure periodicity, but break physicallity
q.y -= iTime*0.1; // animate circles
vec2 uv = fract(q/band+0.5)-0.5; // draw circles
float l = length(uv);
col *= 0.1 + 0.9*smoothstep(0.01,0.04,abs(l-0.35));
col *= 0.1 + 0.9*smoothstep(0.10,0.11,l);
}
fragColor = vec4(col, 1.0);
} | mit | [
1315,
1419,
1505,
1505,
2016
] | [
[
1259,
1259,
1286,
1286,
1313
],
[
1315,
1419,
1505,
1505,
2016
],
[
2018,
2018,
2075,
2104,
3227
]
] | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
| vec4 paBox( in vec2 p,
in vec2 b, in float r,
in float s )
{ |
vec2 q = abs(p)-b;
float l = b.x+b.y + 1.570796*r;
float k1 = min(max(q.x,q.y),0.0) + length(max(q,0.0))-r;
float k2 = ((q.x>0.0)?atan(q.y,q.x):1.570796);
float k3 = 3.0 + 2.0*msign(min(p.x,-p.y)) - msign(p.x);
float k4 = msign(p.x*p.y);
float k5 = r*k2+max(-q.x,0.0);
float ra = s*round(k1/s);
float l2 = l + 1.570796*ra;
return vec4(k1-ra,
k3*l2+k4*(b.y+((q.y>0.0)?k5+k2*ra:q.y)),
4.0*l2,
k1);
} | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paBox( in vec2 p,
in vec2 b, in float r,
in float s )
{ | 1 | 1 |
wlcXD2 | iq | 2020-02-15T06:10:55 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Signed distance and gradient to a box. Probably
// faster than central differences or automatic
// differentiation/dual numbers.
// List of other 2D distances+gradients:
// https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm
//
// Circle: https://www.shadertoy.com/view/WltSDj
// Pie: https://www.shadertoy.com/view/3tGXRc
// Arc: https://www.shadertoy.com/view/WtGXRc
// Isosceles Triangle: https://www.shadertoy.com/view/3dyfDd
// Triangle: https://www.shadertoy.com/view/tlVyWh
// Box: https://www.shadertoy.com/view/wlcXD2
// Quad: https://www.shadertoy.com/view/WtVcD1
// Cross: https://www.shadertoy.com/view/WtdXWj
// Segment: https://www.shadertoy.com/view/WtdSDj
// Hexagon: https://www.shadertoy.com/view/WtySRc
// Vesica: https://www.shadertoy.com/view/3lGXRc
// Smooth-Minimum: https://www.shadertoy.com/view/tdGBDt
// Parallelogram: https://www.shadertoy.com/view/sssGzX
// Other Box functions
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
//
// Intersection: https://www.shadertoy.com/view/ld23DV
// Occlusion: https://www.shadertoy.com/view/4sSXDV
// Occlusion: https://www.shadertoy.com/view/4djXDy
// Density: https://www.shadertoy.com/view/Ml3GR8
// Fake soft shadow: https://www.shadertoy.com/view/WslGz4
// Gradient: https://www.shadertoy.com/view/wlcXD2
// .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgBox( in vec2 p, in vec2 b )
{
vec2 w = abs(p)-b;
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
float g = max(w.x,w.y);
vec2 q = max(w,0.0);
float l = length(q);
return vec3( (g>0.0)?l : g,
s*((g>0.0)?q/l : ((w.x>w.y)?vec2(1,0):vec2(0,1))));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
// corner radious
float ra = 0.1*(0.5+0.5*sin(iTime*1.2));
// sdf(p) and gradient(sdf(p))
vec3 dg = sdgBox(p,vec2(0.8,0.3));
float d = dg.x-ra;
vec2 g = dg.yz;
// central differenes based gradient, for comparison
// g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y);
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85);
col *= 1.0 + vec3(0.5*g,0.0);
//col = vec3(0.5+0.5*g,1.0);
col *= 1.0 - 0.5*exp(-16.0*abs(d));
col *= 0.9 + 0.1*cos(150.0*d);
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) );
fragColor = vec4(col,1.0);
} | mit | [
2583,
2678,
2715,
2715,
2976
] | [
[
2583,
2678,
2715,
2715,
2976
],
[
2979,
2979,
3036,
3036,
3709
]
] | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
| vec3 sdgBox( in vec2 p, in vec2 b )
{ |
vec2 w = abs(p)-b;
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
float g = max(w.x,w.y);
vec2 q = max(w,0.0);
float l = length(q);
return vec3( (g>0.0)?l : g,
s*((g>0.0)?q/l : ((w.x>w.y)?vec2(1,0):vec2(0,1))));
} | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgBox( in vec2 p, in vec2 b )
{ | 4 | 5 |
WldSWX | iq | 2020-02-16T12:20:17 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Space parametrization of a circle.
//
// It shows a global coordinate system were things stretch at the
// sides BUT coodinates are global and physical, and also a local
// coordinate system that is a regular grid full of circles).
// List of all shape parametrizations: https://www.shadertoy.com/playlist/XclfRs
float dot2( in vec2 v ) { return dot(v,v); }
float cro(in vec2 a, in vec2 b) { return a.x*b.y-a.y*b.x; }
// x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paCircle( in vec2 p, float r, float band )
{
float d = length(p) - r;
float ra = band*round(d/band);
float l = (r+ra)*(atan(p.x,p.y)+3.1415927);
return vec4( d-ra, l, 6.283185*(r+ra), d );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// normalized pixel coordinates
vec2 uv = (fragCoord*2.0-iResolution.xy)/iResolution.y;
const float ra = 0.5 ;
const float bandSize = 0.15;
// distance
vec4 b = paCircle(uv,ra,bandSize);
// base color
float d = b.w;
vec3 col = vec3(1.0,0.68,0.35) + vec3(-0.35,0.15,0.6)*step(d,0.0);
col *= 1.0 - 0.6*exp(-64.0*abs(d));
col *= 1.0-smoothstep(0.47,0.50,abs(fract(d/bandSize)-0.5));
col *= 0.9 + 0.2*smoothstep(0.26,0.24,abs(fract(0.5*d/bandSize+0.25)-0.5));
col += smoothstep(0.004, 0.002, abs(d));
if( d>-bandSize*0.5 )
{
vec2 q = b.xy;
// optional - ensure periodicity, but break physicallity
q.y *= floor(b.z/bandSize)*(bandSize/b.z);
// animate circles
q.y -= iTime*0.1;
// draw circles
vec2 uv = fract(q/bandSize+0.5)-0.5;
float l = length(uv);
col *= 0.1 + 0.9*smoothstep(0.01,0.04,abs(l-0.35));
col *= 0.1 + 0.9*smoothstep(0.10,0.11,l);
}
fragColor = vec4(col, 1.0);
} | mit | [
1504,
1608,
1658,
1658,
1820
] | [
[
1398,
1398,
1423,
1423,
1442
],
[
1443,
1443,
1476,
1476,
1502
],
[
1504,
1608,
1658,
1658,
1820
],
[
1822,
1822,
1879,
1915,
2896
]
] | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
| vec4 paCircle( in vec2 p, float r, float band )
{ |
float d = length(p) - r;
float ra = band*round(d/band);
float l = (r+ra)*(atan(p.x,p.y)+3.1415927);
return vec4( d-ra, l, 6.283185*(r+ra), d );
} | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paCircle( in vec2 p, float r, float band )
{ | 1 | 1 |
wtcSDf | iq | 2020-02-16T12:20:37 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Space parametrization of a polygon. It only works for convex
// polygons, has some problems, and I still need to optimize it.
//
// It shows a global coordinate system were things stretch at the
// sides BUT coodinates are global and physical, and also a local
// coordinate system that is a regular grid full of circles).
// List of all shape parametrizations: https://www.shadertoy.com/playlist/XclfRs
float dot2( in vec2 v ) { return dot(v,v); }
float cro(in vec2 a, in vec2 b) { return a.x*b.y-a.y*b.x; }
// https://www.shadertoy.com/view/wdBXRW
float sdPoly( in vec2 p, vec2 verts[5], in float r )
{
const int num = verts.length();
float s = 1.0;
float d = length(p-verts[0]);
for( int i=0; i<num; i++ )
{
vec2 a = verts[i];
vec2 b = verts[(i+1)%num];
vec2 pa = p-a;
vec2 ba = b-a;
float h = clamp(dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
d = min(d,length( pa - ba*h ));
bvec3 cond = bvec3( p.y>=a.y, p.y<b.y, ba.x*pa.y>ba.y*pa.x );
if( all(cond) || all(not(cond)) ) s*=-1.0;
}
return s*d-r;
}
float angle( in vec2 a, in vec2 b )
{
float n = atan(dot(a,b),cro(a,b));
if( n<0.0 )n+=6.283185;
return n;
}
// x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paPoly( in vec2 p, vec2 verts[5], float r, float band )
{
const int num = verts.length();
float od = sdPoly( p, verts, r );
float ra = band*round(od/band);
float d = length(p-verts[0])-ra;
float l = 0.0;
float t = 0.0;
for( int i=0; i<num; i++ )
{
vec2 a = verts[ i ];
vec2 b = verts[(i+1)%num];
vec2 c = verts[(i+2)%num];
vec2 pa1 = p-a; vec2 ba1 = b-a;
vec2 pa2 = p-b; vec2 ba2 = c-b;
float h1 = dot(pa1,ba1)/dot(ba1,ba1);
float h2 = dot(pa2,ba2)/dot(ba2,ba2);
float tmp = length( pa1 - ba1*clamp(h1,0.0,1.0) ) - (r+ra);
float lba = length(ba1);
if( tmp<d || ((i==num-1) && cro(ba1,pa1)>0.0) )
{
d = min(d,tmp);
if( h1>=0.0 && h1<=1.0 )
{
l = t + h1*lba;
}
else if( h1>1.0 && h2<0.0)
{
l = t+lba;
l += (r+ra)*angle(ba1,pa2);
}
}
t += lba+(r+ra)*angle(ba1,vec2(-ba2.y,ba2.x));
}
return vec4(d,l,t,od);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// normalized pixel coordinates
vec2 uv = (fragCoord*2.0-iResolution.xy)/iResolution.y;
// animate polygon
float id = floor((iTime+0.1)/3.0);
vec2 kVerts[5];
for( int i=0; i<5; i++ )
{
kVerts[i] = 0.75*cos( float(i)*vec2(1.1,1.3) + vec2(0,2) + id*11.0 );
}
const float bandSize = 0.15;
// distance
vec4 b = paPoly(uv,kVerts,0.15,bandSize);
// base color
float d = b.w;
vec3 col = vec3(1.0,0.68,0.35) + vec3(-0.35,0.15,0.6)*step(d,0.0);
col *= 1.0 - 0.6*exp(-64.0*abs(d));
col *= 1.0-smoothstep(0.47,0.50,abs(fract(d/bandSize)-0.5));
col *= 0.9 + 0.2*smoothstep(0.26,0.24,abs(fract(0.5*d/bandSize+0.25)-0.5));
col += 1.0-smoothstep(0.0, 0.005, abs(d)-0.003);
if( d>-bandSize*0.5 )
{
vec2 q = b.xy;
// optional - ensure periodicity, but break physicallity
q.y *= floor(b.z/bandSize)*(bandSize/b.z);
// animate circles
q.y -= iTime*0.1;
// draw circles
vec2 uv = fract(q/bandSize+0.5)-0.5;
float l = length(uv);
col *= 0.1 + 0.9*smoothstep(0.0,0.02,abs(l-0.35)-0.03);
col *= 0.1 + 0.9*smoothstep(0.0,0.02,l-0.10);
}
fragColor = vec4(col, 1.0);
} | mit | [
1595,
1636,
1691,
1691,
2194
] | [
[
1489,
1489,
1514,
1514,
1533
],
[
1534,
1534,
1567,
1567,
1593
],
[
1595,
1636,
1691,
1691,
2194
],
[
2196,
2196,
2233,
2233,
2316
],
[
2322,
2426,
2489,
2489,
3567
],
[
3574,
3574,
3631,
3667,
4861
]
] | // https://www.shadertoy.com/view/wdBXRW
| float sdPoly( in vec2 p, vec2 verts[5], in float r )
{ |
const int num = verts.length();
float s = 1.0;
float d = length(p-verts[0]);
for( int i=0; i<num; i++ )
{
vec2 a = verts[i];
vec2 b = verts[(i+1)%num];
vec2 pa = p-a;
vec2 ba = b-a;
float h = clamp(dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
d = min(d,length( pa - ba*h ));
bvec3 cond = bvec3( p.y>=a.y, p.y<b.y, ba.x*pa.y>ba.y*pa.x );
if( all(cond) || all(not(cond)) ) s*=-1.0;
}
return s*d-r;
} | // https://www.shadertoy.com/view/wdBXRW
float sdPoly( in vec2 p, vec2 verts[5], in float r )
{ | 1 | 1 |
wtcSDf | iq | 2020-02-16T12:20:37 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Space parametrization of a polygon. It only works for convex
// polygons, has some problems, and I still need to optimize it.
//
// It shows a global coordinate system were things stretch at the
// sides BUT coodinates are global and physical, and also a local
// coordinate system that is a regular grid full of circles).
// List of all shape parametrizations: https://www.shadertoy.com/playlist/XclfRs
float dot2( in vec2 v ) { return dot(v,v); }
float cro(in vec2 a, in vec2 b) { return a.x*b.y-a.y*b.x; }
// https://www.shadertoy.com/view/wdBXRW
float sdPoly( in vec2 p, vec2 verts[5], in float r )
{
const int num = verts.length();
float s = 1.0;
float d = length(p-verts[0]);
for( int i=0; i<num; i++ )
{
vec2 a = verts[i];
vec2 b = verts[(i+1)%num];
vec2 pa = p-a;
vec2 ba = b-a;
float h = clamp(dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
d = min(d,length( pa - ba*h ));
bvec3 cond = bvec3( p.y>=a.y, p.y<b.y, ba.x*pa.y>ba.y*pa.x );
if( all(cond) || all(not(cond)) ) s*=-1.0;
}
return s*d-r;
}
float angle( in vec2 a, in vec2 b )
{
float n = atan(dot(a,b),cro(a,b));
if( n<0.0 )n+=6.283185;
return n;
}
// x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paPoly( in vec2 p, vec2 verts[5], float r, float band )
{
const int num = verts.length();
float od = sdPoly( p, verts, r );
float ra = band*round(od/band);
float d = length(p-verts[0])-ra;
float l = 0.0;
float t = 0.0;
for( int i=0; i<num; i++ )
{
vec2 a = verts[ i ];
vec2 b = verts[(i+1)%num];
vec2 c = verts[(i+2)%num];
vec2 pa1 = p-a; vec2 ba1 = b-a;
vec2 pa2 = p-b; vec2 ba2 = c-b;
float h1 = dot(pa1,ba1)/dot(ba1,ba1);
float h2 = dot(pa2,ba2)/dot(ba2,ba2);
float tmp = length( pa1 - ba1*clamp(h1,0.0,1.0) ) - (r+ra);
float lba = length(ba1);
if( tmp<d || ((i==num-1) && cro(ba1,pa1)>0.0) )
{
d = min(d,tmp);
if( h1>=0.0 && h1<=1.0 )
{
l = t + h1*lba;
}
else if( h1>1.0 && h2<0.0)
{
l = t+lba;
l += (r+ra)*angle(ba1,pa2);
}
}
t += lba+(r+ra)*angle(ba1,vec2(-ba2.y,ba2.x));
}
return vec4(d,l,t,od);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// normalized pixel coordinates
vec2 uv = (fragCoord*2.0-iResolution.xy)/iResolution.y;
// animate polygon
float id = floor((iTime+0.1)/3.0);
vec2 kVerts[5];
for( int i=0; i<5; i++ )
{
kVerts[i] = 0.75*cos( float(i)*vec2(1.1,1.3) + vec2(0,2) + id*11.0 );
}
const float bandSize = 0.15;
// distance
vec4 b = paPoly(uv,kVerts,0.15,bandSize);
// base color
float d = b.w;
vec3 col = vec3(1.0,0.68,0.35) + vec3(-0.35,0.15,0.6)*step(d,0.0);
col *= 1.0 - 0.6*exp(-64.0*abs(d));
col *= 1.0-smoothstep(0.47,0.50,abs(fract(d/bandSize)-0.5));
col *= 0.9 + 0.2*smoothstep(0.26,0.24,abs(fract(0.5*d/bandSize+0.25)-0.5));
col += 1.0-smoothstep(0.0, 0.005, abs(d)-0.003);
if( d>-bandSize*0.5 )
{
vec2 q = b.xy;
// optional - ensure periodicity, but break physicallity
q.y *= floor(b.z/bandSize)*(bandSize/b.z);
// animate circles
q.y -= iTime*0.1;
// draw circles
vec2 uv = fract(q/bandSize+0.5)-0.5;
float l = length(uv);
col *= 0.1 + 0.9*smoothstep(0.0,0.02,abs(l-0.35)-0.03);
col *= 0.1 + 0.9*smoothstep(0.0,0.02,l-0.10);
}
fragColor = vec4(col, 1.0);
} | mit | [
2322,
2426,
2489,
2489,
3567
] | [
[
1489,
1489,
1514,
1514,
1533
],
[
1534,
1534,
1567,
1567,
1593
],
[
1595,
1636,
1691,
1691,
2194
],
[
2196,
2196,
2233,
2233,
2316
],
[
2322,
2426,
2489,
2489,
3567
],
[
3574,
3574,
3631,
3667,
4861
]
] | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
| vec4 paPoly( in vec2 p, vec2 verts[5], float r, float band )
{ |
const int num = verts.length();
float od = sdPoly( p, verts, r );
float ra = band*round(od/band);
float d = length(p-verts[0])-ra;
float l = 0.0;
float t = 0.0;
for( int i=0; i<num; i++ )
{
vec2 a = verts[ i ];
vec2 b = verts[(i+1)%num];
vec2 c = verts[(i+2)%num];
vec2 pa1 = p-a; vec2 ba1 = b-a;
vec2 pa2 = p-b; vec2 ba2 = c-b;
float h1 = dot(pa1,ba1)/dot(ba1,ba1);
float h2 = dot(pa2,ba2)/dot(ba2,ba2);
float tmp = length( pa1 - ba1*clamp(h1,0.0,1.0) ) - (r+ra);
float lba = length(ba1);
if( tmp<d || ((i==num-1) && cro(ba1,pa1)>0.0) )
{
d = min(d,tmp);
if( h1>=0.0 && h1<=1.0 )
{
l = t + h1*lba;
}
else if( h1>1.0 && h2<0.0)
{
l = t+lba;
l += (r+ra)*angle(ba1,pa2);
}
}
t += lba+(r+ra)*angle(ba1,vec2(-ba2.y,ba2.x));
}
return vec4(d,l,t,od);
} | // x = local dist
// y = local perimeter dist
// z = total local perimeter
// w = global distance (sdf)
vec4 paPoly( in vec2 p, vec2 verts[5], float r, float band )
{ | 1 | 1 |
WtdSDj | iq | 2020-02-15T06:10:52 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Signed distance and gradient to a segment. Probably faster than
// central differences or automatic differentiation/dual numbers.
// List of other 2D distances+gradients:
//
// https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm
//
// and
//
// https://www.shadertoy.com/playlist/M3dSRf
// .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
float d = length(q);
return vec3(d,q/d);
}
#define AA 2
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
vec2 v1 = 0.8*cos( 0.5*iTime*vec2(1.3,1.0) + vec2(2,4) );
vec2 v2 = 0.8*cos( 0.5*iTime*vec2(0.9,1.2) + vec2(1,5) );
float th = 0.15*(0.5+0.5*sin(iTime*1.2+2.0));
// sdf(p) and gradient(sdf(p))
vec3 dg = sdgSegment(p,v1,v2);
float d = dg.x - th;
vec2 g = dg.yz;
// central differenes based gradient, for comparison
//g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y);
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85);
col *= 1.0 + vec3(0.5*g,0.0);
//col = vec3(0.5+0.5*g,1.0);
col *= 1.0 - 0.5*exp(-16.0*abs(d));
col *= 0.9 + 0.1*cos(150.0*d);
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
1399,
1494,
1546,
1546,
1719
] | [
[
1399,
1494,
1546,
1546,
1719
]
] | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
| vec3 sdgSegment( in vec2 p, in vec2 a, in vec2 b )
{ |
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
float d = length(q);
return vec3(d,q/d);
} | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgSegment( in vec2 p, in vec2 a, in vec2 b )
{ | 1 | 3 |
WtySRc | iq | 2020-03-03T05:43:03 | // The MIT License
// Copyright © 2020 Inigo Quilez
// 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.
// Signed distance and gradient to a hexagon. Probably
// faster than central differences or automatic
// differentiation/dual numbers.
// List of other 2D distances+gradients:
//
// https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm
//
// and
//
// https://www.shadertoy.com/playlist/M3dSRf
// .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgHexagon( in vec2 p, in float r )
{
const vec3 k = vec3(-0.866025404,0.5,0.577350269);
vec2 s = sign(p);
p = abs(p);
float w = dot(k.xy,p);
p -= 2.0*min(w,0.0)*k.xy;
p -= vec2(clamp(p.x, -k.z*r, k.z*r), r);
float d = length(p)*sign(p.y);
vec2 g = (w<0.0) ? mat2(-k.y,-k.x,-k.x,k.y)*p : p;
return vec3( d, s*g/d );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
// size
float si = 0.3 + 0.2*cos( iTime );
// corner radious
float ra = 0.3*(0.5+0.5*sin(iTime*2.0));
// sdf(p) and gradient(sdf(p))
vec3 dg = sdgHexagon(p,si);
float d = dg.x-ra;
vec2 g = dg.yz;
// central differenes based gradient, for comparison
//g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y);
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85);
col *= 1.0 + vec3(0.5*g,0.0);
//col = vec3(0.5+0.5*g,1.0);
col *= 1.0 - 0.5*exp(-16.0*abs(d));
col *= 0.9 + 0.1*cos(150.0*d);
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) );
fragColor = vec4(col,1.0);
} | mit | [
1402,
1497,
1540,
1540,
1858
] | [
[
1402,
1497,
1540,
1540,
1858
],
[
1860,
1860,
1917,
1917,
2627
]
] | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
| vec3 sdgHexagon( in vec2 p, in float r )
{ |
const vec3 k = vec3(-0.866025404,0.5,0.577350269);
vec2 s = sign(p);
p = abs(p);
float w = dot(k.xy,p);
p -= 2.0*min(w,0.0)*k.xy;
p -= vec2(clamp(p.x, -k.z*r, k.z*r), r);
float d = length(p)*sign(p.y);
vec2 g = (w<0.0) ? mat2(-k.y,-k.x,-k.x,k.y)*p : p;
return vec3( d, s*g/d );
} | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgHexagon( in vec2 p, in float r )
{ | 2 | 2 |
td2cDd | blackle | 2020-04-22T13:51:28 | //CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/
//To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work.
float smin(float a, float b, float k) {
float h = max(0., k-abs(a-b))/k;
return min(a,b)-h*h*h*k/6.;
}
//smooth triangle wave for smooth domain repetition https://www.desmos.com/calculator/ototv6tja8
vec4 stri(vec4 p, float k) {
return asin(sin(p*3.14)*k)/3.14+0.5;
}
float scene(vec4 p) {
vec4 q = abs(p) - 1.;
float cube = length(max(q,0.0)) + min(max(max(q.x,q.w),max(q.y,q.z)),0.0) - 0.1;
float scale = 1.;
vec4 p2 = p+iTime*0.2;
p2 = (stri(p2/scale, .9)-0.5)*scale;
float spheres = length(p2)-0.2;
spheres = -smin(-(length(p) - 2.), -spheres, 0.1);
return smin(cube, spheres, 0.5);
}
vec4 norm(vec4 p) {
mat4 k = mat4(p,p,p,p) - mat4(0.001);
return normalize(scene(p) - vec4( scene(k[0]),scene(k[1]),scene(k[2]),scene(k[3]) ) );
}
vec3 erot(vec3 p, vec3 ax, float ro) {
return mix(dot(ax,p)*ax,p,cos(ro)) + sin(ro)*cross(ax,p);
}
vec3 srgb(float r, float g, float b) {
return pow(vec3(r,g,b),vec3(2.));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord.xy - 0.5*iResolution.xy)/iResolution.y;
vec2 mouse = (iMouse.xy - 0.5*iResolution.xy)/iResolution.y;
vec4 cam = normalize(vec4(1,uv,0));
vec4 init = vec4(-5,0,0,sin(iTime*0.1));
float wrot = cos(iTime*0.5);
float zrot = cos(iTime*0.25);
float yrot = sin(iTime*0.5);
float zrot2 = iTime;
if (iMouse.z > 0.) {
zrot = mouse.x*2.;
wrot = radians(45.0);
yrot = mouse.y*2.;
zrot2 = 0.;
}
cam.xyw = erot(cam.xyw, vec3(0,1,0), zrot);
init.xyw = erot(init.xyw, vec3(0,1,0), zrot);
cam.xyz = erot(cam.xyz, vec3(0,1,0), yrot);
init.xyz = erot(init.xyz, vec3(0,1,0), yrot);
cam.yzw = erot(cam.yzw, vec3(0,1,0), wrot);
init.yzw = erot(init.yzw, vec3(0,1,0), wrot);
cam.xyz = erot(cam.xyz, vec3(0,0,1), zrot2);
init.xyz = erot(init.xyz, vec3(0,0,1), zrot2);
vec4 p = init;
bool hit = false;
for (int i = 0; i<200 && !hit;i++) {
float dist = scene(p);
hit = dist*dist < 1e-6;
p+=dist*cam;
}
vec4 n = norm(p);
vec4 r = reflect(cam,n);
vec4 aon = reflect(cam, norm(p+r*0.3));
float factor = length(sin(aon*3.)*0.5+0.5)/2.;
vec3 color = mix(srgb(0.1,0.1,0.2), srgb(0.2,0.6,0.9), factor) + pow(factor, 10.);
fragColor.xyz = hit ? color : srgb(0.1,0.1,0.1);
fragColor.xyz = sqrt(fragColor.xyz);
} | cc0-1.0 | [
302,
399,
427,
427,
468
] | [
[
194,
194,
233,
233,
300
],
[
302,
399,
427,
427,
468
],
[
470,
470,
491,
491,
806
],
[
808,
808,
827,
827,
958
],
[
960,
960,
998,
998,
1060
],
[
1062,
1062,
1100,
1100,
1138
],
[
1140,
1140,
1197,
1197,
2486
]
] | //smooth triangle wave for smooth domain repetition https://www.desmos.com/calculator/ototv6tja8
| vec4 stri(vec4 p, float k) { |
return asin(sin(p*3.14)*k)/3.14+0.5;
} | //smooth triangle wave for smooth domain repetition https://www.desmos.com/calculator/ototv6tja8
vec4 stri(vec4 p, float k) { | 1 | 1 |
Ws2cRh | mla | 2020-04-05T15:23:23 | ////////////////////////////////////////////////////////////////////////////////
//
// Projective Conic Envelope
//
// Copyright (c) Matthew Arcus, 2020
// MIT License: https://opensource.org/licenses/MIT
//
// More projective magic - generate an ellipse as an envelope of lines.
// In fact, we start with the ellipse and find a network of lines
// enveloping it: at each point p, find tangents to the ellipse,
// intersect these with a fixed axis at x = -3 - this will be the
// source of the rays. From axis intersection point, map to a radial
// parameter, then find closest displayed ray, map that back to the
// axis and find the tangents from there & these are the lines that
// actually get displayed.
//
// Mouse changes size and orientation of ellipse.
//
////////////////////////////////////////////////////////////////////////////////
const float PI = 3.141592654;
vec3 join(vec3 p, vec3 q) {
// Return either intersection of lines p and q
// or line through points p and q, r = kp + jq
return cross(p,q);
}
float line(vec3 p, vec3 q) {
return abs(dot(p,q)/(p.z*length(q.xy)));
}
// Set tan1 and tan2 to the two tangents to conic X from point p.
// Return false if no tangents (eg. inside an ellipse).
bool tangents(vec3 p, mat3 X, out vec3 tan1, out vec3 tan2) {
vec3 polar = X*p; // Line between tangents
float a = polar.x, b = polar.y, c = polar.z;
// Two points on the polar line. Q is the nearest point to origin,
// R is at infinity, ie. is direction vector.
vec3 Q = vec3(a,b,-(a*a+b*b)/c);
vec3 R = vec3(-b,a,0);
// Find intersection of QR with conic, ie. dot(Q+kR,X*(Q+kR)) = 0
float A = dot(R,X*R), B = dot(Q,X*R), C = dot(Q,X*Q);
float D = B*B-A*C;
if (D < 0.0) return false;
D = sqrt(D);
float k1,k2;
if (B > 0.0) {
k1 = (-B-D)/A; k2 = C/(A*k1);
} else {
k2 = (-B+D)/A; k1 = C/(A*k2);
}
tan1 = join(p,Q+k1*R); tan2 = join(p,Q+k2*R);
return true;
}
vec3 hsv2rgb( in vec3 c ) {
vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb); // cubic smoothing
return c.z * mix( vec3(1.0), rgb, c.y);
}
void mainImage(out vec4 fragColor, vec2 fragCoord ) {
float scale = 3.0;
float t = 0.2*iTime+PI/6.0;
float A = 0.25, B = 1.0;
if (iMouse.x > 0.0) {
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
t += PI*(m.y);
B = exp(-m.x);
}
float cost = cos(t), sint = sin(t);
// Conic matrix - conic is points p with pXp = 0
mat3 X = mat3(A,0,0,0,B,0,0,0,-1);
// Apply tranformation to conic matrix.
mat3 P = mat3(cost,sint,0, -sint,cost,0, 0,0,1);
X = transpose(P)*X*P;
vec3 p = vec3(scale*(2.0*fragCoord - iResolution.xy)/iResolution.y,1);
vec3 col = vec3(0);
float lwidth0 = 0.0;
float lwidth1 = max(0.02,fwidth(p.x));
vec3 tan1,tan2,tan11,tan12,tan21,tan22;
if (tangents(p,X,tan1,tan2)) {
float N = 64.0;
vec3 axis = vec3(1,0,3);
float index1,index2;
vec3 p1 = join(tan1,axis);
float t1 = atan(p1.y/p1.z);
t1 += 0.1*iTime;
t1 *= N/PI; t1 = round(t1); index1 = t1; t1 *= PI/N;
t1 -= 0.1*iTime;
p1 = vec3(-3,tan(t1),1);
p1.y = sign(p1.y)*min(abs(p1.y),1e4); // Fix up silly values
if (tangents(p1,X,tan11,tan12)) {
vec3 c1 = hsv2rgb(vec3(index1/N,1,1));
float d = line(p,tan11);
col = mix(c1,col,smoothstep(lwidth0,lwidth1,d));
float tt = dot(normalize(tan2.xy),normalize(tan12.xy));
// Try not to get the wrong tangent here. There must be a better
// way of doing this.
if (abs(tt) < 0.99) {
d = line(p,tan12);
col = mix(c1,col,smoothstep(lwidth0,lwidth1,d));
}
}
vec3 p2 = join(tan2,axis);
float t2 = atan(p2.y/p2.z);
t2 += 0.1*iTime;
t2 *= N/PI; t2 = round(t2); index2 = t2; t2 *= PI/N;
t2 -= 0.1*iTime;
p2 = vec3(-3,tan(t2),1);
p2.y = sign(p2.y)*min(abs(p2.y),1e4); // Fix up silly values
if (tangents(p2,X,tan21,tan22)) {
vec3 c2 = hsv2rgb(vec3(index2/N,1,1));
float d = line(p,tan22);
col = mix(c2,col,smoothstep(lwidth0,lwidth1,d));
float tt = dot(normalize(tan1.xy),normalize(tan21.xy));
// Try not to get the wrong tangent here.
if (abs(tt) < 0.99) {
d = line(p,tan21);
col = mix(c2,col,smoothstep(lwidth0,lwidth1,d));
}
}
}
col = pow(col,vec3(0.4545));
fragColor = vec4(col,1);
} | mit | [
1104,
1226,
1287,
1287,
1926
] | [
[
879,
879,
906,
1004,
1027
],
[
1029,
1029,
1057,
1057,
1102
],
[
1104,
1226,
1287,
1287,
1926
],
[
1928,
1928,
1955,
1955,
2131
],
[
2133,
2133,
2186,
2186,
4385
]
] | // Set tan1 and tan2 to the two tangents to conic X from point p.
// Return false if no tangents (eg. inside an ellipse).
| bool tangents(vec3 p, mat3 X, out vec3 tan1, out vec3 tan2) { |
vec3 polar = X*p; // Line between tangents
float a = polar.x, b = polar.y, c = polar.z;
// Two points on the polar line. Q is the nearest point to origin,
// R is at infinity, ie. is direction vector.
vec3 Q = vec3(a,b,-(a*a+b*b)/c);
vec3 R = vec3(-b,a,0);
// Find intersection of QR with conic, ie. dot(Q+kR,X*(Q+kR)) = 0
float A = dot(R,X*R), B = dot(Q,X*R), C = dot(Q,X*Q);
float D = B*B-A*C;
if (D < 0.0) return false;
D = sqrt(D);
float k1,k2;
if (B > 0.0) {
k1 = (-B-D)/A; k2 = C/(A*k1);
} else {
k2 = (-B+D)/A; k1 = C/(A*k2);
}
tan1 = join(p,Q+k1*R); tan2 = join(p,Q+k2*R);
return true;
} | // Set tan1 and tan2 to the two tangents to conic X from point p.
// Return false if no tangents (eg. inside an ellipse).
bool tangents(vec3 p, mat3 X, out vec3 tan1, out vec3 tan2) { | 1 | 1 |
wsByDh | andretugan | 2020-04-10T14:41:54 | // Author: https://www.shadertoy.com/user/andretugan
// Creative Commons Attribution-NonCommercial 3.0 Unported License
// https://creativecommons.org/licenses/by-nc/3.0
#define NUM_CHORDS 128
#define M_PI 3.14159265358
#define M_2_PI (2. * 3.14159265358)
#define FADE_INNER_RADIUS 0.8
#define FADE_OUTER_RADIUS 1.1
#define LINE_WIDTH 4.
// Function from Inigo Quilez
// https://www.shadertoy.com/view/MsS3Wc
vec3 hsv2rgb( in vec3 c )
{
vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb); // cubic smoothing
return c.z * mix( vec3(1.0), rgb, c.y);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (2.*fragCoord - iResolution.xy)/iResolution.y;
float uv2 = dot(uv, uv);
if (uv2 > FADE_OUTER_RADIUS * FADE_OUTER_RADIUS) {
fragColor = vec4(0.0,0.0,0.0,1.0);
return;
}
float multiplier = 1.99 + 5. * (.5 - .5 * cos(iTime * 0.03));
float start = multiplier * M_PI * 0.25;
float val = 0.;
for (int i = 0; i < NUM_CHORDS; ++i) {
float angle1 = start + M_2_PI / float(NUM_CHORDS) * float(i);
float angle2 = multiplier * angle1;
vec2 point1 = vec2(cos(angle1), sin(angle1));
vec2 point2 = vec2(cos(angle2), sin(angle2));
vec2 diff = point2 - point1;
vec2 perp = point1 + (dot(uv - point1, diff) / dot(diff, diff)) * diff - uv;
float dist2 = dot(perp, perp);
float line_width = LINE_WIDTH / iResolution.y;
float add_val = smoothstep(line_width * line_width, line_width * line_width * 0.5, dist2);
add_val *= smoothstep(FADE_OUTER_RADIUS * FADE_OUTER_RADIUS, FADE_INNER_RADIUS * FADE_INNER_RADIUS, uv2);
val += add_val;
}
val *= 20. / float(NUM_CHORDS);
val = pow(val, 0.5);
float hue = sin(iTime * 0.05 + uv2 * 0.3);
fragColor = vec4(hsv2rgb(vec3(hue, 1., val)),1.0);
} | cc-by-3.0 | [
342,
413,
440,
440,
618
] | [
[
342,
413,
440,
440,
618
],
[
620,
620,
676,
676,
2123
]
] | // Function from Inigo Quilez
// https://www.shadertoy.com/view/MsS3Wc
| vec3 hsv2rgb( in vec3 c )
{ |
vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb); // cubic smoothing
return c.z * mix( vec3(1.0), rgb, c.y);
} | // Function from Inigo Quilez
// https://www.shadertoy.com/view/MsS3Wc
vec3 hsv2rgb( in vec3 c )
{ | 17 | 54 |
td2fRD | blackle | 2020-05-16T19:52:37 | //CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/
//To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work.
//this shader is an experiment with mapping a 4 dimensional SDF onto a 3 dimensional one, by
//using the components of 3d space plus the distance to a given SDF. this can be used to
//map domain-repeated spheres onto the surface of an SDF, and use those spheres to cut holes
//into it, much like pitted metal. Becase these spheres are restrained to the surface of the
//SDF, we can control exactly what part of the SDF gets pitted. This shader cycles through 3
//possibilities. pitting everything, only pitting inside existing pits, or only pitting
//outside existing pits. There is no visible domain repetition pattern because I am also using
//a modified version of domain repetition where some of the domains are "disabled" such that
//they report the distance as if the neighbour domains are filled, but it is empty. this means
//you can make an arbitrary percentage of domains empty, and therefore no holes will appear
//see https://www.shadertoy.com/view/WsBBRw for the 2d case
//return the SDF for a sphere, or the SDF for an empty region surrounded by spheres
float gated_domain(vec4 p, float scale, bool gated) {
if (!gated) {
p.yzw = abs(p.yzw);
if (p.y > p.z) p.yz = p.zy;
if (p.z > p.w) p.zw = p.wz;
if (p.y > p.z) p.yz = p.zy;
p.w -= scale;
}
return length(p)-scale/2.2;
}
#define FK(k) floatBitsToInt(k)^floatBitsToInt(cos(k))
float hash(float a, float b) {
int x = FK(a); int y = FK(b);
return float((x*x-y)*(y*y+x)+x)/2.14e9;
}
vec3 erot(vec3 p, vec3 ax, float ro) {
return mix(dot(ax,p)*ax,p,cos(ro))+sin(ro)*cross(ax,p);
}
float smin(float a, float b, float k) {
float h = max(0., k-abs(a-b))/k;
return min(a,b)-h*h*h*k/6.;
}
int pittingtype;
float scene(vec3 p) {
float sphere = length(p)-1.;
float cut = p.z;
float top = sphere;
float last = sphere;
for (int i = 0; i < 5; i++) { //5 octaves of noise
//random rotations
p = erot(p, normalize(vec3(1,2,3)), .2);
p = erot(p, normalize(vec3(1,3,2)), .51);
float scale = .5/pow(float(i+1),1.5);
//create 4d coordinates where the first coordinate is the distance to the SDF
vec4 p4d = vec4(last,p);
//domain repetition *only* along the yzw axes
vec3 id = floor(p4d.yzw/scale);
p4d.yzw = (fract(p4d.yzw/scale)-0.5)*scale;
//disable 50% of spheres. see https://www.shadertoy.com/view/WsSBRD for another example of this technique
bool gated = hash(id.x, hash(id.y, id.z)) > 0.;
float holes = gated_domain(p4d, scale, gated);
top = -smin(-top, holes, 0.04*sqrt(scale));
if (pittingtype == 0) last = holes; //add pitting to existing pits
if (pittingtype == 1) last = top; //add pitting everywhere
if (pittingtype == 2) last = sphere; //add pitting only to original surface
}
return max(top,-cut);
}
vec3 norm(vec3 p) {
mat3 k = mat3(p,p,p)-mat3(0.001);
return normalize(scene(p)-vec3(scene(k[0]),scene(k[1]),scene(k[2])));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord-.5*iResolution.xy)/iResolution.y;
vec2 mouse = (iMouse.xy-0.5*iResolution.xy)/iResolution.y;
pittingtype = int(iTime)%3;
vec3 cam = normalize(vec3(2,uv));
vec3 init = vec3(-5,0,0);
float yrot = 0.5;
float zrot = 4.5;
if (iMouse.z > 0.) {
yrot += -4.*mouse.y;
zrot += 4.*mouse.x;
} else {
yrot += cos(iTime)*.2;
zrot += sin(iTime)*.2;
}
cam = erot(cam, vec3(0,1,0), yrot);
init = erot(init, vec3(0,1,0), yrot);
cam = erot(cam, vec3(0,0,1), zrot);
init = erot(init, vec3(0,0,1), zrot);
vec3 p = init;
bool hit = false;
//raymarch
for (int i = 0; i < 100 && !hit; i++) {
float dist = scene(p);
hit = dist*dist < 1e-6;
p+=cam*dist*.9;
if (distance(p,init) > 10.) break;
}
//shading
vec3 n = norm(p);
vec3 r = reflect(cam, n);
float ao = smoothstep(-.1,.1,scene(p+n*.1));
ao *= smoothstep(-.2,.2,scene(p+n*.2));
ao*=ao;
float diff = length(sin(n*2.)*.5+.5)/sqrt(3.);
float spec = length(sin(r*2.)*.5+.5)/sqrt(3.);
float fresnel = 1.-abs(dot(cam,n))*.98;
vec3 col = abs(erot(vec3(0.1,0.04,0.03),r,0.05))*diff*diff*ao + pow(spec, 10.)*fresnel*ao;
fragColor.xyz = sqrt((hit ? col : vec3(0.))*2.) + abs(hash(iTime, hash(uv.x,uv.y)))*.04;
} | cc0-1.0 | [
1180,
1264,
1317,
1317,
1533
] | [
[
1180,
1264,
1317,
1317,
1533
],
[
1590,
1590,
1620,
1620,
1700
],
[
1702,
1702,
1740,
1740,
1802
],
[
1804,
1804,
1843,
1843,
1914
],
[
1933,
1933,
1954,
1954,
3086
],
[
3088,
3088,
3107,
3107,
3221
],
[
3223,
3223,
3280,
3280,
4629
]
] | //return the SDF for a sphere, or the SDF for an empty region surrounded by spheres
| float gated_domain(vec4 p, float scale, bool gated) { |
if (!gated) {
p.yzw = abs(p.yzw);
if (p.y > p.z) p.yz = p.zy;
if (p.z > p.w) p.zw = p.wz;
if (p.y > p.z) p.yz = p.zy;
p.w -= scale;
}
return length(p)-scale/2.2;
} | //return the SDF for a sphere, or the SDF for an empty region surrounded by spheres
float gated_domain(vec4 p, float scale, bool gated) { | 1 | 1 |