### OSLWyvill Lines

Related Tutorial:
RfM: RiMel - WyvillEdges

#### Introduction

This tutorial presents a shader called WyvillLines that uses techniques presented in the following tutorials.
Vectors: Shortest Distance from a Point to a Line
When used to control presence and displacement the shader can produce some interesting results.

The shader reads two custom rib attributes that refer to a list of 3d coordinates. For example,

Attribute "user" "int verts_count" [4]
Attribute "user" "point[400] verts_data" [
0.000 1.200 0.000
0.875 2.160 -0.045
0.000 1.200 0.000
-0.537 0.074 0.367
0 0 0
0 0 0
]

The verts_data attribute specifies a fixed number of 3d coordinates, some which are ignored by the shader. The number of valid coordinates is specified by the verts_count attribute. The valid coordinates specify the positions of lines that will control of the effect of the shader. The non-valid coordinates, 0 0 0, merely pad the list so that verts_data has a fixed number of coordinates.

##### A Fixed List of Coordinates - Why?

When reading an array of values from an attribute, for example,

point data[MAX_VERTICES];
int result = getattribute("user:verts_data", data);

the size of the array (data) that will receive the values must be the same size as the array of values being read by the getattribute() function. Hence the need for a fixed array - MAX_VERTICES arbitrarily set to 400 in the hope that this will be large enough to handle reasonably complex scenes. However, this means the verts_data attribute must, likewise, have 400 coordinates even though in many cases only a relatively small number of coordinates will refer to 3d lines to be used by the shader.

##### How the Coordinates are Used

At each shading point, WyvillLines calculates the distance to each line specified by the valid coordinates of verts_data and assigns values to it's outputs resultMask and resultF. If the distance to a line is less than it's mask_radius input parameter it assigns 0.0 to resultMask. This output is intended to be connected to the "presence" input of PxrSurface.

For example, figure 2 demonstracts effect of using the egdes of a square and a slightly inclined circle to apply "cut" holes in a polygon.

 Additioally, the distance to each line is converted to a Wyvill "field function" value. The sum of the field values are assigned to the resultMask output. This output is intended to be connected to the scaler input of PxrDisplace. For example, figure 3 demonstracts effect of using the egdes of a square and a slightly inclined circle to apply a displacement to a polygon.

Listing 1 - WyvillLines.osl