VRML example: CAR

car.wrl

This example shows some basic built-in features of VRML, such as lighting, transparency, etc.

#VRML V1.0 ascii
All VRML files start with a header to identify the version and information encoding scheme. In this case, ASCII format is used.

DEF World Separator {
First the whole world is specified as one object for good object oriented design and formal VRML language requirement. This enables the current world to be used as a component of a larger world. Separator is used as a object wrapper.

   Transform {
      rotation 1 0 0 0.52359878
   }
Have the whole world rotated 30 degrees on the x axis.

   DirectionalLight {
      on        TRUE
      intensity 1
      color 1 1 1
      direction 1 0 0
   }
A light is put in front of the car. This light is not enclosed in a Separator because it is supposed to illuminate all the objects within this world.

   DEF Car Separator {
Here is the definition of the car object.

      DEF LeftFrontWheel Separator {
         Transform {
            translation -2.0 0 2 
         }

         Material {
            diffuseColor 1.0 0 0
         }

         DEF Wheel Sphere {
            radius 1
         }
      }
The left front wheel is defined here. The transform node is enclosed within an object, so it only affects the children objects. Material node is used to define the color of the wheel.

      DEF RightFrontWheel Separator {
         Transform {
            translation -2.0 0 -2 
         }

         Material {
            diffuseColor 0 1.0 0
         }

         USE Wheel
#        Sphere {
#           radius 1
#        }
      }
Similarly, the right front wheel is defined. However, the actual definition of the wheel is done by aliasing the previously defined object, Wheel. The Wheel object replaced the three commented lines. This can reduce the size of the VRML file.

The keyword, DEF, gives a name to an object node, and the definition of the object can be referenced later by this name. The name need not be unique. If multiple definitions use the same name, then the name refers to the last definition just before the use of the name.

      DEF LeftRearWheel Separator {
         Transform {
            translation 2.0 0 2 
         }

         Material {
            diffuseColor 0 0 1.0
         }

         USE Wheel
#        Sphere {
#           radius 1
#        }
      }

      DEF RightRearWheel Separator {
         Transform {
            translation 2.0 0 -2 
         }

         Material {
            diffuseColor 1.0 1.0 0
         }

         USE Wheel
#        Sphere {
#           radius 1
#        }
      }
Define the rest of the wheels. Note the lighting on the wheels. There is no shadow, and the light seems to past through the front wheels. This artifact appears because the browser can only do shading.

      DEF WindShield Separator {
         Transform {
            translation -2 1 0
            rotation 0 0 1 -1.0471976
         }
         Material {
            diffuseColor 0.2 0.2 0.2
            specularColor 0.8 0.8 0.8
            shininess 0.7
            transparency 0.8
         }
         Cube {
            width 0.01
            height 2.0
            depth 1.5
         }
      }
The windshield is defined by using a transparency field of the material node. The transparency, in this case, is represented a sparse populated cluster of dots. This is specific to this browser.

      DEF LeftSign Separator {
         Transform {
            translation 0 0 1.01
            rotation 0 1 0 -1.5707963
         }
         DEF Sign Separator {
            Material {
               diffuseColor 1.0 1.0 1.0
               specularColor 0.8 0.8 0.8
               shininess 0.7
            }
            Texture2 {
               filename "number.gif"
            }
            Cube {
               width 0.01
               height 1.0
               depth 1.0
            }
         }
      }

      DEF RightSign Separator {
         Transform {
            translation 0 0 -1.01
            rotation 0 1 0 -1.5707963
         }
         USE Sign
#        Material {
#           diffuseColor 1.0 1.0 1.0
#           specularColor 0.8 0.8 0.8
#           shininess 0.7
#        }
#        Texture2 {
#           filename "number.gif"
#        }
#        Cube {
#           width 0.01
#           height 1.0
#           depth 1.0
#        }
      }
The race car sign is constructed by a texture mapping, a gif image, on to the surface of a cube. The material node gives the sign's surface properties. Texture map in VRML is always rectangular.

      DEF DrivingWheel Separator {
         Transform {
            translation -1.2 0.5 0.2
            rotation 0 0 1 -1.5707963
         }
         Material {
            diffuseColor 0.2 0.2 0.2
         }
         Cylinder {
            parts ALL
            radius 0.5
            height 0.01
         }
      }

      DEF Body Separator {
         Material {
            diffuseColor 1.0 1.0 1.0
         }
         Cube {
            width 6
            height 1
            depth 2
         }
      }
Here we are defining the rest of the parts. The cylinder can be specified to show only the sides, the top, the bottom or everything. Material can be specified for each part, top, bottom and/or sides. Texture mapping is applied to each part differently.

      DEF RightHeadLight Separator {
         Transform {
            translation -3.0 0.3 -0.5
         }
         DEF HeadLight Separator {
            Separator {
               Transform {
                  translation -0.2 0 0
                  rotation 0 0 1 -1.5707963
               }
               Material {
                  diffuseColor 1.0 0 0
               }
               Cone {
                  parts SIDES
                  bottomRadius 0.09
                  height 0.5
               }
            }
            Separator {
               Transform {
                  translation -0.45 0 0
               }
               Material {
                  emissiveColor 1.0 1.0 0
               }
               PointLight {
                  intensity 1
                  color 1.0 1.0 0.9
               }
               Sphere {
                  radius 0.1
               }
            }
         }
      }

      DEF LeftHeadLight Separator {
         Transform {
            translation -3.0 0.3 0.5
         }
         USE HeadLight
#        Separator {
#           Transform {
#              translation -0.2 0 0
#              rotation 0 0 1 -1.5707963
#           }
#           Material {
#              diffuseColor 1.0 0 0
#           }
#           Cone {
#              parts SIDES
#              bottomRadius 0.09
#              height 0.5
#           }
#        }
#        Separator {
#           Transform {
#              translation -0.45 0 0
#           }
#           Material {
#              emissiveColor 1.0 1.0 0
#           }
#           PointLight {
#              intensity 1
#              color 1.0 1.0 0.9
#           }
#           Sphere {
#              radius 0.1
#           }
#        }
      }
   }
The headlight consists of three objects, a bottomless cone, a sphere and a point light source. The PointLight is located inside of the sphere and it gives out slightly yellow light. The light is filtered by the sphere's surface properties, emissive yellow color. The result is a yellow light with a hint of high intensity.

   Transform {
      translation 0 -2 5
   }
Move all the rest of the objects away from the car.

   WWWAnchor {
      name "http://www.lamborghini.com/"

      Transform {
         translation 4.5 0 0
         rotation 1 0 0 -1.0471976
         scaleFactor 0.125 0.125 0.125
         center 0 0 0
      }

      AsciiText {
         string "Lamborghini"
         spacing 1
         justification CENTER
         width   0
      }
   }
The string can be transformed as any other objects. In addition, links can be added as in other VRML object.

   WWWAnchor {
      name "http://www.bmwusa.com/"

      Transform {
         translation -4.5 0 0
         rotation 1 0 0 -1.0471976
         scaleFactor 0.125 0.125 0.125
         center 0 0 0
      }

      AsciiText {
         string "BMW"
         spacing 1
         justification CENTER
         width   0
      }
   }

   WWWAnchor {
      name "http://www.ford.com/"

      Transform {
         translation 4.5 -1 2
         rotation 1 0 0 -1.0471976
         scaleFactor 0.125 0.125 0.125
         center 0 0 0
      }

      AsciiText {
         string "Ford"
         spacing 1
         justification CENTER
         width   0
      }
   }

   WWWAnchor {
      name "http://www.gm.com/"

      Transform {
         translation -4.5 -1 2
         rotation 1 0 0 -1.0471976
         scaleFactor 0.125 0.125 0.125
         center 0 0 0
      }

      AsciiText {
         string "General Motors"
         spacing 1
         justification CENTER
         width   0
      }
   }
}