2 // Fragment shader for particles, which simulates depth of field using
3 // a combination of procedural texturing, alpha testing and alpha blending.
6 uniform float uAlphaTestRefValue;
7 uniform vec2 uFadeRange; // near, far
10 flat in float vFocalDistance;
11 flat in float vOpacity;
15 const float REF_VALUE_THRESHOLD = 1. / 64.;
19 // Softened disc pattern from normalized UVs
20 float value = 1.f - dot(vUvUnit, vUvUnit);
22 // Decrease area of particles 'in-focus'.
23 float refValue = (1.f - vFocalDistance) * .5f;
24 float threshold = REF_VALUE_THRESHOLD * (1.f + vDepth);
25 float alpha = pow(value, vFocalDistance) * smoothstep(refValue - threshold, refValue + threshold, value);
26 if (alpha < uAlphaTestRefValue)
35 // Fade particles out as they get close to the near and far clipping planes
36 alpha *= smoothstep(.0f, uFadeRange.x, vDepth) * smoothstep(1.f, uFadeRange.y, vDepth);
38 oFragColor = vec4(vColor, alpha);