|
|
New node types
Creating new node typesVRML prototypes can be used to create new node types that encapsulate behaviour and geometry. Instances of prototypes are created in the same way as the standard nodes provided by VRML browsers. They can be used to manage the complexity of virtual worlds and to facilitate reuse of VRML code. PROTOtypesThe structure of a Proto definition is as follows:
PROTO prototypename [
eventIn eventtypename name
eventOut eventtypename name
exposedField fieldtypename name defaultvalue
field fieldtypename name defaultvalue
...
]
{
Zero or more routes and prototypes
First node (defines the node type of the prototype)
Zero or more nodes (any type), routes, and prototypes
}
NameThe prototypename is the name of the new node type that the PROTO defines. InterfaceThe fields, exposedFields, eventIns, and eventOuts within the [ ] are the interface of the prototype, which is very similar to that of Script nodes. eventIns, eventOuts, and exposedFields are used to provide an event interface, fields and exposedFields are used to specify the node attributes. Fields and exposedFields should be supplied with default values. BodyThe body of the prototype describes the new node type and is defined between the {}. It can contain any other VRML nodes, including other node types defined using prototypes previously. In addition to nodes, the protoype body can contain routes that define an event circuit in the new node. At it's simplest, you can simply place some geometry in a prototype body, and use the new node type to define nodes with that geometry in a model. For example, a billboard and a texture of a tree could be placed in a prototype body of a prototype with name 'Tree', with no interface, and could then be instantiated by writing Tree{} in the VRML file. At it's most complex, a prototype can be a complete encapsulated virtual world. Important: The type of the new node is that of the first node in the body of the prototype. This ensures that you can create new nodes that can be used following the usual node type rules that apply to USE-ing DEF-ined nodes. For example, to create new nodes of node type Material to encapsulate colour definintion, you can write something like this:
PROTO Red [] {
Material { diffuseColor 1 0 9 }
}
#Test it:
Shape {
appearance Appearance {
material Red {}
}
geometry Sphere {}
}
Using the interface from the bodyTo associate items defined in the interface with fields in the body, the keyword 'IS' is used. Instead of supplying a value after a field, 'IS interfaceItemName' is used instead (where interfaceItemName is the name of the interface item...). For example:
PROTO ColorSphere [
field SFColor theColor 1 0 0 #default is red
] {
Shape {
appearance Appearance {
material Material {
diffuseColor IS theColor
}
}
geometry Sphere {}
}
}
This also applies to Script nodes, so you can access an eventIn or eventOut of a Script node within the prototype body via an eventIn or eventOut in the prototype's interface using the IS keyword. We will look at some more examples in the next section. External PrototypesPrototypes are fine, but they have to be defined in the same VRML file that they are used in before they are used. If you want to create a reusable library of PROTOtypes then that is not very convenient since you would have to cut and paste all of your code into every file that used a particular prototype. External Prototypes solve this problem. An external prototype (EXTERNPROTO)has a prototypename and an interface, almost like a normal prototype, but instead of a body, a url is defined. The url point to a file containing the complete prototype. Unlike a normal PROTOtype interface, the interface of the EXTERNPROTOtype does not include default values for interface items. For example: EXTERNPROTO ColorSphere [ field SFColor theColor ] "ColorSpherePROTO.wrl" You can actually supply a different prototypename to the one used in the url, if you want to change the node type name, although that might be confusing to debug... Prototype libariesIf the url refers to a file containing several PROTOtypes (for example if you want to us a 'library' of prototypes where the prototypes are defined in the same file) then the # symbol should be used followed by the prototype name to indicate which prototype the EXTERNPROTO refers to. e.g "ColorShapeLibrary.wrl#ColorSpherePROTO". |
|
|
Michael Louka, October 23, 2001 |