I have recently started getting into Shader Writing in OSL (actually rather Pattern Writing just to not get frustrated too quickly :-) ). To start things off I wrote a simple Pattern that lets you create a life 3d mask in your shading network that can be used to alter different shading effects in very particular areas of an object without the need to paint texture masks. On top of that being OSL it should work in any renderer that supports it (e.g. PRMan/RIS, Cycles in Blender, VRay, etc).

The Pattern itself only has a few simple controls:


PMask OSL Pattern inside Maya with RenderMan (PxrOSL Node)


Here’s a quick explanation:
Mapping – determines whether to use world- or object-space for the 3d mask (choose PWorld or PRef). Unfortunately the PxrOSL node in Maya ignores shader metadata for creating a nice dropdown menu here (Blender users might be luckier :)).
Radius – The X, Y, Z (-> R, G, B) scale of the spherical mask.
Whitepoint – Values below 1 will move the core towards the outside edge
Blackpoint – Values above 0 will move the outside edge towards the core
Gamma – Can be used to increase or decrease the falloff

All of these parameters are mappable, too! In the case of this Tool this has some very nice benefits.

The first one is that you can create a locator for example and map its translation to the Pos input. If you are using PWorld as your Mapping type it will use the world space coordinates so wherever you move the locator the mask will go as well. However assuming you are not animating the locator this will be a static mask in worldspace and any objects which are animated will swim through the mask.
If you want your mask to follow your object you can set the Mapping to PRef. This will use the coordinates of the objects translation and deformation from a reference position (zero’d out transforms) and will make the mask stick to the object wherever it’s going. Keep in mind that the Origin for the position (0,0,0) is always wherever the object has zero’d out transforms. This means that you might need an offset if you have frozen your transforms at one point. Another alternative would be to parent or constraint a locator to a moving object and leave the Mapping set to PWorld.
To double-check the coordinates the OSL node allows you to output PWorld and PRef for debugging purposes (you can take the RGB color values of a point in the render as your Pos value).

Also most of the time a simple sphere is not very desirable, so you can also map the radius with other procedurals or textures to break up its shape.

Because this is based on position data it’s completely independent of the object’s UV’s.

You can download it HERE.

Here’s a quick demo video:

3D Position Masks in OSL for Shading Networks from Julius Ihle on Vimeo.