From 5cb5dcd785aed986179a3b8997fb606218984182 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Wed, 26 Jul 2023 18:25:45 +0200 Subject: [PATCH 1/5] libgarbage: fix depth in reflections when using LIMIT_DEPTH_TO_MESH; fix flickering when camera is inside volume --- Assets/raymarched/lib/libgarbage.cginc | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 23b114e..b49ce6b 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -26,6 +26,11 @@ START_RAYS_IN_BOX // TODO: implement START_RAYS_IN_SPHERE // TODO: implement */ +// sky used as background unless DISCARD_ON_MISS is set, and used in reflections +// TODO: implement +#ifndef SKY_FN +#define SKY_FN default_sky +#endif // scene sdf that includes material data #ifndef SCENE_FN @@ -159,15 +164,14 @@ FragOut frag (V2F i) { float ray_len = 0; float3 ray_dir = normalize(i.hit_pos - i.cam_pos); - float3 ray_origin = i.cam_pos; - ray_origin /= SCENE_SCALE; - SurfacePoint point_data; + float3 ray_origin = i.cam_pos / SCENE_SCALE; Ray ray; float3 col; float color_used = 0;// what amount of the final color has been calculated float prev_gloss = 0; float3 first_hit; + bool inside_shape; int ray_num = 0; for (; ray_num < REFLECTIONS + 1;) { @@ -175,10 +179,7 @@ FragOut frag (V2F i) { if (ray_num == 0) { // before any bounces col = LIGHT_FN(ray); first_hit = ray.hit_pos; - if (ray.steps == 0) { - // discard; - // TODO: sky fn - } + inside_shape = ray.steps == 0; } else { float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used)); col = lerp(LIGHT_FN(ray), col, col_amount); @@ -206,10 +207,14 @@ FragOut frag (V2F i) { depth_point = i.hit_pos; #ifdef LIMIT_DEPTH_TO_MESH - if (length(ray.start - ray.hit_pos) > length(i.cam_pos - i.hit_pos)) + if (length(ray.start - first_hit) > length(i.cam_pos - i.hit_pos)) depth_point = i.hit_pos; #endif + if (inside_shape) { + depth_point = i.cam_pos + normalize(i.hit_pos - i.cam_pos) * 0.01; + } + #ifdef USE_WORLD_SPACE float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1)); #else @@ -265,6 +270,10 @@ Ray cast_ray(float3 start, float3 dir, float start_len) { return ray; } +float3 default_sky(float3 dir) { + return lRenderSky(dir, normalize(float3(2, 1, 0))); +} + float default_distance_sdf(float3 p) { return sdSphere(p, 0.5); } @@ -286,5 +295,4 @@ float3 default_lighting(Ray ray) { return col; } - #endif // LIBGARBAGE_INCLUDED From bf23886a73adf6a26d91a45db1402028b90571be Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Jul 2023 16:41:25 +0200 Subject: [PATCH 2/5] libgarbage: fix depth AGAIN --- Assets/raymarched/lib/libgarbage.cginc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index b49ce6b..8e4b87d 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -108,6 +108,7 @@ struct Material { float3 col; float gloss; }; + #define DEFAULT_MAT {float3(1, 1, 1), 0} Material mat(float3 col = float3(1, 1,1 ), float gloss = 0) { Material m; @@ -115,6 +116,9 @@ Material mat(float3 col = float3(1, 1,1 ), float gloss = 0) { m.gloss = gloss; return m; } +Material mat(float r, float g, float b) { + return mat(float3(r, g, b)); +} struct SurfacePoint { float dist; @@ -191,7 +195,7 @@ FragOut frag (V2F i) { } prev_gloss = ray.mat.gloss; ray_dir = reflect(ray_dir, ray.normal); - ray_origin = ray.hit_pos + ray_dir * 0.01; + ray_origin = ray.hit_pos + ray_dir * SURF_DIST * 2; } #ifdef DISCARD_ON_MISS @@ -199,7 +203,7 @@ FragOut frag (V2F i) { #endif FragOut o; - o.col = col; + o.col = clamp(col, 0, 1); #ifndef DISABLE_DEPTH float3 depth_point = first_hit * SCENE_SCALE; @@ -207,7 +211,7 @@ FragOut frag (V2F i) { depth_point = i.hit_pos; #ifdef LIMIT_DEPTH_TO_MESH - if (length(ray.start - first_hit) > length(i.cam_pos - i.hit_pos)) + if (length(i.cam_pos - first_hit) > length(i.cam_pos - i.hit_pos)) depth_point = i.hit_pos; #endif From a19707240abdd7106ef8314b14a2779d7826a2b6 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Jul 2023 16:41:50 +0200 Subject: [PATCH 3/5] libgarbage: fix default lighting being too bright --- Assets/raymarched/lib/libgarbage_lighting.cginc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/raymarched/lib/libgarbage_lighting.cginc b/Assets/raymarched/lib/libgarbage_lighting.cginc index 09328e5..024ffd3 100644 --- a/Assets/raymarched/lib/libgarbage_lighting.cginc +++ b/Assets/raymarched/lib/libgarbage_lighting.cginc @@ -18,11 +18,11 @@ float3 lRenderSky(float3 ray_dir, float3 sun_dir) { } //calculate sky light -float3 lSky(float3 normal, float3 sky_col = float3(0.5, 0.8, 0.9)) { +float3 lSky(float3 normal, float3 sky_col = float3(0.1, 0.16, 0.18)) { return sky_col * (0.5 + 0.5 * normal.y); } -float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(7, 5.5, 3)) { +float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(1, 0.78, 0.43)) { return sun_col * max(dot(normal, sun_dir), 0); } From 633680777a242052597f21af7855c2f42fe68742 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Jul 2023 16:43:23 +0200 Subject: [PATCH 4/5] libgarbage: add sdfs: hex prism, cylinder, infinite cylinder, helix, line --- Assets/raymarched/lib/libgarbage_shapes.cginc | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Assets/raymarched/lib/libgarbage_shapes.cginc b/Assets/raymarched/lib/libgarbage_shapes.cginc index 506180e..f341934 100644 --- a/Assets/raymarched/lib/libgarbage_shapes.cginc +++ b/Assets/raymarched/lib/libgarbage_shapes.cginc @@ -49,5 +49,47 @@ float sdPlaneY(float3 p, float height) { } _MAT_VARIANT1(mPlaneY, sdPlaneY, float, height) +float sdHexPrism(float3 p, float width, float height) { + const float3 k = float3(-0.8660254, 0.5, 0.57735); + p = abs(p); + p.xz -= 2.0 * min(dot(k.xy, p.xz), 0) * k.xy; + float2 d = float2(length(p.xz - float2(clamp(p.x,-k.z * width, k.z * width), width)) * sign(p.z - width), p.y - height); + return min(max(d.x, d.y), 0) + length(max(d, 0)); +} +_MAT_VARIANT2(mHexPrism, sdHexPrism, float, width, float, height) + +float sdInfCylinder(float3 p, float3 c) { + return length(p.xz - c.xy) - c.z; +} +_MAT_VARIANT1(mInfCylinder, sdInfCylinder, float3, c) + +float sdCylinder(float3 p, float r, float h) { + float2 d = abs(float2(length(p.xz), p.y)) - float2(r, h); + return min(max(d.x, d.y), 0) + length(max(d, 0)); +} +_MAT_VARIANT2(mCylinder, sdCylinder, float, r, float, h) + +float sdHelix(float3 p, float r1, float r2, float incline) { + float x2 = length(p.xz) - r1; // vertical plane + float angle = atan2(p.z, p.x); // angle around y axis + float y = angle * incline - p.y; + y = fmod(y, UNITY_PI * incline * 2) + UNITY_PI * incline; + return length(float2(x2, y)) - r2; +} +_MAT_VARIANT3(mHelix, sdHelix, float, r1, float, r2, float, incline) + +float sdLine(float3 p, float3 a, float3 b, float r) { + float3 pa = p - a; + float3 ba = b - a; + float h = clamp(dot(pa, ba) / dot(ba, ba), 0, 1); + return length(p- a - (b-a) * h) - r; +} +_MAT_VARIANT3(mLine, sdLine, float3, a, float3, b, float3, r) +SurfacePoint mDummy(float d, Material mat = DEFAULT_MAT) { + SurfacePoint o; + o.dist = d; + o.mat = mat; + return o; +} From ffdaf36ebcb06eb9025bfeaffa716c1338bbeee8 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Jul 2023 17:17:31 +0200 Subject: [PATCH 5/5] libgarbage: fix depth when using LIMIT_DEPTH_TO_MESH with SCENE_SCALE --- Assets/raymarched/lib/libgarbage.cginc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 8e4b87d..22167e1 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -211,7 +211,7 @@ FragOut frag (V2F i) { depth_point = i.hit_pos; #ifdef LIMIT_DEPTH_TO_MESH - if (length(i.cam_pos - first_hit) > length(i.cam_pos - i.hit_pos)) + if (length(i.cam_pos - first_hit * SCENE_SCALE) > length(i.cam_pos - i.hit_pos)) depth_point = i.hit_pos; #endif