Skip to content

Instantly share code, notes, and snippets.

@0b5vr
Last active February 11, 2021 17:57
Show Gist options
  • Save 0b5vr/2fbab871c1d05a4b2ce6858658d30a96 to your computer and use it in GitHub Desktop.
Save 0b5vr/2fbab871c1d05a4b2ce6858658d30a96 to your computer and use it in GitHub Desktop.
help (shader royale protips + training + final outcome)

haha

Basics

  • Use shorter syntax / shorter variable name as possible
    • You still might want to leave comments for visual cue
  • One pass! No additional passes!
    • Most emissive / specular oriented scenes will suffer since there is no bloom available
  • You should ask the organizer about their compo machine
    • You might want to consider the factor in order to achieve constant 60fps?
    • This time the machine uses a machine that is slightly lesser than mine so I've tried to do less intensive a bit

Bonzomatic specifics

  • gl_FragCoord is gl_FragCoord
  • You can rename the variable name for output color
    • I prefer o, out_color is terrible since you have to hit an underscore
  • Redo is Ctrl+Y. You cannot use Shift+Ctrl+Z
  • Apply is Ctrl+R
  • Don't push Ctrl+T, it messes the code up
    • If you have pressed this, be calm, and press Ctrl+Z

You have to remember them

These are stuff I don't want to forget and fucking die

  • #define time fGlobalTime
    • because I can't remember fGlobalTime
  • #define saturate(a) clamp((a),0.,1.)
  • #define linearstep(a,b,t) saturate(((t)-(a))/((b)-(a)))
    • DON'T FORGET THE saturate
  • randSphere
    • theta is PI*2.*rand
    • phi is acos(rand*2.-1.)
    • output y is cos(p)
  • easeCeil
    • floor(x)+(.5+.5*cos(PI*exp(-30.0*fract(x))))
  • ifs
    • parameters are p, rot, and shift
    • state are pt and t
    • procedure:
      • pt=abs(pt)-abs(t*pow(2.0,-i)
        • apply lofi to the t might be good to remove artifacts
      • rotate the t using rot
      • fold using if(pt.x<pt.y), if(pt.y<pt.z)
  • sdBox
    • max(distance to nearest edge, 0.0) + min(distance to nearest surface plane, 0.0)
    • You can forget the second one, it just makes the internal distance being constant zero
  • distance function
    • You should make it vec4 instead of float for future extensibility
    • if you want to scroll repetitions, translate first, then repetite
    • if you want to grab something that is specific to each repetition instances, grab it before repetite
    • combine a repetition after the ifs, it would make things +100% cool
  • ambient occlusion
    • choose an appropriate ao function for the geometry you are going to use
      • this itme you really cannot trust your distance function to be uniform
    • today's theory is like this:
      • offset a ray position by (0.02+0.02*i)*hemisphere
      • accumulate linearstep(0.02,0.0,map(pt))
      • repeat above 64 times
      • substract saturate(2.0*sqrt(accum)) from 1.0
  • ray generation
    • who needs a camera? nooope
    • distortion -> rotate -> dof
  • shading
    • definition of fog should be outside of hit test branch
    • if you want to do glow, it also should be outside of hit test branch
    • invest in ao, no lights required
  • post processes
    • don't forget gamma correction .4545
    • vignette
    • color correction
      • rules, you defo should do this in earlygame
    • cheesy scanline,,,,,,
      • trust me, it actually makes it look better..............

Tenkai

Since vote happen in every 15+5n minutes you have to keep building your shader constantly Do something dynamic, keep evolving, utilize previous contributions

  • Earlygame
    • it seems we have plenty of time to implement entire ifs raymarcher before the first encounter. go for it
      • I will use edge rendering at this point, since there is no time to implement shading
    • post processing is recommended to make it look good at low cost
    • probably make it tunnel as a temporary treatment?
    • You might want to implement easeCeil asap
      • cool animation is cool
  • Midgame
    • easeCeil will be required at this point
    • let's repetite the ifs
    • fog would be good to implement after repetite things
  • Lategame
    • do the ao
    • Make ifs glow
    • dof?
  • If you really run out of everything
    • reflection
      • costs so much, not recommended
    • glitch
      • can mess everything you've built so far, but somehow everyone love glitches
// FMS_Cat!!
// Since it's written in the shader royale compo,
// the code is a total mess that is not intended to be read.
// I heard the compo machine is not a beast enough
// so I made this less intensive a bit.
// There are several artifacts that is difficult to ignore
#version 410 core
#define BEAT (time*170.0/60.0)
#define PI 3.14159265
#define time fGlobalTime
#define lofi(x,d) (floor((x)/(d))*(d))
#define saturate(a) (clamp((a),0.,1.))
#define linearstep(a,b,t) (saturate(((t)-(a))/((b)-(a))))
uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)
uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
uniform sampler1D texFFTIntegrated; // this is continually increasing
uniform sampler2D texChecker;
uniform sampler2D texNoise;
uniform sampler2D texTex1;
uniform sampler2D texTex2;
uniform sampler2D texTex3;
uniform sampler2D texTex4;
float seed;
layout(location = 0) out vec4 o; // out_color must be written in order to see anything
float fractsin(float v)
{
return fract(sin(v*121.445)*34.59);
}
float rand()
{
seed=fractsin(seed);
return seed;
}
vec3 randsphere()
{
float t=PI*2.*rand();
float p=acos(rand()*2.-1.);
return vec3(sin(p)*sin(t),cos(p),sin(p)*cos(t));
}
float easeceil(float t, float fac)
{
return floor(t)+.5+.5*cos(PI*exp(fac*fract(t)));
}
mat2 rot2d(float t)
{
return mat2(cos(t),-sin(t),sin(t),cos(t));
}
vec3 ifs(vec3 p,vec3 rot,vec3 shift)
{
vec3 pt=abs(p);
vec3 t=shift;
for(int i=0;i<6;i++)
{
pt=abs(pt)-abs(lofi(t*pow(1.8,-float(i)),1.0/512.0));
t.yz=rot2d(rot.x)*t.yz;
t.zx=rot2d(rot.y)*t.zx;
t.xy=rot2d(rot.z)*t.xy;
pt.xy=pt.x<pt.y?pt.yx:pt.xy;
pt.yz=pt.y<pt.z?pt.zy:pt.yz;
}
return pt;
}
float sdbox(vec3 p,vec3 s)
{
vec3 d=abs(p)-s;
return length(max(d,0.));
}
// ======= map!!!!!!!!!! ====================================
vec4 map(vec3 p)
{
vec3 pt=p;
vec3 haha=lofi(pt,5.0);
float scrphase=mod(999.9*fractsin(haha.y+haha.z+3.88),PI*2.0);
float scr=(mod(haha.y+haha.z,2.0)*2.0-1.0)*20.0*smoothstep(-0.5,0.5,sin(time*0.5+scrphase));
pt.x+=scr;
haha=lofi(pt,5.0);
float phase=BEAT/8.0;
phase+=dot(haha,vec3(2.75,3.625,1.0625));
phase=easeceil(phase,-10.0);
pt=mod(pt,5.0)-2.5;
vec3 pm=pt;
pt.yz=rot2d(.5*PI*phase+.25*PI)*pt.yz;
float clampBox=sdbox(pt,vec3(2.25,1.5,1.8));
pt=ifs(pt,vec3(3.6,3.0+0.4*phase,3.1),vec3(3.0,2.3,3.5));
pt=mod(pt-.5,1.)-.5;
float dist=sdbox(pt,vec3(.17));
dist=max(dist,clampBox);
return vec4(
dist,
sin(PI*fract(phase)),
step(0.0,0.01-abs(pt.x+pt.y)),
abs(pm.x)+abs(pm.y)+abs(pm.z)
);
}
vec3 normalFunc(vec3 p,vec2 d)
{
return normalize(vec3(
map(p+d.yxx).x-map(p-d.yxx).x,
map(p+d.xyx).x-map(p-d.xyx).x,
map(p+d.xxy).x-map(p-d.xxy).x
));
}
float aoFunc(vec3 p,vec3 n)
{
float accum=0.;
for(int i=0;i<32;i++){
vec3 d=(0.02+0.02*float(i))*randsphere();
d=dot(d,n)<.0?-d:d;
//accum+=step(map(p+d).x,0.0)/64.0;
accum+=linearstep(0.02,0.0,map(p+d).x)/64.0;
}
return 1.0-sqrt(saturate(6.0*accum));
}
vec2 glitch(vec2 v)
{
vec2 vt=v;
for(int i=0;i<6;i++)
{
float fac=4.0*pow(2.2,-float(i));
float s=fractsin(lofi(vt.x,1.6*fac));
s+=fractsin(lofi(vt.y,0.4*fac));
s+=fractsin(time);
float proc=fractsin(s);
vt+=0.2*step(proc,0.4*exp(-3.0*mod(BEAT,8.0))-0.01)*(vec2(
fractsin(s+22.56),
fractsin(s+17.56)
)-0.5);
}
return vt;
}
void main(void)
{
vec2 p=(gl_FragCoord.xy*2.0-v2Resolution)/v2Resolution.y;
o=vec4(0,0,0,1);
seed=texture(texNoise,p).x;
seed+=fGlobalTime;
vec2 po=p;
p=glitch(p);
vec3 ro=vec3(4.0*time,0,lofi(BEAT,8.0));
vec3 rd=vec3(p,-1);
rd.z+=0.6*length(p);
float camphase=lofi(BEAT,8.0)+mod(BEAT,8.0)*0.2;
rd.yz=rot2d(0.33*camphase+0.03*sin(3.0*time))*rd.yz;
rd.zx=rot2d(0.78*camphase+0.03*cos(3.0*time))*rd.zx;
rd.xy=rot2d(0.048*camphase)*rd.xy;
rd=normalize(rd);
vec3 fp=ro+rd*5.0;
ro+=0.02*randsphere();
rd=normalize(fp-ro);
vec4 dist;
float rl=0.01;
float glow=0.0;
vec3 rp=ro+rl*rd;
for(int i=0;i<69;i++){ // nice
dist=map(rp);
glow=dist.y;
rl+=dist.x*0.7;
rp=ro+rl*rd;
}
float fog=exp(-0.1*max(0.,rl-5.0));
o.xyz+=(1.0-fog)*vec3(1.);
vec3 n2=normalFunc(rp,vec2(0.0,1E-2+4E-2*dist.y));
vec3 n=normalFunc(rp,vec2(0.0,2E-3));
float edge=length(n-n2);
float gorge=dist.z;
o.xyz+=fog*0.1*vec3(15.0,1.0,1.5)*glow;
if(dist.x<1E-3)
{
float ao=aoFunc(rp,n);
o.xyz+=fog*vec3((0.4-0.1*gorge)*ao);
o.xyz+=fog*edge*dist.y*vec3(15.0,1.0,1.5);
o.xyz+=fog*gorge*vec3(2.0,15.0,5.0)*exp(-10.0*mod(time+dist.w,1.0));
}
o.xyz+=length(p-po)*2.0*sin(3.0+4.0*o.x+vec3(0.0,2.0,4.0));
o.xyz=pow(o.xyz,vec3(0.4545));
o.xyz-=0.2*length(p);
o.xyz=vec3(
smoothstep(0.1,0.9,o.x),
linearstep(0.0,0.8,o.y),
smoothstep(-0.2,1.1,o.z)
);
o.xyz*=1.0+0.1*sin(vec3(0.,1.,2.)+gl_FragCoord.y*2.0);
}
#version 410 core
#define linearstep(a,b,t) clamp(((t)-(a))/((b)-(a)),0.,1.)
const float BPS = 140.0/60.0;
const float PI = 3.14159265;
uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)
uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
uniform sampler1D texFFTIntegrated; // this is continually increasing
uniform sampler2D texChecker;
uniform sampler2D texNoise;
uniform sampler2D texTex1;
uniform sampler2D texTex2;
uniform sampler2D texTex3;
uniform sampler2D texTex4;
float seed;
layout(location = 0) out vec4 o; // out_color must be written in order to see anything
float fractSin(inout float v)
{
v=fract(sin(1723.33*v)*223.18);
return v;
}
vec3 randSphere(inout float v)
{
float t=PI*2.0*fractSin(v);
float p=acos(fractSin(v)*2.0-1.0);
return vec3(sin(p)*cos(t),cos(p),sin(p)*sin(t));
}
mat2 rot2d(float t)
{
return mat2(cos(t),-sin(t),sin(t),cos(t));
}
vec3 ifs(vec3 p, float factor, vec3 rot, vec3 shift)
{
vec3 pt=p;
vec3 t=vec3(shift);
for(int i=0;i<5;i++){
float p2i=pow(factor,-float(i));
pt = abs(pt)-abs(floor(0.5+t*1024.0*p2i)/1024.0);
t.yz=rot2d(rot.x)*t.yz;
t.zx=rot2d(rot.y)*t.zx;
t.xy=rot2d(rot.z)*t.xy;
if(pt.x<pt.y){pt.xy=pt.yx;}
if(pt.y<pt.z){pt.yz=pt.zy;}
if(pt.x<pt.z){pt.zx=pt.xz;}
}
return pt;
}
float sdBox(vec3 p, vec3 s)
{
vec3 d=abs(p)-s;
return length(max(d,0.0))+min(max(d.x,max(d.y,d.z)),0.0);
}
vec2 map(vec3 p)
{
vec3 pt=p;
float phase=fGlobalTime / 16.0 * BPS;
pt.x += (2.0*mod(floor(pt.y/5.0)+floor(pt.z/5.0),2.0)-1.0)*fGlobalTime;
phase -= dot(vec3(6.25,2.125,4.5625),floor(pt/5.0));
pt.xyz=mod(pt.xyz,5.0)-2.5;
vec3 pm=pt;
phase=floor(phase)+(0.5+0.5*cos(PI*exp(-30.0 * fract(phase))));
pt.yz=rot2d(0.5*PI*phase+0.25*PI)*pt.yz;
float clampBox=sdBox(pt,vec3(2.4,1.5,1.5));
pt=ifs(pt, 2.0, vec3(0.21,0.92,1.03), 2.0*sin(phase*2.0/vec3(3.4,4.7,4.1)));
pt=mod(pt-0.5,1.0)-0.5;
float dist = max(max(sdBox(pt, vec3(0.15)),-abs(p.y)+0.0),clampBox);
return vec2(
dist,
sin(PI*fract(phase))*exp(-1.0 * abs(dist))
);
}
float aoFunc(vec3 p, vec3 n)
{
float accum=0.0;
for(int i=0;i<64;i++){
vec3 dir=(0.02+0.02*float(i))*randSphere(seed);
vec3 pt=dot(dir,n)<0.0?(p-dir):(p+dir);
accum+=linearstep(0.02,0.0,map(pt).x)/64.0;
}
return max(0.0,1.0-2.0*sqrt(accum));
}
vec3 normalFunc(vec3 p, float d)
{
vec2 dd =vec2(0.0,d);
return normalize(vec3(
map(p+dd.yxx).x-map(p-dd.yxx).x,
map(p+dd.xyx).x-map(p-dd.xyx).x,
map(p+dd.xxy).x-map(p-dd.xxy).x
));
}
void main(void)
{
seed=gl_FragCoord.x;
fractSin(seed);
seed+=gl_FragCoord.y;
fractSin(seed);
seed+=fGlobalTime;
vec2 p = (gl_FragCoord.xy * 2.0 - v2Resolution)/v2Resolution.y;
vec3 ro = vec3(0.0,0.0,-fGlobalTime);
vec3 rd = vec3(p.xy, -1.0);
rd.z += 0.2*length(p);
rd = normalize(rd);
rd.yz = rot2d(0.01*sin(fGlobalTime*3.2)+0.06*fGlobalTime)*rd.yz;
rd.xy = rot2d(0.01*sin(fGlobalTime*3.7)+0.034*fGlobalTime)*rd.xy;
vec3 fp = ro+rd*5.0;
ro+=0.02*randSphere(seed);
rd=normalize(fp-ro);
o=vec4(0.0);
vec3 rem=vec3(1.0);
float rl = 0.01;
vec3 rp = ro+rd*rl;
vec2 dist;
float glow;
for(int i=0;i<99;i++){
dist = map(rp);
glow += dist.y;
rl+=0.6 * dist.x;
rp=ro+rd*rl;
}
float fog=exp(-0.08*max(rl-5.0,0.0));
o.xyz=rem*(1.0-fog)*vec3(1.0);
rem*=fog;
vec3 n =normalFunc(rp,1E-3);
vec3 n2=normalFunc(rp,1E-2+1E-1*dist.y);
float edge = linearstep(0.1,0.2,length(n-n2));
if(abs(dist.x)<1E-3){
float ao = aoFunc(rp,n);
o.xyz+=mix(
rem*0.4*vec3(ao)*(1.0-edge),
rem*vec3(12.0,1.0,1.5)*vec3(edge),
dist.y
);
}
o.xyz+=0.002*glow*vec3(12.0,1.0,1.5);
o.xyz=pow(o.xyz,vec3(.4545));
o.xyz=clamp(o.xyz,vec3(0.0),vec3(1.0));
o.xyz-=0.2*length(p);
o.xyz=vec3(
smoothstep(0.1,0.9,o.x+0.1*p.y),
smoothstep(0.0,0.8,o.y),
smoothstep(-0.2,1.2,o.z)
);
}
#version 410 core
#define PI 3.14159265
#define time fGlobalTime
#define saturate(x) clamp((x),0.,1.)
#define linearstep(a,b,t) saturate(((t)-(a))/((b)-(a)))
#define lofi(x,y) (floor((x)/(y))*(y))
float seed;
uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)
uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
uniform sampler1D texFFTIntegrated; // this is continually increasing
uniform sampler2D texChecker;
uniform sampler2D texNoise;
uniform sampler2D texTex1;
uniform sampler2D texTex2;
uniform sampler2D texTex3;
uniform sampler2D texTex4;
layout(location = 0) out vec4 o; // out_color must be written in order to see anything
float easeCeil(float t,float factor)
{
return floor(t)+(.5+.5*cos(PI*exp(factor*fract(t))));
}
float fractsin(float v)
{
return fract(sin(v*165.22)*298.55);
}
float rand()
{
seed=fractsin(seed);
return seed;
}
vec3 randsphere()
{
float t=PI*2.*rand();
float p=acos(rand()*2.-1.);
return vec3(
sin(p)*cos(t),
cos(p),
sin(p)*sin(t)
);
}
mat2 rot2d(float t)
{
return mat2(cos(t),-sin(t),sin(t),cos(t));
}
float sdbox(vec3 p, vec3 s)
{
vec3 d=abs(p)-s;
return length(max(d,0.))+min(0.0,max(d.x,max(d.y,d.z)));
}
vec3 ifs(vec3 p,vec3 rot,vec3 shift)
{
vec3 pt=abs(p);
vec3 t=shift;
for(int i=0;i<6;i++){
pt=abs(pt)-lofi(abs(t)*pow(2.,-float(i)),1./1024.);
t.yz=rot2d(rot.x)*t.yz;
t.zx=rot2d(rot.y)*t.zx;
t.xy=rot2d(rot.z)*t.xy;
pt.xy=pt.x<pt.y?pt.yx:pt.xy;
pt.yz=pt.y<pt.z?pt.zy:pt.yz;
}
return pt;
}
vec4 map(vec3 p)
{
vec3 pt=p;
vec3 haha=floor(pt/5.0);
pt.x+=time*(mod(haha.y+haha.z,2.0)*2.0-1.0);
haha=floor(pt/5.0);
float phase=time/8.0;
phase+=dot(vec3(1.47,2.28,6.71),haha);
phase=easeCeil(phase,-30.0);
pt=mod(pt,5.)-2.5;
pt.yz=rot2d(PI*.5*phase+PI*.25)*pt.yz;
vec3 pm=pt;
float clampBox=sdbox(pt,vec3(2.4,1.0,1.7));
pt=ifs(pt,vec3(2.3+0.8*phase,2.4+0.2*phase,3.5+0.3*phase),vec3(3.9,2.5,4.8));
pt=mod(pt-0.5,1.0)-.5;
float dist=sdbox(pt,vec3(.2));
dist=max(dist,clampBox);
return vec4(
dist,
sin(PI*fract(phase)),
max(0.0,0.01-abs(pt.x+pt.y)),
abs(pm.x)+abs(pm.y)+abs(pm.z)
);
}
vec3 normalFunc(vec3 p,vec2 d)
{
return normalize(vec3(
map(p+d.yxx).x-map(p-d.yxx).x,
map(p+d.xyx).x-map(p-d.xyx).x,
map(p+d.xxy).x-map(p-d.xxy).x
));
}
float aoFunc(vec3 p,vec3 n)
{
float accum=0.0;
for(int i=0;i<64;i++){
vec3 d=(.02+0.02*float(i))*randsphere();
d=dot(d,n)<0.?-d:d;
accum+=linearstep(0.02,0.0,map(p+d).x)/64.;
}
return 1.0-sqrt(saturate(4.0*accum));
}
vec2 glitch(vec2 v)
{
vec2 vt=v;
for(int i=0;i<5;i++){
float size=pow(2.2,-float(i));
float s=time+lofi(vt.y,0.277*size)+lofi(vt.x+4.4,1.33*size);
float proc=fractsin(s);
float amp=exp(-10.0*mod(time,2.0))-0.03;
vt+=step(proc,0.5*amp)*size*(0.4-0.8*vec2(fractsin(s+22.4),fractsin(s+45.3)));
}
return vt;
}
void main(void)
{
seed=fGlobalTime;
rand();
seed+=gl_FragCoord.x;
rand();
seed+=gl_FragCoord.y;
vec2 p=(gl_FragCoord.xy*2.-v2Resolution)/v2Resolution.y;
vec2 po=p;
p=glitch(p);
vec3 ro=vec3(0,0,time);
vec3 rd=vec3(p,-1.0);
rd.z+=0.4*length(p);
rd=normalize(rd);
float camphase=0.1*mod(time,2.0)+lofi(time,2.0);
rd.zx=rot2d(camphase*0.93+0.01*sin(3.0*time))*rd.zx;
rd.yz=rot2d(camphase*1.67+0.02*cos(3.0*time))*rd.yz;
rd.xy=rot2d(camphase*0.77+0.02)*rd.xy;
vec3 fp=ro+rd*5.;
ro+=randsphere()*.02;
rd=normalize(fp-ro);
float rl=.01;
vec4 dist;
float glow=0.0;
vec3 rp=ro+rd*rl;
for(int i=0;i<99;i++){
dist=map(rp);
rl+=dist.x*.6;
glow+=dist.y;
rp=ro+rd*rl;
}
o=vec4(0,0,0,1);
float fog=exp(-0.04*max(.0,rl-5.));
o.xyz+=(1.-fog)*vec3(1.);
o.xyz+=fog*0.004*vec3(12.0,1.0,1.5)*glow;
if(dist.x<.01){
vec3 n=normalFunc(rp,vec2(0.0,1E-3));
vec3 n2=normalFunc(rp,vec2(0.0,1E-2+1E-1*dist.y));
float edge=linearstep(.1,.5,length(n-n2));
float ao=aoFunc(rp,n);
vec3 ref=reflect(rd,n);
o.xyz+=fog*mix(
vec3((0.4-0.1*step(0.001,dist.z))*ao),
vec3(edge)*vec3(12.0,1.0,1.5),
dist.y
);
o.xyz+=step(0.001,dist.z)*vec3(1.0,4.0,2.0)*exp(-10.0*fract(time+dist.w));
}
o.xyz+=length(p-po)*sin(vec3(0.0,2.0,4.0)+4.0*o.x+2.0);
o.xyz=pow(o.xyz,vec3(0.4545));
o.xyz=saturate(o.xyz);
o.xyz-=0.2*length(p);
o.xyz=(1.0+0.1*sin(gl_FragCoord.y*2.0+vec3(0.0,1.0,2.0)))*vec3(
smoothstep(.1,.9,o.x),
smoothstep(.0,.9,o.y),
smoothstep(-.1,1.2,o.z)
);
}
#version 410 core
#define PI 3.14159265
#define time fGlobalTime
#define saturate(a) clamp((a),0.,1.)
#define linearstep(a,b,t) (saturate(((t)-(a))/((b)-(a))))
#define lofi(a,d) (floor((a)/(d))*(d))
uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)
uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
uniform sampler1D texFFTIntegrated; // this is continually increasing
uniform sampler2D texChecker;
uniform sampler2D texNoise;
uniform sampler2D texTex1;
uniform sampler2D texTex2;
uniform sampler2D texTex3;
uniform sampler2D texTex4;
float seed;
layout(location = 0) out vec4 o; // out_color must be written in order to see anything
float fractsin(float v)
{
return fract(sin(v*163.28)*268.76);
}
float rand()
{
seed=fractsin(seed);
return seed;
}
vec3 randsphere()
{
float t=2.*PI*rand();
float p=acos(rand()*2.-1.);
return vec3(sin(p)*cos(t),cos(p),sin(p)*sin(t));
}
float easeceil(float v,float fac)
{
return floor(v)+.5+.5*cos(PI*exp(fac*fract(v)));
}
vec4 plas( vec2 v, float time )
{
float c = 0.5 + sin( v.x * 10.0 ) + cos( sin( time + v.y ) * 20.0 );
return vec4( sin(c * 0.2 + cos(time)), c * 0.15, cos( c * 0.1 + time / .4 ) * .25, 1.0 );
}
mat2 rot2d(float t)
{
return mat2(cos(t),-sin(t),sin(t),cos(t));
}
vec3 ifs(vec3 p,vec3 rot,vec3 shift)
{
vec3 pt=abs(p);
vec3 t=shift;
for(int i=0;i<6;i++)
{
float sc=pow(1.8,-float(i));
pt=abs(pt)-abs(t*sc);
t.yz=rot2d(rot.x)*t.yz;
t.zx=rot2d(rot.y)*t.zx;
t.xy=rot2d(rot.z)*t.xy;
pt.xy=pt.x<pt.y?pt.yx:pt.xy;
pt.yz=pt.y<pt.z?pt.zy:pt.yz;
}
return pt;
}
float sdbox(vec3 p,vec3 s)
{
vec3 d=abs(p)-s;
return length(max(d,0.0))+min(0.0,max(d.x,max(d.y,d.z)));
}
// ============== map!!!!! ==============================
vec4 map(vec3 p)
{
vec3 pt=p;
vec3 haha=lofi(pt,5.0);
float scr=(mod(haha.y+haha.z,2.0)*2.0-1.0);
scr*=2.0*(time+sin(time+PI*2.*fractsin(haha.y+haha.z)));
pt.x+=scr;
haha=lofi(pt,5.0);
float phase=time/4.0;
phase+=dot(vec3(4.436,3.167,5.382),haha);
phase=easeceil(phase,-20.0);
pt=mod(pt,5.)-2.5;
vec3 pm=pt;
pt.yz=rot2d(.5*PI*phase+.25*PI)*pt.yz;
float clampBox=sdbox(pt,vec3(2.4,1.8,1.5));
pt=ifs(pt,vec3(2.0+0.19*phase,4.0+0.4*phase,4.0+0.27*phase),vec3(5.1,4.2,3.8));
pt=mod(pt-.5,1.)-.5;
float dist=sdbox(pt,vec3(.2));
dist=max(dist,clampBox);
return vec4(
dist,
sin(PI*fract(phase)),
saturate(0.008-abs(pt.x+pt.y)),
-abs(pm.x)-abs(pm.y)-abs(pm.z)
);
}
vec3 normalFunc(vec3 p,vec2 d)
{
return normalize(vec3(
map(p+d.yxx).x-map(p-d.yxx).x,
map(p+d.xyx).x-map(p-d.xyx).x,
map(p+d.xxy).x-map(p-d.xxy).x
));
}
float aoFunc(vec3 p,vec3 n)
{
float accum=0.;
for(int i=0;i<64;i++){
vec3 d=(.02+.02*float(i))*randsphere();
d=dot(d,n)<0.?-d:d;
accum+=linearstep(0.02,0.0,map(p+d).x)/64.0;
}
return 1.0-sqrt(saturate(4.0*accum));
}
// ======= main! ===================================================
void main(void)
{
vec2 p=(gl_FragCoord.xy*2.-v2Resolution)/v2Resolution.y;
seed=texture(texNoise,p).x;
seed+=time;
o=vec4(0,0,0,1);
vec3 ro=vec3(0,0,-time);
vec3 rd=vec3(p,-1.0);
rd.z+=0.4*length(p);
rd.yz=rot2d(time*0.133+0.01*sin(time*4.0))*rd.yz;
rd.zx=rot2d(time*0.271+0.01*cos(time*4.0))*rd.zx;
rd=normalize(rd);
vec3 fp=ro+rd*5.0;
ro+=0.02*randsphere();
rd=normalize(fp-ro);
vec4 dist;
float glow;
float rl=0.01;
vec3 rp=ro+rl*rd;
for(int i=0;i<49;i++){
dist=map(rp);
glow+=dist.y;
rl+=dist.x*0.9;
rp=ro+rl*rd;
}
float fog=exp(-0.07*max(0.0,rl-5.));
o.xyz+=(1.0-fog);
o.xyz+=fog*glow*0.005*vec3(12.0,1.0,1.5);
if(dist.x<1E-3){
vec3 n=normalFunc(rp,vec2(0,1E-3));
vec3 n2=normalFunc(rp,vec2(0,3E-2+1E-1*dist.y));
float ao=aoFunc(rp,n);
float edge=length(n-n2);
float gorge=step(0.001,dist.z);
o.xyz+=fog*vec3((0.4-0.1*gorge)*ao);
o.xyz+=fog*edge*dist.y*vec3(12.0,1.0,1.5);
o.xyz+=fog*gorge*vec3(1.0,8.0,2.0)*exp(-10.0*fract(time+dist.w));
rd=normalize(reflect(rd,n)+0.1*randsphere());
fog*=0.3;
}
ro=rp;
rl=.01;
glow=0.0;
for(int i=0;i<29;i++){
dist=map(rp);
glow+=dist.y;
rl+=dist.x*0.9;
rp=ro+rl*rd;
}
if(dist.x<1E-3){
vec3 n=normalFunc(rp,vec2(0,1E-3));
vec3 n2=normalFunc(rp,vec2(0,3E-2+1E-1*dist.y));
float edge=length(n-n2);
float gorge=step(0.001,dist.z);
o.xyz+=fog*vec3((0.4-0.1*gorge));
o.xyz+=fog*edge*dist.y*vec3(12.0,1.0,1.5);
o.xyz+=fog*gorge*vec3(1.0,8.0,2.0)*exp(-10.0*fract(time+dist.w));
}
o.xyz=pow(o.xyz,vec3(0.4545));
o.xyz=saturate(o.xyz);
o.xyz-=length(p)*.2;
o.xyz*=1.0+0.2*sin(gl_FragCoord.y*2.0*vec3(0,1,2));
o.xyz=vec3(
smoothstep(0.1,0.9,o.x),
linearstep(0.0,0.9,o.y),
smoothstep(-0.2,1.1,o.z)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment