in Maths, Science, Tutorial

The Mathematics of Thin-Film Interference

This post continues our journey through the Mathematical foundations of iridescence. This time, we will discuss a new way in which material can split light: thin-film interference. This is how bubbles (and car paint) get their unique reflections.

You can find the complete series here:

A link to download the Unity project used in this series is also provided at the end of the page.


In the previous posts, we have described how materials with certain repeated patterns exhibit iridescent reflections. Such a phenomenon is called diffraction grating. However, that is not the only way in which a material can split light into its components. Another one is called thin-film interference and occurs when a material is made out of multiple layers.

The diagram below shows a typical example of thin-film interference. Let’s imagine this to be a thin layer of water distributed onto a different surface. When a ray of light hits the water, it splits in two; a part is reflected back, while the rest goes deeper into the material. The latter ray is subjected to refraction, a phenomenon that causes light to “bend” when entering or exiting different materials.

Reflection and refraction can only happen where two different materials meet. For this reason, light continues to bounce inside the material until there’s energy left.

The reason why this phenomenon causes iridescent reflections to appear is, once again, due to the fact that light behaves like a wave. All those rays have travelled different distances; hence, they will reach the eye of the viewer with different phases. Under certain circumstances, some of those light rays will be in phase, amplifying each other. As a result, some colours will be boosted in the final reflection, while other will cancel each other out. Once again, iridescence is caused by constructive interference.

As you can see, there can be countless occasions for light to interfere. A calculation of that proportion is often unfeasible for rendering games in real-time. In this tutorial, we will focus on a simplified version of this problem, which only takes into account the interference of the first two rays:

❓ What about subsurface scattering?
The first diagram presented in this introduction indeed refers to subsurface scattering (or SSS). That is the process through which light travels inside a medium, to exit from a different point.

There are few important differences in the way thin-film interference and subsurface scattering appear. The first one is that thin-film interference causes a specular reflection, while sub surface scattering results in a diffuse one. This is because in SSS a single ray of light travels chaotically inside a material; when it finally exists, it has an arbitrary and unpredictable direction. In thin-film interference, instead, light is very well behaved and we can trace each ray deterministically.

Optical Path Difference

Like we did in the tutorial on diffraction grating, once again we have to calculate the difference in length travelled by the two reflected rays. However, there are a couple of important difference that needs to be highlighted. Diffraction grating is a diffusive phenomenon: rays are scattered in all directions. In that context, it made sense for an incoming ray of light from a light direction L to hit a viewer looking at an arbitrary view direction V. Thin-film interference is a specular phenomenon. Surfaces are perfectly smooth, and rays are reflected with the same incident angle.

Another issue is that rays are travelling through different media. This has a direct effect on their phase, as light travels at different speeds in different media. This forces us to rely on a quantity called optical path difference, which (loosely speaking) measures the phase offset of two light rays. For rays travelling through the same medium, the optical path difference is equal to the difference in length of their paths. For different media, we have to take into account their refractive index. This is a property that determines how much light “bends” when it enters into or exits from a material. The refractive index of empty space is equal to 1; for air is 1.000293 and for water is 1.333.

Looking at our diagram above, we can see that the grey parts of common to both rays. The first ray travels the distance \overline{AD} in a medium with refractive index n_1. The second ray travels the distance \overline{AB}+\overline{BC} in a medium with refractive index n_2. The optical path difference is given by:

    \[OPD = n_2 \left(\overline{AB}+\overline{BC}\right) - n_1  \overline{AD}\]

Path Length of the Second Ray

Let’s start by calculating the path length of the second ray, since is the easier one. It is easy to see that both \overline{AB} and \overline{BC} have the same length. Also, the \widehat{ABE} is a right triangle. We assume to know the thickness of the second layer, d, which is equal to the side \overline{BE} of the triangle.

If you are unfamilair with trigonometry, the following diagram should help you understanding how to calculate the length of all the unknown sides of the \widehat{ABE} triangle: By refreshing your knowledge of trigonometry, we have that:


    \[\overline{AB} = \overline{BC} = \boxed{d \sec{\theta_R}}\]

    \[\overline{AB}+\overline{BC} = 2 \boxed{d \sec{\theta_R}}\]

Path Length of the First Ray

Calculating the length of \overline{AC} is easy, and we have done it already in The Mathematics of Diffraction Grating. In that post we concludede that:

    \[\overline{AD} = \boxed{\overline{AC}} \sin{\theta_L}\]

By working on the \widehat{ABE} triangle, we can also find the length of its other side \overline{AE}:

    \[\overline{AC} = \overline{AE} + \overline{EC}\]

    \[\overline{AE} = \overline{EC} = d \tan{\theta_R}\]

    \[\overline{AC} = \boxed{2 d \tan{\theta_R}}\]


    \[\overline{AD} = \boxed{ 2 d \tan{\theta_R}} \sin{\theta_L}\]

Path Difference

To sum up:

    \[OPD = n_2 \boxed{\left(\overline{AB}+\overline{BC}\right) }- n_1 \boxed{ \overline{AD}  }=\]

    \[= n_2 \boxed{2 d \sec{\theta_R}} - n_1 \boxed{2 d \tan{\theta_R} \sin{\theta_L}} =\]

    \[=2d \left(      n_2  \sec{\theta_R} - n_1 \tan{\theta_R} \sin{\theta_L}    \right)\]

Expanding with the following trigonometric identities \sec{\theta_R} = \frac{1}{\cos{\theta_R}} and \tan{\theta_R} = \frac{\sin{\theta_R}}{\cos{\theta_R}}:

    \[OPD=2d \left(      n_2  \boxed{\sec{\theta_R}} - n_1 \boxed{\tan{\theta_R}} \sin{\theta_L}    \right)=\]

    \[= 2 d \left ( n_2 \boxed{\frac{1}{\cos{\theta_R}}} - n_1 \boxed{\frac{\sin{\theta_R}}{\cos{\theta_R}}} \sin{\theta_L} \right) =  \]

    \[= \frac{2d}{\cos{\theta_R}} \left( n_2 - n_1 \sin{\theta_R} \sin{\theta_L } \right)\]

To progress in this expansion even further, we need to mention a foundamental equation in optics, called the Snell’s law. It connects the refractive indices to the incident and reflected angles:

    \[n_1 \sin{\theta_L} = n_2 \sin{\theta_R}\]

From which we can extract:

    \[\sin{\theta_L} = \frac{n_2}{n_1} \sin{\theta_R}\]

Substituting \sin{\theta_L} and grouping by n_2:

    \[OPD = \frac{2d}{\cos{\theta_R}} \left( n_2 - n_1 \sin{\theta_R} \boxed{\sin{\theta_L }} \right) =\]

    \[= \frac{2d}{\cos{\theta_R}} \left( n_2 - \not{n_1} \sin{\theta_R} \boxed{\frac{n_2}{ \not{n_1}} \sin{\theta_R}} \right) =\]

    \[= \frac{2d}{\cos{\theta_R}} \left( \boxed{n_2} - \sin{\theta_R} \boxed{n_2}\sin{\theta_R} \right) =\]

    \[= \frac{\boxed{n_2} 2 d}{\cos{\theta_R}} \left( 1 - \sin^2{\theta_R} \right)\]

From Pythagora’s Theorem:

    \[\cos^2{\theta_R} + \sin^2{\theta_R} = 1\]

    \[1 - \sin^2{\theta_R} = \cos^2{\theta_R}\]

Substituting and simplifying:

    \[OPD= \frac{n_2 2 d}{\cos{\theta_R}} \left(\boxed{1 - \sin^2{\theta_R}}\right) =\]

    \[= \frac{n_2 2 d}{\not{\cos{\theta_R}}} \boxed{\cos^{\not{2}}{\theta_R}} =\]

    \[= n_2 2 d\cos{\theta_R} = OPD\]

❓ How come the diffraction grating equation was not derived using the OPD?
In Part 5 of the tutorial on Iridescence, The Mathematics of Diffraction Grating, we have seen something very similar. We calculated the path difference between two rays, to find out which wavelength interfered constructively with each other. In that tutorial, however, there is no mention of the optical path difference. There is actually no mention of the refractive index!This is because both

This is because, in diffraction grating, both rays are travelling the same medium: air. Following the current approach proposed in this post, the corrected diffraction grating equation becomes:

    \[\sin{\theta_L}-\sin{\theta_V} = \frac{n \cdot w}{d \cdot n_{air}}\]

However, as we have discussed before, n_{air} = 1.000293. For the purposes of this tutorial, we can assume n_{air} to be exactly 1.

Yes, this also means that diffraction grating appears different in different media

❓ The two light rays are exiting the material from different points!
In this derivation we are making a very important simplification. We assume that the two light rays are actually coming out from the same point.

As it can be seen from the first diagram, this is clearly not the case. The second ray exits the material at a different point, so it might not be close enough to interfere with the first one. While we have indeed simplified the problem, this only has a marginal effect.

The first ray does not interfere with the first one, although it will with another ray that hit the material at an earlier time. This can be seen clearly in the following diagram:

Two incident rays of light (green and red) hit the material. The two reflected green rays do not interfere with each other; similarly, the two reflected red rays do not interfere with each other. However, the first green ray and the second red ray do exit the material from the same point. Hence, they are close enough to interfere.

By reasoning like this, we can assume that all the reflected rays generated by a single ray interfere with each other. Even if they actually exit the material at different points.

While this works relatively well for materials that are flat, it will cause inaccuracies on curved or uneven surfaces. However, such approximation errors are negligible when compared with the fact that we are only considering the interference between two rays.

Phase Shift

Like we discussed in The Mathematics of Diffraction Grating, when the OPD between two light rays is an integer multiple of a wavelength, that wavelength is amplified by constructive interference. We could be tempted to simply derive the following condition:

    \[n_2 2 d  \cos{\theta_R} = n \codt w\]

with n being an integer number. However, this would be a mistake. There is another optical phenomenon that we have not encountered before in this tutorial: phase shift. When a ray of light bounces off a medium, its wavelength can be subjected to a phase shift of 180 degrees. This will happen to all reflected rays travelling from medium A to medium B, when n_A < n_B.

If we take the example of a soap bubble, the first ray to be reflected off the surface film will indeed be subjected to a phase shift since n_{air} < n_{film}. This means that the phase of the first ray will be shifted by 180 degrees.

Before changing our equation, however, we need to check whether a similar phase shift occurs to the second ray or not. The only moment in which the second ray is reflected is when it crosses the film barrier. However, n_{film} > n_{air}, so no phase shift occurs. This means that, in the case of a soap bubble floating in the air, only the first ray is subjected to a phase shift.

As a result, we have to alter our equation to take this into account:

    \[n_2 2 d \cos{\theta_R} = \left(n + \frac{1}{2}\right) \codt w\]

We will see, in the next part of this tutorial, an efficient way to do this procedurally based on the refractive indices of the media.

❓ How come the diffraction grating equation did not take into account the phase shift?
As discussed before, we are not interested in the phase shift itself. What we really care is whether both light rays are in phase or not.

In diffraction grating, both rays bounce off the same material in the same way. This means that if a phase shift occurs, they will both experience it. Which also means that it is not going to affect the final phase.


This tutorial introduces the Mathematical foundations of the optical phenomenon known as thin-film interference. In the next post, we will see how we can actually implement that in a Shader.

You can find the complete series here:

You can download the Unity package for the CD-ROM Shader and Thin-Film Shader effects on Patreon.

💖 Support this blog

This website exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.

Patreon Patreon_button

📧 Stay updated

You will be notified when a new tutorial is released!

📝 Licensing

You are free to use, adapt and build upon this tutorial for your own projects (even commercially) as long as you credit me.

You are not allowed to redistribute the content of this tutorial on other platforms, especially the parts that are only available on Patreon.

If the knowledge you have gained had a significant impact on your project, a mention in the credit would be very appreciated. ❤️🧔🏻

Write a Comment



  • Car Paint Shader: Thin-Film Interference - Alan Zucconi September 6, 2022

    […] post completes the journey started in The Mathematics of Thin-Film Interference, by explaining how to turn the equations previously presented into actual shader […]

  • Understanding Diffraction Grating - Alan Zucconi September 6, 2022

    […] Part 9. The Mathematics of Thin-Film Interference […]