We want to implement trace sets in an example shader. The shader has two string attributes: reflection_set and shadow_set. These define the name of the trace sets against which reflection and shadow rays will be traced.

There are two types of trace sets:

  • When a trace set is exclusive, rays are traced against all geometry except the tagged nodes.
  • When a trace set is inclusive, rays are traced against tagged nodes, but also against nodes that are not tagged at all (see example .ass below).

By convention, our shader will consider all trace sets as inclusive, except if the first character of the string is '-', in which case the rest of the name will be treated as an exclusive trace set.

This shader could be implemented like this:

shader_evaluate
{
   ...

   // direct lighting (both diffuse & specular)
   AiShaderGlobalsSetTraceSet(sg, shadow_set, shadow_set_inclusive);
   AiLightsPrepare(sg);
   while (AiLightsGetSample(sg))
   {
      ...
   }
   AiShaderGlobalsUnsetTraceSet(sg);

   // mirror Reflection
   if (sg->Rr_refl < 2)
   {
      ...
      AiShaderGlobalsSetTraceSet(sg, reflection_set, reflection_set_inclusive);
      AiMakeRay(&ray, AI_RAY_REFLECTED, &sg->P, &reflected, AI_BIG, sg);
      AiShaderGlobalsUnsetTraceSet(sg);
      ...
   }
}


An example render and its .ass file that demonstrates the different trace set cases follows:



options
{
 AA_samples 3
 xres 320
 yres 240
 GI_diffuse_depth 0
}

persp_camera
{
 name mycamera
 position 0.5 5 -5 
 look_at 0.5 0.2 0
 fov 38
 handedness left
}

point_light
{
 name mylight
 position 0.5 5 -2
 decay_type constant
}

standard
{
 name s1
 Kd 0.5
 Kd_color 1 0 1
 Ks 0.26
 specular_roughness 0.45
}

standard_tsets
{
 name s2
 Kd 0.5 
 Kd_color 1 1 1
 Kr 1
 reflection_trace_set "reflection"  # inclusive set
 shadow_trace_set "-shadow"     # exclusive set (this is parsed in the shader code)
}

plane
{
 name myplane
 shader s2
 normal 0 1 0
}

# "reflection" is used in the shader as an inclusive trace set, 
#     so only geometry tagged with it will be traced
#
# "shadow" is used in the shader as an exclusive trace set,
#     so only geometry NOT tagged with it will be traced

# invisible in reflections, visible in shadows
sphere
{
 shader s1
 trace_sets 2 1 STRING "xxx" "yyy"
 radius 0.4
 center -1 1 0
}

# this is visible to both refl and shadows 
sphere
{
 name centerSphere
 shader s1
 trace_sets 2 1 STRING "xxx" "reflection"
 radius 0.4
 center -0.2 1 0
}

# invisible to shadows, visible for reflection
sphere
{
 shader s1
 trace_sets 2 1 STRING "shadow" "reflection"
 radius 0.4
 center 0.6 1 0
}

# visible for shadows and reflections
# when no trace_set is assigned to a node, it is
# always visible to all rays
sphere
{
 shader s1
 radius 0.4
 center 1.4 1 0
}

# visible for shadows, invisible for reflections
# if any trace set is specified (even a null one)
# the geometry will participate in the trace set
# mechanism (visible by default for exclusive sets,
# invisible by default for inclusive sets)
sphere
{
 shader s1
 trace_sets 1 1 STRING ""
 radius 0.4
 center 2.2 1 0
}




  • No labels