Tell Friends about VRMLSite
Read VRML
News Yet?

VRML Goes Dynamic

by Jeff Sonstein, M.A.

PROTO and ROUTE

I thought about entitling this article PROTOs Are Our Friends. With the addition of the PROTO construct, we get reusable objects and independent copies of objects. With the addition of the ROUTE statement to VRML 2.0, we get to shift from creating static scenes and objects to creating dynamic ones. This article is basically about using the PROTO construct in VRML 2.

VRML 1 let us build objects and to place them into scenes. After placing an object in a VRML 1 scene, that's what you've got: an object in a scene. The camera viewpoint can fly around in a VRML 1 scene, but nothing changes there. VRML 1 worlds can have an eerie, uninhabited feel because of their frozen nature. Nothing in a VRML 1 scene can change or move but the camera viewpoint.

A VRML 2 scene is a whole different creature. Where a spider in a VRML 1 scene couldn't do much more than serve as a hyperlink, a VRML 2 spider can complain aloud when I click on it with the mouse. A VRML 2 spider can be made to change color, to move around and to communicate with the audience and with other objects. A VRML spider can be created as a reusable object, and multiple independent clones may exist at once in a scene. Cloning is one of the techniques in which PROTO gets really powerful.

VRML 1 gave us DEF/USE, and it was good. VRML 2 gives us PROTO, and it is powerful. To make sense out of PROTO, we have to talk about object-oriented thinking a bit.

Object-Oriented Thinking

A big part of what makes VRML 2 much more powerful and more readily applicable to commercial applications is the object-oriented thinking which has gone into the new specifications. Mossenback defines what I think object-oriented thinking is about in Object-Oriented Programming in Oberon-2 (Springer-Verlag, 1994, pp 2-3):
The object-oriented way of thinking focuses on the data rather than on the procedures. The data and the operations applicable to them constitute objects that can be asked to handle certain requests... The most significant features are:
  • Information hiding
  • Data abstraction
  • Inheritance
  • Dynamic binding

Information hiding is about encapsulating data structures inside the object. Data abstraction is about restricting access to the encapsulated data structures to an abstract view. Inheritance is about being able to create new types out of old and the new types retaining and possibly expanding on the attributes of the old types. Dynamic binding is about being able to link objects at run-time. Add these up together and we get extensible objects, which add significantly to the dynamic possibilities in VRML 2.

What is OO thinking and why is it important?

There is much of stage-craft in constructing a VRML scene. What people tend to focus on in constructing VRML are the visible logical objects. If I can encapsulate everything needed to run an object within that object, then it is easier to reuse. If I can provide a restricted view into the object for other objects to use, then I can reuse the object in different ways. If I can make an object which is not bound to a particular scene or to a particular other object within a scene, then I can reuse it in different ways and in different contexts.

Messages: VRML Gets The Word

The ROUTE statement is new to VRML 2, and is central to dynamic objects in VRML. What the ROUTE statement is designed to do is to provide a mechanism for one object to send messages to or receive messages from one or more other objects. For example, the ROUTE statement allows the abstract view of an on-off switch to tell the abstract view of a desk lamp that the on-off switch has been clicked on. Object creation isn't much of a change, but objects talking to each other is quite a change.

What is a PROTO?

The specification says:
Prototyping is a mechanism that allows the set of node types to be extended from within a VRML file. It allows the encapsulation and parameterization of geometry, behaviors, or both.

PROTO Details

The details of the specification look like this:
 
PROTO protoName [ eventIn

fieldtypename name

                  eventOut     fieldtypename name

                  exposedField fieldtypename name

                  defaultValue(s) field        fieldtypename

                  name defaultValue(s) ... ] {

  mini scene graph

  possibly with nodes, prototypes, and routes

  and containing IS statements

  }

An eventIn can take a message from another node's eventOut, if they both are of the same type. An eventOut can send a message to another node's eventIn, again if they are of the same type. Declaring an exposedField implies declaring an eventIn which can be used to set the value of the exposedField, and it implies declaring an eventOut which can be used to notice changes in the value of the exposedField.

At first glance it may seem confusing, but it will make sense as you use the new constructs in VRML.

PROTOs Can Contain Mini-Worlds

Remember, each PROTO is a mini-VRML world all in itself. A PROTO may contain nodes, other PROTO statements, and ROUTE statements. You should also remember that the Script node is just another node. A PROTO may contain Script nodes as well as any other node type. This means that it is possible to completely encapsulate the logic needed to run something like a desk lamp within the desk lamp.

TheIS Statement

The IS statement tells what eventIn, eventOut, or exposedField from the PROTO abstract interface to the rest of the world to connect with what part of the inside of the PROTO. For example:

 PROTO exampleThingie [ exposedField

SFVec3f putWhere 0 0 0 ] {

  Transform { translation IS putWhere }

}

To use this prototype in a VRML file, you could just put it wherever you might otherwise put a Transform node. The outermost node type in a PROTO determines what the PROTO may be substituted for in a VRML scene.

Simple Example: A Three-Point Lighting Array

Below is a very simple example of a PROTO node: a standard three-point lighting array. I have kept it simple, for the sake of being a clear example. This PROTO provides a common scene element of general utility. Because the intensity of each DirectionalLight node within the PROTO is exposed in the public interface, the scene in which it is used can turn the lights up and down.

 PROTO threePointLighting [

exposedField SFFloat keyBright  1

                           exposedField SFFloat sideBright 1

                           exposedField SFFloat fillBright 1 ] {

  Group {

    children [

      DirectionalLight {

        direction 0 -0.5 0

        intensity IS keyBright

        color 0.75 0.75 0

      }

      DirectionalLight {

        direction -0.5 -0.5 0

        intensity IS sideBright

        color 0 0.5 1

      }

      DirectionalLight {

        direction 0 0 -1

        intensity IS fillBright

        color 0.8 0.8 0.8

      }

    ]

  }

}

It is useful to be able to place, move, and turn up and down a set of lights in a VRML scene. The outermost node type in our PROTO is a Group node, so this can be used anywhere a Group might be used. If I used this PROTO in a scene, it might look something like this:

 
#VRML Draft #2 V2.0 utf8 
PROTO threePointLighting [ # details removed

  ] { }



Group {

  children [

    threePointLighting { keyBright 2.5 sideBright 2 fillBright 2 }

    Shape { geometry Box {} }

  ]

}

I get basic stage lighting for little work, and I get to reuse it over and over again.

When and Why to Use PROTO Constructs

You should use a PROTO to create stock elements. By "stock elements" I mean objects or assemblies which I'll probably want to reuse, either within one scene or in several scenes. When browsers finally begin supporting EXTERNPROTO, I'll be able to gather up PROTOs into libraries of related stock elements.

You should use PROTO to organize the production of your scenes. By decomposing a complex problem (like a scene) into smaller and more manageable pieces you can make your life easier. I can do the layout work first, and work on one part of the scene at a time to keep from getting overwhelmed by the details.

You should use PROTO to encapsulate all the behaviors and logic and pieces needed for an object into one package. The browsers don't yet fully support this, but they are getting there. In my next article I will present some "workarounds" to deal with these VRML browser implementation weaknesses. Besides helping with reusability, encapsulation helps you to create VRML code which is readable. Readable code is reusable down the road.

PROTOs and ROUTEs and the object-oriented thinking which went into creating VRML version 2 make VRML much more suitable for commercial, educational, and entertainment applications. The dynamic nature of the scenes which we can create in VRML 2 greatly extends the possibilities for 3D on the Internet.

Examples

The new Version 2 vrmLab is under construction at The Community Company

You can find a number of VRML 2 objects at the vrmLab Warehouse. The objects within the sub-directories at vrmLab Warehouse are in various stages of construction, and they all work. Looking at running code can be useful.

From time to time, you could also take a look at the current state of the vrmLab home scene. You can click on the spider and fly into the scene. Try turning the desk lamp on and off (CosmoPC browser bug: the ceiling stays lit) or dragging on the world globe (CosmoPC browser bug: SphereSensor doesn't yet work).

Remember being made to drive your car on a zigzag course through those stupid orange cones in Drivers' Education class? There are two additional demos at the vrmLab: ConeRide world and spiderRide world.

The coneRide scene demonstrates animating a Viewpoint through a scene, changing the orientation and location using a TimeSensor and interpolators. The spiderRide scene shows the same things, but animates you and a spider guide through the scene together. Have a nice flight.


Related URLs


Jeff Sonstein, M.A. is the Networks Administrator for New College of California, runs the consulting firm of Sonstein and Associates, and started the vrmLab in early 1995 as a virtual space for VRML experimentation. Jeff holds an M.A. in Social-Clinical Psychology, has been playing with computers since the early 1970s, and currently lives with his family in San Francisco.


#VRML V2.0 utf8 | WorldInfo | ViewPoint | Shape
NavigationInfo | Java Chat | Subscribe | Tell Friends

© 1996 Aereal, Inc. Please send suggestions to suggest@vrmlsite.com.