column

VRML Tech Talk

Last time, we looked at the nodes that create shapes in VRML. In this issue, we'll examine one of the nodes that affects the appearance of those shapes: the Material node. In order to understand how it works, we need to examine how light interacts with surfaces.

by Bernie Roehl

When light shines on an object, some of it reflected and some is absorbed. What we think of as the "color" of an object is actually the set of wavelengths of light that the object reflects; all the other wavelenghts are absorbed. For example, if we shine a white light on a red wall, then the wall absorbs all the green and blue light and reflects the red. If we were to shine a green light on a red wall, the wall would appear to be black; all the green light would be absorbed, and nothing would be reflected.

In theory, the color of an object is a function of the wavelength of the light striking it. In practice, however, it's usually sufficient to give just three numbers which measure the amount of reflection in three separate wavelength bands: the ones we think of as red, green and blue.

This RGB color scheme is used throughout the computer graphics industry, and is the basis for specifying color in VRML.

Diffuse and Specular Reflection

Depending on the very fine details of a surface, light can be reflected in either of two ways. If the microscopic structure of the surface is very rough, then the light that shines on the surface is reflected off in all directions; this scattered light is called "diffuse" reflection.

On the other hand, if the surface at a microscopic level is very smooth, then the light will be reflected off an angle equal to that of the incoming light. This is called "specular" reflection.

If you look at a glossy photograph, what you're seeing is specular reflection; the surface appears shiny because the light is being reflected mostly in one specific direction. If you look at a matte-finish photograph, there's very little specular reflection and lots of diffuse. This magazine is printed on glossy paper, and has a moderately high specular reflectivity; a newspaper, on the other hand, has almost no specular reflectivity at all. Metallic surfaces have a high specularity, whereas fabric has low specularlity.

No real-world surface is entirely smooth or entirely rough, so they all have some combination of diffuse and specular reflection. Both the diffuse and specular reflection properties are expressed as a color, using three numbers corresponding to the red, green and blue values.

For example, a material might have a diffuseColor of 0 0.5 0, meaning that it's a shade of green. This is because the first and last of the three numbers, the red and blue values, are both zero. The green value is 0.5, and since the maximum value for any one color component is 1.0, the green is only moderately bright.

The diffuseColor is basically the "color" of the surface itself. The specularColor determines the color of the specular highlights on the material. For example, the following node:

Material {
   diffuseColor 0.7 0.3 0
   specularColor 0 0 0.8
}

creates a material that's yellow (red plus green) with blue highlights.

The three components of the specularColor will usually be equal. This means that it's basically a shade of white, so it winds up taking on the color of the incident light (which is what happens with specular reflections in the real world). If the light has a high red component, then the specular highlights will also appear red.

Other Fields

In addition to the diffuseColor and specularColor, there are two other color fields defined in the Material node. The emissiveColor is used for those surfaces which are intended to be glowing. This isn't the same as being a light source; a light source has an effect on other surfaces, whereas the emissiveColor value for a material has no effect on anything else. For example, if you wanted a heating element on a stove to glow, you might usse an emissiveColor. Same for traffic lights, elevator buttons, and so forth.

The ambientColor determines how much of the ambient light in a room is diffusely reflected by a material. Since there's no explicit mechanism in VRML for setting the ambient light level, the meaning of this field is a bit vague; for now, think of it as a color that's simply added in after all the lighting calculations are done.

There's another property, the "shininess" factor, which determines how large the specular highlights are. A higher value (closer to 1.0) causes tightly focussed highlights, whereas a smaller value causes broader highlights.

The final material property is the transparency, which determines how much you can see through the surface to whatever's on the other side. A high transparency value (up to 1.0) causes the material to be more transparent, whereas a lower value makes it opaque.

Multiple Materials

A single Material node can define an entire array of values. For example, a Cube has six faces; each of those faces can have a separate material. Multiple values are separated by commas and surrounded by square brackets, so a multi-colored cube with a different shininess value on each face might look like this:

#VRML V1.0 ascii
Separator {
    Material {
        diffuseColor [ 1 0 0, 0 1 0, 0 0 1, \
1 1 0, 1 0 1, 0 1 1 ]
        shininess [ 0.8, 0.6, 0.3, 0.1, 0.9, 1.0 ]
    }
    Cube { }
}

Well, that's it for our discussion of the Material node. Next issue we'll look at how to apply a texture map to the surface of a shape.

If you need more details about VRML, a good starting point is the *VRML Repository, which has links to software, documentation and more. You may also want to check out Special Edition: Using VRML, a book I've co-authored with Stephen Matsuba. It discusses VRML in considerable detail, and gives you everything you need to start building complex and interesting worlds.


Bernie Roehl is probably best known for REND386, a freeware VR library for PC's. He's also the author of two books on virtual reality, including the popular "Playing God: Creating Virtual Worlds". Bernie is currently a regular columnist for both VR Special Report and VR News. He's also active on the VRML mailing lists, and is maintaining a collection of proposed changes to the VRML spec. His email address is broehl@sunee.uwaterloo.ca.