vec2 rotate(vec2 pos, float angle)
{
float c = cos(angle);
float s = sin(angle);
return mat2(c,s,-s,c) * pos;
}
float plane(vec3 pos)
{
return pos.y;
}
float sphere(vec3 pos, float radius)
{
pos.y -= 4.0;
vec3 q = pos;
float r = 3.0 + sin(iTime + (q.x * 3.));
r += 1.0 + cos(iTime + (q.y * 5.));
return length(pos) - r;
}
float map(vec3 pos)
{
float planeDist = plane(pos);
//pos = mod(pos + 5.0, 10.0) - 5.0;
return min(planeDist, sphere(pos, 2.0));
}
vec3 computeNormal(vec3 pos)
{
vec2 eps = vec2(0.1, 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)
));
}
vec3 material(vec3 pos)
{
return vec3(smoothstep(0.4, 0.41, fract(pos.x + sin(pos.z * 0.4 + iTime))));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord -.5 * iResolution.xy) / iResolution.y;
vec3 pos = vec3(0.0, 5.0, -15.0);
// Rayon que l'on envoie dans l'espace pour chacuns des pixels
vec3 dir = normalize(vec3(uv.x, uv.y, 1));
vec3 col = vec3(0.);
for(int i = 0; i < 64; i++)
{
float d/*istance*/ = map(pos);
// Point is near enough
if(d < 0.01)
{
col = computeNormal(pos);
col += fract(pos * 0.1);
col/= 2.0;
break;
}
pos += d * dir;
}
// Output to screen
fragColor = vec4(col, 1.0);
}