Results 1 to 6 of 6

Thread: CATIA - Glossy/Diffuse reflection & refraction

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Location
    Paris
    Posts
    3

    Default CATIA - Glossy/Diffuse reflection & refraction

    Hello,

    Can you provide some samples of Glossy/Diffuse reflections & refractions in metaSL?
    I've looked into the metalSL spec and I've found some clues but when I try it in mental ray for CATIA, it's not working as expected. I already have those effects embedded in CATIA but not in metaSL external code.

    Thank you,

    Jerome

  2. #2
    Join Date
    Jun 2007
    Location
    Berlin
    Posts
    447

    Default

    Hi Jerome,

    I am currently building an example for reflection, explaining how to do importance sampling and how to set the right trace options. The example will be posted here soon.

    All the Best,
    Ruediger

  3. #3
    Join Date
    Jun 2007
    Location
    Berlin
    Posts
    447

    Default

    In this post we will set up a MetaSL shader that does glossy reflections. This post explains in detail what is going on in the shader file that is attached to this post.

    In MetaSL rays can be traced by setting up some Ray data structures. Attached to this post is a small example shader that you can start working with.

    Creating a reflection can be done easily in MetaSL by using the reflect() statement. Just use the direction and the normal as the first and second argument and you get the perfect specular reflection direction.

    Glossiness:
    If you trace glossy rays that always implies that you need to shoot a bunch of rays instead of a single reflection ray for perfect mirror-like surfaces. So all rays that are shot must be scattered around the reflection direction. To achieve a nice distribution a sample iterator should be used which gan generate pseudo random samples that are evenly distributed within the range [0, 1). A sample iterator can be declared with

    Sample_iterator si(num_samples);

    The sample iterator takes the number of samples as argument. If that the argument is 0 or omitted, evaluating the sample iterator will run until a break or return statement is encountered.

    To shoot the glossy rays, we need to iterate over the sample iterator and generate pseudo random samples (Quasi Monte Carlo Samples, QMC for short) that we use to perturb the reflection direction with. The scattered reflection direction will be generated in a local (spherical) coordinate system that is transformed into the internal coordinate system.

    Code:
    // retreive a Quasi Monte Carlo (QMC) sample
    sample = si.sample2();
    
    // Compute spherical coordinates (phi, theta) for the QMC sample
    float phi = 2.0 * PI * sample.x;
    // Make a nice Gaussian falloff for theta, which is the elevation angle in
    // spherical coordinates. The width of this falloff can be controlled with
    // the parameter "gloss_sharpness". Higher values yield sharper reflections.
    float theta = atan(sqrt(-log(1-sample.y)) /gloss_sharpness); 
    // phi and theta are defined in a custom local coordinate system that needs to be
    // transformed into internal coordinates.
    float x = cos(phi) * sin(theta);
    float y = sin(phi) * sin(theta);
    float z = cos(theta);
    
    // NOTE: This code does not take care of the special case where the
    // 'texture_tangent[0]' and 'main_dir' align!!
    float3 u = cross( main_dir ,texture_tangent[0] );
    float3 v = cross( main_dir,u);
    float3 refleDir = x * u + y * v + z * main_dir;
    After the new reflection direction has been calculated, the reflection ray can be created.

    Ray ray(float3(0), float3(0)); // set up an empty ray
    ...
    ray = Ray(position, refleDir);


    Setting up the trace options:
    To take full advantage of raytracing it is recommended to adjust the trace options before tracing a ray. Different values should be set in order to allow other shaders to optimize their performance based on the trace options.

    Code:
    Trace_options trace_options;
    // indicate that a reflection ray will be cast
    trace_options.set_ray_type("reflect");
    
    trace_options.set_ray_dispersal_group("glossy");
    
    trace_options.set_importance(trace_importance / num_samples);
    Importance sampling:
    Tracing rays from a glossy surface always requires a large number of rays to be traced. If the reflection rays hit another glossy surface, more rays will be spawned resulting in an exponential growth of the number of rays with each level of reflection.
    This would cause rendertimes to explode. To avoid this explosion it is crucial to implement so called importance sampling.
    When a shader is evaluated it has access to a state variable called "importance". It indicates the contribution of the current ray to the final sample and ranges from 0 to 1 where 1 is the default value. An eye ray (primary ray) has an importance of 1 since it is fully contributing. A ray spawned by a glossy reflection has less importance: If the shader spawns 10 rays, each ray contributes only 1/10 to the final sample, thus the importance of each ray is 0.1 only. This allows shader writers to make optimizations in their shader calls if the importance is low.

    In the given example we account for this by scaling the number of samples to be taken by the importance of the current ray at the beginning of the shader. This brings render times down considerably while the impact on quality is small.

    The importance is set via
    trace_options.set_importance(importance / num_samples);

    The trace call:
    When all parameters have been set, the ray can be traced.

    trace(ray, // the reflection ray that was generated
    ray_dx, // ray differential,
    ray_dy, // ray differential
    trace_options, // the trace options
    "result", // the result parameter called "result" will be evaluated
    trace_return_val); // the variable that will hold the returned value


    The final division:
    After all samples have been taken, the accumulated result is divided by the number of samples taken.


    Check out the shader that is attached to this post. Try disabling the line
    trace_options.set_importance(importance / num_samples);
    to see how much importance sampling impacts render time.
    Attached Files Attached Files
    Last edited by ruediger; January 8th, 2010 at 17:57. Reason: Fixed and improved parts of the code sample and the posed

  4. #4
    Join Date
    Sep 2007
    Location
    Paris
    Posts
    3

    Default

    Thank you ruediger,

    I will take a look.

  5. #5
    Join Date
    Dec 2010
    Posts
    13

    Default

    hi,ruediger
    how to create a mi for the msl file?
    It not work in maya 2011

  6. #6
    Join Date
    Jun 2007
    Location
    Berlin
    Posts
    447

    Default

    Check out this tutorial on how to get MetaSL shaders render with Maya. You will find a complete description of all necessary steps.

    http://forum.mentalimages.com/showthread.php?t=7092

    Best Regards,
    Ruediger

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •