Thursday, September 17, 2015

Rendering Maya nHair Using Mental Ray

In the previous post, we saw how we can create an nHair system in Maya using FiberMesh in ZBrush. In this post, we will see how we can render the nHair system in Maya using Mental Ray and get as render like this.
nHair system rendered using Mental Ray.

Before we start, make sure that Mental Ray is loaded and that it is the active renderer. Here is my render at this point.
Figure 1. Render at the starting point.

1. Creating Physical Sun and Sky

We are going to light the scene using Mental Ray's Physical Sun and Sky system. (In 3ds Max, the equivalent is the Daylight System.) Creating physical sun and sky is easy:
  1. Open the "Render Settings" dialog.
  2. Go to "Indirect Lighting" tab (see Figure 2).
  3. Expand the "Environment" group.
  4. Click the "Create" button next to "Physical Sun and Sky".
Figure 2. Creating Physical Sun and Sky.
Here are what happened when we pressed the "Create" button:
  • Final Gather is automatically enabled (see Figure 3 left).
  • A directional light called "sunDirection" is created (see Figure 3 right).
Figure 3. Left: Final Gather is enabled. Right: a directional light is created.
There are more things happening behind the scene that we need to understand.
  • Let us start by selecting the "sunDirection" light.
    • The Attribute Editor shows the parameters of "sunShape" (see Figure 4 left).
    • Notice that "sunDirection" light is assigned a mia_physicalsun shader (see Figure 4 left). This is a Mental Ray light shader. It makes the directional light to behave physically correct during rendering.
  • Next, click the boxed arrow button on the right of "Light Shader" (see Figure 4 left).
    • The Attribute Editor now shows the mia_physicalsun parameters (see Figure 4 middle).
    • Notice that there are 5 parameters ("Multiplier", "Haze", "Red-Blue Shift", "Saturation", and "Horizon Height") highlighted in yellow. They are connected to the parameters of the physical sky shader (which we will inspect next). Remember that you can break any of these connections so that the sun shader uses a different parameter from the sky shader.
  • Click the "mia_physicalsky1" tab of the Attribute Editor (see Figure 4 right).
    • Notice that this shader uses the "sunDirection" light as its "Sun" parameter. This means you can create a light and assign it as the "Sun" of this sky shader.
    • Also, experiment setting the value of "Multiplier" to 2.0, then seeing the "mia_physicalsun2" tab. Confirm that "mia_physicalsun2" "Multiplier" value is also 2.0. The same connection applies to 4 other parameters as discussed above.
    • Make sure you set the "Multiplier" value back to 1.0 before proceeding.
Figure 4. More things happening behind the scenes. Middle: Mental Ray physical sun shader. Right: Mental Ray physical sky shader.
But that's not all. Select your render camera and see the Attribute Editor:
  • The physical sky shader, "mia_physicalsky1", is attached as "Environment Shader" (see Figure 5 left).
  • Another Mental Ray shader called "mia_exposure_simple1" is attached as "Lens Shader" (see Figure 5 left).



Figure 5. Mental Ray shaders attached to the camera. Left: the camera shape node. Right: the mia_exposure_simple shader.

We are particularly interested in the lens shader mia_exposure_simple:
  1. Click the boxed arrow icon on the right of "Lens Shader". The Attribute Editor will show the parameters for the mia_exposure_simple shader (see Figure 5 right).
  2. Notice that the "Gamma" value is 2.2.
  3. Render your scene.
    • My render looks like this.
Figure 6. Rendered image with Gamma value 2.2.
The rendered image looks deceptively alright, but it is actually overly bright because of incorrect gamma value.
  1. Set the "Gamma" value to 1.0.
  2. Render your scene again.
    • My render now looks like this.
Figure 7. Rendered image with Gamma value 1.0.

The shadow area looks more natural. However, the hair looks grainy and hard. We will now fix this.

2. Improving Sampling Quality

First, we will improve Mental Ray's sampling quality:
  1. Open "Render Settings" dialog.
  2. Go to "Quality" tab (see Figure 8).
  3. Expand "Sampling" section.
    • By default, the "Sampling Mode" is "Unified Sampling" and the "Quality" is set to 0.25 (see Figure 8 left).
  4. Set "Quality" value to 1.0.
  5. Render your scene.
    • The hair should look less grainy now, at the cost of longer render time.
Figure 8. Mental Ray Sampling parameters. Left: default settings. Right: my personal preference.
I personally find "Unified Sampling" mode make render time much longer than necessary; so I prefer to use "Legacy Sampling Mode" (see Figure 8 right). This mode gives me more control over the quality-render time trade-off. For example:
  • I set "Min Sample Level" to -1 & "Max Sample Level" to 1 for fast renders.
  • I set "Min Sample Level" to 2 & "Max Sample Level" to 3 for high quality renders.
I use the settings shown on Figure 8 right and here is my render.
Figure 9. Rendered image with improved sampling quality.
The hair still has grainy look to it, caused by hair shadow. We will fix this next.

3. Improving Hair Shadow

Select the light "sunDirection" and open the Attribute Editor:
  1. Expand the "Shadow" section (see Figure 10 left).
    • By default, the light uses ray traced shadows. The render scenes with hair, use depth map shadows instead.
  2. Check "Use Depth Map Shadows" (see Figure 10 right).
  3. Set the following parameters:
    • "Resolution": 4096;
    • "Use Mid Dist": unchecked;
    • "Filter Size": 4 (or higher; the higher, the more blurred the shadows are);
    • "Bias": 0.05.
Figure 10. Shadow settings.

Credit: I learned about hair shadow settings from the article "Set up hair self-shadowing" at Autodesk Knowledge Network. Read this article for more insight behind the values I set above.

Render your scene. My render looks like this.
Figure 11. Rendered image with improved hair shadows.
And that is it! You can now work on your lighting and get a nice hair render.