Titania X3D Editor

Bring Colours to Your Life.

Titania v4.3.4

Bring Colours to Your Life.

X3D Reference

Writing program scripts with ECMAScript

Motivation

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 field:

XML Encoding

Classic Encoding

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:

XML Encoding

Classic Encoding

Script fields needed:

  • Bounce height

XML Encoding

Classic Encoding

Inputs and outputs needed:

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

XML Encoding

Classic Encoding

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

XML Encoding

Classic Encoding

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

XML Encoding

Classic Encoding

Routes needed:

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

XML Encoding

Classic Encoding

XML Encoding

Classic Encoding

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:

XML Encoding

Classic Encoding

Using a toggle switch

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

XML Encoding

Classic Encoding

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.

XML Encoding

Classic Encoding

Using a color selector

XML Encoding

Classic Encoding

Summary

  • 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

Cookies and other Technologies

By clicking »Accept« or continuing to use the site, you agree to the use of CREATE3000 and third-party cookies and other similar technologies to enhance your browsing experience, analyze and measure your engagement with our content, and provide more relevant ads on CREATE3000 websites and others. Learn more about cookies. You can withdraw your consent at any time.