libgarbage: fix depth in reflections when using LIMIT_DEPTH_TO_MESH; fix flickering when camera is inside volume

This commit is contained in:
Crispy 2023-07-26 18:25:45 +02:00
parent 299cf8ff32
commit 5cb5dcd785

View file

@ -26,6 +26,11 @@ START_RAYS_IN_BOX // TODO: implement
START_RAYS_IN_SPHERE // 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 // scene sdf that includes material data
#ifndef SCENE_FN #ifndef SCENE_FN
@ -159,15 +164,14 @@ FragOut frag (V2F i) {
float ray_len = 0; float ray_len = 0;
float3 ray_dir = normalize(i.hit_pos - i.cam_pos); float3 ray_dir = normalize(i.hit_pos - i.cam_pos);
float3 ray_origin = i.cam_pos; float3 ray_origin = i.cam_pos / SCENE_SCALE;
ray_origin /= SCENE_SCALE;
SurfacePoint point_data;
Ray ray; Ray ray;
float3 col; float3 col;
float color_used = 0;// what amount of the final color has been calculated float color_used = 0;// what amount of the final color has been calculated
float prev_gloss = 0; float prev_gloss = 0;
float3 first_hit; float3 first_hit;
bool inside_shape;
int ray_num = 0; int ray_num = 0;
for (; ray_num < REFLECTIONS + 1;) { for (; ray_num < REFLECTIONS + 1;) {
@ -175,10 +179,7 @@ FragOut frag (V2F i) {
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) { inside_shape = 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);
@ -206,10 +207,14 @@ FragOut frag (V2F i) {
depth_point = i.hit_pos; depth_point = i.hit_pos;
#ifdef LIMIT_DEPTH_TO_MESH #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; depth_point = i.hit_pos;
#endif #endif
if (inside_shape) {
depth_point = i.cam_pos + normalize(i.hit_pos - i.cam_pos) * 0.01;
}
#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
@ -265,6 +270,10 @@ Ray cast_ray(float3 start, float3 dir, float start_len) {
return ray; return ray;
} }
float3 default_sky(float3 dir) {
return lRenderSky(dir, normalize(float3(2, 1, 0)));
}
float default_distance_sdf(float3 p) { float default_distance_sdf(float3 p) {
return sdSphere(p, 0.5); return sdSphere(p, 0.5);
} }
@ -286,5 +295,4 @@ float3 default_lighting(Ray ray) {
return col; return col;
} }
#endif // LIBGARBAGE_INCLUDED #endif // LIBGARBAGE_INCLUDED