Subsections


2.3 Prototyping

Once you build more complex objects you want to reuse smaller elements in different ways. So, learning how to define =$>$PROTO node is important before you really start to produce a lot of VRML code. PROTOs define a new node name with custom fields you can specify. In other words, PROTOs allow you to extend the language by adding your own nodes. In this respect, PROTOs are much more powerful than simple DEFs/USEs that only allow to apply transformations to reused objects.

The structure of a simple PROTO is the following:


PROTO prototypename [ field fieldtypename name defaultValue
                      field fieldtypename name defaultValue
                      ... ]
                    {node}
Let's look shortly at the definition of fields. Here is an example taken from below:

                field SFColor legColor  .8 .4 .7
field is just a keyword you must use. SFColor is a valid VRML ``field type name'' (see section 8.4 on page [*]. legColor is the name that the field will have and ``.8 .4 .7'' are it's default values. If this is unclear, go through the next two examples and see how they are used.

Example 2.3.1   Three Stools

VRML: ../examples/inter/inter-proto-1.wrl
Source: ../examples/inter/inter-proto-1.text

Here is an example of three stools. It is taken from the =$>$VRML II Specification.

It defines a ``TwoColorStool'' node with two public fields named ``legColor'' and ``seatColor'' that one can modify when making instances. Note that there are default colors (pink legs and a yellow seat)


#VRML V2.0 utf8

PROTO TwoColorStool [ field SFColor legColor  .8 .4 .7
                      field SFColor seatColor .6 .6 .1 ]
{
   Transform {
      children [
         Transform {   # stool seat
            translation 0 0.6 0
            children
            Shape {
               appearance Appearance {
                  material Material { diffuseColor IS seatColor }
               }
               geometry Box { size 1.2 0.2 1.2 }
            }
         }
         
         Transform {   # first stool leg
            translation -.5 0 -.5
            children
            DEF Leg Shape {
               appearance Appearance {
                  material Material { diffuseColor IS legColor }
               }
               geometry Cylinder { height 1 radius .1 }
            }
         }
         Transform {   # another stool leg
            translation .5 0 -.5
            children USE Leg
         }
         Transform {   # another stool leg
            translation -.5 0 .5
            children USE Leg
         }
         Transform {   # another stool leg
            translation .5 0 .5
            children USE Leg
         }
        ] # End of root Transform's children
   } # End of root Transform
} # End of prototype

# The prototype is now defined. Although it contains a number of nodes,
# only the legColor and seatColor fields are public. 

# Instead of using the default legColor and seatColor,
# this instance of the stool has red legs and a green seat:

TwoColorStool {
   legColor 1 0 0 seatColor 0 1 0
}

# The chair to the right has yellow legs and a bright blue seat:

Transform {
   translation 2 0 0
   children [
      TwoColorStool {
         legColor 1 1 0 seatColor 0 1 1
      }
      ]
}

# The chair to the left uses the default colors

Transform {
   translation -2 0 0
   children [
      TwoColorStool {
      }
     ]
}

NavigationInfo { type "EXAMINE"}      # Use the Examine viewer

You can look at an another (locally produced) example that uses embedded protos: Patrick Jermann's MOO Ants.

Example 2.3.2   Moo Ants PROTO Example

VRML: ../examples/inter/inter-proto-ant.wrl
Source: ../examples/inter/inter-proto-ant.text

This example uses PROTOS within PROTOS, a facility which can add additional modularity to your VRML worlds.


2.3.1 Exercises

Exercice 2.3.1   A forest without Snow White

Build a little forest on a flat plane


D.K.S. - 1999-04-28