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

64 lines
1.6 KiB
HLSL
Raw Normal View History

// --------------------------------
// common lighting operations
// --------------------------------
float3 lRenderSun(float3 ray_dir, float3 sun_dir) {
float alignment = min(acos(dot(ray_dir, sun_dir)), 1);
float sun_amount = smax(min(0.03 / alignment, 5) - 0.06, 0, 0.15);
return sun_amount* float3(0.8, 0.4, 0.1);
}
// a basic procedural sky
float3 lRenderSky(float3 ray_dir, float3 sun_dir) {
float3 rendered_sun = lRenderSun(ray_dir, sun_dir);
// float3 rendered_sun = max(0, pow(dot(ray_dir, sun_dir) + 0.4, 10)-28) * float3(0.8, 0.4, 0);
return float3(0.7, 0.75, 0.8) - abs(ray_dir.y) * 0.5 + rendered_sun;
// return rendered_sun;
}
//calculate sky light
float3 lSky(float3 normal, float3 sky_col = float3(0.5, 0.8, 0.9)) {
return sky_col * (0.5 + 0.5 * normal.y);
}
float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(7, 5.5, 3)) {
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)];
}