Mental Ray Colorspace Shader

Lately (well, it was last November to say the truth) I’ve been playing quite a lot with colorspaces and gamma correction.

Different Colorspaces

Fig. A

In the last production I worked on we were asked to deliver sequences for a Rec709 colorspace.
While I was quite used to work and set up a proper linear workflow for a sRBG or gamma 2.2 (or at least I thought so) I was missing some points that become quite obvious when changing the final colorspace.
In figure A you can see a graph with the different colorspaces curves. (click to enlarge)

When you want to linearize your textures, you have to think about your final colorspace and linearize them taking out the gamma that you are going to apply back when finalizing the image (or in the render view  talking about maya).

So, if you are going to save the final images as gamma 2.2, you can put a gamma node at 1/2.2 (0.4545) just after your texture or use the embedded color management.

The best way (as explained here) would be thinking about the colorspace workflow since texturing, painting the textures in the right colorspace and saving them as linear directly from your painting app.
Then you will still have to correct the color swatches for your shaders and lights (talking about mental ray in maya here) but at least the textures will be already ok.

Fig. B

It is quite noticeable that all the colorspaces are different but, while the difference between sRGB and gamma 2.2 is quite small, the difference between rec709 and gamma 1.95 has some big differences in the dark tones.
This can led to problems if a texture is made, for example, of a deep brown because the bad conversion (using a gamma node at 1/1.95 to linearize and then putting a rec709 curve in the viewer) will change the Hue and the saturation, as shown in figure B and C.

As far as I know the color management is still quite buggy (I was still using Maya 2011) but it could be a good option if you want to convert the textures with the right math… unless you have already converted all you textures to map because, in that case, Maya will think that they are already linear and will skip the conversion (thank you Maya!).

Fig. C

After a lot of research and headaches trying to get the right colors or trying to get rid of dark spots on the renders, I ended up writing a Mental Ray shader to take care of the conversion (Fig. D).
You can download the shader from and it should appear under the Mental Ray Textures tab.

It is like a gamma node, but you can choose between 4 different colorspaces as inputs (sRGB, rec709, gamma 2.2 and gamma 1.8) and let it make all the math for you, giving back a linear texture/color as output.

I also added a gain and an offset if you are converting existing shading networks and you need to map a different gain or offset on the file node.

Fig. D

The shader is really simple and fast, it has been used in production for more than 8 months without any problem and can be used to convert textures, colorswatches or whatever you need. It also works with Maya embedded pass system 🙂

The shader have been compiled for linux64 with Maya2011 and for win64 and mac64 with maya2013. If anyone could compile it for other versions and platforms I would be really happy to add it to the archive and give credit.

A big thank goes to Pixero (website) for all the cool shaders he did and for releasing the source code. It was a very good starting point.
Another big thank goes to Andrew Hazelden (website), for helping me with the compilers. It was a huge help from you.


About this entry