In Part 1, we created a Lens Distortion effect. In this part, we will add Chromatic Aberration to our Lens Distortion.
Chromatic Aberration is the splitting of light into multiple colours. We can get this effect at the corner and edges of a lens due to uneven thickness. It is noticeable in older analogue cameras, binoculars, etc. This effect can be achieved in software by slightly varying the distortion strength of red, green and blue channels separately.
Continuing from our previous part, let's move on to the next steps.
Distorting RGB channels Individually
Let's create three properties _LensDistortionOffset_R
, _LensDistortionOffset_G
, _LensDistortionOffset_B
. They will be used to offset the distortion strength of each channel.
Now, we can calculate distorted uv
for each channel as uvRed
, uvGreen
, uvBlue
. These values will be calculated in the same way as the calculation of uvDistorted
in Part 1. The only change is the addition of LensDistortionOffset
to _lensDistortionOffset
in each channel so that the channels vary from each other.
After getting the separated uv
, we can get the pixel value at the uv
position and set it as the pixel value of _finalColour
on the corresponding channel.
Out of bound will be detected when the uv
value of any channel lies outside of the 0-1 range. In this case, the pixel's value will be set to _OutOfBoundColour
.
(Optional) Adding Distortion to Textures Rendered by the Camera
If you are adding this distortion shader to the camera, you need to add post-processing to it as well. We will pass the image rendered by the camera to this shader.
Unity has OnRenderImage
event function that gets called once image render is complete. It consists of source
and destination
as its parameters. The rendered image is obtained as the source. This source needs to be processed and set as a destination. The shader can be applied using Graphics.Blit()
method.
A material is required to use this method, so create one with this shader. Also, add the ExecureInEditMode
attribute in the beginning to apply the shader when the game is not running.
Finally, attach this script to the camera.
Further Modifications
- Set
RenderType
tag toTransparent
if you want transparency. Doing this will allow the use of a transparent out of bounds colour, which will be quite useful when hiding extra space. - Calculating
distortionMagnitude
asdistortionMagnitude=sqrt(uv_centered[0]*uv_centered[0]+uv_centered[1]*uv_centered[1])
 in the first step in creating a Lens Distortion effect will generate a map in a circular pattern. This can be used to create distortion in circular lenses.
- Experiment with multiple types of equations for
smoothedDistortionMagnitude
. They can produce interesting results by distorting images in different patterns. It can be used to make lenses of different cross-sections.
This shader can be used to make concave and convex lenses, mirrors, fish-eye views etc.
A demo of this effect is shown below.
So concludes our lengthy tutorial. Leave a comment if you faced any difficulties.