Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created September 22, 2017 09:04
Show Gist options
  • Save MagnusThor/3d7150bee3955ae6d0ac13287e5af449 to your computer and use it in GitHub Desktop.
Save MagnusThor/3d7150bee3955ae6d0ac13287e5af449 to your computer and use it in GitHub Desktop.
#ifdef GL_ES
precision mediump float;
#endif
#extension GL_OES_standard_derivatives : enable
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
float aspect_ratio = resolution.y / resolution.x;
float ray_scale = (1. / max(resolution.x, resolution.y)) * .47;
float fov = 1.7;
float de( vec3 p )
{
p = abs(1.0-mod(p,2.0));
float scale = 2.0;
for( int i=0; i<7;i++ )
{
p = -1.0 + 2.0*fract(0.5*p+0.5);
float r2 = dot(p,p);
float k = 1.0/r2;
p *= k;
scale *= k;
}
return 0.25*abs(p.y) / scale;
}
vec3 normal(in vec3 p) {
vec2 e = vec2(0.005, -0.005);
return normalize(e.xyy * de(p + e.xyy) + e.yyx * de(p + e.yyx) + e.yxy * de(p + e.yxy) + e.xxx * de(p + e.xxx));
}
float detail = 0.0002;
float calcAO( const vec3 pos, const vec3 nor ) {
float aodet=detail*80.;
float totao = 0.0;
float sca = 10.0;
for( int aoi=0; aoi<5; aoi++ ) {
float hr = aodet + aodet*float(aoi*aoi);
vec3 aopos = nor * hr + pos;
float dd = de( aopos );
totao += -(dd-hr)*sca;
sca *= 0.75;
}
return clamp( 1.0 - 5.0*totao, 0.0, 1.0 );
}
float raymarch(in vec3 from, in vec3 dir) {
float l = 0.;
float d = 0.;
float e = 0.0002;
float it = 0.;
for(int i=0;i<64;i++){
d = de(from);
from += d * dir;
l += d;
if(d<e) break;
e = ray_scale * l;
it++;
}
float f = 1. - (it / float(64));
return f;
}
vec3 postProcess(vec3 color){
color*=vec3(1.,.94,.87);
color=pow(color,vec3(1.2));
color=mix(vec3(length(color)),color,.85)*.95;
return color;
}
void main( void ) {
vec2 uv = (gl_FragCoord.xy / resolution) - .5;
float fov = 1.5;
vec3 dir = vec3(uv.x * fov, uv.y * fov * aspect_ratio, 1.);
vec3 cam = .4*time*vec3(0.1,0.,0.0);
vec3 backg=vec3(0);
vec3 col = vec3(1.0);
float k = raymarch(cam,dir);
float l = calcAO(cam*dir,normal(dir));
if(k > 0.0002){
col*=vec3(1.,.85,.8)*.6;
color = mix(color, backg, 1.0-exp(-1.3*pow0.5,1.3)));
col *= k * l;
}else{
col = vec3(0);
}
col+=vec3(1,.85,.7)*pow(max(0.,.3-length(uv-vec2(0.,.03)))/.3,1.5)*.35;
gl_FragColor = vec4( postProcess(col),1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment