VRMLScript
/ JavaScript - Méthodes
Exemple 3 : Dans cet exemple, nous utilisons l'objet Math
et sa méthode random, pour d'une part,
positionner 2 sphères de façon aléatoire, et d'autre
part, modifier aléatoirement leur couleur.
Source:
#VRML
V2.0 utf8
#-
KDO - Cours VRML
#-
Méthodes JavaScript: Exemple 3
#---------------------------------
NavigationInfo
{
}
DirectionalLight
{
}
DEF
T_R1 Transform
{
children
[
Shape
{
appearance
Appearance {
material
DEF CO1
Material {
}
}
geometry
Sphere {}
}
]
}
DEF
T_R2 Transform
{
children
[
Shape
{
appearance
Appearance {
material
DEF CO2
Material {
}
}
geometry
Sphere {}
}
]
}
#-----------------------------------------
#
Ce script anime 2 balles aléatoirement
#-----------------------------------------
DEF
monScript Script
{
eventIn
SFTime anim
eventOut
SFVec3f Pos1
eventOut
SFVec3f Pos2
eventOut
SFColor Coul1
eventOut
SFColor Coul2
url
"javascript:
function
anim(t) {
X
= Math.random()
Y
= Math.random()
Z
= Math.random()
Coul1
= new SFColor(X,
Y, Z)
Coul2
= new SFColor(1-X,1-Y,1-Z)
X
= 3 -(X * 6)
Y
= 3 -(Y * 6)
Z
= 5 -(Z * 10)
Pos1
= new SFVec3f(X,
Y, Z)
Pos2
= new SFVec3f(Y,
Z, X)
}
"
}
DEF
Timer TimeSensor
{
cycleInterval
0.1
loop
TRUE
}
#-----
Appel du Script
ROUTE
Timer.cycleTime
TO monScript.anim
ROUTE
monScript.Coul1
TO CO1.set_diffuseColor
ROUTE
monScript.Coul2
TO CO2.set_diffuseColor
ROUTE
monScript.Pos1
TO T_R1.set_translation
ROUTE
monScript.Pos2
TO T_R2.set_translation
Analyse:
Notez que l'on a pas besoin de créer un objet Math
(il est prédéfini) . Dans le script, nous obtenons 3 valeurs
aléatoires (comprises entre 0 et 1), puis pour transformer ces 3
valeurs distinctes en une valeur de type SFColor
ou en coordonnées de type SFVec3f,
nous avons utilisé respectivement l'instruction new
SFColor(X,
Y, Z)
et l'instruction new
SFVec3f(X,
Y, Z).