Interpolators

 

Interpolators define a Dictionary from a key, ranging from 0.0 to 1.0, to a keyValue, which can take several types of data depending on the Interpolator. All fields and events are equal for all Interpolators, only the data type varies.

In VRML the following interpolators are available:

·  ColorInterpolator

·  CoordinateInterpolator

·  NormalInterpolator

·  OrientationInterpolator

·  PositionInterpolator

·  ScalarInterpolator

Interpolators have an eventIn, set_fraction , and an eventOut value_changed. These two events are wired together, i.e. when the node receives a set_fraction event it generates a value_changed event.

The set_fraction event specifies a key value, the value_changed event outputs the data, specified in keyValue which corresponds to the key value given.

Interpolators combined with TimeSensors can be used to create animations, color variations, morphing objects, etc.... In each of the interpolator nodes, listed at the beggining of this section, an example will be provided.

One of the events generated by the TimeSensor node is the fraction_changed event. This event can be linked to the set_fraction event from the interpolator using a ROUTE. The interpolator will in turn generate the event value_changed. This event can then be routed to an exposed field of another node, for instance the color field of the Color node. The end result of this series of ROUTES is that the color of a given shape will change over time. The following figure shows the chaining of events between the nodes (read the figure from left to right):

Here is a  short disscussion of the fields present in all interpolators, key and keyValue, is presented. For instance consider that the following values for key and keyValue are given:

·  key [0 1]

·  keyValue [-0.5 0.5]

If the set_fraction event inputs 0 then the value_changed event outputs -0.5. Similarly when the set_fraction event inputs 1 then the value_changed event outputs 0.5.

So what happens when the set_fraction event inputs 0.3? Well, it is time to look at the title of this section. The nodes being dealt with in here are interpolators, so when 0.3 is inputed, the output will be -0.2, i.e. the value which would correspond to the key 0.3 if all possible pairs ( key, keyValue) were specified and assuming that the pairs mentioned correspond to a straight line. The following figure shows how the keyValue for key 0.3 was computed.

The above example is a very simple one, you just specify two values for the key and the interpolator node does the rest.

This is OK if you want to specify a linear relation between key and keyValue. However there are situations when the desired relation is not linear.

You can specify as many elements as desired in the key and keyValue lists, note however that the lists must have the same length.

For instance consider that the following values for key and keyValue are given:

·  key [0 0.5 1]

·  keyValue [-0.5 0.0 -0.5]

The above figure represents the line segments which define the interpolator. In this example, a key of 0.3 and a key of 0.7 correspond to the same keyValue.

By using a larger number of points you can approximate a curve.

 


#######

# #http://www.cs.montana.edu/hunterl/vrml/cs525/525sensor.wrl

 

#VRML V2.0 utf8

######################################################

#               SENSORS

#The following is going to be an example of basic

# events and routing in VRML. It will use a sensor

# to send events down a route for a particular action to

# take place.

#I also included an example of a Time sensor.

######################################################

 

Group {

   children [

      DEF TS TouchSensor {

          #enabled TRUE  

          #hitNormal_changed

          #hitPoint_changed

          #hitTexCoord_changed

          #touchTime

          #two more I used here are below in routing

           

      }

      Shape {

        appearance Appearance{

 

              material  DEF MAT Material {

                 diffuseColor 0     .5 0

             }

        }

        geometry Sphere{}

      }

 

  

      DEF Clock1 TimeSensor {

           cycleInterval 5.0

           enabled TRUE

           loop TRUE

           startTime 1.0

           stopTime  0.0

           #cyleTime (an event that is sent out every interval)

           #fraction_changed

           #isActive

           #time

      }

 

      DEF Highlight DirectionalLight {

         color 1 1 1

         on FALSE

      }

 

      DEF ActiveLight DirectionalLight {

          color 0 0 1

          on FALSE

      }

 

      DEF ColorPath ColorInterpolator{

          key [ 0.0, 0.5, .5, 1.0 ]

          keyValue [

              0.0 1.0 0.0,   1.0 1.0 0.0,

               0.0 0.0 1.0,   0.0 0.0 1.0,

          ]

      }  

   ]

 

}

 

 ROUTE TS.isOver TO Highlight.set_on    #When the mouse is over the touch sensor the light comes on.

 ROUTE TS.isActive TO ActiveLight.set_on # when the touch sensor is active the Active light is turned on.

 ROUTE Clock1.fraction TO ColorPath.set_fraction #When the clock ticks the colorpath interpolator is changed.

 ROUTE ColorPath.value_changed TO MAT.set_diffuseColor

 

 

Another example of sensors, and timers working. This is a door that is shut and opens and shuts when clicked on. When clicked on it goes to shut first if not already shut then opens.

 

 #VRML V2.0 utf8

 

DEF Touch TouchSensor { }

DEF Rotate Transform {

    center -2 0 0

    children Shape {

        appearance Appearance {

            material Material { }

        }

        geometry Box { size 4 9 0.1 }

    }

}

DEF OpenInterp OrientationInterpolator {

    key [ 0 1 ]

    keyValue [ 0 1 0 0, 0 1 0 -2 ]

}

DEF Open TimeSensor { cycleInterval 2 }

 

DEF CloseInterp OrientationInterpolator {

    key [ 0 1 ]

    keyValue [ 0 1 0 -2, 0 1 0 0 ]

}

DEF Close TimeSensor { cycleInterval 3 }

 

DEF S Script {

    eventIn SFTime touchTime

    eventOut SFTime startClose

    url "vrmlscript:

        function touchTime(value) {

            startClose = value + 4;

        }"

}

 

ROUTE Touch.touchTime TO Open.startTime

ROUTE Touch.touchTime TO S.touchTime

ROUTE Open.fraction_changed TO OpenInterp.set_fraction

ROUTE S.startClose TO Close.startTime

ROUTE Close.fraction_changed TO CloseInterp.set_fraction

ROUTE OpenInterp.value_changed TO Rotate .rotation

ROUTE CloseInterp.value_changed TO Rotate .rotation

 

 

#www.cs.montana.edu/hunterl/vrml/cs525/525view.wrl

#Animate the viewpoint

#VRML V2.0 utf8

DEF VP Viewpoint { position 0 0 10 }

 

Group {

    children [

        DEF Touch TouchSensor { }

        Transform {

            translation 2 0 0

            children Shape {

                geometry Cone{ }

                appearance Appearance {

                    material Material {

                        diffuseColor 0 0.6 0

                    }

                }

            }

        }

        Shape {

            geometry Box { }

            appearance Appearance {

                material Material {

                    diffuseColor 0.2 0 0.6

                }

            }

        }

    ]

}

 

DEF O OrientationInterpolator {

    key      [ 0, 0.2, 0.4, 0.5, 0.6, 0.8, 1]

    keyValue [ 0 1 0 0, 0 1 0 0, 0 1 0 1.57, 

               0 1 0 3.14, 0 1 0 4.71, 0 1 0 0,

               0 1 0 0 ]

}

 

DEF P PositionInterpolator {

    key      [ 0, 0.2, 0.4, 0.5, 0.6, 0.8, 1]

    keyValue [ 0 0 10, 0 0 5, 5 0 0, 0 0 -5,

               -5 0 0, 0 0 5, 0 0 10 ]

}

 

DEF Time TimeSensor {

    cycleInterval 20

}

 

ROUTE Touch.touchTime TO Time.startTime

ROUTE Time.fraction_changed TO P.set_fraction

ROUTE Time.fraction_changed TO O.set_fraction

ROUTE P.value_changed TO VP.set_position

ROUTE O.value_changed TO VP.set_orientation

 

 

 

 

Here is a link to a tutorial on the VRML Scripting language….

http://www.lighthouse3d.com/vrml/tutorial/index.shtml?intro