2023-02-25 19:03:39 +00:00
|
|
|
#ifndef LIB_PARTICLE_SOFT
|
|
|
|
#define LIB_PARTICLE_SOFT
|
2021-10-20 16:42:18 +00:00
|
|
|
|
2023-02-25 19:03:39 +00:00
|
|
|
#include "lib/util/quickstep.glsl"
|
|
|
|
|
|
|
|
float viewDepth(float depth, float near, float far)
|
2021-10-20 16:42:18 +00:00
|
|
|
{
|
|
|
|
#if @reverseZ
|
|
|
|
depth = 1.0 - depth;
|
|
|
|
#endif
|
|
|
|
return (near * far) / ((far - near) * depth - far);
|
|
|
|
}
|
|
|
|
|
2023-02-25 19:03:39 +00:00
|
|
|
float calcSoftParticleFade(
|
|
|
|
in vec3 viewDir,
|
|
|
|
in vec3 viewPos,
|
|
|
|
in vec3 viewNormal,
|
|
|
|
float near,
|
|
|
|
float far,
|
|
|
|
float depth,
|
|
|
|
float size,
|
2023-10-28 17:23:48 +00:00
|
|
|
bool fade,
|
|
|
|
float softFalloffDepth
|
2023-02-25 19:03:39 +00:00
|
|
|
)
|
2021-10-20 16:42:18 +00:00
|
|
|
{
|
2022-06-30 01:15:12 +00:00
|
|
|
float euclidianDepth = length(viewPos);
|
|
|
|
|
2021-10-20 16:42:18 +00:00
|
|
|
const float falloffMultiplier = 0.33;
|
|
|
|
const float contrast = 1.30;
|
|
|
|
|
2023-02-25 19:03:39 +00:00
|
|
|
float sceneDepth = viewDepth(depth, near, far);
|
|
|
|
float particleDepth = viewPos.z;
|
|
|
|
float falloff = size * falloffMultiplier;
|
2021-10-20 16:42:18 +00:00
|
|
|
float delta = particleDepth - sceneDepth;
|
|
|
|
|
2022-06-30 01:15:12 +00:00
|
|
|
float viewBias = 1.0;
|
2021-10-20 16:42:18 +00:00
|
|
|
|
2023-04-21 05:02:12 +00:00
|
|
|
if (fade)
|
|
|
|
{
|
2022-06-30 15:23:28 +00:00
|
|
|
float VdotN = dot(viewDir, viewNormal);
|
2023-10-28 17:23:48 +00:00
|
|
|
viewBias = abs(VdotN) * quickstep(euclidianDepth / softFalloffDepth) * (1.0 - pow(1.0 - abs(VdotN), 1.3));
|
2022-06-30 15:23:28 +00:00
|
|
|
}
|
2021-10-20 16:42:18 +00:00
|
|
|
|
2022-06-30 01:15:12 +00:00
|
|
|
const float shift = 0.845;
|
|
|
|
return shift * pow(clamp(delta/falloff, 0.0, 1.0), contrast) * viewBias;
|
2021-10-20 16:42:18 +00:00
|
|
|
}
|
2023-02-25 19:03:39 +00:00
|
|
|
|
|
|
|
#endif
|