Last active
February 27, 2017 15:01
-
-
Save eulersson/ebbcf7ea71433da18c3e8e5999501172 to your computer and use it in GitHub Desktop.
Metaballs (Synthclipse)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniform vec3 iResolution; // viewport resolution (in pixels) | |
uniform float iGlobalTime; // shader playback time (in seconds) | |
uniform float iChannelTime[4]; // channel playback time (in seconds) | |
uniform vec3 iChannelResolution[4]; // channel resolution (in pixels) | |
uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click | |
uniform vec4 iDate; // (year, month, day, time in seconds) | |
uniform bool WithGamma; //! checkbox[true] | |
uniform float Gamma; //! slider[0.1, 2.2, 10] | |
uniform float tete; //! slider[0.0, 0.1, 0.2] | |
uniform bool SynthclipseCamera; //! checkbox[true] | |
#include <Camera> | |
vec3 lig = normalize( vec3(0.0,0.0,-0.9) ); | |
vec3 getMaterial(in vec3 pos, in vec3 nor) | |
{ | |
return vec3(0.5, 0.2, 0.1); | |
} | |
float map(in vec3 pos) { | |
return (length(pos) - 5.0) + clamp(sin(tete*(pos.x * pos.y * pos.z)), 0.0, 1.0); | |
} | |
vec3 calcNormal(in vec3 pos, in float t) { | |
vec3 eps = vec3( max(0.02, t * 0.001), 0.0, 0.0); | |
return normalize( | |
vec3( | |
map(pos + eps.xyy) - map(pos - eps.xyy), | |
map(pos + eps.yxy) - map(pos-eps.yxy), | |
map(pos+eps.yyx) - map(pos-eps.yyx) | |
) | |
); | |
} | |
float raymarch(in vec3 ro, in vec3 rd) { | |
float maxd = 30.0; | |
float precis = 0.001; | |
float h = 1.0; | |
float t = 0.1; | |
for (int i = 0; i < 200; i++) { | |
if (abs(h) < precis || t > maxd) | |
continue; //break; | |
h = map(ro + rd * t); | |
t += h; | |
} | |
if (t > maxd) | |
t = -1.0; | |
return t; | |
} | |
void main() { | |
vec2 uv = gl_FragCoord.xy / iResolution.xy; | |
vec3 ro, rd; | |
if (SynthclipseCamera) { | |
getCamera(ro, rd); | |
} | |
vec3 col = vec3(0.32, 0.36, 0.4) - rd.y * 0.4; | |
float sun = clamp( dot(rd, -lig), 0.0, 1.0 ); | |
col += vec3(1.0,0.8,0.2) * 0.2 * pow( sun, 9.0 ); | |
col += vec3(1.0, 0.6, 0.2) * pow( sun, 9.0 );// * clamp( (rd.y+0.4)/(0.0+0.4),0.0,1.0); | |
vec3 bcol = col; | |
float t = raymarch(ro, rd); | |
if (t > 0.0) { | |
vec3 pos = ro + t * rd; | |
vec3 nor = calcNormal(pos, t); | |
vec3 ref = reflect( rd, nor); | |
float hh = 1.0 - smoothstep( -2.0, 1.0, pos.y ); | |
float sun = pow(clamp(dot(nor, -lig), 0.0, 1.0), 2.0); | |
float sky = 0.5 * nor.y + 0.5; | |
col = vec3(0.4); | |
vec3 lin = vec3(0.0); | |
lin += sun * vec3(1.8, 1.4, 1.2); | |
lin += sky * vec3(0.16, 0.3, 0.5); | |
col = lin * col; | |
col = mix( col, (1.0-0.7*hh)*bcol, 1.0-exp(-0.00006*t*t*t) ); | |
} | |
if (WithGamma) {col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / Gamma));} | |
gl_FragColor = vec4(col, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment