I'm new to OSL and Blender. I'm using the following code to generate a circle on a plane. The plane, however, appears to be split into two triangles. Can anyone explain how to fix this so my OSL circle is mapped correctly? Thank you!

shader Circle(
        float rad=0.75 [[float min=0, float max=1]],
        float sharp=0.8 [[float min=0, float max=1]],
        int rep=1[[int min=1, int max=10]],
        point cent=point(0.5, 0.5, 0),
    output color c = 0)
    point pt = point(rep*u, rep*v,0);
    float ctx = floor(pt[0])+cent[0];
    float cty = floor(pt[1])+cent[1];
    point ct = point(ctx, cty, 0);
    float d = fmod(distance(ct, pt), rep)/rad;

    c = 1-smoothstep(0.5*sharp, 1-0.5*sharp, d);




1 Answer 1


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:

enter image description here

I hope this answer saves you some time..:)

  • 1
    $\begingroup$ Yes, it did save me time. I would never have known to include the Image Texture. Thank you, Robin! $\endgroup$ Dec 4, 2020 at 19:30
  • $\begingroup$ @Dr.Pontchartrain np! It would be possible, I guess, to get shot of the UV map altogether.. relying on the fact that the diagonal of the quad is always going to be the longest edge of your triangle, and putting center at the middle of that.. (dPdu and 'dPdv`) give you tha actual World-space vectors of two edges.. see blender.stackexchange.com/a/196566/35559. $\endgroup$
    – Robin Betts
    Dec 4, 2020 at 19:40

Not the answer you're looking for? Browse other questions tagged .