This tutorial page explains basic vrmlscript in particular directOutput and array access methods.
Most commands in javascript should have a ; at the end of each command, which denotes the end of the command. Leaving the ; out will lead to Scripts which work in some vrml browsers but not others, any unexplained errors may well be due to missing ;'s.
For example x=y;
exceptions are the if (...) {...}, else {...} , for (...) {...}, and while (...) {...} loops/branches which are explained later in this guide.
Functions
Functions are included in javascript which are used much like normal definied functions.
First we look at the Math.xxx series of functions. They are generally used in the form of a=Math.xxx(x); Also normal functions can be made to return values in the same way
Math.xxx
function usage
Math.abs(x)
Returns the absolute value of x.
Math.acos(x)
Returns the arc cosine (in radians) of x.
Math.asin(x)
Returns the arc sine (in radians) of x.
Math.atan(x)
Returns the arc tangent (in radians) of x.
Math.ceil(x)
Returns the least integer greater than or equal to x.
Math.cos(x)
Returns the cosine of the variable x.
Math.exp(x)
Returns e, to the power of x (i.e. ex).
Math.floor(x)
Returns the greatest integer less than or equal to x.
Math.log(x)
Returns the natural log of the variable x.
Math.max(x,z)
Returns the larger of the two variables x and z.
Math.min(x,z)
Returns the smaller of the two variables x and z.
Math.pow(x,z)
Returns the value of the variable x to the zth power.
Math.random(x)
Returns a random number between 0 and x. If not used x is assumed to equal 1
Math.round(x)
Returns the variable x rounded to the nearest integer.
Math.sin(x)
Returns the sine of x, where x is expressed in radians.
Math.sqrt(x)
Returns the square root of x.
Math.tan(x)
Returns the tangent of x, where x is expressed in radians .
Now we look at the Browser.xxx series of functions.
Browser.xxx
function usage
Browser.getName()
Returns a string with the name of the VRML browser.
Browser.getVersion()
Returns a string containing the version of the VRML browser.
Browser.getCurrentSpeed()
Returns the floating point current rate at which the user is traveling in the scene.
Browser.getCurrentFrameRate()
Returns the floating point current instantaneous frame rate of the scene rendering, in frames per second.
Browser.getWorldURL()
Returns a string containing the URL of the currently loaded world.
Browser.replaceWorld(MFNode nodes)
Replace the current world with the passed list of nodes.
Browser.createVrmlFromString(String vrmlSyntax)
Parse the passed string into a VRML scene and return a the list of root nodes from the resulting scene.
Parse the passed URL into a VRML scene. When complete send the passed event to the passed node. The event is a string with the name of an MFNode eventIn in the passed node.
Remove the route between the passed eventOut and passed eventIn, if one exists.
Browser.loadURL(MFString url, MFString parameter)
Load the passed URL, using the passed parameter string to possibly redirect it to another frame. If the destination is the frame containing the current scene, this method may never return.
Browser.setDescription(String description)
Set the browser dependent description. This is the same function as that performed when the description field of the Anchor node is displayed
Strings
In any VRML node field which contains text, SFString and MFString fields, the text itself is contained within a set of "". In javascript this is no possible as the javascript itself is contained with a set of " ". Therefore we use ' ' instead.
for example:
x="test";
should read
x='test';
The print statement
print (*string*);
examples :
print ('a line');
print ( someValue ); // print the value contained in someValue
print ('a line' + someValue ); // print 'a line ' then the value contained in someValue
Where there is a need to put any " within a string, the usage of the escape code \ (backslash) is needed. The escape code is placed just before the code, in this case the " and the browser then ignores the code.
There are other special uses for the escape code. For example \n means add a return code to the string. This code \n is very usefull when you wish to print a stored string to the console for debugging or test purposes. There are also other letters which have special meanings so feel free to experiment to find out what they do.
Adding additional text to stored strings can be done two ways.
The first is to use the + symbol to add extra text to the end of what already is stored. For example text=text + 'a';
The second is to use +=. For example text+='b';
Also very usefull is the print(...); command. This also is used in much the same way as setting a string varable, as in it also allows the usage of + to join several strings tegether.
DEF AnyScript Script {
# A standard SFString field
field SFString text1 ""
# A standard SFString field
field SFString text2 "another new line"
url "javascript:
function initialize () {
// set text1 with two strings joined together
text1 = 'test results\n' + 'a new line\n'
// append the contents of text2 within a set of quotes
text1+='\"' + text2 + '\"'
print (text1 + '\nthirdline');
}
"
}
If... else
The for () command in javascript is used to generate looping commands.
The general form is:
if (*some argument*) {
// if branch
}
else {
// else branch
}
Valid arguments are:
(x==1) or (x==y) if first value equals than the second then branch
(x!=1) or (x!=y) if first value does not equal than the second then branch
(x<1) or (x<y) if first value is less than the second then branch
(x>1) or (x>y) if first value is greater than the second then branch
(x<=;1) or (x<=y) if first value is less than or equal to the second then branch
(x>=1) or (x>=y) if first value is greater than or equal to the second then branch
Also arguments can be combined.
if ((*argument1*)&&(*argument2*)) { if argument1 is true and argument2 is true then.
if ((*argument1*)||(*argument2*)) { if argument1 is true or argument2 is true then.
if ((*argument1*)!(*argument2*)) { if argument1 is true and not argument2 is true then.
DEF AnyScript Script {
# A standard SFInt32 field
field SFInt32 i 1
url "javascript:
function initialize () {
// put any scripting or calls to other functions
// which are run at startup of the world here
if (i==1) {
// if this (i equals 1) is true then code here is excuted
}
else {
// otherwise this code here is excuted
}
}
"
}
For... loops
The for () command in javascript is used to generate looping commands.
The general form is:
for (x=0; *some argument*; x++;) {
// commands to be excuted within this loop
// The line x++ means x is incremented by one while within this loop
}
the argument is the same as used with if () commands.
In the code above x begins at 0 and is incremented by one each time.
By using x-- instead of x++ the value of x is reduced each time.
DEF AnyScript Script {
# A standard SFInt32 field used as a counter
field SFInt32 i 0
# A standard MFVec3f field
field MFVec3f translations [ 1 1 1, 2 2 2, 3 3 3, ]
url "javascript:
function initialize () {
// the for loop prints each value of translations in turn
for (i = 0; i<translations.length; i++) {
print(translations[i]);
}
}
"
}
While... loops
The while () command in javascript is used to generate looping commands.
The general form is:
While (*some argument*;) {
// commands to be excuted within this loop
// do something which will make the above argument false
// when there is a need to exit this loop
}
the argument is the same as used with if () commands.
In the code above x begins at 0 and is incremented by one each time.
By using x-- instead of x++ the value of x is reduced each time.
DEF AnyScript Script {
# A standard SFInt32 field used as a counter
field SFInt32 i 0
# A standard MFVec3f field
field MFVec3f translations [ 1 1 1, 2 2 2, 3 3 3, ]
url "javascript:
function initialize () {
// the while loop prints each value of translations in turn
while (i<translations.length) {
print(translations[i]);
// increment i by 1
i++;
}
}
"
}