/////////////////////////////////////////////////////////////////////////////
// greenmarble.osl -- green veined marble.
//
// Description::
//   Makes a marble-like surface using a turbulence function.
//   
// Parameters:
//   Ka, Kd, Ks, roughness, specularcolor - work just like the plastic
//   darkcolor, lightcolor - colors of the underlying substrate
//   veincolor - color of the bright veins
//   veinfreq - controls the frequency of the veining effects
//   sharpness - how sharp the veins appear
//   Pref - if supplied, gives the reference pose
//   shadingspace - space in which the pattern is laid out
//   shadingfreq - overall scaling factor for the pattern
//
/////////////////////////////////////////////////////////////////////////////
// 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)
/////////////////////////////////////////////////////////////////////////////


#include "noises.h"
#include "material.h"


surface
greenmarble
    [[ string description = "Marble, green, procedural" ]]
(
    float Ka = 0.1
        [[  string description = "Ambient scaling",
            float UImin = 0, float UIsoftmax = 1 ]],
    float Kd = 0.6
        [[  string description = "Diffuse scaling",
            float UImin = 0, float UIsoftmax = 1 ]],
    float Ks = 0.4
        [[  string description = "Specular scaling",
            float UImin = 0, float UIsoftmax = 1 ]],
    float roughness = 0.1
        [[  string description = "Surface roughness (specular highlight size)",
            float UImin = 0, float UImax = 1,
            string UIenabler = "Ks" ]],
    color specularcolor = 1
        [[  string description = "Specular highlight color",
            string UIenabler = "Ks" ]],

    color darkcolor = color(0.01, 0.12, 0.004)
        [[ string description = "Base marble color #1" ]],
    color lightcolor = color(0.06, 0.18, 0.02)
        [[ string description = "Base marble color #2" ]],

    color veincolor = color(0.47, 0.57, 0.03)
        [[  string description = "Vein color" ]],
    float veinfreq = 1
        [[  string description = "Vein frequency" ]],
    float sharpness = 25
        [[  string description = "Vein sharpness" ]],

    point Pref = P
        [[  string description = "Reference pose (defaults to P)" ]],
    string shadingspace = "shader"
        [[  string description = "Shading space",
            string UItype = "coordsys" ]],
    float shadingfreq = 1
        [[  string description = "Shading frequency" ]]
  )
{
    point PP = shadingfreq * transform (shadingspace, Pref);
    float dPP = sqrt(area(PP));

    // First calculate the underlying color of the substrate
    //    Use turbulence - use frequency clamping
    float turb = 0.5 * turbulence (PP, dPP, 5, 2, 0.5);
    color Ct = mix (darkcolor, lightcolor, smoothstep(0.1,.35,turb));

    // Now we layer on the veins

    // perturb the lookup
    PP += vector(35.2,-21.9,6.25) + 0.5 * vfBm (PP, dPP, 6, 2, 0.5);

    // Now calculate the veining function for the lookup area
    float turbsum = 0;
    float freq = 1;
    PP *= veinfreq;
    float i;
    for (i = 0;  i < 3;  i += 1) {
	turb = abs (filteredsnoise (PP*freq, dPP*freq));
	turb = pow (smoothstep (0.8, 1, 1 - turb), sharpness) / freq;
	turbsum += (1-turbsum) * turb;
	freq *= 2;
    }
    turbsum *= smoothstep (-0.1, 0.05, snoise(2*(PP+vector(-4.4,8.34,27.1))));

    Ct = mix (Ct, veincolor, turbsum);
  
    // Shade like plastic
    normal Nf = faceforward (normalize(N), I);
    C = MaterialPlastic (Nf, Ct, Ka, Kd, Ks, roughness);
    C *= opacity;
}
