Creating a bokeh effect in a shader

Astronaut with orange bokeh shader effect applied

Learn how to create a bokeh effect in a GLSL shader.

Introduction

“Bokeh comes from the Japanese word boke (暈け or ボケ), which means ‘haze’ or ‘blur’. The ‘h’ was introduced to help English speakers pronounce the word correctly [BO – KEH]. The official definition translates as: “the way the lens renders out-of-focus points of light.”. “

What is Bokeh Effect in photography? | Adobe

Constants

First define some constants, starting with the value for PI.

Next define some constants for the following degree angles in radians: 0, 90, 180, 270.

Finally define some constants for the radius of the different circles.

Uniforms

We will now create some uniforms, which we will use to customise the effect outside of the shader.

First we need to pass in the image size.

Then we will create uniforms for the brightness and opacity of the bokeh circles.

We will pass in the time in seconds, and use it to animate the bokeh circles later on.

Finally we will pass in a tint value and use it to tint the bokeh circles. Orange by default.

Circle

We will need a function to get the distance to the centre of a circle, like so:

Bokeh circle

Next we will create another function and use it to create the bokeh circles.

The function takes the following arguments: position, offset, radius and an angle.

The position will be from the centre of the image. So we can use the offset to offset the position of the circle. The cos and sin functions are used to make the circle orbit it’s starting position. Hence why we pass in the angle added with the time in seconds. The speed value determines the orbit distance.

We create the circle using the Circle function. Then smooth it using the smoothstep function.

Finally we will adjust the opacity of the bokeh circles over time, so they fade in and out. Then return the circle distance value.

Pixel colour

Now we will get the texture pixel colour from inside the main function.

Aspect ratio

Next calculate the aspect ratio using the resolution uniform.

Position

Create a position vector from the centre of the image, and aspect ratio correct it.

Create circles

Now we will create the bokeh circles.

First we need a variable to hold the circle distance values, and a variable for the offset. The half size variable is half the distance from the centre of the image, to the edge of the image.

The bokeh circles can be created like so:

Each circle uses the same position and a different offset, based on the percentage of the half size variable. The small, medium and large variables control the size of the bokeh circles. And the radian values control the starting position for the orbiting.

Add more bokeh circles until you are happy, by playing with different offset, size and radian values.

Brightness

Use the brightness uniform, to scale the brightness of the circle distance totals.

Mix

Mix the pixel colour with the tint uniform, using the distance of the bokeh circles. The tint is multiplied vs the circle distance so that there is a gradient from the centre of the circle. The colour in the centre of the circle will be brighter than the edges of the circle. The opacity uniform will control the opacity of the bokeh circles vs the pixel colour.

Alpha

Finally we just need to set the output alpha. First clamp the circle distance value, so that it doesn’t go above 1.

Then set the output alpha to a mixture of the pixel alpha and the circle distance. Use the opacity uniform again to control the opacity of the bokeh circles vs the pixel alpha.

Conclusion

Thank you for reading this tutorial. Let me know in the comments section if you enjoyed it, or have any questions!

Leave a comment