PROTOs for Dummies II
Creating Modular Prototypes
 More of this Feature
• Part 1: Intro to Prototypes
• Part 3: Adding Interaction
• Part 4: Speak to Me
 
 Join The Discussion
"Have certain sites increased revenue because of 3D features?"
AHEDSTROM
 
 Related Resources
• VRML Repository
• VRML PROTO Repository
 

Dateline: 3/30/98
 
This is the second of a multi-part series on VRML Prototypes, most often called PROTOs.

PROTOs are great an encapsulating the geometry and behavior of objects. But in order to reuse your PROTOs you really need to be able to keep them in separate files. The mechanism used to accomplish this is called EXTERNPROTO. The EXTERNPROTO let's you refer to other files, via a URL, which contain PROTO definitions. Libraries of PROTOs can be built and in fact there are a few small collections of PROTOs available on the web. Check out the PROTO page on this site for a collection of links to these sites.

To continue with the EyeBall and extend this to more controls for the  PROTO let's look at the use of EXTERNPROTOs for the creation of a complete pair of Eyes by using the previously defined single eyeball PROTO.

The complete EyeBall code, is in the earlier article mentioned earlier so I won't reprint it here. If the EyeBall PROTO is in a file, as it is, which we can refer to by the URL: http://vrml.about.com/library/weekly/eyepr1.wrl. We can use this previously defined  PROTO as follows.

#VRML V2.0 utf8
#EXTERNPROTO Definition
EXTERNPROTO EyeBall [ field SFFloat pupilRadius ] "http://vrml.about.com/library/weekly/eyepr1.wrl"

#Now we are ready to use the EyeBall
Transform {
    children EyeBall { }
}
This simply creates a single eyeball but it uses the previous EyeBall PROTO defined in another file.

Now let's say that someone else has a cool set of EyeBalls and they also call their PROTO EyeBall and you want to make a VRML file using both EyeBall PROTOs. You can call the EyeBall PROTO anything you like in your file, but it must be unique in your file. So this new file could look like:

#VRML V2.0 utf8
#PROTO Definition
EXTERNPROTO EyeBall [ field SFFloat pupilRadius ] "http://vrml.about.com/library/weekly/eyepr1.wrl"
EXTERNPROTO ThinEyeBall [ field SFFloat pRadius ]
"http://www.eyeballs.com/eyeballproto.wrl"

#Now we are ready to use the EyeBall
EyeBall { }
Transform {
    children [
        Transform {
            translation 2 0 0
            children ThinEyeBall { }
                }
        ]
 }

Silly looking but it does work :-)

Notice that although the PROTO files each call their PROTO's EyeBall we have effectively renamed them in our new file. This is a necessary capability in order to use libraries of PROTOs which have not coordinated names with each other...in other words everyone's PROTO library. Also notice that you must declare the interface section of the PROTO in your local file. This is also necessary  to allow the browser to parse (geek term for interpret) the VRML file even if the PROTO is not available, for example if the URL has changed or the site is not available. Actually the intent is to refer to PROTOs via URN's a more general reliable mechanism than URLs however not widely implemented in the Web yet.

Now let's combine the functionality of an eyeball with pupil size parameters, with an eyeball that can be squashed and stretched. In other words (hold on to your hats) a PROTO with TWO parameters!

To cut to the chase the following two PROTOs (one for a single eye, and the second for a pair of eyeballs) give you the two parameter control (it also available as the files eyetPr.wrl, eyestPr.wrl and the instantiation of them as itEyes.wrl) [Name note: Eye for single eyeball, Eyes for pair, i - instantiation, t - thin]:

#VRML V2.0 utf8
# file: eyetPr.wrl
#PROTO Definition begins of Eye with two controlling parameters
PROTO Eye [ field SFFloat pupilRadius 0.2
            field SFVec3f squashFactor 1 1 1]
{
Transform {
    scale IS squashFactor
    children [
        Shape {
            appearance Appearance {
                material Material { # color of the whites part
                    diffuseColor 0.8 0.8 0.8 #light gray
                }
            }
        geometry Sphere { }
        }
        Transform { #the pupil
            translation 0 0 1 #move the pupil sphere forward
            scale 1 1 0.1     #squash the pupil sphere to flatten it out
            children [
                Shape {
                    appearance Appearance {
                        material Material {
                            diffuseColor 0.2 0.2 0.2 #color of the pupil
                    }
                }
                geometry Sphere { radius IS pupilRadius }
                }
            ]
        }
    ]
}
} #PROTO Definition ends

In the proto for a pair of eyes we must be sure to pass both parameters along to the new "Eyes" PROTO which creates the pair.

#VRML V2.0 utf8
#file: eyestPr.wrl
EXTERNPROTO Eye [ field SFFloat pupilRadius
                  field SFVec3f squashFactor
] "eyetPr.wrl"

PROTO Eyes [ field SFFloat pupilRadius 0.4
             field SFVec3f squashFactor 1 1 1]
{
Transform {
    children [

# Parameters are passed along via the IS mechanism which associates the incoming
# parameter name with the name which must be use, in this case the same but not
# necessarily so. The data types must match.

        Eye { pupilRadius IS pupilRadius
              squashFactor IS squashFactor }
        Transform {
            translation 2 0 0
            children Eye { pupilRadius IS pupilRadius
                            squashFactor IS squashFactor }
        }
    ]
}
}
 
Finally the file which actually uses the PROTOs and creates the world:

#VRML V2.0 utf8
#file: itEyes.wrl
EXTERNPROTO Eyes [field SFFloat pupilRadius
                    field SFVec3f squashFactor ] "eyestPr.wrl"

#Time to create (instantiate) these Eyes

# Becauase I'm tired of the black background
Background {
    skyColor 0 0 0.5
}
 
Eyes {pupilRadius 0.2
    squashFactor 1 0.3 1}
Transform {
    children [
        Transform {
            translation 0 -2.5 0
            children Eyes { pupilRadius 0.5 }
        }
    ]
}

Which looks as follows:

You can see that as we increase the complexity and functionality of the PROTO, the actual  VRML world creation file (the instantiation file) becomes simpler. By encapsulating the details in PROTOs you will wind up with simpler, more modular, easier to maintain worlds.

So far we've just discussed simple geometry PROTOs next week we will look at the use of PROTOs to encapsulate interaction. Stay tuned! Same web station, same web time.

Previous Features