#### Topic: Diamond textures

**Intro**

It was January 2015 when I saw this picture as an illustration on some business news.

It has some resemblance to a brilliant cut diamond, but anyone can see, that the the forms are irregular and at a closer look some some defining forms are actually missing entirely. Not to mention the "radiating" light, which is down right ugly.

Around the same time there was another one that obviously was not supposed to be a brilliant but, what is known as the "Passion cut". Thisone is much better than the one above, but this artist too had taken a few short cuts.

**The Brilliant shape**

So I started to look for information, how it really should be done. It turns out that there are several different standards, quality criteria and "schools" about brilliants and to my surprise all the details are not even fixed, but come with options. Eventually I chose, what is called the "Eulitz Brilliant" as my starting point. The Euliz Brilliant is said to have the main angles and dimensions mathematically calculated. I think I made a tiny mistake with the proportions but at least the angles should be as correct a possible.

I have already forgotten some details of how I eventually got the model done. Anyway, it's a PolyMesh. I'll try post something to the Modeling section some day.

**The Diamond material**

Making the diamond material was basically easy. Just set IoR to about 2.42 and there you go! Then I also gave the material a light cyan-blue transparent color. This is supposed to have the effect, that when a ray bounches a few times inside the diamond, it gets a little bit more colured every time and should produce some areas, that are more blueish than others on the render. *Eventually, when I was working on the colur separation ("rainbow effect" if you like), I ended up using 2.418 for the average IoR of the material.*

The high IoR alone does not make the diamond look very impressive. This one is with just a single layer texture with fixed reflectivity.

It could be a little bit improved by making a procedural texture that uses the ViewAngle to adjust the reflections but that is still not, what I was after. I knew I had to dig into physics books to get it right. At this point I put the project on docks for a couple of months.

**Developing the textures**

I knew that the diamond (or any transparent material) needs a different texture on the outside and the inside surfaces. Then is stumbled on a paper that had the reflectances drawn into curves in relation to the angle.* (I did not ask permission to use it, but it was in a public source....).*

Of course the ViewAngle in AoI is backwards compared to the angles used in the physics class. So I just drew custom curves, that somehow represent those. The first attempt to render was a bit of a disappointment. When I mapped the inside texture into the the model, it turned all black....

That was because the ViewAngles on the inside are negative. -- Once I got that corrected (set the ViewAngle to Abs), I knew I had something happening.

But I wanted to do it right.

I remembered this dicussion and there were some valuable tips in there, like that the ViewAngle actually puts out the cosine of the angle and not the angle of incidence (Theta_i) itself. That approach actually normalizes the value between -1 and 1, which is handier to use than ±pi/2 on the editor and also the cosine makes the colour slides on a sphere look much nicer.

Now I needed to turn the cosine into the angle. Unfortunately there is no arccos-function available. The available angle-function is basically an arctan-function where the tangent is expressed by two variables (a.k.a. atan2). Well' it' been over three decades since high school so...

Somehow I managed to dig out from my memory a couple of formulas that seemed relevant to this case: tan = sin/cos and the Pythagorean theorem. So after the mathematical work-out, the angle has to be:

**Theta_i = angle(sqrt(1-ViewAngle^2), ViewAngle)**

Phwefff... And then to work.

Now, the changing the cosine back into the angle actually turns, what was backwards, forwards again. So now the reflectance physics formulas can be used without modification. On the outside texture there is nothing special to it. On the inside the critical angle (Theta_c) needs to be defined. Of course the formula for Theta_c is based on sine, so I had to do the same thing as above, but the other way around. -- On the wrong side of this limit the equations would not work (produces "NaN").

The content of the procedural wiring is simply "If Theta_i > Theta_c, reflectance is 100%. Else use the formula." In Wikipedia the needed equations are scattered into several articles, so I went for my old physics books to be sure.

And then to render:

Now you'll notice that the there is a a general grayness on it and that light is leaking through the diamond. The grayness happens because the diamond is reflecting the partially illuminated plane under it, but the light leakage should not be possible. So let's try it with the caustics on.

Now of course the diamond reflects all the direct light back up and the area under it is in complete darkness. The obvious solution would be to use and environment color other than black. The problem there is that it can not be used with caustics. The photon map may or may not be computed -- (often it somehow works two times in a row, but not the third time and in any case only with relatively low photon counts.)

What you can do is to render an image with the environment and another one with the light and then combine them in an image processor....

**Separating colours**

As we know AoI can not separate colours by diffraction on it's own. However, it can do it with a little help.

In the Tools library, there is a script that can be modified to change the Index of Refraction of a material 'on flight' and the sub-images system in the movie renderer offers a handy way of combining several renders in different colours. Now all it takes is to change the color of the light synchronized with the changing of the IoR. Then I began to think, how to change the colours of several lights and the environment simultaneously and decided, that it was easier to leave the light as they are and use a colour filter instead. The filter is a small transparent sphere in the camera location, with the transparent color changing.

So far so good, but how exactly should the IoR change in relation to the colour? After some research it turned out that there if quite a bit of variation how each source translates wavelengths to colors. I chose to follow the scale that was used in Wikipedia and go from slightly infrared to slightly ultraviolet in the spectrum.

It also became obvious, that the as function of wavelength IoR behaves like a hyperbola (not on very long wavelengths but in the visible light area very much so).

Then I found a series of IoR:s for different wavelengths in **Crown Glass**. I normalized the wavelengths from 700 nm to 350 nm into 0.0 - 1.0 *(yes: turned the scale backwards just out of the habbit of having the red colour on the on the left end of the hue-bar)* and divided the IoR:s by the IoR for green color, which is nicely in the middle of the scale. Now the equation for the IoR is of the form **(b0/(2-x) + b1)**.

The **2** comes from the normalization of the scale: **700/(700-350)**. With a longer or shorter colour scale it would be something else. Then I wrote a script for **least squares** with the normalized data:

```
// BeanShell
import Jama.*; // I don't think this is necessary
// The equation: y = b0/(2.0-x) + b1
Matrix y = new Matrix(
new double[][]{
{0.996068152},
{0.997378768},
{0.998034076},
{1.0},
{1.00327654},
{1.007863696}});
Matrix X = new Matrix(
new double[][]{
{0.53030303, 1.0},
{0.573770492, 1.0},
{0.603448276, 1.0},
{0.636363636, 1.0},
{0.744680851, 1.0},
{0.853658537, 1.0}});
Matrix b = (X.transpose().times(X)).inverse().times(X.transpose()).times(y);
for (i=0; i<b.getArray().length; i++)
print (b.getArray()[i][0]);
```

This gave me **b0 = 0.03655** and **b1 = 0.9764** Then I only needed to multiply the equation by the average IoR of diamond. This should now work for any material just by changing the one number.

Now, where the IoR of a material can be directly controlled by a script, the parameters of a texture are controlled by the parameters track. There is one major annoyance with those: **The track forgets which parameters should be controlled, every time you open a texture or a material** of the object for editing. A part of this 'forgetfullness bug' was already fixed a few years back, but obviously it was a bit deeper than we thought at the time.

Now the IoR-Controller script controls the IoR-parameters of the textures as well. Unfortunately, effectwise, this seems to work against itself a bit, but that's, what the equations say.

Any way, then to rendering. A one second movie, with just one frame/second with 12 sub images. Now, as the colours from yellow to blue are actually compressed into the middle of the scale the outcome is very strongly tinted with red and blue.

Edit: I changed this picture into one where the colour separation worked. Despite the obvious problem with the general color you can actually see it.

For some reason the colour filtered sub images appear rather over exposed on the screen at render time, but the final picture is much dimmer. I compensated this in two ways: I made the filter amplify the light and then adjusted the light itself to find a nice level.

The compensation to the color should, of course, be done in the colour filter, but since at this point, I had given up the thought of using more than one light at the time, I took a shortcut and made the compensation to color of the light. The exact tone was calculated backwards from a supposedly gray area of the picture before.

Below I used a version of the material, where there is no blue color in it. There are two spot lights, one shadowless and one normal with 50/50 shares. The result is *basically* similar to having rendered the images separately and then blended.

And WHAT THE FOOOOK????

It is a perfectly beautiful gray scale picture of the darn diamond, but the colour separation that I programmed into it is missing entirely.

I need to get drunk. NOW!

EDIT: Added the texture picture.

EDIT: Note on the spotlight for the last picture.

EDIT: Added the reflectance link.

*Last edited by peteihis (September 28, 2015, 11:22 am)*

*...there's more to the picture than meets the eye...*