/////////////////////////////////////////////////////////////////////////////
// shownormals.osl - simple shader to color based on N
//
// Description:
//   This is a debugging shader to help you examine the surface
//   normals by stuffing them in the color channels, xyz -> rgb.
//
//   Note that the sign of normals is subject to the "orientation"
//   attribute and the handedness of the current transform.
// 
// Parameters:
//   space - coordinate space of normals, e.g., "world", "object".
//           Defaults to "common" i.e. "camera".
//   bias -  how the normal vectors get translated into colors.  Values are:
//           0:  Show the full range of possible normals.
//               The range [-1,1] gets mapped to colors [0,1].
//               (Example: green = 0.5 means Ny = 0.)
//           1:  Show positive values only.  This shows less
//               information, but is often easier to interpret visually,
//               since it looks like there are red, green, and blue lights
//               shining from the +X, +Y, and +Z directions respectively.
//           -1: Show negative values only.  Flips the "lights" to the
//               negative axes, -X -Y and -Z.
//              
/////////////////////////////////////////////////////////////////////////////
// Copyright 2004 NVIDIA Corporation.  All Rights Reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// 
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// (This is the Modified BSD License)
/////////////////////////////////////////////////////////////////////////////


surface
shownormals 
    [[ string description = "Visualize surface normals" ]]
(
    string space = ""
        [[  string description = "Coordinate system",
            string UItype = "coordsys" ]],
    float bias = 0
        [[  string description = "Range bias",
            string UItype = "int", float UImin = -1, float UImax = 0 ]]
  )
{
    normal NN = N;
    if (space != "")
        NN = transform (space, NN);
    NN = normalize(NN);

    if (bias == 0) {
        // Color range [0,1] shows full range of normal [-1,1]
        NN = 0.5 + (NN * 0.5);
    } else if (bias < 0) {
        // Color range [0,1] shows negative components of normal
        NN = -NN;
    }
    // else, color range [0,1] shows positive components of normal

    C = color (NN);
    C *= opacity;
}
