Lighthouse3d.com

[an error occurred while processing this directive] Send me bugs and suggestions, please
VRML Interactive Tutorial
Full list

Script Tutorial

Introduction and Sintax
Defining Fields and Events
Accessing Fields and Events
When are scripts executed
Script Example: Highlight
Browser Script Interface
Appendix: Data types

Script Examples: Loading Contents to a Frame


In this section we shall see how to change the contents of a frame using a simple script. For this particular example we want to change the contents of a frame when the user approaches predefined zones of our world. In order to do this we need to define ProximitySensors for the required zones and a Script node to change the contents of the frame. The Script node uses the function loadUrl from the browser script interface.

The example for this section uses two frames. One contains a standard html page. The other contains the VRML world. The objective is to change the contents of the html frame when the user approaches certain zones of the VRML world.

First lets define our static world. In this case we are going to hava a small box and a small cone plus two ProximitySensors which define zones containing each of the geometries.

Example:
#VRML V2.0 utf8
Transform {
translation -2 0 0
children [
DEF psb ProximitySensor {
center 0 0 0
size 1 1 1
}
Shape {geometry Box{size 0.2 0.2 0.2}}
]
}
Transform {
translation 2 0 0
children [
DEF psc ProximitySensor {
center 0 0 0
size 1 1 1
}
Shape {geometry Cone{height 0.2 bottomRadius 0.1}}
]
}


In the static world two zones were defined by the proximity sensors. Depending on which zone the user approaches a different page is loaded in the html frame. In order to achieve this a script was defined with two EventIns, one for each url we are going to load into the html frame.

The ProximitySensor generates a boolean event isActive with the value TRUE when the user enters the defined zone, and the value FALSE when the user leaves the zone. We want to load contents in the html frame when the user enters the zone but there is no need to do it again when the user leaves the zone. Therefore each function of the script tests the parameter containing the events value. The function only loads the url into the new frame if the value is TRUE.

Example:
DEF sim Script {

eventIn SFBool enterBox

eventIn SFBool enterCone

url "javascript:

function enterBox(value,timeStamp) {

if (value)

browser.loadURL('http://sim.di.uminho.pt/vrmltut/sebox.htm','TARGET=HTML');

}

function enterCone(value,timeStamp) {

if (value)

browser.loadURL('http://sim.di.uminho.pt/vrmltut/secone.htm','TARGET=HTML');

}"

}


The above script has a little performance problem: the script attempts to load a page to the frame every time the user enters the frame regardless of the fact that the page may already be present in the frame. It is possible to avoid such a situation by using a field to keep the status of the html frame.

A SFString field, named status, was defined for this effect. The initial value of this field is 'None' which indicates that the user has not yet entered neither of the zones defined by the ProximitySensors. When a function is evaluated the status field is changed to reflect the update of the html frame. Also the value of this field is checked before the html frame is updated to prevent the loading of a frame which is already present.

Example:
DEF sim Script {

eventIn SFBool enterBox

eventIn SFBool enterCone

field SFString status 'None'

url "javascript:

function enterBox(value,timeStamp) {

if ((value) && (status != 'Box'))

{

status = 'Box';

browser.loadURL('http://www.lighthouse3d.com/vrml/tutorial/sebox.htm','TARGET=HTML');

}

}

function enterCone(value,timeStamp) {

if ((value) && (status != 'Cone'))

{

status ='Cone';

browser.loadURL('http://www.lighthouse3d.com/vrml/tutorial/secone.htm','TARGET=HTML');

}

}"

}



Now you can see the frames and the source code for the VRML world.