Occlusion

Ambient Occlusion

Occlusion is a fast and simple method to simulate the effects of global illumination. Occlusion is the extent to which the area above a point is covered by other geometry. This is achieved by tracing a number of probe rays inside the hemispherical area above the point and testing to what extent this region is blocked.

Typical use of this shader is ambient occlusion, where the shader is used to scale the contribution of ambient lighting (which in turn may come from a diffuse environment map shader). For this use the shader should be assigned to the ambient parameters of materials like mib_illum_phong.

Another use of occlusion is reflective occlusion, where the shader is used to scale the contribution from a reflection map. For this use, the shader is generally plugged into an environment or reflection shader slot of a material, and the actual environment map image is plugged into the bright parameter. An alternative is to allow the return value of the occlusion shader to modulate the strength attribute of a reflection map shader.

A third use is to create files for external compositing, where the occlusion shader is assigned as the primary material shader on every object in the scene. The output can be used to modulate other render passes to achieve proper compositing in post-production.

The shader may also be used as a light shader. The light source must be an area light of the user type. This generates an "ambient" lightsource with built in occlusion.

This shader only depends on base ray tracing features provided by mental ray, and does neither use nor benefit from advanced computations like final gathering or global illumination.

mib_amb_occlusion
    color "mib_amb_occlusion" (
        integer         "samples"            default 16,
        color           "bright"             default 1 1 1 1,
        color           "dark"               default 0 0 0 0,
        scalar          "spread"             default 0.8,
        scalar          "max_distance"       default 0,
        boolean         "reflective"         default off,
        integer         "output_mode"        default 0,
        boolean         "occlusion_in_alpha" default off
        # Version 2 parameters
        scalar          "falloff"            default 1.0,
        integer         "id_inclexcl"        default 0,
        integer         "id_nonself"         default 0,
    )
    version 2
    apply texture, light
samples
is the number of probe rays that will be sent. More rays yield a smoother image. Occlusion mapping will never be as smooth as well-tuned final gathering or photons, but is computationally cheap.
bright
is the color used when no occluding objects are found.
dark
is the color used when total occlusion occurs. In most practical cases it is set to black. For partial occlusion, a gradual mix between the bright and darkcolors is returned.
spread
defines how large an area of the hemisphere above the point is sampled. The value defines a cone around the sampling direction which is narrower for small values and wider for large values. It ranges from 0.0, for a cone that is a single direction, to 1.0 for a cone that covers the entire hemisphere.
max_distance
is the range within which geometry is probed. If it is zero, the entire scene is sampled. If it is a nonzero value, only objects within this distance will be considered (which makes sampling much faster) where objects outside this range do not occlude at all and objects that are closer occlude more strongly as the distance approaches zero.
reflective
if turned off sampling is performed in a cone area based around the surface normal. If reflective is on, the samples are instead distributed around the reflection direction. This generates reflective occlusion which can enhance the realism of reflection mapping greatly.
output_mode
defines what the returned color is: 0 enables standard occlusion behaviour; 1 enables environment sampling. This changes the behavior of the shader slightly. As directions are probed for occlusion, the current environment is also sampled and weighted based on how occluded that particular direction is. In mode 1 the output of the function is the gathered weighted environment colors multiplied by the bright color, and as a convenience the dark color is added.
Setting output_mode to 2 enables bent normals. The average unoccluded world space normal direction is calculated and returned encoded as a color where red is x, green is y and blue is z. Setting output_mode to 3 is the same as 2 but the normals are encoded in the camera coordinate space instead.
occlusion_in_alpha
if turned on, the scalar occlusion value is put in the returned colors alpha component, regardless of what output_mode is set to. The other color components remain as before.
falloff
only matters when max_distance is nonzero and defines the speed at which the occlusion is attenuated by distance. Technically, it is a power function applied to the normalized distance from 0 to max_distance. The default value of 1.0 is a linear falloff. Values below 1.0 makes the falloff more rapid. Practically this means that for lower values occlusion is stronger for short distances (in small corners and crevices) and softer over larger distances.
id_inclexcl
defines an object instance label number to be included (or excluded) from being considered occluding by the shader. Set to 0 the parameter has no effect. A positive integer means that only object instances with a label that matches this number will cause occlusion. A negative reverses the condition and includes all object instances except those with a matching label.
id_nonself
works similar to id_inclexcl in that it is an object instance label determining which objects do not self-occlude. Set to 0 it has no effect. Set to a positive integer will prevent any object with that label from occluding other objects with the same label. This is useful for "matte" or "stand-in" objects that represents existing geometry in a photographic background since any inter-occlusion between such objects are already present in the background photo. Simple exclusion by id_inclexcl cannot be used in this case because occlusion cast by "stand-in" background geometry onto foreground geometry and vice versa is both desired - only background-to-background occlusion needs to be prohibited.

Fast Occlusion

This shader is an alternative version to mib_amb_occlusion with similar funcionality, but it is fully utilizing the ambient occlusion facilities built into mental ray. This offers optimal rendering performance and the possibility to benefit from the ambient occlusion cache, if enabled.

Note, this shader can not be used as a light shader.

mib_fast_occlusion
    color "mib_fast_occlusion" (
        integer "samples"            default 0,
        color   "bright"             default 1 1 1 1,
        color   "dark"               default 0 0 0 0,
        scalar  "spread"             default 0,
        scalar  "spread_exp"         default 1,
        scalar  "max_distance"       default 0,
        boolean "reflective"         default off,
        integer "output_mode"        default 0,
        boolean "occlusion_in_alpha" default off,
        integer "cache_points"       default 0
    )
    version 1
    apply texture
samples
is the number of probe rays that will be sent. More rays yield a smoother image. If the ambient occlusion cache is enabled, this attribute will be ignored: the number of points to use for cache interpolation is controlled by the cache_points attribute, see below.
bright
is the color used when no occluding objects are found and dark is the color used when total occlusion occurs. In most practical cases it is set to black. For partial occlusion, a gradual mix between the two colors is returned.
spread
defines how large an area of the hemisphere above the point is sampled. Unlike the mib_amb_occlusion shader, this value is the cosine of the angle between the normal and the maximum direction. Its value ranges from 0.0 for a cone that covers the entire hemisphere to 1.0 for a cone that is a single direction.
spread_exp
is the exponent of the spreading angle. It controls how the rays shot from the sampling point concentrate around the normal. A higher value means more density around the normal.
Currently not supported by mental ray but provided for future extensions.
max_distance
is the range within which geometry is probed. If it is zero, the entire scene is sampled. If it is a nonzero value, only objects within this distance will be considered (which makes sampling much faster) where objects outside this range do not occlude at all and objects that are closer occlude more strongly as the distance approaches zero.
reflective
if turned off sampling is performed in a cone area based around the surface normal. If reflective is on, the samples are instead distributed around the reflection direction. This generates reflective occlusion which can enhance the realism of reflection mapping greatly.
output_mode
defines what the returned color is: 0 enables standard occlusion behaviour; 1 enables environment sampling. This changes the behavior of the shader slightly. As directions are probed for occlusion, the current environment is also sampled and weighted based on how occluded that particular direction is. In mode 1 the output of the function is the gathered weighted environment colors multiplied by the bright color, and as a convenience the dark color is added.
Setting output_mode to 2 enables bent normals. The average unoccluded world space normal direction is calculated and returned encoded as a color where red is x, green is y and blue is z. Setting output_mode to 3 is the same as 2 but the normals are encoded in the camera coordinate space instead.
occlusion_in_alpha
if turned on, the scalar occlusion value is put in the returned colors alpha component, regardless of what output_mode is set to. The other color components remain as before.
cache_points
is used to control the number of ambient occlusion points to lookup for interpolation in the ambient occlusion cache. If the latter is not enabled this attribute will be ignored. A value of 0 means the number of ambient occlusion points will be determined by mental ray settings, any value greater then 0 will override the setting for the current object. Values smaller than 64 give more precise results but artifacts can appear. Higher values tend to produce a blurry image.

Ambient Occlusion using Final Gathering

The mental ray final gathering algorithm always computes a scalar occlusion value. This utitily shader simply returns this value as a grayscale result.

mib_fg_occlusion
      color "mib_fg_occlusion" (
	  color "result_when_fg_is_off" default 1 1 1 1
      )
      version 1
result_when_fg_is_off
becomes the return value when final gathering is off. When final gathering is on, the occlusion value is returned as a grayscale value, and the parameter is never evaluated at all or used in any way.
Hence, connecting mib_amb_occlusion to the result_when_fg_is_off parameter, the result will be occlusion calculated by final gathering when it is on, and occlusion calculated by mib_amb_occlusion when final gathering is off.

Bent Normals

"Bent normal" is a term used for the average un-occluded direction vector from a surface point. For completely un-occluded surfaces this is the same as the normal vector, but for surfaces occluded by other geometry it points in the direction in which the least amount of occluding geometry is found. Bent normals are used as an acceleration technique for ambient occlusion, allowing very fast rendering that look like global illumination or final gathering lit by an environment at a small fraction of the rendering time.

Ambient occlusion as done by the mib_amb_occlusion shader is a ray tracing technique that casts potentially large numbers of probe rays to determine to which extent a surface point is occluded. The speed of this operation depends on the number of rays (samples), the reach of the rays (max_distance) and the complexity of the scene.

When rendering an animation (or multiple views of the same scene), any object that does not move, does not change shape, or has no moving occluding object nearby will yield the same result for every frame. Therefore one can "bake" (render to a file) the ambient occlusion solution once in a first rendering pass, and re-use this result in subsequent rendering passes for any number of frames, with potentially huge performance gains. If one also "bakes" the average un-occluded direction (the bent normal) to a texture, the entire process of lighting the object based on an environment is moved to this second rendering pass, without having to trace a single ray.

By setting the output_mode parameter to 2, 3, or 4 on the ambient occlusion shader mib_amb_occlusion, bent normals are returned with the vector being encoded as a color where x is red, y is green, and z is blue. If occlusion_in_alpha is enabled, the scalar occlusion value is returned in the alpha channel. This color can be baked into a texture, for example with the help of mib_lightmap_write by connecting mib_amb_occlusion to its input parameter and rendering.

Once the texture file is generated, mib_bent_normal_env can be assigned to a surface shader (for example to the ambient parameter of mib_illum_phong to look up an environment to light the object based on the baked bent normal texture, which is placed in the bent_normals parameter of mib_bent_normal_env.

This allows an extremely low-overhead simulation of global illumination-like effects for rigid objects and is especially suitable for animations. The technique does not work on deformable objects since their occlusion, shape, and normals can vary from frame to frame. For deformable objects, it is better to apply the occlusion shader directly, without baking its output.

mib_bent_normal_env
    color "mib_bent_normal_env" (
        color           "bent_normals"        default 0 0 0 1,
        boolean         "occlusion_in_alpha"  default on,
        color           "occlusion"           default 1 1 1,
        scalar          "strength"            default 0.2,
        shader          "environment",
        integer         "coordinate_space"    default 2,
        integer         "env_samples"         default 1,
        scalar          "samples_spread"      default 0.0,
        transform       "matrix"              default 1 0 0 0
                                                      0 1 0 0
                                                      0 0 1 0
                                                      0 0 0 1
    )
  
bent_normals
is the bent normal data output by the occlusion shader. It should be baked to the object. This is usually attached to a node that does the texture lookup such as mib_texture_lookup. The texture mapping used for generating the texture must match with the one used for lookup.
occlusion_in_alpha
specifies whether the scalar occlusion value is already baked into the alpha channel of the "bent normals" pass. This uses slightly less memory but many "bake to texture" functions of OEM integrations of mental ray do not bake the alpha channel.
occlusion
is the separate occlusion channel, to be used if it was not already baked into the alpha channel.
strength
is a simple scalar multiplier for the effect.
environment
is the environment shader to look up. If none is provided, the environment from the material is used. To avoid image noise, it is often desirable to use a separate, highly blurred environment map with no high-resolution detail.
coordinate_space
defines the coordinate space of the bent normal:
The coordinate space numbers match the output_mode parameter values of the ambient occlusion shader, where 2 is world space, 3 is camera space, and 4 is object space.
env_samples
is the number of samples to take from the environment map. Normally, a highly-blurred environment map is used where only one sample is sufficient, but it is possible to multi-sample the environment map by setting a non-zero value.
sample_spread
is the spread factor for each individual environment sample. The range is from 0.0 (infinitely thin rays -- a single sample) to 1.0 (the entire hemisphere is sampled).
matrix
is an explicit transformation matrix applied to the normal data when coordinate_space is 1. This allows arbitrary transformations.

Copyright (©) 1986-2009 by mental images GmbH