演習3 センサーとインターポレータ

アニメーションを作ってみよう

文法上の詳しい説明は
VRMLの文法
◆センサーノード
◆インターポレータノード
◆センサーとインターポレータの例
を参照してください。

 演習2で作成したファイルにセンサーノードやインターポレータノードを追加してアニメーションを作成してみましょう。

サッカーボールを回そう

 TimeSensorノードとOrientationInterpolatorノードを加えてサッカーボールを回転させましょう。


#VRML V2.0 utf8
#TimeSensor と 回転(Soccerball)    filename=ex03_puppet.wrl

Background { skyColor 0.8  0.8 1 }

EXTERNPROTO Doughnut                             #Doughnut PROTO
     [ exposedField SFColor doughnutColor ]
     [ "../ex02wrl/ex02_doughnut_proto.wrl" ]

EXTERNPROTO Soccerball                         #Soccerball PROTO
     [ exposedField MFColor soccerballColor ]
     [ "../ex02wrl/ex02_soccer_proto.wrl" ]

Transform { 
  children [
    Transform {                                          #puppet
      scale 0.8 0.8 0.8 
      children [ Inline { url "../ex01wrl/ex01_puppet2.wrl"} ]
    }
    Transform {                                         #Doughnut
      rotation 0 0 -1 0.375
      children [ 
        Transform {
          translation -0.27 1.5 0
          rotation -1 0 0  1.57
          scale 0.35 0.35 0.35 
          children [ Doughnut { doughnutColor 0.4 0.8 0.6 } ] }
    ]} 
    DEF Ball Transform {                                #Soccerball
      translation -0.8 -1.5 0.4
      scale 0.2 0.2 0.2 
      children [ Soccerball {  } ]
    }
]}

    DEF Balltime TimeSensor { cycleInterval 4.0  loop TRUE } 

    DEF Ballrotation OrientationInterpolator {
        key      [ 0,  0.33,   0.66,    1.0 ]
        keyValue [ 0 0 1 0, 0 0 1 2.1, 0 0 1 4.2, 0 0 1 0 ]
    }

ROUTE Balltime.fraction_changed TO Ballrotation.set_fraction
ROUTE Ballrotation.value_changed TO Ball.rotation

サッカーボールが回転 (クリックすると上の例がVRMLブラウザに表示されます)


クリックしたら回転させよう

 サッカーボールは常に回転していますが、人形をクリックしたら回転するようにしてみましょう。

 次にクリックすると回転するVRMLの記述例を示します。TouchSensorが設定されている部分(ここでは人形全体)にマウスカーソルを移動するとカーソルの形が変化します。


#VRML V2.0 utf8
#TimeSensor と 回転(Soccerball)    filename=ex03_puppet2.wrl
#TouchSensor と 回転(Puppet)

Background { skyColor 0.8  0.8 1 }

EXTERNPROTO Doughnut                             #Doughnut PROTO
     [ exposedField SFColor doughnutColor ]
     [ "../ex02wrl/ex02_doughnut_proto.wrl" ]

EXTERNPROTO Soccerball                         #Soccerball PROTO
     [ exposedField MFColor soccerballColor ]
     [ "../ex02wrl/ex02_soccer_proto.wrl" ]

DEF Puppet Transform { 
  translation 4 0 0
  center -4 0 0
  children [
    Transform {                                          #puppet
      scale 0.8 0.8 0.8 
      children [ Inline { url "../ex01wrl/ex01_puppet2.wrl"} ]
    }
    Transform {                                         #Doughnut
      rotation 0 0 -1 0.375
      children [ 
        Transform {
          translation -0.27 1.5 0
          rotation -1 0 0  1.57
          scale 0.35 0.35 0.35 
          children [ Doughnut { doughnutColor 0.4 0.8 0.6 } ] }
    ]} 
    DEF Ball Transform {                                #Soccerball
      translation -0.8 -1.5 0.4
      scale 0.2 0.2 0.2 
      children [ Soccerball {  } ]
    }
    DEF Puppetclick TouchSensor {}
]}

    DEF Balltime   TimeSensor { cycleInterval 2.0  loop TRUE } 
    DEF Puppettime TimeSensor { cycleInterval 4.0 } 

    DEF Ballrotation OrientationInterpolator {
        key      [ 0,  0.33,   0.66,    1.0 ]
        keyValue [ 0 0 1 0, 0 0 1 2.1, 0 0 1 4.2, 0 0 1 0 ]  }

    DEF Puppetrotation OrientationInterpolator {
        key      [ 0,  0.33,   0.66,    1.0 ]
        keyValue [ 0 -1 0 0, 0 -1 0 2.1, 0 -1 0 4.2, 0 -1 0 0 ]  }

ROUTE Balltime.fraction_changed TO Ballrotation.set_fraction
ROUTE Ballrotation.value_changed TO Ball.rotation

ROUTE Puppetclick.touchTime TO Puppettime.startTime
ROUTE Puppettime.fraction_changed TO Puppetrotation.set_fraction
ROUTE Puppetrotation.value_changed TO Puppet.rotation

クリックしたら回転 (クリックすると上の例がVRMLブラウザに表示されます)


はちまきの色を変えよう

 今度は、人形をクリックしたら回転しながらはちまきの色も変わるようにしてみましょう。次に変更の手順を示します。


#VRML V2.0 utf8
#TimeSensor と 回転(Soccerball)    filename=ex03_puppet3.wrl
#TouchSensor と 回転(Puppet)
#               色を変える(Doughnut)

Background { skyColor 0.8  0.8 1 }

EXTERNPROTO Doughnut                             #Doughnut PROTO
     [ exposedField SFColor doughnutColor ]
     [ "../ex02wrl/ex02_doughnut_proto.wrl" ]

EXTERNPROTO Soccerball                         #Soccerball PROTO
     [ exposedField MFColor soccerballColor ]
     [ "../ex02wrl/ex02_soccer_proto.wrl" ]

DEF Puppet Transform { 
  translation 4 0 0
  center -4 0 0
  children [
    Transform {                                          #puppet
      scale 0.8 0.8 0.8 
      children [ Inline { url "../ex01wrl/ex01_puppet2.wrl"} ]
    }
    Transform {                                         #Doughnut
      rotation 0 0 -1 0.375
      children [ 
        Transform {
          translation -0.27 1.5 0
          rotation -1 0 0  1.57
          scale 0.35 0.35 0.35 
          children [ DEF D_nut Doughnut { doughnutColor 0.4 0.8 0.6 } ] }
    ]} 
    DEF Ball Transform {                                #Soccerball
      translation -0.8 -1.5 0.4
      scale 0.2 0.2 0.2 
      children [ Soccerball {  } ]
    }
    DEF Puppetclick TouchSensor {}
]}

    DEF Balltime   TimeSensor { cycleInterval 2.0  loop TRUE } 
    DEF Puppettime TimeSensor { cycleInterval 4.0 } 

    DEF Ballrotation OrientationInterpolator {
        key      [ 0,  0.33,   0.66,    1.0 ]
        keyValue [ 0 0 1 0, 0 0 1 2.1, 0 0 1 4.2, 0 0 1 0 ]  }

    DEF Puppetrotation OrientationInterpolator {
        key      [ 0,  0.33,   0.66,    1.0 ]
        keyValue [ 0 -1 0 0, 0 -1 0 2.1, 0 -1 0 4.2, 0 -1 0 0 ]  }

    DEF Dcolor ColorInterpolator {
        key      [ 0,  0.5,  1.0 ]
        keyValue [ 0.4 0.8 0.6, 1 0 0, 0.4 0.8 0.6 ]  }

ROUTE Balltime.fraction_changed TO Ballrotation.set_fraction
ROUTE Ballrotation.value_changed TO Ball.rotation

ROUTE Puppetclick.touchTime TO Puppettime.startTime
ROUTE Puppettime.fraction_changed TO Puppetrotation.set_fraction
ROUTE Puppetrotation.value_changed TO Puppet.rotation

ROUTE Puppettime.fraction_changed TO Dcolor.set_fraction
ROUTE Dcolor.value_changed TO D_nut.doughnutColor

はちまきの色 (クリックすると上の例がVRMLブラウザに表示されます)


はちまきを飛ばそう

 今度は、回転しながらはちまきが飛び上がるようにしてみましょう。次に変更の手順を示します。


#VRML V2.0 utf8
#TimeSensor と 回転(Soccerball)    filename=ex03_puppet4.wrl
#TouchSensor と 回転(Puppet)
#               色を変える(Doughnut)
#               位置を変える(Doughnut)

Background { skyColor 0.8  0.8 1 }
Viewpoint { position 0 0 12.5 }

EXTERNPROTO Doughnut                             #Doughnut PROTO
     [ exposedField SFColor doughnutColor ]
     [ "../ex02wrl/ex02_doughnut_proto.wrl" ]

EXTERNPROTO Soccerball                         #Soccerball PROTO
     [ exposedField MFColor soccerballColor ]
     [ "../ex02wrl/ex02_soccer_proto.wrl" ]

DEF Puppet Transform { 
  translation 4 0 0
  center -4 0 0
  children [
    Transform {                                          #puppet
      scale 0.8 0.8 0.8 
      children [ Inline { url "../ex01wrl/ex01_puppet2.wrl"} ]
    }
    DEF D_nutnut Transform {                            #Doughnut
      rotation 0 0 -1 0.375
      children [ 
        Transform {
          translation -0.27 1.5 0
          rotation -1 0 0  1.57
          scale 0.35 0.35 0.35 
          children [ DEF D_nut Doughnut { doughnutColor 0.4 0.8 0.6 } ] }
    ]} 
    DEF Ball Transform {                                #Soccerball
      translation -0.8 -1.5 0.4
      scale 0.2 0.2 0.2 
      children [ Soccerball {  } ]
    }
    DEF Puppetclick TouchSensor {}
]}

    DEF Balltime   TimeSensor { cycleInterval 2.0  loop TRUE } 
    DEF Puppettime TimeSensor { cycleInterval 4.0 } 

    DEF Ballrotation OrientationInterpolator {
        key      [ 0,  0.33,   0.66,    1.0 ]
        keyValue [ 0 0 1 0, 0 0 1 2.1, 0 0 1 4.2, 0 0 1 0 ]  }

    DEF Puppetrotation OrientationInterpolator {
        key      [ 0,  0.33,   0.66,    1.0 ]
        keyValue [ 0 -1 0 0, 0 -1 0 2.1, 0 -1 0 4.2, 0 -1 0 0 ]  }

    DEF Dcolor ColorInterpolator {
        key      [ 0,  0.5,  1.0 ]
        keyValue [ 0.4 0.8 0.6, 1 0 0, 0.4 0.8 0.6 ]  }

    DEF Dposition  PositionInterpolator {
        key      [ 0,  0.25, 0.5, 0.75, 1.0 ]
        keyValue [ 0 0 0, 0 2 0, 0 1 0, 0 2 0, 0 0 0 ]  }

ROUTE Balltime.fraction_changed TO Ballrotation.set_fraction
ROUTE Ballrotation.value_changed TO Ball.rotation

ROUTE Puppetclick.touchTime TO Puppettime.startTime
ROUTE Puppettime.fraction_changed TO Puppetrotation.set_fraction
ROUTE Puppetrotation.value_changed TO Puppet.rotation

ROUTE Puppettime.fraction_changed TO Dcolor.set_fraction
ROUTE Dcolor.value_changed TO D_nut.doughnutColor

ROUTE Puppettime.fraction_changed TO Dposition.set_fraction
ROUTE Dposition.value_changed TO D_nutnut.translation

はちまきが飛ぶ (クリックすると上の例がVRMLブラウザに表示されます)