u
and v
are global variables provided by OSL. They refer to the parametric U and V; the normalized barycentric coordinates of the shading point in the currently shaded triangle. They are the same U and V as are returned in the 2D vector you get from the Geometry node > Parametric output.
To get the U and V of the quad, as mapped, you have to look up the U and V from the object's active UV map, using getattribute()
:
shader Circle(
float radius = 0.75,
float sharpness = 0.8,
int tiles = 1,
point center = point(0.5, 0.5, 0.0),
output color c = (0.0)
)
{
vector UV = (0.0);
int hasUV = getattribute( "geom:uv", UV);
point pt = point(tiles*UV[0], tiles*UV[1],0);
float ctx = floor(pt[0])+center[0];
float cty = floor(pt[1])+center[1];
point ct = point(ctx, cty, 0);
float d = fmod(distance(ct, pt), tiles)/radius;
c = 1-smoothstep(0.5*sharpness, 1-0.5*sharpness, d);
}
.. but here, we run into an unfulfilled promise in the Blender manual, which judging by the age of this answer, you might expect to have been fixed by now. Bizarrely, as far as the user is concerned, Blender's implementation of OSL refuses to give a value to geom:uv
, unless there is an Image Texture node somewhere in the tree. (If you want to evaluate some other UV map by name in your OSL, the tree must also contain a like-named UV Map node, connected to the Image Texture node.)
So your tree winds up looking like this:
I hope this answer saves you some time..:)