lygia
/lighting
/raymarch
/fog
)Add fog to the scene. See https://iquilezles.org/articles/fog/
Use:
vec3 raymarchFog(in <vec3> pixelColor, float distanceToPoint)
#ifndef FOG_DENSITY
#define FOG_DENSITY 0.0
#endif
#ifndef FOG_FALLOFF
#define FOG_FALLOFF 0.0
#endif
#ifndef FOG_COLOR_COOL
#define FOG_COLOR_COOL vec3(0.5, 0.6, 0.7)
#endif
#ifndef FOG_COLOR_WARM
#define FOG_COLOR_WARM vec3(1.0, 0.9, 0.7)
#endif
#ifndef FNC_RAYMARCH_FOG
#define FNC_RAYMARCH_FOG
vec3 raymarchFog(in vec3 col, // color of pixel
in float t) { // distance to point
float fogAmount = 1.0 - exp(-t * FOG_DENSITY);
return mix(col, FOG_COLOR_COOL, fogAmount);
}
vec3 raymarchColorFog(in vec3 col, // color of pixel
in float t, // distance to point
in vec3 rd, // camera to point
in vec3 lig) { // sun direction
float fogAmount = 1.0 - exp(-t * FOG_DENSITY);
float sunAmount = max(dot(rd, lig), 0.0);
vec3 fogColor = mix(FOG_COLOR_COOL, FOG_COLOR_WARM, pow(sunAmount, 8.0));
return mix(col, fogColor, fogAmount);
}
vec3 raymarchHeightFog( in vec3 col, // color of pixel
in float t, // distnace to point
in vec3 ro, // camera position
in vec3 rd) { // camera to point vector
float fogAmount = (FOG_DENSITY / FOG_FALLOFF) * exp(-ro.y * FOG_FALLOFF) * (1.0 - exp(-t * rd.y * FOG_FALLOFF)) / rd.y;
return mix(col, FOG_COLOR_COOL, saturate(fogAmount));
}
vec3 raymarchFog(in vec3 col, in float t, in vec3 ro, in vec3 rd){
if (FOG_DENSITY > 0.0 && FOG_FALLOFF > 0.0) {
return raymarchHeightFog(col, t, ro, rd);
}
else if (FOG_DENSITY > 0.0) {
#ifdef LIGHT_DIRECTION
return raymarchColorFog(col, t, rd, LIGHT_DIRECTION);
#else
return raymarchFog(col, t);
#endif
}
else {
return col;
}
}
#endif
Use:
float3 raymarchFog(in <float3> pixelColor, float distanceToPoint)
#ifndef FOG_DENSITY
#define FOG_DENSITY 0.0
#endif
#ifndef FOG_FALLOFF
#define FOG_FALLOFF 0.0
#endif
#ifndef FOG_COLOR_COOL
#define FOG_COLOR_COOL float3(0.5, 0.6, 0.7)
#endif
#ifndef FOG_COLOR_WARM
#define FOG_COLOR_WARM float3(1.0, 0.9, 0.7)
#endif
#ifndef FNC_RAYMARCH_FOG
#define FNC_RAYMARCH_FOG
float3 raymarchFog(in float3 col, // color of pixel
in float t) // distance to point
{
float fogAmount = 1.0 - exp(-t * FOG_DENSITY);
return lerp(col, FOG_COLOR_COOL, fogAmount);
}
float3 raymarchColorFog(in float3 col, // color of pixel
in float t, // distance to point
in float3 rd, // camera to point
in float3 lig) // sun direction
{
float fogAmount = 1.0 - exp(-t * FOG_DENSITY);
float sunAmount = max(dot(rd, lig), 0.0);
float3 fogColor = lerp(FOG_COLOR_COOL, FOG_COLOR_WARM, pow(sunAmount, 8.0));
return lerp(col, fogColor, fogAmount);
}
float3 raymarchHeightFog(in float3 col, // color of pixel
in float t, // distnace to point
in float3 ro, // camera position
in float3 rd) // camera to point vector
{
float fogAmount = (FOG_DENSITY / FOG_FALLOFF) * exp(-ro.y * FOG_FALLOFF) * (1.0 - exp(-t * rd.y * FOG_FALLOFF)) / rd.y;
return lerp(col, FOG_COLOR_COOL, saturate(fogAmount));
}
float3 raymarchFog(in float3 col, in float t, in float3 ro, in float3 rd)
{
if (FOG_DENSITY > 0.0 && FOG_FALLOFF > 0.0) {
return raymarchHeightFog(col, t, ro, rd);
}
else if (FOG_DENSITY > 0.0) {
#ifdef LIGHT_DIRECTION
return raymarchColorFog(col, t, rd, LIGHT_DIRECTION);
#else
return raymarchFog(col, t);
#endif
}
else {
return col;
}
}
#endif
LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.
Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).
It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.
Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository