From 5cb5dcd785aed986179a3b8997fb606218984182 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Wed, 26 Jul 2023 18:25:45 +0200 Subject: [PATCH] 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