Defining and Instancing Nodes

VRML allows you to define a a set of nodes, or a node with particular field values, as a new node type. Suppose you want to draw a set of shapes, all having the same Appearance node. 

There are two ways of doing this: the hard way, which requires the Appearance node to be repeated for each shape; or the easy way in which you define the common Appearance node as being a new node type. 

The latter approach is not only easier, since you don't have to rewrite the defined node over and over again, but it also guarantees that the shapes share the same Appearance node. 

This has clear advantages when, for instance, you decide that the defined node should be altered. Defining the node once and using it multiple times you have to change only the node which is being defined instead of changing all occurrences of the defined node. 

Two keywords are provided: DEF and USE.

 Syntax for DEF: 
DEF name node 
where node is any of the VRML nodes, and name is the new node identifier. 

Syntax for USE: 
USE name 
where name is an identifier which has been previously defined using DEF. 

Note: When defining nodes in a inlined file the defined identifier can only be instanciated inside the inlined file, i.e. the identifier is not recognized outside the file where it is defined. 

Example: suppose that you want two red Shapes in your world. The VRML code to define this world could be something like: 

Shape { appearance 
DEF common_appearance Appearance
material Material {diffuseColor 1 0 0}
geometry Sphere { }


Transform {
 
translation -2 0 0 
children [
 
Shape {
 
appearance USE common_appearance 
geometry Cone { }
}


The result of the above code is: