425

Writing program scripts with JavaScript
JavaScript script example code

DEF Ball Transform {
    children [
        Shape {
            appearance Appearance {
                material Material {
                    ambientIntensity 0.5
                    diffuseColor 1.0 1.0 1.0
                    specularColor 0.7 0.7 0.7
                    shininess 0.4
                }
                texture ImageTexture { url "beach.jpg" }
                textureTransform TextureTransform { scale 2.0 1.0 }
            }
            geometry Sphere { }
        }
    ]
}
DEF Clock TimeSensor {
    cycleInterval 2.0
    startTime 1.0
    stopTime 0.0
    loop TRUE
}
DEF Bouncer Script {
    field    SFFloat bounceHeight 3.0
    eventIn  SFFloat set_fraction
    eventOut SFVec3f value_changed

    url "vrmlscript:
        function set_fraction( frac, tm ) {
            y = 4.0 * bounceHeight * frac * (1.0 - frac);
            value_changed[0] = 0.0;  // X
            value_changed[1] = y;    // Y
            value_changed[2] = 0.0;  // Z
        }"
}
ROUTE Clock.fraction_changed TO Bouncer.set_fraction
ROUTE Bouncer.value_changed  TO Ball.set_translation