Pinecoast Software Inc.
      Home
Subscribe
 

Tutorial: Routes and Messaging

Traffic Light

The traffic light on the left is build in a similar way to the lamp of the previous tutorial. The head is a box and the lights are spheres of the appropriate colors. In this case the material nodes have been given names using a DEF statement to prepare for animation. The Material node for the red light is called RedLight.

 


Transform { 
  translation 0 2 0
  children 
    Shape { 
      appearance Appearance { 
        material DEF RedLight Material { 
          diffuseColor 0.5 0 0
        }
      }
      geometry Sphere { 
      }
    }
}

 



A Time Sensor node has also been added to control the animation. Since the loop fiald has been set to TRUE and the cycleInterval is set to 10 seconds, the sensor will send out fraction messages ranging from 0 to 1 over a ten second interval and then will repeat indefinitely. The TimeSensor node is named Timer.

 


DEF Timer TimeSensor { 
  cycleInterval 10
  loop TRUE
}

 



A ColorInterpolator node for each of the lights will control the material color for that light. The key values are the colors, which in this case are dark red or red. Since the cycleInterval of the Time Sensor node is 10 seconds, from 0 to 0.2 seconds of the cycle the ColorInterpolator will send out a color changing from dark red to red. From 0.2 to 4 seconds it will be red. From 4 to 4.2 seconds it will change from red to dark red and will remain dark red until the end of the cycle at 10 seconds, when a new cycle will begin.

 


DEF RedCI ColorInterpolator { 
  key [ 0, 0.02, 0.4, 0.42, 0.92, 0.94, 1 ]
  keyValue [ 0.5 0 0, 1 0 0, 1 0 0, 0.5 0 0, 
             0.5 0 0, 0.5 0 0, 0.5 0 0 ]
}

 



The other lights turn on and off in a similar manner but at different times in the cycle.



The animation is now created with ROUTE statements that specify how the nodes receive messages.

 


ROUTE Timer.fraction_changed TO RedCI.set_fraction
ROUTE RedCI.value_changed TO RedLight.diffuseColor

 



The first ROUTE statement specifies that the TimeSensor node named Timer will send fraction_changed messages to the ColorInterpolator node. The fraction is a number from 0 to 1 that indicates the proportional of the current cycle that has been completed. When the ColorInterpolator receives a set_fraction event, it calculates a value, which will be a color. The second ROUTE statement specifies that the calculated color will be sent to the diffuseColor field of the node named RedLight. This will change the diffuse color of the Material node, and the light will change color.

Download tutorial files





Copyright © Pinecoast Software 2000-2008. All rights reserved.