Ray Type Switching Shaders

Ray Type Switching Shaders

Generic Switchers

The mip_rayswitch and mip_rayswitch_advanced utility shaders allows different types of rays to return different results. There are many cases in which this can be useful, including but not limited to:

The mip_rayswitch shader is a simple shader that accepts a set of other colors (generally set to other subshaders) to apply for certain classes of rays.

declare shader "mip_rayswitch" (
        color  "eye",
        color  "transparent",
        color  "reflection",
        color  "refraction",
        color  "finalgather",
        color  "environment",
        color  "shadow",
        color  "photon",
        color  "default"
    )
    version 1
    apply material, texture, environment
end declare

For primary rays, eye sets the result for eye rays.

For secondary rays, transparent is the result for transparency rays, reflection for reflection rays and environment for environment rays.

The finalgather is the result for final gather rays as well as child rays to final gather rays.

Similarly, shadow is the result for shadow rays, photon catches all photon rays.

Finally, the default is the result for any other ray type. It is not a fall-through default, however, each of the above returns their respective result whether connected to a shader or not (i.e. generally 0 0 0 0 black).

If one wants fall-through defaults, one must use the advanced version of the shader:

declare shader "mip_rayswitch_advanced" (
        shader "eye",
        shader "transparent",
        shader "reflection",
        shader "refraction",
        shader "finalgather",
        shader "environment",
        shader "any_secondary",
        shader "shadow",
        shader "photon",
        shader "default"
    )
    version 1
    apply material, texture, environment
end declare

This shader works very similar to mip_rayswitch, but instead of accepting inputs of type "color", it accepts inputs of type "shader".

While this no longer allows assigning a fixed color directly, it instead allows fall-through defaults.

Each of the parameters works in a similar way: eye is the shader for eye rays, transparent for transparency rays, reflection for reflection rays, refraction for refraction rays, etc.

The difference is if one of these shaders are not specified, one of the fall-through cases take over. If either of the specific secondary ray type shaders are not specified, and a ray of that type arrives, the any_secondary acts as a catch-all for all the secondary ray types not explicitly set.

Similarly, the default parameter works as a catch all for every unspecified shader above it.

Environment Switcher

A classical issue one runs into with mental ray is that there is simply a single concept of "the environment", whereas one often, in practical use, wants to separate the concept of a background to that of an environment.

This shader accomplishes exactly that:

declare shader "mip_rayswitch_environment" (
        color   "background"  default 0 0 0 0,
        color   "environment" default 0 0 0 0,
    )
    apply texture, environment
    version 1
end declare

The shader returns background for any eye ray, transparency ray that is a child of an eye ray, or any refracted ray that travels in the same direction as said transparency ray would (i.e. rays of type miRAY_REFRACT, but that was refracted by an IOR of 1.0 and is a direct child of an eye ray).

For any other type of rays (reflection, refraction, final gathering etc.) the shader returns the environment color.

The shader is intended to be used as the camera environment, but will function anywhere in a shading graph as a ray switching node, in many cases where one need to distinguish between "primary" and "secondary" rays.

For example, this is the ideal shader to switch between primary- and secondary rays to support the "best of both worlds" usage of mip_matteshadow described on page Best-of-both-worlds.

Render Stage Switcher

Sometimes one desires to use different colors or subshaders depending on where in the rendering pipeline mental ray is. For example, one may wish to make a certain material completely opaque to final gather rays, or for a light to have a different color while emitting photons, or similar.

declare shader "mip_rayswitch_stage" (
        color "unknown",
        color "main_render",
        color "finalgather_precomp",
        color "ao_precomp",
        color "caustic_photons",
        color "globillum_photons",
        color "importon_emit",
        color "lightmapping"
    )
    version 1
    apply material, texture, environment
end declare

The parameters all work in a similar way to all the other switcher shaders.

unknown is for any "unknown" stage. This should normally never be called, but exists as a safety precaution to safeguard against any new rendering stages introduced in future mental ray versions that are unknown at this time.

During the normal tile rendering pass, the shader returns the value of the main_render input.

During finalgather precomputation phase, the shader returns the value of finalgather_precomp.

During ambient occlusion precomputation phase, the shader returns the value of ao_precomp. Note that mental ray 3.7 does not call shaders at all during this phase, so this will never be called - but future versions of mental ray may act differently.

During caustic and global illumination photon tracing, the values for caustic_photons and globillum_photons inputs are used respectively.

During the importon emission phase, the value from importon_emit is used.

Finally, during the light mapping preprocessing phase (used, for example, by the subsurface scattering shaders) the value in lightmapping is used.