Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

VEX THEORY

1 VEX THEORY
This is not a lesson, but rather a collection of ideas and concepts that will help a user who is
becoming familiar with VEX/SHOPs and how it all fits into Houdini.

1.1 VEX DIRECTORY STRUCTURES


This is meant as a guideline for managing your VEX operators when dealing with multiple
projects. This also contains different methods for installing VEX SOPs, POPs, CHOPs and
COPs. This does not deal with SHOPs (i.e. VEX fog, lights, displacements and shaders. Before
you even get into writing VEX OPerators, it is important to understand how to properly manage
your files on different levels.

Path Structure

$HFS/houdini

VEX

SOP
POP
CHOP
COP

VEXsop
VEXpop
VEXchop
VEXcop

Root

directories
containing
.vex .vfl
and .ds
files

files for loading operators


in the UI

Transport this directory structure.

or

$JOB/houdini/

Same as above.

$HOUDINI_PATH only set when you want vex files in other than the
following directories:
Windows NT .; ./houdini ; $HOME/houdini ; $HFS/houdini/
IRIX .: ./houdini : $HOME/houdini : $HFS/houdini/

Houdini Training

Side Effects Software 1997

VEX THEORY

for example: you want $JOB to be at root


setenv HOUDINI_PATH $JOB; $HFS/houdini/
$JOB/vex
/sop
/pop.......etc.
And VEX*op include files edited also.

Include File Method


Suggested
$HFS/houdini/vex

Start root

$HOME/houdini/vex

include $HFS/houdini/vex/VEX*op

$JOB/houdini/vex

include $HOME/houdini/vex/VEX*op

etc...
Option2
$HFS/houdini/vex

root

$JOB/houdini/vex

include $HFS/houdini/vex/VEX*op

Option 3

$JOB/houdini/vex

root

/usr/local/houdini

include $JOB/houdini/vex/VEX*op

Side Effects Software 1997

Houdini Training

VEX THEORY

In Summary
You can control what Houdini sees. It is possible to have job control over vex operations.
This will also allow you to limit the number of OPs in the UI. If you had several hundred custom OPs, then it would be a nightmare.

1.2 VEX OPS VS SHOPS

CAN YOU SPOT THE DIFFERENCE?

If somebody talks about VEX, they are more than likely talking about writing a custom shader
for Houdinis Mantra4 renderer. VEX is much broader than just a shader writing tool. VEX can
be broken up into two different distinct groups. VEX OPs, and SHOPs.

VEX OPs

SHOPs

- SOPs, COPs, POPs, CHOPs


- OPs just dealing with point information
- COPs and CHOPs only consider X,Y
- do not need vmantra to use.

- Are the closest thing to being a VEX


object (i.e. VEX light, fog)
- Cannot be viewed without using Vmantra
- Surface shaders can only effects colour.

1.3 VCC
Not only does the VCC command act as a compiler, but if you when you are in the shell, and
type vcc -X <opname> then you get a help menu that lists all of the context information about
that particular VEX operator.
For help information on VCC type vcc -h then you will get the general helplist printed to the
screen.

1.4 HTML FILES


In the $HFS/houdini/vex/html directory it contains several different html documents that are
not in the manuals. These contain examples of vex code, information on variables, and file management. This is different than the material in the manuals, as it is updated every version.

1.5 WRITING VEX CODE


Whether you are writing a VEX displacement shader or a VEX CHOP, there are some basic
fundamentals to writing you VEX code which will make your code easier to decipher for you,
and anybody reading it. Lets have a look at a simple VEX Lambert shader.

Houdini Training

Side Effects Software 1997

VEX THEORY

#pragma hint amb color


#pragma hint diff color

Pragmas help user undertand what the parameters


do.

A pragma label will be the name in


the UI parameter field corresponding
to the VEX variable

#pragma label amb Ambient Colour


#pragma label diff Diffuse Colour

Place these at the beginning of the


code
This tells the compiler what type of operator it is.

surface

Lambert(
vector amb=1;
vector diff={0.545, 0.525, 0.306};
)
{
vector nml;

nml = frontface(normalize(N),I);
Cf = amb * ambient()+diffuse(nml);
Cf*=diff;
}

The Lambert is the name of


the shader. The remainder
of this section declares the
varables that will be used in the
body of the code. Varablies
should be given a name that
is similar to its function in the
body of the code. Instead of:
vector first = 1 use vector amb

This is the body of the code.


This is where all of your hard
work gets calculated.
This can be many lines, or as you
can see...just a few.

1.6 DEALING WITH DISPLACEMENTS


When you apply a displacement shader you must consider that when you normalize the vector
normalize(N), the normal is calulated, and the surface displaced in world space. If you are scaling
your object in SOP space, the surface displacement will not match the scale change. To do this
you normalize the vector:
vector Nn = normalize (N);
P+=Nn * (amp/(length (wt_nspace(Nn)));

Side Effects Software 1997

Houdini Training

VEX THEORY

Displacement bounds - When VMantra is rendering a scene, it calculates the spacial bounds for
each primitive in the scene. When you use a displacement shader on a primitive, the value of the
displacment may cause areas of the object to protrude outside of the pre-calculated spatial bound.
This will result in ugly black triangles appearing on the rendered geometry. To fix this, you must
adjust the Displace Bound parameter in the objects Render folder. This value must be slightly
larger than the value of you displacment.
It is also important to note that you can use a combination of a displacement SOP, which displaces the points on a surface, and use the displacment values (map or random values) with a displacment shader. This creates a hybrid vex operator.
Antialiasing - When a continuous signal is sampled at equally spaced sample points, it is possible to take the sampled data, and reconstruct the signal through interpolation.

If the frequency of the signal changes, and the rate of the sampling remains, then the reconstructed signal can be dramatically different.

To compensate for this, you must sample the data at twice the highest frequency of the data. This
is called the Nyquist frequency.

Houdini Training

Side Effects Software 1997

VEX THEORY

You can see that even though the sample rate has been doubled, there are still errors in the reconstructed signal.
How do you know what the sample rate of your material is to avoid aliasing?
There are several different ways to deal with the problem. Some of which are reletovely simple,
but may result in much longer render time. Others can be much more complex techinically speaking.

Brute Force - This method is easy, you simply increase the shading quality of the object. This
will more than likely fix any antialiasing problem that you have, but the trade off is that every
time you increase the shading quality, you rendering times will increase dramatically.
Texture maps - You can use a texture map instead of relying on the renderer to generate the
image. This does has its downsides which stem from the fact that you are using a texture map.
Filter clamping - This is bassed on taking an estimation of the highest frequency, and clamping
the frequency to increase the accurac of the sample.

1.7 NOISE
Procedural noise can be used to help create a variety of natural phenomenon. Noise can be
used to add slight bumping or displacements to surfaces that seem regular. It can be used to add
noise to to edges, stripes, bricks or any other transition between different colours or regions.
dSince there are several different types of noise, here is a breakdown of the different value
ranges.
noise (value)
- returns a value from 0 to 1.
- generates a rough looking noise.
- general noise used most of the time.
onoise (value)
- returns values form -1 to 1.
- for purists, this is exact perlin noise function.
snoise
- returns values from -1.7 to 1.7.
- generates a bubbly looking noise.
anoise
- returns values from -1 to 1.
Noise can be used to vary the color of a surface for example. In the following lines of code, an
offset value is included as wellas a frequencey value to control the shape of the noise.

surface
randcolor(

Side Effects Software 1997

Houdini Training

VEX THEORY

vector diff = 1;
float offset = 0;
float freq = 1;
float amp

= 1;

)
{
float turb;
vector PP;

PP = (wo_space(P) + offset) * freq;


turb = noise(PP) * amp; //compute noise

Cd *= turb; //compute color


}

This generates a simple variation in the saturation of the colour. You can try substituting noise with snoise(PP), onoise (PP) and anoise(PP). Remember the value that the noise
generates, you may have to add abs in front of the noise. High frequency noise values can
cause buzzing in the noise.
The rest attribute - The rest attribute is generated in houdini using the rest SOP, and will
Lock down your points to the geometry. If your geometry deforms, the noise will stick. You
have to declare rest as a vector in the parameters in order to pick this up from the geometry.

1.8 TEXTURE FUNCTION


The texture function is used to index textures to paint surfaces. The basic use of the function
is to supply the name of a texture image, and two indices generally called ss and tt. THe texture
function can be used to affect the specular, diffuse roughness, specular roughness and all other
properties of a shader.
Below is a basic shader that applies a texture as a constant painted surface.

surface
paintedtex(
vector diff = 1;
float intensity = 1;
string map = ;

Houdini Training

Side Effects Software 1997

VEX THEORY

vector uv=0
)
{
float

ss, tt;

vector

dclr;

vector4 mapclr;

//used to pick up rgba

dclr = diff;
if (map != )

//test to see if texture map is supplied

{
if (isbound(uv)) //test to see if uvs are bound to surface
{
ss = uv.x;

//set the uvs x component to ss

tt = uv.y;

//set the uvs y component to tt

}
else
{
ss = s;

//pass the parametric s to ss

tt = t;

//pass the parametric t to tt

}
if (ss >= 0 && ss < 1 && tt >= 0 && tt < 1)
{
// Look up the color (with alpha)
mapclr = texture(map, ss, tt); //picks up alpha too
dclr *= (1 - mapclr.a); //Scale the original color by alpha
dclr += mapclr;

//Assume that the map is pre-filtered

dclr *= intensity;
}
}
Cf = dclr;
Of = Cf;
Af = mapclr.a;
}

Side Effects Software 1997

Houdini Training

VEX THEORY

This shader picks up both RGB and Alpha by setting mapclr to be a vector4 variable. This
shader also incorperates if() statements to make the shader more efficient in time and memory
consumption.
isbound - The use of the isbound function is used to test if the uvs are present on the surface,
and if they are, use them to set ss and tt.
The function is mapclr = texture(map, ss, tt);
This returns the correct colour pre-filtered to reduce sparkles. It is best to use .rat image files,
as they reduce the likelihood of sparkles, and are extremely memory efficient due to the fact
that the image has embedded multiple resolutions.

1.9 CSPLINE FUNCTION


Many times you are writing a shader, you want to include a color ramp or a spline curve to be
referenced by some function. This is done by incorperating the cspline() function. The cspline
function is defined as follows.
cspline (t; val1, val2, val3, .... valn,)
The t variable is the index value and the val* variables are the values located at even divisions
along the cardinal spline. Because of the nature of cardinal splines, you have to repeat the first
and last values to anchor the end points of the cardinal spline.

surface
metallic(
vector BROWN = { 0.1307, 0.0609, 0.0355 },
BLUE0 = { 0.4274, 0.5880, 0.9347 },
BLUE1 = { 0.1221, 0.3794, 0.9347 },
BLUE2 = { 0.1090, 0.3386, 0.8342 },
BLUE3 = { 0.0643, 0.2571, 0.6743 },
BLUE4 = { 0.0513, 0.2053, 0.5377 },
BLUE5 = { 0.0326, 0.1591, 0.4322 },
BLACK = { 0, 0, 0 };
vector spec = 1,
opac = 1;
float amb = 1;
)
{
vector Nf = normalize(frontface(N, I));
vector V = normalize(-I);
vector R;

Houdini Training

// reflection direction in world space

Side Effects Software 1997

VEX THEORY

vector Rn;

// normalized reflection vector

vector Ct;
float altitude;

R = 2 * Nf * dot(Nf, V) - V; // Calculate the reflection vector


Rn = normalize( R );
altitude = 0.5 * R.z + 0.5; // Take z component of Rn
Ct = cspline(altitude,
BLACK, BROWN, BROWN, BROWN, BROWN,
BROWN, BLUE0, BLUE1, BLUE2, BLUE3,
BLUE4, BLUE5, BLACK);
Of = opac;
Cf = Of * spec * Ct;
Af = amb;
}

Notice that the first and last colours in the cspline() function are repeated to get the correct
shape in the cardinal spline curve.
lspline() - Similar to the cspline() function there is the lspline() function which generates a linear spline between the points evenly along the curve. This function does not require the doubling up of the first and last points.
ckspline() - A third function is the ckspline() function which can place the points at arbitrary
positions along the curve form 0 to 1.
ckspline (t; val1, key1, val2, key2, ...valn, keyn)
This function requires two values to place a knot or point in the curve: The value to place (val),
and where along the curve to place the value (key).

1.10 MESSAGE PASSING


The passing of parameters between shaders.
It is possible to have shaders communicate to other shaders down stream. It is not possible to
have shaders talk back and forth though.
The path is also fixed by the order in which shaders are called as a surface is processed by
vmantra.

10

Side Effects Software 1997

Houdini Training

VEX THEORY

basic information in
- geometry
- lights both ambient and normal
- atmospheric objects
- bound shaders

Displacement
Shader
Light
Shader

Light Shadow
Shader
- dimport()

Surface Shader
-dimport()
-limport()

Fog Shader
- dimport()
- limport()
- simport()
This diagram shows how message passing occurrs in the render pipeline, and what information
can be extracted. For example, a surface can extract an exported parameter from a displacement shader, but a displacement shader cannot pick up anything from any of the bound shaders.
You have to decalare an export function in the parameters of a function:
displace
foodisplace(export float turb = 1);
{
...
}

Houdini Training

Side Effects Software 1997

11

VEX THEORY

Now in the surface shader, you can pick up this variable by picking this in the body of the
shader.

surface
foosurface (diff = 1)

{
float surfturb;
dimport (turb, surfturb);
...
}

12

Side Effects Software 1997

Houdini Training

You might also like