/* $Revision: 1.1 $ */
/*
**  kbtile
**  
**  produces tiles swapping betweeen 2 materials, grout and tile in a grid pattern
**
**  it is partially based on mtor blinn shader.
**  it includes a simple Anti-alias technique from Advanced Renmderman - Glitz
**  This Shader was produced for educational purposes and not for commercial redistribution
**  Copyrights for Pixar and Autodesk apply
**_____________________________________________________________________
**
** Copyright (c) 1998 PIXAR.  All rights reserved.  This program or
** documentation contains proprietary confidential information and trade
** secrets of PIXAR.  Reverse engineering of object code is prohibited.
** Use of copyright notice is precautionary and does not imply
** publication.
**
**                    RESTRICTED RIGHTS NOTICE
**
** Use, duplication, or disclosure by the Government is subject to the
** following restrictions:  For civilian agencies, subparagraphs (a) through
** (d) of the Commercial Computer Software--Restricted Rights clause at
** 52.227-19 of the FAR; and, for units of the Department of Defense, DoD
** Supplement to the FAR, clause 52.227-7013 (c)(1)(ii), Rights in
** Technical Data and Computer Software.
**
** Pixar
** 1001 W Cutting Blvd
** Richmond, CA  94804
**
** ______________________________________________________________________
*/
#include "walias.h"

#define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - \
			   smoothstep((b)-(fuzz),(b),(x)))
#define repeat(x,freq)    (mod((x) * (freq), 1.0))
#define union(a,b)        ((a) + (b) - (a) * (b))
#define blend(a,b,x) ((a) * (1 - (x)) + (b) * (x))

surface
kbtile ( color ambientColor = color(0);
         color incandescence = color(0);
         float diffuseCoeff = 0.8;
         color specularcolor = color(1);
         float eccentricity = 0.5;
         float specularRollOff = 0.1;
         float sfrequency  = 1,
               tfrequency  = 1,
               gridwidth   = 0.1,
               gridoffsetu = 0.5,
               gridoffsetv = 0.5;
         color groutcolor = color (0, 0, 0)           
       )
{
    normal Nf;
    color Ia, Id, Itr, Is, Ir, Isr;
    color tile_color, grout_color;
    color grout_opac;
    float ss, tt;

/* Calculate basic Blinn shader values */
    
    Nf = faceforward( normalize(N), I );
    Ia = waliasAmbient(Nf, ambientColor);
    Id = diffuseCoeff*diffuse(Nf);
    Itr = waliasTranslucence(Nf, 0);
    Is = waliasBlinn(Nf, eccentricity, specularRollOff, Ir);
    Isr = specularcolor * Is;
    tile_color = Cs * (Ia + Id + Itr + Isr);
    
/* Calculate grout grid */

    ss = repeat(s, sfrequency);
    tt = repeat(t, tfrequency);
    grout_opac = union(pulse(gridoffsetu - gridwidth - 0.02, gridoffsetu + gridwidth + 0.02, 0.035, ss),
       		         pulse(gridoffsetv - gridwidth - 0.02, gridoffsetv + gridwidth + 0.02, 0.035, tt));
    grout_color = groutcolor * (Ia + Id);		       

/* final mix  */
    
    Oi = Os;
    Ci = Oi * blend(tile_color, grout_color, grout_opac);
    
}
