A Scales OSL shader for Blender

The second OSL shader I have implemented for Blender is a scales pattern. It provides an overlapping pattern of semicircles that can be used for things like fish scales or roof shingles.

Like the hexagon shader this is a generic shader, i.e. a shader that provides a color pattern irrespective of lighting conditions so it is possible to use it in different contexts. In the code shown below the shader is defined with two input colors, coordinates (typicallt uv-coordinates of the object) and a parameter called n which can be used to alter the shape of the scales.
Beside a color and an index we provide an output for the distance to the center of a scale (which can be used for nice displacement effects) and a vector Vindex which is unique for each scale but the same within the scale. This can be plugged into a texture to provide each individual scale with its own distinct color. In the example noodle for the image shown at the beginning you can see how this is done.
shader scales(
    color Diffuse_Color1 = color(0.2, 0.8, 0.2),
    color Diffuse_Color2 = color(0.8, 0.2, 0.2),
    vector Coordinates = 0,
    float n = 0,
    output color Color = 0,
    output int Index = 1,
    output float Distance = 0,
    output vector Vindex = 0)
    float sx = mod(Coordinates[0],1);
    float sy = mod(Coordinates[1],1);
    vector p  = vector(sx,sy,0);
    vector p0 = vector(0.5,0,0);
    vector p1 = vector(0.5,1,0);
    vector p2 = vector(0,0.5,0);
    vector p3 = vector(1,0.5,0);
    vector cell = vector(floor(Coordinates[0]),floor(Coordinates[1]),0);    
    int oddx = int(cell[0])%2;
    int oddy = int(cell[1])%2;
    float dist(vector a, vector b, float n){
        float x = b[0]-a[0];
     float y = b[1]-a[1];
     float r2 = x*x+y*y;
     if ( n != 0.0 ) {
            float theta = atan2(y,x);
         float cost, sint;
            sincos(theta, sint, cost);
         float cost2= cos(theta*2);
         float Y = pow(abs(sint),1+n*(1-cost2*cost2));
         r2 /= cost*cost+Y*Y;
        return sqrt(r2);
    float d1 = dist(p,p0,n);
    if ( d1<=0.5 ){
        Color = Diffuse_Color1;
        Index = 0 ;
        Distance = d1;
        Vindex = cell + p0;
    } else {
        float d2 = dist(p,p2,n);
        float d3 = dist(p,p3,n);
        if ( d2 <= 0.5 ) {
            Color = Diffuse_Color2;
            Index = 1;
            Distance = d2;
            Vindex = cell + p2;
        } else if ( d3 <= 0.5 ) {
            Color = Diffuse_Color2;
            Index = 1;
            Distance = d3;
            Vindex = cell + p3;
        } else {
            Color = Diffuse_Color1;
            Index = 0;
            Distance = dist(p,p1,n);
            Vindex = cell + p1;

Example node setup

The image of the abstract Koi carp provided at the beginning was created with a node setup that is shown below (click on the image for a larger view):

Future steps

The next shader I will implement I think, will be an irridescence shader so we can all enjoy some soap bubbles.

Example image

Just to illustrate that with scales you do a lot of interesting stuff, here is a picture of the exact same model but with different colors and displacement mapping added to the scales. Looks like a pine cone, right? (Maybe a bit chocolaty one :-)

No comments:

Post a Comment