Titania X3D Editor

Bring Colours to Your Life.

Titania v4.3.0

Bring Colours to Your Life.

X3D Reference

Writing program scripts with ECMAScript


A program script implements the Script node using values from the interface:

  • The script responds to inputs and sends outputs

A program script can be written in JavaScript, ECMAScript, and other languages

  • JavaScript is easier to program
  • ECMAScript is essentially JavaScript

Declaring a program script interface

For a JavaScript program script, typically give the script in the Script node's url fiel:

Initializing a program script

The optional initialize function is called when the script is loaded:

Initialization occurs when:

  • the Script node is created (typically when the browser loads the world)

Shutting down a program script

The optional shutdown function is called when the script is unloaded:

Shutdown occurs when:

  • the Script node is deleted
  • the browser loads a new world

Responding to events

An input function can be declared for each input.

The input function is called each time an event is received, passing the event's:

  • value
  • time stamp

Processing events in JavaScript

If multiple events arrive at once, then multiple input functions are called.

The optional eventsProcessed function is called after all (or some) input functions have been called:

Accessing fields from JavaScript

Each interface field is a ECMAScript variable:

  • Read a variable to access the field value
  • Write a variable to change the field value

Accessing outputs from ECMAScript

Each interface output field is a ECMAScript variable

  • Read a variable to access the last output field value
  • Write a variable to send an event on the output field

A sample JavaScript script

Create a Bouncing ball interpolator that computes a gravity-like vertical bouncing motion from a fractional time input.

Nodes needed:

Script fields needed:

  • Bounce height

Inputs and outputs needed:

  • Fractional time input
  • Position value output
  • Enabled input/output

Initialization and shutdown actions needed:

  • None - all work done in input function

Event processing actions needed:

  • set_fraction input function
  • No need for eventsProcessed function

Calculations needed:

  • Compute new ball position
  • Send new position event

Use a ball position equation roughly based upon Physics:

  • See comments in the X3D file for the derivation of the equation

Routes needed:

  • Clock into script's set_fraction
  • Script's value_changed into transform

Building user interfaces

Program scripts can be used to help create 3D user interface widgets:

  • Toggle buttons
  • Radio buttons
  • Rotary dials
  • Scrollbars
  • Text prompts
  • Debug message text

Building a toggle switch

A toggle script turns on at 1st touch, off at 2nd:

Using a toggle switch

Use the toggle switch to make a lamp turn on and off

Building a color selector

The lamp on and off, but the light bulb doesn't change color!

A color selector script sends an on color on a TRUE input, and an off color on a FALSE input.

Using a color selector


  • The initialize and shutdown functions are called at load and unload
  • An input function is called when an event is received
  • The eventsProcessed function is called after all (or some) events have been received
  • Functions can get field values and send event outputs