Blender Cycles OSL: Color Squares

Hi everybody. Today I'm going to show you a color squares pattern material, similar to present papers. The materials looks like this:


Here the code is:
/*
*   Migrated from "http://glsl.heroku.com/e#5335.0"
*   Author: elbrujodelatribu
*/

// Returns fractional part
float fract(float x)
{
    return (x - floor(x));
}

// Returns fractional part, by coordenates
point fract(point x)
{
    return point(fract(x[0]),fract(x[1]),fract(x[2]));
}

// Colorize squares
point tri(point p, float r)
{
    point pos = p;

 pos *= r;
 point x = fract(pos);
 point i = pos - x;
 point c = point(sin(i[0]*2.0), sin(i[1]*2.0), 0.0)*0.2+0.2;

 return (((x[0] < x[1]) == (x[0] < 1.0 - x[1])) ? 1.0 : 0.0) * c;
}


shader color_squares(
    float Scale = 1.0,
    vector Vector = P,
    output color ColorOut = 0.8)    
{
    vector pos = Vector*Scale; 
 
    float r = 20.0;
    point x = fract(pos*r);
    point i = pos*r - x;
 
    float w = 50.0;
    float l = clamp(0.0, 1.0, (0.5 - abs(x[0]-0.5))*w);
    l *= clamp(0.0, 1.0, (0.5 - abs(x[1]-0.5))*w);
    l *= clamp(0.0, 1.0, abs(x[0] - x[1])*w);
    l *= clamp(0.0, 1.0, abs(1.0 - x[0] - x[1])*w);
 
    point tc = tri(pos, r);
    tc += tri(pos, r*0.5);
    tc += tri(pos, r*0.25);
    tc += tri(pos, r*0.125);
 
    // Returns color
    ColorOut = color(tc*l);
}

The node setup for the material above is a simple diffuse node. But if you want it looks more like a present paper, you can add some glossiness with this setup:


This is the result:


Or this setup to get a more metallic finish, like a satin paper:




Bye


Comentarios

Entradas populares de este blog

Santa Alejandra

Stucco Material III

Blender Cycles: Black Body Lights Reference (New Feature in Blender 2.69)