libgarbage: fix depth of sky, allow limiting depth to mesh, add step multiplier setting

This commit is contained in:
Crispy 2023-07-25 12:34:08 +02:00
parent 86a3bad992
commit a1030ca528

View file

@ -19,6 +19,9 @@
# don't write depth (will have the depth defined by the original mesh) # don't write depth (will have the depth defined by the original mesh)
DISABLE_DEPTH 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_BOX // TODO: implement
START_RAYS_IN_SPHERE // TODO: implement START_RAYS_IN_SPHERE // TODO: implement
*/ */
@ -43,6 +46,11 @@ START_RAYS_IN_SPHERE // TODO: implement
#define LIGHT_FN default_lighting #define LIGHT_FN default_lighting
#endif #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) // max ray steps, from last bounce (reset on each reflection)
#ifndef MAX_STEPS #ifndef MAX_STEPS
#define MAX_STEPS 128 #define MAX_STEPS 128
@ -156,16 +164,22 @@ FragOut frag (V2F i) {
float prev_gloss = 0; float prev_gloss = 0;
float3 first_hit; 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); ray = cast_ray(ray_origin, ray_dir);
if (ray_num == 0) { // before any bounces if (ray_num == 0) { // before any bounces
col = LIGHT_FN(ray); col = LIGHT_FN(ray);
first_hit = ray.hit_pos; first_hit = ray.hit_pos;
if (ray.steps == 0) {
// discard;
// TODO: sky fn
}
} else { } else {
float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used)); float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used));
col = lerp(LIGHT_FN(ray), col, col_amount); col = lerp(LIGHT_FN(ray), col, col_amount);
color_used = col_amount; color_used = col_amount;
} }
ray_num++;
if (ray.missed || ray.mat.gloss < 0.01) { if (ray.missed || ray.mat.gloss < 0.01) {
break; break;
} }
@ -175,7 +189,7 @@ FragOut frag (V2F i) {
} }
#ifdef DISCARD_ON_MISS #ifdef DISCARD_ON_MISS
if (ray.missed && ray_num == 0) discard; if (ray.missed && ray_num == 1) discard;
#endif #endif
FragOut o; FragOut o;
@ -183,8 +197,14 @@ FragOut frag (V2F i) {
#ifndef DISABLE_DEPTH #ifndef DISABLE_DEPTH
float3 depth_point = first_hit * SCENE_SCALE; float3 depth_point = first_hit * SCENE_SCALE;
if (ray.missed && ray_num == 0) if (ray.missed && ray_num == 1)
depth_point = i.hit_pos; 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 #ifdef USE_WORLD_SPACE
float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1)); float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1));
#else #else
@ -225,7 +245,7 @@ Ray cast_ray(float3 start, float3 dir, float start_len) {
break; break;
} }
ray.min_dist = min(ray.min_dist, ray.dist); 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; if (ray_len > MAX_DIST) break;
} }