#VRML V2.0 utf8
##:@* geometry from scripts
##- This example demonstrates the creation
##- of geometry. This is accomplished through
##- the ECMAScript's createVrmlFromString method.
##-
##- This code may be reused in part or total.
##- Please include this paragraph when it is used.
##- Copyright 2000, Leonard Daly
##@ world characteristics
Viewpoint {
position 0 10 30
}
NavigationInfo {
type ["FLY", "ANY"]
headlight FALSE
}
Background {
skyColor [0.1 0.8 1]
groundColor [.3 .8 .2, .3 .8 .8]
groundAngle [1.57]
}
##@ ground
## Define the ground and the controlling shapes
DEF Ground Transform {
children [
Shape { # Transparent "ground"
appearance Appearance {
material Material {
transparency 1
}
}
geometry Box {
size 200 .01 200
}
}
]
}
##@ animation control
## The animation controls with lighting
## The up-pointing cone adds spheres with looping animation
## The down-pointing cone adds spheres without looping animation
Transform {
translation 0 1 0
children [
DirectionalLight {
direction 0 -1 0
}
DirectionalLight {
direction 0 1 0
}
Transform { # Looping animation addition
translation 2 0 0
children [
DEF TouchLoop TouchSensor {}
Shape {
appearance Appearance {
material Material {
diffuseColor 0.8 0.4 0
specularColor 0 1 1
shininess .7
}
}
geometry Cone {}
}
]
}
Transform { # Non-looping animation addition
translation -2 0 0
rotation 1 0 0 3.14
children [
DEF TouchSingle TouchSensor {}
Shape {
appearance Appearance {
material Material {
diffuseColor 0.4 0.8 0
specularColor 1 0 1
shininess .7
}
}
geometry Cone {}
}
]
}
]
}
##@ parent for bubbles
## Parent node with lighting for bubbles. The 'Crystal' name
## is left-over from a previous incarnation
Group {
children [
DirectionalLight {
direction -.47 -.47 -.47
color 1 1 1
}
DirectionalLight {
direction .47 .47 .47
color .8 .8 1
}
DEF CrystalNode Group {}
]
}
##@ timesensors
## Need 2 TimeSensors -- 1 for each animation control. This
## could be reduced to 1 with a more complex script mechanism
## for tracking which control was being processed.
DEF TimerLoop TimeSensor {
cycleInterval 1
loop TRUE
enabled FALSE
startTime 1
stopTime 0
}
DEF TimerSingle TimeSensor {
cycleInterval 1
loop TRUE
enabled FALSE
startTime 1
stopTime 0
}
##@ script
## Controlling script. This script generates the coordinates,
## cycle time, and color of the new bubble. It also inserts
## the new bubble into the geometry of this world.
DEF Manager Script {
directOutput TRUE
field SFNode CrystalNode USE CrystalNode
field SFFloat seed 432113
field SFInt32 ndxN -1
field SFString tstr ""
field SFString gstr ""
eventIn SFTime loopCrystal
eventIn SFTime singleCrystal
##@ functions
url "vrmlscript:
function loopCrystal (value, time) {
addCrystal (value, time, 'TRUE');
}
function singleCrystal (value, time) {
addCrystal (value, time, 'FALSE');
}
##@ coordinates
// Compute coordinates. Allowable range is a box
// that sits above the X-Z plane and extends +/-5
// in X or Z and 20 in Y. The color is determined
// randomly in red, green, and blue. The cycle time
// is also determined randomly from 1 to 20 seconds.
function addCrystal (value, time, loop) {
sx = Math.floor (Math.random(seed) * 20) + 1;
sz = Math.floor (Math.random(seed) * 10) + 1;
sy = Math.floor (Math.random(seed) * 20)/10 + .1;
xc = (Math.floor (Math.random(seed) * 20 - 10) * 100) / 100;
zc = (Math.floor (Math.random(seed) * 20 - 10) * 100) / 100;
yc = (Math.floor (Math.random(seed) * 12 + 1) * 100) / 100;
rd = (Math.floor (Math.random(seed) * 100)) / 100;
gn = (Math.floor (Math.random(seed) * 100)) / 100;
bl = (Math.floor (Math.random(seed) * 100)) / 100;
tt = (Math.floor (Math.random(seed) * 190)) / 10 + 1;
##@ create crystals
// For sphere crystals, use the rotation as the color
ndxN ++;
gstr = 'SphereCrystal{startTime ' + time + ' cycleInterval ' + tt + ' loop ' + loop + ' color ' + rd + ' ' + gn + ' ' + bl + '}';
tstr = 'EXTERNPROTO SphereCrystal [exposedField SFTime startTime exposedField SFTime cycleInterval exposedField SFBool loop exposedField SFColor color] ';
// tstr = tstr + '\"http://www.realism.com/vrml/Example/Bubbles/CubeCrystal.wrl#SphereCrystal\" ';
tstr = tstr + '\"@vr-x-crystal.wrl#SphereCrystal\" ';
tstr = tstr + 'DEF N' + ndxN + ' Transform {translation ' + xc + ' ' + yc + ' ' + zc;
tstr = tstr + ' children [ ' + gstr + '] }';
##@ comments
// To see the VRML string used to create the node, uncomment
// the 'print' line and look in the console window.
// 'xform' contains the parsed version of the VRML string.
// createVrmlFromString is a method in the Browser object.
// Once the VRML is parsed, it can be added to the scene
// geometry with the addChildren method (event) of the
// appropriate grouping node (CrystalNode in this case).
// print (tstr);
xform = new MFNode();
xform = Browser.createVrmlFromString(tstr);
CrystalNode.addChildren = xform;
}
"
}
##@ event routing
## Connect the TimeSensors to the Script eventIns to make
## thinks work.
ROUTE TouchSingle.isActive TO TimerSingle.enabled
ROUTE TimerSingle.cycleTime TO Manager.singleCrystal
ROUTE TouchLoop.isActive TO TimerLoop.enabled
ROUTE TimerLoop.cycleTime TO Manager.loopCrystal
##>