polkadot like texture with OSL for bump mapping etc

anybody could help me and tell me how I could create a texture with OSL which produces just polkadots like this:

so I can use it as a bump texture for products to render without using UV unwrapping but the object texture space?

Hi Claas,

I have found this:
http://blenderthings.blogspot.co.uk/2013/06/random-points-on-unit-sphere-in-osl.html

but if you want, you can download all my OSL scripts:

more 110 OSL files!

Bye bye
Spirou4D


/*****
 * polka dots on  the unit square
 * with grout options
 */ 
shader p2d_polka (
    point Vec = P,
    float scale = 1,
    float Intense = 1,
    float radius =0.5,
    float grad1 = 0.1,
    output float Fac = 1.0)
    
{
    float it = Intense;
    point Pos;
    point Pun;                          // even or odd rows and cols
    point Pcen = point(0.5,0.5,0);      // center of the ngon
    point d,dn;
    int i;
    float dist;
    
    float epsi = 0.01;
 
    // work in object coordinate if no infeed
    if (Vec == P)
        Pos = transform("object",Vec)*scale;
    else
        Pos = Vec*scale;


    d = Pos - floor(Pos);
    d[2]=0;
    dn=d-Pcen;
    
    Pun=mod(floor(Pos),point(2,2,1));
    it=1;
    
    dist = length(dn);
        
        if ((Pun[0]==0)&&(Pun[1]==1)||(Pun[1]==0)&&(Pun[0]==1)) {
            if (radius - dist <grad1){
                    it=(radius-dist)/grad1;
            } 
            if (dist >radius) 
                it=0; 
        } else 
            it=0;
     
    clamp(it*Intense,0,0.99);    
    Fac = it;
}

2D texture . can work with any coordinate system uv or object.
radius should be kept < 0.5.
the grad1 control a gradient from black to white. Set to 0 if you don’t want it

see my other tiling textures in the procedural tiling thread

Thks burnin.
Spirou4D

oh boy I am not a coder and all this makes no sense to me when I read through the code ;(

all I need would be something like the polka texture a simple square with a circle inside texture would be enough and then you can tile it as much as needed to generate the size.

would this be hard to write in OSL? I am a total illiterate at this.

But Claas, Luke_p has made an very nice answer!!!

Great Thaks Luke_p!



Claas, this is what my code produce.
Left, straight B/W polka
Right, with a gradient.

save the code as e.g. polka.osl and load it in a script node.

Spirou4d thanks.

1 Like

note that you can have fun results with a different distance metric.
e.g. with Minkovsky value of 0.7 :


1 Like

He must have written his reply the same moment I was writing! lol
thats why I did not see it!

thank you so much for the script. Is there any way to make the texture flow over the object like a procedural texture would do?

I get this problem here and my students don’t want to UV unwrap STL imports.


It is a 2D texture on the XY plane , so UV is the best way to go if your object surface is not aligned with this plane. 3D objects faces aligned with that plane will work well, but not an object of revolution like this vase.

Cylindrical coordinates and sphere distance won’t work as dot size would change depending on where the surface intersect the spheres.

3D coordinates and spheres would have the same problem plus changes along axis

In fact the only solution is to make a cylindrical texture radiating cylinders, but that will work only for revolution objects centered at (0,0) and even that will show some distortion at points of extreme curvature and the spacing of the dots won’t be constant.

The distance function is a bit more complex.

I will look into that, but I may not have the time tomorrow.

thank you a lot for looking into this!

Hi!
If you can tame this one you can go the distance!


shader Polka(
output float Fact = 0
){
vector Loc;
getattribute(“object:location”,Loc);

vector p = floor(length(distance(point(1.5,0.2,0.2),distance(P,Loc)))*37);
mod(p,2) == 0 ? Fact = 0 : Fact = 1;

return;

}

Hi Tree3D,

You provide an interesting solution here for a the problem of texture distortions at the sphere by simply changing the layout and size.

This could turn into a perfect OSL texture to render overmodled rubber for products with a dot texture that is distortion free.

the following code create a base UV map around a cylinder by default at (0,0,0) on the z axis

feed the coord val to the vec input of the texture. make sure scale input of texture is 1.
you can see some distortion but it is inevitable.

better solution as it works with any 2d texture


shader c3D_to_tiled_cyl(
    point Vec=P,
    point  cen=0,
    vector axis=point(0,0,1),
    vector pol_orig=point(1,0,0),
    int tile_freq=8,
    float h_scale=1,
    
    output vector Coord=vector(0,0,0),
    output float hue=0)
{
    vector Pos, hPos, mPos;
    float mX, mY, radius, tile_val;
    
    // work in object coordinate if no infeed
    if (Vec == P)
        Pos = transform("object",Vec);
    else
        Pos = Vec;


    Pos = Pos - cen;
    hPos = dot(Pos,axis)*axis;      // projection on axis
    mPos = Pos-hPos;                // projection on plane normal to axis
    radius = length(mPos);
    mPos = mPos / radius;           // normalized
    
    mX = dot(mPos,pol_orig);        // projection on polar origin
    mY = dot(mPos-mX*pol_orig,cross(axis,pol_orig));
    
    tile_val = (tile_freq)*(atan2(mY,mX)/M_2PI+M_PI);
    if (tile_val&lt;0)
        tile_val=0;
    Coord[0] = tile_val;
    Coord[1] = hPos[2]*h_scale;
    Coord[2] = 0;
    hue = tile_val;
} 


Hi Luke_P,

Very good your job Luke! You’re a Mastro! Thks a lot.
It’s amazing this OSL, no? I love it!

My collection count 160 files now with the tip of this Goodness page by Burnin.

Distortions are impossible to hide here anyway when the shape changes.
Thank you so much!

Thank you for this useful information!!