Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created September 22, 2017 20:07
Show Gist options
  • Save MagnusThor/448ecbe41cd97f8a33df4bbdb1d0b58a to your computer and use it in GitHub Desktop.
Save MagnusThor/448ecbe41cd97f8a33df4bbdb1d0b58a 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;
vec4 orb = vec4(1000.0);
float aspect_ratio = resolution.y / resolution.x;
float ray_scale = (1. / max(resolution.x, resolution.y)) * 0.1;
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;
orb = min( orb, vec4(abs(p),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 raymarch(in vec3 from, in vec3 dir) {
float maxd = 30.0;
float t = 0.01;
for( int i=0; i<200; i++ )
{
float precis = 0.001 * t;
float h = de( from+dir*t) ;
if( h<precis||t>maxd ) break;
t += h;
}
if( t>maxd ) t=-1.0;
return t;;
}
vec3 postProcess(vec3 color){
return color;
color*=vec3(1.,.94,.87);
color=pow(color,vec3(1.2));
color=mix(vec3(length(color)),color,.85)*.95;
return color;
}
float shadow( in vec3 ro, in vec3 rd)
{
float res = 1.0;
float t = 0.05;
float h;
for (int i = 0; i < 8; i++){
h = de( ro + rd*t );
res = min(6.0*h / t, res);
t += h;
}
return max(res, 0.0);
}
void main( void ) {
vec2 uv = (gl_FragCoord.xy / resolution) - .5;
/*
vec3 camPos = .2*time*vec3(1.0,0.,0.0);
vec3 lookAt = camPos + vec3(1.,0.0*cos(time),0.);
vec3 camUp = vec3(1.0,0,1.);
vec3 camDir = normalize(lookAt-camPos);
camUp = normalize(camUp-dot(camDir,camUp)*camDir);
vec3 camRight = normalize(cross(camDir,camUp));
vec3 rd = normalize(camDir + (uv.x*camRight + uv.y*camUp)*1.5); ;//vec3(uv.x * fov, uv.y * fov * aspect_ratio, 1.0);
vec3 ro = camPos;
*/
float fov = 1.;
vec3 col = vec3(0.0);
vec3 rd = vec3(uv.x * fov, uv.y * fov * aspect_ratio, 1.0);
vec3 ro = .3*time*vec3(0.,0.3,1.5);
float t = raymarch(ro,rd);
if(t > 0.0){
vec3 pos = ro + t*rd;
vec4 tra = orb;
vec3 nor = normal( pos);
// lighting
vec3 light1 = vec3( 0.577, 0.577, -0.577 );
vec3 light2 = vec3( -0.707, 0.000, 0.707 );
float key = clamp( dot( light1, nor ), 0.0, 1.0 );
float bac = clamp( 0.2 + 0.8*dot( light2, nor ), 0.0, 1.0 );
float amb = (0.7+0.3*nor.y);
float ao = pow( clamp(tra.w*2.0,0.0,1.0), 1.2 );
vec3 brdf = 1.0*vec3(0.40,0.40,0.40)*amb*ao;
brdf += 1.0*vec3(1.00,1.00,1.00)*key*ao;
brdf += 1.0*vec3(0.40,0.40,0.40)*bac*ao;
// material
vec3 rgb = vec3(0.0);
rgb = mix( rgb, vec3(1.0,0.80,0.2), clamp(6.0*tra.y,0.0,1.0) );
rgb = mix( rgb, vec3(1.0,0.55,0.0), pow(clamp(1.0-2.0*tra.z,0.0,1.0),8.0) );
// color
col = rgb*brdf*exp(-0.2*t);
col = sqrt(col);
}
gl_FragColor = vec4( postProcess(col),1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment