diff --git a/examples/test/shaders/test.frag b/examples/test/shaders/test.frag index 7389eca..ac06bfa 100644 --- a/examples/test/shaders/test.frag +++ b/examples/test/shaders/test.frag @@ -1,11 +1,11 @@ #version 330 core -#define FOV 90 +#define FOV 24 #define EPSILON 0.01 #define MAX_STEPS 64 #define NEAR_D 0. -#define FAR_D 20. +#define FAR_D 120. #define GRAD_EPSILON 0.0001 @@ -17,21 +17,39 @@ uniform float u_Time; out vec4 color; +#include "hg_sdf.glsl" #include "utils.glsl" #include "march_prolog.glsl" -#include "hg_sdf.glsl" #include "colormap_cool.glsl" +#define MAT_NORMALS 1. + vec3 ray_dir(float fov, vec2 uv) { float z = 1./tan(radians(fov)/2.); return normalize(vec3(uv, z)); } SceneResult scene_f(vec3 p) { - SceneResult box = SceneResult(fBox(p, vec3(1)), 1.); - SceneResult sphere = SceneResult(fSphere(p - vec3(1), 1.0), 2.); + SceneResult ball = SceneResult(length(p-vec3(0, 2, 0)) - 2, MAT_NORMALS); + SceneResult plane = SceneResult(p.y, 2.); + + // SceneResult box = SceneResult( + // fOpUnionRound(fBox(p, vec3(1)), fSphere(p-vec3(.8), 1.), .2), 0.); + + //SceneResult res = SceneResult( + // mix(fBox(p, vec3(1.)), fSphere(p, 1.), + // pow(sin(.5*TAU*u_Time), 0.8)), + // 1.); + + // SceneResult res = SceneResult(-p.z, 1.); - return min_sr(box, sphere); + // SceneResult res = SceneResult( + // fBox(p, vec3(1)), + // 1.); + + SceneResult res = min_sr(ball, plane); + + return res; } vec3 estimate_scene_normal(vec3 p) { @@ -55,11 +73,21 @@ vec3 raymarch(vec3 o, vec3 d, float start, float end) { t += sr.d; } - return vec3(end, -1., MAX_STEPS); + return vec3(end, N_HIT_FAR_PLANE, MAX_STEPS); } vec4 shade_material(vec3 p, vec3 norm, float mat_idx) { - return vec4(1.); + if (mat_idx <= 0.0) { + return vec4(0); + } + if (mat_idx <= 1.0) { + // return colormap((mod(p.x, 3.) + 1)/2.5); + return vec4(norm, 1.0); + } + if (mat_idx <= 2.0) { + return vec4(sign(mod(floor(p.x) + floor(p.z), 2.0))); + } + //return } vec4 shade(vec3 p, float mat_idx) { @@ -71,13 +99,13 @@ vec4 shade(vec3 p, float mat_idx) { void main() { vec2 mouse_uv = u_Mouse.xy * 2.0 / u_Resolution.xy - 1.0; - + vec2 uv = gl_FragCoord.xy * 2.0 / u_Resolution.xy - 1.0; uv.x *= u_Resolution.x/u_Resolution.y; - float an = 0.3 * u_Time; - float d = 2. + sin(an) * 1.6; - vec3 eye = vec3(3. * sin(an), 2., 3. * cos(an)) * d; + float an = 0.3 * u_Time; + float d = 6.; + vec3 eye = vec3(3. * sin(an), 4., 3. * cos(an)) * d; vec3 target = vec3(0., 0., 0.); mat3 lookAt = look_mat(eye, target, 0); vec3 dir = normalize(lookAt * ray_dir(FOV, uv)); @@ -89,15 +117,15 @@ void main() { float mat_idx = result.y; float iters = result.z; if (depth >= FAR_D) { - color = vec4(0.); + color = vec4(.1, .3, .9, 1.); return; } - // color = colormap(iters/MAX_STEPS+0.5); + //color = colormap(iters/MAX_STEPS+0.5); vec3 p = eye + dir * depth; // recast the ray color = shade(p, mat_idx); - // gamma - color = vec4(pow(clamp(color.xyz, 0.0, 1.0), vec3(0.4545)), 1.0); + // gamma + color = vec4(pow(clamp(color.xyz, 0.0, 1.0), vec3(0.4545)), 1.0); }