Physically accurate depth of field calculated from a vertex position.
Based on a post from Martins Upitis and with the help of this thread.
#pragma glslify: dof = require('glsl-dof')
void main() {
vec4 finalPosition = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
float depth = finalPosition.z / finalPosition.w;
float focalDepth = 1.5;
float d = dof(depth, focalDepth);
}
Use npm to install and glslify to consume the function in your shaders.
npm install glsl-dof
Default values: focalLength
= 100.0, fstop
= 2.0, near
= 0.01 and far
= 100.0.
MIT, see LICENSE.md for details.