From a1030ca528e96c6afd4c1d755c214023aa524a0e Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Tue, 25 Jul 2023 12:34:08 +0200 Subject: [PATCH] libgarbage: fix depth of sky, allow limiting depth to mesh, add step multiplier setting --- Assets/raymarched/lib/libgarbage.cginc | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 705a4f5..dff2c74 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -19,6 +19,9 @@ # don't write depth (will have the depth defined by the original mesh) DISABLE_DEPTH +# meant for frontface culled meshes, makes depth no further than the mesh edges + LIMIT_DEPTH_TO_MESH + START_RAYS_IN_BOX // TODO: implement START_RAYS_IN_SPHERE // TODO: implement */ @@ -43,6 +46,11 @@ START_RAYS_IN_SPHERE // TODO: implement #define LIGHT_FN default_lighting #endif +// set to less than one if there are artifacts in distorted sdfs +#ifndef STEP_MULTIPLIER +#define STEP_MULTIPLIER 1 +#endif + // max ray steps, from last bounce (reset on each reflection) #ifndef MAX_STEPS #define MAX_STEPS 128 @@ -156,16 +164,22 @@ FragOut frag (V2F i) { float prev_gloss = 0; float3 first_hit; - for (int ray_num = 0; ray_num < REFLECTIONS + 1; ray_num++) { + int ray_num = 0; + for (; ray_num < REFLECTIONS + 1;) { ray = cast_ray(ray_origin, ray_dir); if (ray_num == 0) { // before any bounces col = LIGHT_FN(ray); first_hit = ray.hit_pos; + if (ray.steps == 0) { + // discard; + // TODO: sky fn + } } else { float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used)); col = lerp(LIGHT_FN(ray), col, col_amount); color_used = col_amount; } + ray_num++; if (ray.missed || ray.mat.gloss < 0.01) { break; } @@ -175,7 +189,7 @@ FragOut frag (V2F i) { } #ifdef DISCARD_ON_MISS - if (ray.missed && ray_num == 0) discard; + if (ray.missed && ray_num == 1) discard; #endif FragOut o; @@ -183,8 +197,14 @@ FragOut frag (V2F i) { #ifndef DISABLE_DEPTH float3 depth_point = first_hit * SCENE_SCALE; - if (ray.missed && ray_num == 0) + if (ray.missed && ray_num == 1) depth_point = i.hit_pos; + + #ifdef LIMIT_DEPTH_TO_MESH + if (length(ray.start - ray.hit_pos) > length(i.cam_pos - i.hit_pos)) + depth_point = i.hit_pos; + #endif + #ifdef USE_WORLD_SPACE float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1)); #else @@ -225,7 +245,7 @@ Ray cast_ray(float3 start, float3 dir, float start_len) { break; } ray.min_dist = min(ray.min_dist, ray.dist); - ray_len += ray.dist; + ray_len += ray.dist * STEP_MULTIPLIER; if (ray_len > MAX_DIST) break; }