cvr-props/Assets/raymarched/lib/libgarbage_lighting.cginc

92 lines
2.4 KiB
HLSL
Raw Normal View History

// --------------------------------
// common lighting operations
// --------------------------------
2023-07-30 12:26:54 +02:00
float3 lRenderSun(float3 ray_dir, float3 sun_dir, float3 sun_col = float3(0.8, 0.4, 0.1)) {
float alignment = min(acos(dot(ray_dir, sun_dir)), 1);
2023-07-30 12:26:54 +02:00
float sun_amount = pow(0.025/alignment, 3);
return sun_amount * sun_col;
}
// a basic procedural sky
float3 lRenderSky(float3 ray_dir, float3 sun_dir) {
float3 rendered_sun = lRenderSun(ray_dir, sun_dir);
2023-07-30 12:26:54 +02:00
float a = 1 - abs(ray_dir.y);
return lerp(float3(0.3, 0.3, 0.7), float3(0.6, 0.7, 0.9), a) + rendered_sun;
}
//calculate sky light
float3 lSky(float3 normal, float3 sky_col = float3(0.35, 0.43, 0.46)) {
return sky_col * (0.5 + 0.5 * normal.y);
}
float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(1, 0.78, 0.43)) {
return sun_col * max(dot(normal, sun_dir), 0);
}
// soft shadows
float lShadow(float3 p, float3 sun_dir, float sharpness = 8) {
float shadow = 1;
for (float ray_len = 0.001; ray_len < MAX_DIST / 2.0;)
{
float dist = DISTANCE_FN(p + sun_dir * ray_len);
if (dist < SURF_DIST) return 0;
shadow = min(shadow, sharpness * dist / ray_len);
ray_len += dist;
}
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
// --------------------------------
float3 HSV(float h, float s, float v) {
h *= 6;
float c = s * v;
float x = c * (1 - abs(fmod(h, 2) - 1));
float m = v - c;
c += m;
x += m;
float3 colors[6] = {
float3(c, x, m),
float3(x, c, m),
float3(m, c, x),
float3(m, x, c),
float3(x, m, c),
float3(c, m, x)};
return colors[int(h)];
}