libgarbage: better metallic reflections, fix colour space

This commit is contained in:
Crispy 2023-08-08 20:22:50 +02:00
parent 9befad0a59
commit c9af7c9a1f
11 changed files with 386 additions and 79 deletions

View file

@ -132,6 +132,7 @@ struct Ray {
float min_dist; // smallest distance encountered along the path (only useful for misses; shadow calculations)
};
#define UP float3(0, 1, 0)
float DISTANCE_FN(float3 p);
SurfacePoint SCENE_FN(float3 p);
@ -166,9 +167,7 @@ FragOut frag (V2F i) {
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 col_acc = 1;
float3 first_hit;
bool inside_shape;
@ -176,19 +175,15 @@ FragOut frag (V2F i) {
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;
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);
color_used = col_amount;
}
float3 light = LIGHT_FN(ray);
col_acc *= ray.mat.col * lerp(light, 1, ray.mat.gloss);
ray_num++;
if (ray.missed || ray.mat.gloss < 0.01) {
break;
}
prev_gloss = ray.mat.gloss;
ray_dir = reflect(ray_dir, ray.normal);
ray_origin = ray.hit_pos + ray_dir * SURF_DIST * 2;
}
@ -198,7 +193,9 @@ FragOut frag (V2F i) {
#endif
FragOut o;
o.col = clamp(col, 0, 1);
// something something sRGB, colour space is annoying
col_acc = pow(col_acc, 2.2);
o.col = clamp(col_acc, 0, 1);
#ifndef DISABLE_DEPTH
float3 depth_point = first_hit * SCENE_SCALE;
@ -292,7 +289,7 @@ float3 default_lighting(Ray ray) {
float3 light = lSun(ray.normal, sun_dir);
light *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
light += lSky(ray.normal);
return light * ray.mat.col;
return light;
}
#endif // LIBGARBAGE_INCLUDED

View file

@ -56,8 +56,8 @@ Shader "CrispyPin/LibGarbageExample"
float atime = fmod(_Time.y * 0.3, 1);
p.x += smoothstep(0, 0.5, atime) * 2;
p.z += smoothstep(0.5, 1, atime) * 2;
return lerp(0.08,
checkers(p - 1, 0.06, 0.12, 2),
return lerp(0.42,
checkers(p - 1, 0.35, 0.45, 1),
smoothstep(64, 0, length(p))
);
}
@ -114,14 +114,14 @@ Shader "CrispyPin/LibGarbageExample"
)
);
d.mat = mat(float3(0.1, 0.6, 1.0), 0.5);
d.mat = mat(float3(0.35, 0.8, 1), 1);
float3 p2 = p - float3(0, 2, sin(_Time.x * 5) * 3);
d = qSub(d, mBox(p2, float3(5.6, 3.6, .25)), 0.02);
d = qUnion(d, qRound(mBox(p2 - float3(0, -1.9, 0), float3(6, .2, .2), mat(0.1)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(0, 1.9, 0), float3(6, .2, .2), mat(0.1)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(2.9, 0, 0), float3(.2, 4, .2), mat(0.1)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(-2.9, 0, 0), float3(.2, 4, .2), mat(0.1)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(0, -1.9, 0), float3(6, .2, .2), mat(0.8)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(0, 1.9, 0), float3(6, .2, .2), mat(0.8)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(2.9, 0, 0), float3(.2, 4, .2), mat(0.8)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(-2.9, 0, 0), float3(.2, 4, .2), mat(0.8)), 0.01));
d = qUnion(d, minimal_floor(p, float3(-11.5, 0, -3.5), float3(3.5, 0, 7.5)));
return d;
@ -146,15 +146,10 @@ Shader "CrispyPin/LibGarbageExample"
}
}
float3 col = lSun(ray.normal, sun_dir);
col *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
col += lSky(ray.normal);
// col = clamp(col, 0, 1);
// col = smoothstep(0,1,col);
// col = pow(col, 1.3);
return ray.mat.col * col ;
// return col*0.2;
float3 light = lSun(ray.normal, sun_dir);
light *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
light += lSky(ray.normal);
return light;
}
ENDCG

View file

@ -17,7 +17,7 @@ float3 lRenderSky(float3 ray_dir, float3 sun_dir) {
}
//calculate sky light
float3 lSky(float3 normal, float3 sky_col = float3(0.1, 0.16, 0.18)) {
float3 lSky(float3 normal, float3 sky_col = float3(0.35, 0.43, 0.46)) {
return sky_col * (0.5 + 0.5 * normal.y);
}
@ -38,6 +38,35 @@ float lShadow(float3 p, float3 sun_dir, float sharpness = 8) {
return shadow;
}
// max ray steps, from last bounce (reset on each reflection)
#ifndef SHADOW_MAX_STEPS
#define SHADOW_MAX_STEPS 128
#endif
// max ray length, from last bounce (length is reset on each reflection)
#ifndef SHADOW_MAX_DIST
#define SHADOW_MAX_DIST 128
#endif
// the largest surface distance that is counted as a hit
#ifndef SHADOW_SURF_DIST
#define SHADOW_SURF_DIST 0.001
#endif
// soft shadows
// float lShadow(float3 p, float3 sun_dir, float sharpness = 8) {
// float light = 1;
// float ray_len = SHADOW_SURF_DIST*2;
// for (int steps = 0; steps < SHADOW_MAX_STEPS; steps++) {
// float dist = DISTANCE_FN(p + sun_dir * ray_len);
// light = min(light, sharpness * dist / ray_len);
// ray_len += dist;
// if (dist < SHADOW_SURF_DIST) return 0;
// if (ray_len > SHADOW_MAX_DIST) return light;
// }
// return light;
// }
// --------------------------------
// misc
// --------------------------------