Bokeh computation with BokehShader2

I have just started learning how to use and create shaders in ThreeJS and, while going through some shaders in the example folder, I have come across BokehShader2. Inside the shader, blur is computed using a formula, but I dont really understand where this formula comes from and what it really computes. Here is some code:

“float f = focalLength; // focal length in mm",
"float d = fDepth*1000.0; // focal plane in mm",
"float o = depth*1000.0; // depth in mm",

"float a = (o*f)/(o-f);",
"float b = (d*f)/(d-f);",
 "float c = (d-f)/(d*fstop*CoC);",

"blur = abs(a-b)*c;"

What I don’t understand is what c is and how I can then use it to compute the blur. Can anyone help me out?

1 Like

Maybe this post will help you a bit - it’s not exactly the same code, but shows how both CoC and blur are calculated in a simple bokeh shader.


Hey, thank you for responding me :slight_smile: unfortunately, while the post is very informative, it does not revolve around a fixed CoC limit and I dont understand where the formulas I posted come from. I think it is somehow relatable to the approach used in the post, but I have been looking around to understand what my code is actually computing and I didnt manage to find any matching real-world formula :frowning:

The implementation of BokehShader2 is based on Martins Upitis code from this article:

DOF implementations vary and you will have problems if you read one resource and study the code of a different one.

Yes, I can confirm. In fact the general idea is very different and that is why I have not been able to find any reference to those formulas. I believe it has to be somewhat of a blur factor, but I haven’t been able to find anything confirming it