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

62 lines
1.5 KiB
HLSL

// --------------------------------
// common lighting operations
// --------------------------------
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);
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);
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.1, 0.16, 0.18)) {
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;
}
// --------------------------------
// 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)];
}