write and use a prototype

Prototype is an reusable VRML object.
A prototype is equivalent to a VRML node but it is create by developers.

Note:
Before write a prototype, see prototypes conventions chapter.


This tutorial present the creation of flower prototype (prototype draw a flower).

First, we create a new VRML file in src folder: flower.wrl.

A prototype definition start with PROTO keyword and the name of prototype.

Note:
<name> start by a upper case (by convention).

We start the prototype with the following code:


We define 7 circles to draw the flower.
Flower color is configurable via a color field and default color is yellow (exposedField SFColor color 0.9 0.9 0.0).
We use IS keyword to use the color field (emissiveColor IS color).
Flower prototype source code :


In previous part, we created the Flower prototype. In this part, we use the prototype.

On main VRML file, we declare and import prototype with EXTERNPROTO keyword:


Prototype is imported. We can use the prototype in current scene like a node.

Note:
We can use the color field to modify flower color.


To add interactivity with the prototype, we can use events.
We declare event in fields part with eventIn and eventOut keywords.
eventIn receive an event and we can process it. eventOut send an event and event can be catch by a ROUTE instruction.

Example with flower prototype:

All flower events return the current time in second.

On flower.wrl file, we declare the events:


Flower animation require a timer and a scalar interpolator (See animation tutorial).
We add TimeSensor and ScalarInterpolator node to flower prototype.

Note:
cycleInterval field value is 0, else animation is started when widget is launched.


We move flower shapes into a Transform2D node to apply rotation at all circles.


And link timer to interpolator, and interpolator to flower shapes.


Animation part is created. Now, we create a script node to process events and manage animation.


Into script node, we declare events and fields.
We use IS keyword to link script events to prototype events.


We create 3 functions to process events.

launchAnimation() function is launched when launchAnimation event is received.
launchAnimation() function starts the timer (and the animation) also if animation is not already started.


stopAnimation() function is launched when stopAnimation event is received.
stopAnimation() function stops the timer (and the animation) and sends animationStopped event.


endOfAnimation() function is launched when timer is stopped.
endOfAnimation() function sends =animationStopped= event.


Final prototype source code:


Custom main VRML file to launch prototype animation:


Note:
Circle shape does not support rotation...


MeMo SDK contains some prototypes.
See Prototypes chapter to more informations.