home     graphics   guestbook  

            

 

                    /* Mosaic Shader Source Code by Celambarasan Ramasamy */

                                    

 

#include <project.h>    /* use the the same project.h header file from advanced renderman*/

float pulse (float edge0, edge1, x)

{

return step(edge0,x) - step(edge1,x);

}

 

float pulsetrain (float edge, period, x)

{

return pulse (edge, period, mod(x,period));

}

 

#define snoise(p) (2 * (float noise(p)) - 1)

#define filteredsnoise(p,width) (snoise(p) * (1-smoothstep (0.2,0.75,width)))

float fBm (point p; float filtwidth;

uniform float octaves, lacunarity, gain)

{

uniform float amp = 1;

varying point pp = transform("shader",p);

varying float sum = 0, fw = filtwidth;

uniform float i;

for (i = 0; i < octaves; i += 1) {

#pragma nolint

sum += amp * filteredsnoise (pp, fw);

amp *= gain; pp *= lacunarity; fw *= lacunarity;

}

return sum;

}

 

/* Tilepattern function from the ceramictile shader in advanced renderman,used to produce

the tile patterns on the surface*/

float

tilepattern (float ss, tt,ds,dt ;

point pshad;

float groovewidth, grooveheight;

float nbh,nbv; )

{

float gw = ((groovewidth * .8) + .012*fBm (point((ss),(tt*2)+90,(ss+tt)+25)*1000,filterwidthp(point((ss*30)-53,(tt*2)-9,(ss+tt)-54)),4,2,.1))*nbh/4;

float gh = ((grooveheight * .8) + .012*fBm (point((ss)-53,(tt*2)-9,(ss+tt)-54)*5000,filterwidthp(point((ss*30)-53,(tt*2)-9,(ss+tt)-54)),4,2,0.1))*nbv/4;

float bw = 1 - gw;

float bh = 1 - gh;

return pulsetrain (gw,bw,ss+groovewidth/2)

*pulsetrain (gh,bh,tt+grooveheight/2);

}

 

/* mosaic shader developed by celambarasan ramasamy 4-4-2006

based on ceramic tiles shader from advanced renderman */

 

surface

mosaic(float Ka = .4, /* ambient brightness */

Kd = .5, /* basic brightness */

Ks = 0.7, /* hilite brightness */

Kr=.25,

roughness = 0.21;/* hilite spread */

varying color hilitecolor = .95; /* hilite color */

float freqb=19,shapeb=1.12; /* gives the frequency and size of the black chips found in the mosaic*/

float freqg=10,shapeg=.76;/* gives the frequency and size of the green chips found in the mosaic*/

float freqr=12,shaper=.80;/* gives the frequency and size of the red chips found in the mosaic*/

float freqp=14,shapep=.80;/* gives the frequency and size of the pink chips found in the mosaic*/

float freqw=14,shapew=.83;/* gives the frequency and size of the white chips found in the mosaic*/

float groveh=0.005,grovew=0.005;/*gives the size of the horizontal and vertical grooves respectivly*/

float nbh=4,nbv=4;/* gives the number of horizontal and vertical blocks of mosaic you want to have on the polygon */

float change=0.04;/* gives the amount of color difference between individual blocks of mosaic */

float dust_intensity=.15;/*gives the intensity of the dust on the mosaic */

float groovedepth=.9; /*groove depth intensity in bump mapping*/

)

{

float mx[16] = {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};

float ss,tt,dss,dtt;

vector i;

color ambientcolor, diffusecolor, speccolor,

surfcolor,c, surfopac = Os;

ProjectTo2D("st",P,"shader",array_to_mx(mx),ss,tt,dss,dtt); /*function from advanced renderman,maps the points in  the shader space on to a specific coordinate system,present in project.h */

point pshad = transform("shader",P);

float nib = float noise((ss*freqb*nbh)-200,(tt*freqb*nbv)+300)*shapeb;

float monob =1-(8*smoothstep(0.80,1,nib)); /* we threshold the noise so that only noise above a particular frequency

                                                                           show up on the surface,producing the appearence of the mosaic chips.

                                                                            -> monob identifies all the chips that are to be colored black */

float nig = float noise((ss*freqg*nbh)-356,(tt*freqg*nbv)-1078)*shapeg;

float monog =1-(8*smoothstep(0.60,1,nig)); /* monog identifies all the chips that are to be colored green */

float nir = float noise((ss*freqg*nbh)+500,(tt*freqg*nbv)-650)*shaper;

float monor =1-(8*smoothstep(0.60,1,nir));/* monor identifies all the chips that are to be colored red */

float nip = float noise((ss*freqp*nbh)-20,(tt*freqp*nbv)-70)*shapep;

float monop =1-(8*smoothstep(0.60,1,nip));/* monop identifies all the chips that are to be colored pink */

float niw = float noise((ss*freqw*nbh)-490,(tt*freqw*nbv)-149)*shapew;

float monow =1-(8*smoothstep(0.60,1,niw));/* monow identifies all the chips that are to be colored white */

float swhichtile,twhichtile,stile,ttile;

float SS=mod(ss*nbh,1),TT=mod(tt*nbv,1);/* SS,TT maps the actual ss,tt coordinates according to the number

of blocks of mosaic needed on the surface */

float gh=groveh;

float gw=grovew;

float dss1=dss;

float dtt1=dtt;

float intile = tilepattern(SS,TT,dss1,dtt1,pshad,gw,gh,nbh,nbv); /* tilepattern function generates the tile patern needed for the mosaic */

float offset =cellnoise((ss*nbh)+6,(tt*nbv)-10);/* gives color variation between individual blocks of tiles */

float dust = dust_intensity*clamp(fBm((P),filterwidthp(P),30,12,01),-.2,1);/* adds a dusty look */

/*lighting */

normal n = normalize(N);

normal nf = faceforward(n, I);

/*bump maps the groove depth*/

if(intile!=1 && monob==1)

{

nf = calculatenormal(P-groovedepth*normalize(nf));

nf= normalize(nf);

}

if(intile!=1 && monob!=1)

{

nf = calculatenormal(P-groovedepth*normalize(nf));

nf= normalize(nf);

}

i =normalize(-I);

speccolor = Ks * specular(nf, i, roughness)*hilitecolor;

/*spots inside the tiles*/

if(monob !=1 && intile==1) /*assigns the color to all the chips that are both inside the mosaic and are to be painted black*/

{

surfcolor=mix(color (0.05,0.05,0.05),hilitecolor,specular(nf, i, roughness));

}

if(monow !=1 && intile==1)/*assigns the color to all the chips that are both inside the mosaic and are to be painted white*/

{

surfcolor=mix(color (0.65,0.66,0.73),hilitecolor,specular(nf, i, roughness));

}

if(monop !=1 && intile==1)/*assigns the color to all the chips that are both inside the mosaic and are to be painted pink*/

{

surfcolor=mix(color (0.46,0.54,0.63),hilitecolor,specular(nf, i, roughness));

}

if(monog !=1 && intile==1)/*assigns the color to all the chips that are both inside the mosaic and are to be painted green*/

{

surfcolor=mix(color (.31,.28,.218),hilitecolor,specular(nf, i, roughness));

}

if(monor !=1 && intile==1)/*assigns the color to all the chips that are both inside the mosaic and are to be painted red*/

{

surfcolor=mix(color (.25,.1,.1),hilitecolor,specular(nf, i, roughness));

}

 

if(intile!=1 && monob!=1)/*assigns the color to all the chips that fall on the mortar*/

{

surfcolor=mix(color(.9,.9,.9),color (.14,.14,.19),specular(nf, i, roughness));

}

if(intile!=1 && monob==1)/* assigns the color to the mortar */

{

surfcolor=mix(color(.9,.9,.9),color (.14,.14,.19),specular(nf, i, roughness));

}

if(intile==1 && monob==1 && monog==1 && monor==1 && monop ==1 && monow==1)/*white background inside tiles*/

{

float vary=change*offset;

surfcolor=((1-dust)*color(.78+vary,.78+vary,.78+vary))+(dust*color(.31,.19,.13));

}

Oi = surfopac;

color reflectcolor = (0,0,0);

ambientcolor = Ka * (1-specular(nf, i, roughness)/1.9) * ambient();

if((intile==1 && monob==1 && monog==1 && monor==1 && monop ==1 && monow==1 )||(intile!=1 && monob==1)||(intile!=1 && monob!=1))

{

diffusecolor = Kd * (1-specular(nf, i, roughness)/1.9) * diffuse(nf);

}

i = normalize(I);

vector reflectRay = reflect(i, nf);/*adds raytracing to simulate reflection*/

reflectcolor = Kr*trace(P, reflectRay);

Ci = Oi * Cs * surfcolor * (ambientcolor + diffusecolor + speccolor + reflectcolor);

}

© 2004-6 Celambarasan Ramasamy. All rights reserved.