3D Animation Workshop: Lesson 28: VRML 97 Free Fall | 2 | WebReference

# 3D Animation Workshop: Lesson 28: VRML 97 Free Fall | 2

## Lesson 28 - VRML 97 Free Fall - Part 3

Just as we can use the DEF tag to define the specific values in the fields of a node, we can define completely new node types built from existing nodes. In this example, we create a simple table with four legs. The essence of this example is the use of a prototype. A table node is defined using other nodes and called TwoColorTable. This is only the definition, however. There are as yet no tables in the scene.

After the PROTO definition of the table is complete, that definition is then used to actually create two tables, using the new TwoColorTable node. Each table is in its own Transform, and each is translated one unit to the left and right, respectively, of the world transform center. Note how this differs from the DEF approach. DEF is used on nodes that actually exist in the scene so that they can be copied elsewhere. A PROTO statement defines a completely new node type, but nothing happens until the node is actually used in a scene.

Here is the code and the result.

```#VRML V2.0 utf8
PROTO TwoColorTable Â Â [Â Â field SFColor legColor .5 1 .5
Â Â Â Â Â Â Â Â Â Â field SFColor topColor 1 .5 1]  #end of public interface
{
Â Â Transform {
Â Â Â Â children [
Â Â Â Â Â Â Transform { # table top
Â Â Â Â Â Â Â Â translation 0 .6 0
Â Â Â Â Â Â Â Â children [
Â Â Â Â Â Â Â Â Â Â Shape {
Â Â Â Â Â Â Â Â Â Â Â Â geometry Box {size 1.2 0.2 1.2}
Â Â Â Â Â Â Â Â Â Â Â Â appearance Appearance {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â material Material {diffuseColor IS topColor}
Â Â Â Â Â Â Â Â Â Â Â Â }
Â Â Â Â Â Â Â Â Â Â }#end of shape
Â Â Â Â Â Â Â Â ]
Â Â Â Â Â Â } #end of table top
Â Â Â Â Â Â Transform { # first leg
Â Â Â Â Â Â Â Â translation -.5 0 -.5
Â Â Â Â Â Â Â Â children [
Â Â Â Â Â Â Â Â Â Â DEF Leg Shape {
Â Â Â Â Â Â Â Â Â Â Â Â geometry Cylinder {height 1 radius 0.1}
Â Â Â Â Â Â Â Â Â Â Â Â appearance Appearance {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â material Material {diffuseColor IS legColor}
Â Â Â Â Â Â Â Â Â Â Â Â }
Â Â Â Â Â Â Â Â Â Â }
Â Â Â Â Â Â Â Â ]
Â Â Â Â Â Â }
Â Â Â Â Â Â Transform { #second leg
Â Â Â Â Â Â Â Â translation .5 0 -.5
Â Â Â Â Â Â Â Â children [
Â Â Â Â Â Â Â Â Â Â USE Leg
Â Â Â Â Â Â Â Â ]
Â Â Â Â Â Â }
Â Â Â Â Â Â Transform { #third leg
Â Â Â Â Â Â Â Â translation -.5 0 .5
Â Â Â Â Â Â Â Â children [
Â Â Â Â Â Â Â Â Â Â USE Leg
Â Â Â Â Â Â Â Â ]
Â Â Â Â Â Â }
Â Â Â Â Â Â Transform { #fourth leg
Â Â Â Â Â Â Â Â translation .5 0 .5
Â Â Â Â Â Â Â Â children [
Â Â Â Â Â Â Â Â Â Â USE Leg
Â Â Â Â Â Â Â Â ]
Â Â Â Â Â Â }
Â Â Â Â ]#End of children of root transform
Â Â }#End of root transform
}#End of PROTO
Transform {
Â Â Â Â children [
Â Â Â Â Transform {
Â Â Â Â Â Â translation -1 0 0
Â Â Â Â Â Â children [
Â Â Â Â Â Â TwoColorTable {
Â Â Â Â Â Â Â Â legColor 1 0 0
Â Â Â Â Â Â Â Â topColor 0 1 0
Â Â Â Â Â Â }
Â Â Â Â Â Â ]
Â Â Â Â }
Â Â Â Â Transform {
Â Â Â Â Â Â translation 1 0 0
Â Â Â Â Â Â children [
Â Â Â Â Â Â Â Â TwoColorTable {}
Â Â Â Â Â Â
Â Â Â Â Â Â ]
Â Â Â Â }
Â Â Â Â ] #end of world children
}#end of world transform system
```

There is so much to learn here.

The PROTO statement is divided into two parts. The first part, in the square brackets, defines the fields that can be changed when the new node is actually implemented. In this case, these fields are the colors of the table legs and top. Default color values are supplied. When the two tables are implemented at bottom of the file, the first node inserts new colors (red and green) and the second node uses the default colors by failing to state any values. This is typical of VRML nodes. For example, we have seen that failing to enter a size value in a geometry node (like Sphere or Box) results in a default value of 1. Notice how the IS statement is used to connect the color names at the top of the PROTO definition with the applicable Material nodes deeper in the definition. For example, the diffuseColor of the first leg is the legColor parameter (whether the default value or any new value supplied when the PROTO is implemented).

This example also repeats what we learned in the previous one. A Shape node is named Leg using the DEF statement, and thus the other three legs are easily created as instances of the first. They share both the geometry and appearance of the first. This not only makes the code more readable, but assures that any change in the geometry or appearance of the first leg will be passed to all the others. Placing each leg in a separate Transform node permits them to be separately translated into position at the corners of the table. Likewise, assigning each entire table to a Transform permits each to move, rotate and scale as a single functional unit in the scene.