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