The Switch node
Introduction
This tutorial page assumes a basic understanding of vrmlscript and VRML.
Unlike many other nodes which proform functions the Switch node can only be control from a Script node.
The Switch node
Example 1
How to have everything in the choice field active at once. Its easy if you use DEF and USE.
#VRML V2.0 utf8
# example of Switch node structure
Switch {
whichChoice 2
choice [
DEF A Shape {}# 0
DEF B Shape {}# 1
DEF C Shape {}# 2
# 3
DEF ALL Group { children [ USE A USE B USE C ] }
]
}
Example 2
This example uses a Script node to Control a Switch node. This in turn chooses whether the box or sphere is shown
#VRML V2.0 utf8
# Clicking on the object changes it's shape
# Group node
Group {
children [
DEF Touch TouchSensor {}
DEF Sw Switch {
whichChoice 0
choice [
DEF Sphere Shape{
appearance Appearance {
material Material {
diffuseColor 1 0 0
}
}
geometry
Sphere {}
}
DEF Box Shape{
appearance Appearance {
material Material {
diffuseColor 0 1 0
}
}
geometry
Box {}
}
]
}
]
}
# Basicaly this script outputs 0, 1, 0, 1 etc with a change each time is triggered
DEF Control Script {
eventIn SFBool isActive
eventOut SFInt32 set_whichChoice
field SFInt32 shapeChoice 0
url "javascript:
function isActive (val,ts) {
// if val is true then mouse button clicked
if (val) {
if (shapeChoice==0) {
shapeChoice=1;
set_whichChoice=shapeChoice;
return;
}
if (shapeChoice==1) {
shapeChoice=0;
set_whichChoice=shapeChoice;
return;
}
}
}
"
}
# ROUTE TouchSensor to Script
ROUTE Touch.isActive TO Control.isActive
# ROUTE Script to Switch
ROUTE Control.set_whichChoice TO Sw.whichChoice
Examples
The above example in completed form Basic Switch node
Notes
Setting the whichChoice field of a Switch node to -1 hides all the nodes within it.
|