OSL
Attribute to Color


return to main index


Code:
    attrToColor.osl



Introduction

This tutorial presents a shader called attrToColor that creates colors controlled by the value of a float attribute. For example, figure 1 shows 2365 teapots colored randomly by the shader.



Figure 1

For information about this scene refer to the tutorial RiMel - ParticleInstancer. A section of the beauty pass rib shows that each teapot archive was assigned a unique user defined attribute named "particle_id".

TransformBegin 
    Translate -0.273621 -0.148285 -1.03048
    Rotate 0 1 0 0
    Rotate 291.711 0 1 0
    Rotate 0 0 0 1
    Scale 0.2 0.2 0.2
    Attribute "user" "float particle_id" [6]
    ReadArchive "RIB_Archive/pot.0021.rib" 
TransformEnd 

The OSL function getattribute() retrieves the value of a named attribute, for example,

float id;
getattribute(attrName, id);

where the value of attrName might be a custom attribute such as "user:particle_id" or a standard attribute such as "identifier:id".


The Shader UI


Figure 2




Listing 1 - attrToColor.osl


shader
attrToColor
[[
int rfm_nodeid = 20,
string rfm_classification = "rendernode/RenderMan/pattern",
string help = "Brief description goes here."
]]
(
    string attrName = "user:particle_id" 
        [[
        string help = "another example 'identifier:id'",
        ]],
    color missingAttrColor = color(1,0,1) 
        [[
        string label = "Missing Attr Color",
        ]],
    float saturation = 1,
    output color resultRGB = 0)
{
float id;
if(getattribute(attrName, id)) {
    resultRGB = cellnoise( point(id, id + 7.0, id + 13.0) );
    color hsvColor = transformc("hsv", resultRGB);
    hsvColor[1] = hsvColor[1] * saturation;
    resultRGB = transformc("hsv", "rgb", hsvColor);
    }
else
    resultRGB = missingAttrColor;
}


Shader Registration

The metadata that follows immediately after name of the shader ie.

[[
int rfm_nodeid = 20,
string rfm_classification = "rendernode/RenderMan/pattern",
string help = "Brief description goes here."
]]

is "embedded" within the shader (attrToColor.oso) when it is compiled. If the users, RenderMan_for_Maya.ini and
RMSWorkspace.ini
files have been set up as explainded in the tutorial RfM: Customizing the shader will appear in the list of "Pattern" nodes in HyperShade. However, if the reader is not interested in pre-registering this OSL shader the metadata can be deleted, in which case the compiled .oso shader must be used with the PxrOSL node.

For information about editing and compiling shaders with the Cutter text editor refer to,
    Cutter: Open Shading Language








© 2002- Malcolm Kesson. All rights reserved.