VRML
Contents
About
VRML (Virtual Reality Modeling Language), often pronounced "vermal" is a standard text file format for representing 3D interactive vector graphics, designed with the World Wide Web in mind. VRML has since been superseded by X3D, however VRML remains widely used as a file format for interchange of 3D models, particular from 3D animation programs and CAD programs. On this page I have listed out websites and code snippets pertaining to VRML. Note that there are two version of VRML - VRML1 (released 1994) and VRML2 (released 1997), and this article focusses mostly on the latter.
Brief History
VRML was first introduced in 1994, a version now known as VRML1 and mostly obsolete. In 1997 a new version of the format, VRML2 (although often called VRML97), was finalized and became an ISO standard. VRML files have a .vrml or .wml extension. Although VRML generated much interest and support in education and research, it has never seen widespread use on the internet. Few web browsers ever allowed native viewing of these 3D files and instead require you to install 3rd party plugins before you can view .vrml and .wrl inside your web browser (which is a bit of a pain). The VRML Consortium changed its name to the Web3D Consortium, and released X3D in 2004 as an XML-based successor to VRML. Although VRML is technically superseded by X3D, and rarely are either seen on the www nowadays, many 3D programs, especially older ones, still support only VRML97 or in rare cases VRML1 for importing and exporting, hence it still an important 3D format for file interchange.
Web Sites
- Installing a VRML viewer:
- VRML Plugin and Browser Detector - Helps you detect and install a plugin so that you can view VRML files in your browser. This website is supported by NIST, and they actually list about 30 standalone programs and toolkits for VRML and X3D. The one I installed on my Mac is called [FreeWRL] and another popular free tool is [OpenVRML].
- FreeWRL/FreeX3D - FreeWRL is a cross platform (window, Mac, Linux), plugin (allows you to view these files inside browser), standalone (allows you to view files outisde the browser) and toolkit for viewing VRML and X3D files. One nice feature is that it comes with a directory containing a few dozen .wrl files and one .x3d file which you can open in FreeWRL to view or open in any text editor to see the source code.
- OpenVRML - another free multi-platform plugin for VRML.... but unfortunately is a bit tricky to install.
- Learning VRML:
- Introduction to VRML 2.0 - a site which is still under construction, but represents and excellent overview of all the main concepts and the basics of VRML, and has some really great examples, including the pyramid I copied below.
- VRML2.0 Specifications - describes the ISO standard for VRML, including the all important list of Nodes (there are about 50!).
- The Annotated VRML 97 Reference - gives a good introduction and (more importantly) has a great Node reference section where they give an example for every node with source code and an "example" link to launch the .wml file.
Code Snippets
Example 2: Creating a simple mesh
Often you may want to represent complex shapes, and to do this the easiest way is using a "IndexFacSet" geometry, where all the points are first listed in a "point" array, and then sets of points joined together to form each face in the "coordIndex" array. In the example below we have generated a red square-based triangular pyramid with five points, and five faces. You'll notice the bottom face is a square (it lists 4 points instead of 3), but this will effectively get drawn as or converted to two triangules in most 3D programs.
#VRML V2.0 utf8
# A red pyramid with a square base, used to demonstrate the
# "IndexedFaceSet" geometry to generate a triangular mesh
Shape {
appearance Appearance{
material Material {
diffuseColor 1 0 0 # Simple red.
}
}
geometry IndexedFaceSet {
coord Coordinate {
point [
# bottom points:
-1.0 0.0 1.0, # Vertex 0 (bottom, front, right).
1.0 0.0 1.0, # Vertex 1 (bottom, front, left).
1.0 0.0 -1.0, # Vertex 2 (bottom, back, left).
-1.0 0.0 -1.0, # Vertex 3 (bottom, back, right).
# top point:
0.0 2.0 0.0 # Vertex 4 (top point).
]
}
coordIndex [
0, 3, 2, 1, 0, -1, # Bottom square
0, 1, 4, -1, # Triangular side1 (front).
1, 2, 4, -1, # Triangular side2 (left).
2, 3, 4, -1, # Triangular side3 (back).
3, 0, 4, -1, # Triangular side4 (right).
]
}
}
Example 3: Setting up a scene hierarchy and reusing elements with "DEF"
In this next example we build on the previous example, but we want to add three white spheres below the pyramid to look like a head and two eyes. Whenever you want multiple objects to share the same material/appearance, it is tedious and inefficient to list it out each time, so this is a good place to highlight how you can use the "DEF" command to define something - in this case an appearance, but it can also be useful for duplicating shapes or groups of shapes - and then use it multiple times. The DEF tag also gives that object a name in the scene hierarchy, which is useful if you intend to import the VRML file into another program and see logical names. Once something is defined, it can be reused as many times as possible, and although the most code-efficient method would be to create our "MAT_obj2_whiteMat" within the first sphere, it can lok a little messy, so I've instead defined both appearances/materials at the beginning of the file in shape objects which have no geometry.
#VRML V2.0 utf8
# A tilted red pyramid and three spheres in the shape of a
# face with a pyramid hat
DEF character Transform {
children [
# DEFINE APPEARANCES/MATERIALS:
Shape{
appearance DEF MAT_obj1_red_Mat Appearance {
material Material {
ambientIntensity 0.5 # How much ambient light reflects.
diffuseColor 1 0 0 # Basic red.
shininess 0.5 # We'll make it shiny.
specularColor 1 1 1 # Make the shine color white (this and above dictate specular highlights).
}
} # A shape typically includes an appearance and geometry,
# but here we have omitted a geometry.
}
Shape{
appearance DEF MAT_obj2_whiteMat Appearance {
material Material {
diffuseColor 1 1 1 # Basic white.
transparency 0.5 # We'll make it transparent to make it more interesting.
}
}
}
# SETUP SCENE HIRARCHY AND APPLY THE RELEVANT
# MATERIAL TO EACH SHAPE:
DEF obj1_hat Transform {
children [
DEF pyramid_hat Transform {
center 0 0 0 # Center of rotation.
rotation 1 0 0 0.2618 # Rotate 15 degs (15/180*pi) around X.
children [
Shape { # our pyramid from the previous example
appearance USE MAT_obj1_red_Mat
geometry DEF FACESET_poly IndexedFaceSet {
ccw FALSE
solid FALSE
coord Coordinate {
point [ -1 0 1, -1 0 -1, 1 0 -1, 1 0 1, 0 2 0 ]
}
coordIndex [ 0,3,2,1,-1,
4,3,0,-1,
4,2,3,-1,
4,1,2,-1,
4,0,1,-1
]
}
}
]
}
]
}
DEF obj2_head Transform {
translation 0 0 0 # Adjust this to move the whole head (including both eyes).
children [
DEF head Transform {
translation 0 -1 0
children [
Shape {
appearance USE MAT_obj2_whiteMat # Here we say what appearance to use.
geometry Sphere {
radius 1.2 #radius in units
}
}
]
}
DEF left_eye Transform {
translation -1 -1 0.5
children [
Shape {
appearance USE MAT_obj2_whiteMat
geometry Sphere {
radius 0.5
}
}
]
}
DEF right_eye Transform {
translation -1 -1 -0.5
children [
Shape {
appearance USE MAT_obj2_whiteMat
geometry Sphere {
radius 0.5
}
}
]
}
]
}
]
}
DEF simple_point_light Transform { # And lets add a light too.
translation 0 5 0 # Put this above the head.
children [
PointLight {
on TRUE
intensity 1
ambientIntensity 0
color 1 1 1
location 0 0 0
attenuation 1 0 0
radius 100
}
]
}
I designed this file to import nicely into Cinema 4D, although if you use this particular program be sure to uncheck "optimize hierarchy" if you want every level maintained (otherwise it will collapse anything with only one child). The hierarchy of "scene graph" of this file looks like this:
- character (transform / null object)
- obj1_hat (transform / null object)
- pyramid_hat (shape / null object)
- poly (indexed face set / polygon)
- pyramid_hat (shape / null object)
- obj2_head (group / null object)
- head (shape / null object)
- Sphere (sphere)
- left_eye (shape / null object)
- Sphere (sphere)
- right_eye (shape / null object)
- Sphere (sphere)
- head (shape / null object)
- obj1_hat (transform / null object)
- simple_point_light (shape / null object)
- Light (point light)
VRML Format
VRML1 versus VRML2
As mentioned before, VRML1 is obsolete, but for anyone interested in the differences between VRML1 and VRML2 there is a summary below courtesy of VRML Works by Bob Crispen. Briefly, VRML 1.0 worlds are static. VRML 2.0 worlds can move and interact with the visitor to those worlds.
VRML1.0 | VMRL2.0 |
---|---|
* Standard objects (cube, sphere, cone, cylinder, text)
|
All VRML1.0 features plus:
|
See Also
- OBJ file format - a 3D vector file format which is easier to read/write than VRML, but is much less versatile.
Links
- VRML - Wikipedia
- VRML Plugin and Browser Detector - helps detect if your browser has a VRML plugin installed and if not (since you probably don't), will help you install one.
- Notes on VRML1, VRML2, and X3D files - quite extensive notes on VRML written by Blaze Montanuh. A lot of inspiration for this page was taken from Blaze's notes.