[arrow_icon] [action_button] [arrow_icon]


6. VRML 2.0 Examples


Simple Example
Stool
Directional Light
Point Set
Level of Detail
Color - Interpolation
Sensor
Clock with alarm
Collision Detection


A simple Example

#VRML V2.0 utf8

For easy identification of VRML 2.0 files, every file must begin with #VRML V2.0 utf8. The identifier utf8 allows for international characters to be displayed in VRML using the UTF-8 encoding of the ISO 10646 standard.

The # character begins a comment. But note, that comments and whitespaces may not be sent by the VRML-server to the user in order to save transmission time. Information like author and copyright should therefore be stored in the WorldInfo node.

Generally speaking a VRML file can contain any combination of the following:

In this simple example we used children nodes. Grouping nodes (Transform) are used to create hierarchical transformation graphs. Grouping nodes have a children field that contains a list of nodes which are the transformation descendants of the group. Each grouping node defines a coordinate space for its children. This coordinate space is relative to the parent node's coordinate space - that is, transformations accumulate down the scene graph hierarchy.

We set headlight FALSE because we defined our own light source (DirectionalLight). In general, shape nodes are illuminated by the sum of all the lights in the world that affect them. This includes the contribution of both direct and ambient illumination from light sources. Ambient illumination results from the scattering and reflection of light originally emitted directly by light sources. The amount of ambient light is associated with the individual lights in the scene.

In VRML 2.0 there are three types of light sources:

Directional light illuminate only the objects descended from the light's parent grouping node (including descendants). Point and Spot lights illuminate all objects in the world that fall within their volume of lighting influence, regardless of location in the VRML file.

All light sources contain following fields:

The rest of the code should be rather clear and not too different from VRML 1.0

To the Top of this page


Stool

#VRML 2.0 utf8

PROTO TwoColorStool [ field SFColor legColor .8 .4 .7

field SFColor seatColor .6 .6 .1 ]

{ Transform {

}]}}

TwoColorStool {

NavigationInfo { type "EXAMINE" }

A node may be referenced in a VRML file multiple times. This is called instancing and can be accomplished using the DEF and USE keywords.

The DEF keyword defines a node's name and creates a node of that type. The USE keyword indicates that a reference to a previously named node should be inserted into the scene graph. This has the effect of sharing a single node in more than one location in the scene. If the node is modified, then all references to that node are modified, too. DEF/USE name scope is limited to a single file. If multiple nodes are given the same name, then the last DEF encountered during parsing is used for the USE definitions.

Prototyping is a mechanism that allows the set of nodes to be extended from within a VRML file. It allows the encapsulation and parameterization of geometry and attributes.

The definition of a prototype consists of the keyword PROTO, the name of the new node type, the prototype declaration and the prototype definition.

The prototype declaration contains a list of public eventIns and eventOuts that can send and receive events, a list of public exposedFields and fields with default values.

The prototype definition contains a list of one ore more nodes, and zero or more routes and prototypes. The nodes in this list may also contain the IS statement, which associates field and event names contained within the prototype definition with the events and fields names in the prototype declaration.

To the Top of this page


Directional Light

Transform {

As mentioned before there are three types of light-sources:

The example above shows that only two of the four defined shapes will be lit by the directional light. You have to make sure that the objects that should be lit by the directional light source are in within the child-node that contains the DirectionalLight { ... } statement. You may specify the direction, the intensity and the RGB-color for this light-source.

To the Top of this page


Point Set

Shape {

}

Shape {

geometry PointSet {

}

This simple example defines a PointSet composed of 3 Points. The first point is red (RGB values 1 0 0), the second point is green (RGB 0 1 0), and the third point is - what surprise - blue (RGB 0 0 1). The second PointSet instances the Coordinate node defined in the first PointSet, but defines different colors.

To the Top of this page


Level of Detail

LOD {

Shape { }]}

In this example, the browser is free to chose either a detailed or a less-detailed version of the object when the viewer is closer than 100 meters, measured in the coordinate space of the LOD. The browser should however display the less-detailed version of the object if the viewer is between 100 and 1000 meters and should display nothing at all if the viewer is farther than 1000 meters. Browsers should try to honor the hints given by the programmers, and the programmers should try to give browers as much freedom as they can to choose levels of detail based on performace.

The LOD node is typically used for switching between different versions of geometry at specified distances from the viewer. But if the range field is left at its default value the browser selects the most appropriate child from the list given. It can make this selection based on performance or perceived importance of the object. Children should be listed with most detailed version first just as for the normal case. This `performace LOD' feature can be combined with the normal LOD function to give the browser a selection of children from which to choose at each distance.

To the Top of this page


Color - Interpolation

DEF myColor ColorInterpolator {

DEF myClock TimeSensor {

ROUTE myClock.fraction_changed TO myColor.set_fraction

The connection between the node generating the event and the node receiving the event is called a route. A node that produces events of given type can be routed to a node that receives events of the same type using the following syntax:

ROUTE NodeName.eventOutName_changed TO Nodename.set_eventInName

The Prefix set_ and the suffix _changed are recommended conventions, no strict rules. Thus, when creating prototypes or scripts, the names of the eventIns and the eventOuts may be any legal identifier name.

Routes are not nodes. ROUTE is merely a syntactic construct for establishing event paths betweeen nodes.

Interpolator nodes are designed for linear keyframe animation. That is, an interpolator node defines a piecewise linear function on an interval. The piecewise linear function is defined by n values of t called key and the n corresponding values of the function, called keyValue. The keys must be monotonic non-decreasing and are not restricted to any interval. An interpolator node evaluates the function given any value of t via the set_fraction eventIn.

To the Top of this page


Sensor

DEF XForm Transformation { children [

ROUTE Clicker.touchTime TO TimeSource.startTime

ROUTE TimeSource.fraction_changed TO Animation.set_fraction

ROUTE Animation.value_changed TO XForm.rotation

A pointing device sensor is activated when the user points to geometry that is influemced by a specific pointing device sensor. These sensors have influence over all geometry that is descendant from the sensor's parent node.

To the Top of this page


Clock with alarm

Group { children [

ROUTE Hour.cycleTime TO Sounder.startTime

Time sensor nodes can execute for 0 or more cycles. Be careful when setting loop to TRUE since it incurs continous overhead.

In this example we set the time between two calls of the soundfile alarm.mid to one hour. cycleIntervall interprets the specified argument as seconds between two calls. That's why we set it to 3600 seconds, i.e. one hour.

The ROUTE statement connects the timer to sounder, which causes the soundfile at the URL http://clocks.at/alarm.mid to be executed once per hour.

To the Top of this page


Collision detection

DEF Walls Collision { children [

ROUTE Wall.collision TO Autsch.startTime

Browsers are responsible for defining the navigation behaviour when collisions occur. For example, when the user comes sufficiently close to an object to trigger a collision, the browser may have the user bounce of the object, come to a stop, or glide along the object's surface.

By default, all objects in the scene are solid, thus collidable. Browsers detect geometric collisions between the user's avatar and the scene's geometry, and prevent the avatar from entering the geometry.

If you want a ghost-like avatar that floats through objects you may use the Collision node to turn off collision detection for its descendants; you can also specify other events that should be triggered.

In this example we trigger the execution of the sound-file beule.wav which is located at the URL http://autsch.au/beule.wav

To the Top of this page


[arrow_icon] [action_button] [arrow_icon]

This page was created by Helmut Doleisch and Edgar Weippl on November 26, 1996.