2018-01-12: GPU users, please see here for a GPU compatible version including all materials.
This Open Shading Language script implements a calculation of the Fresnel reflectance for metals and dielectrics, which may have complex indices of refraction (also known as n and k values, see complex refractive index). Here, n stands for the 'classic' index of refraction and k stands for the extinction coefficient, which describes absorption. This combination accurately describes the interaction of light with many types of materials, including glass-like materials, metals and car paints.
The main feature, next to true Fresnel calculations for metals, is that the base layer of the material may be coated with one or two thin, possibly absorbing, layers in which interference can occur. Light can interfere with itself, leading to a variety of rainbow-colored phenomena seen in for example oil spills on the street and soap bubbles. See the images for examples rendered with the script (bubbles lit by and background part of free HDR image pack by www.hdrlabs.com [not included]). The previews of single materials are rendered with the provided .blend file.
The script implements the calculation, whereas the blend file contains 31 materials that I have made with the script. It's just a small selection of the possibilities, and intended to get the user started. For example, refractiveindex.info lists combinations of n and k for many materials, which allows one to recreate all those materials listed.
To clarify the license: the code is CC BY-NC-SA 4.0. Feel free to use the output of the code for any purpose.
Some helpful hints:
Choosing values for n and k
The shader allows you to enter the values to use for the refractive index (n) and the extinction coefficient (k) for the red, green and blue channels. These values are often listed as a function of the wavelength of the light, for example at refractiveindex.info. Wavelengths that are perceived as red run from something like 625 nm to 670 nm, green is about 520 nm to 550 nm and blue is about 440 nm to 470 nm. Which wavelength you choose to use for red, green and blue depends on your own preference; there is no wrong choice, and the shader will behave physically correct regardless. Of course for different choices of n and k at slightly different wavelengths, the material is likely to change appearance slightly.
Optimized values of n and k for metals
So-called spectral renderers use the tabulated n and k values for each wavelength in the visible light spectrum to calculate what a material should look like. Cycles on the other hand only uses three color channels, one for red, one for green and one for blue. You might wonder what n and k values would best approximate the results of a spectral renderer. I have optimized the values of n and k for the metals that are included in the blend file to look as close as possible to the output from a spectral renderer, when the metal is illuminated with white light.
Documentation See the original thread here.