media @ VU
[] readme course preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthoughts appendix references examples resources _

talk show tell print

web3d-x-creation.vr

web3d-x-creation.vr (wrl ) [ flux / bitmanagement / cortona / octaga ]

Web3D/VR

geometry from scripts

This example demonstrates the creation of geometry. This is accomplished through the ECMAScript's createVrmlFromString method.

This code may be reused in part or total. Please include this paragraph when it is used. Copyright 2000, Leonard Daly

world characteristics


  
  Viewpoint {
    position 0 10 30
  }
  NavigationInfo {
    type ["FLY", "ANY"]
    headlight     FALSE
  }
  
  Background {
    skyColor [0.1 0.8 1]
    groundColor [.3 .8 .2, .3 .8 .8]
    groundAngle [1.57]
  }
  
  

ground


  
  
  
  DEF Ground Transform {
    children [
      Shape {                             # Transparent "ground"
        appearance Appearance {
          material Material {
            transparency 1
          }
        }
        geometry Box {
          size 200 .01 200
        }
      }
    ]
  }
  
  

animation control


  
  
  
  
  Transform {
    translation 0 1 0
    children [
      DirectionalLight {
        direction 0 -1 0
      }
      DirectionalLight {
        direction 0 1 0
      }
  
      Transform {                         # Looping animation addition
        translation 2 0 0
        children [
          DEF TouchLoop TouchSensor {}
          Shape {
            appearance Appearance {
              material Material {
                diffuseColor 0.8 0.4 0
                specularColor 0 1 1
                shininess .7
              }
            }
            geometry Cone {}
          }
        ]
      }
  
      Transform {                         # Non-looping animation addition
        translation -2 0 0
        rotation 1 0 0 3.14
        children [
          DEF TouchSingle TouchSensor {}
          Shape {
            appearance Appearance {
              material Material {
                diffuseColor 0.4 0.8 0
                specularColor 1 0 1
                shininess .7
              }
            }
            geometry Cone {}
          }
        ]
      }
    ]
  }
  
  

parent for bubbles


  
  
  
  
  Group {
    children [
      DirectionalLight {
        direction -.47 -.47 -.47
        color 1 1 1
      }
      DirectionalLight {
        direction .47 .47 .47
        color .8 .8 1
      }
      DEF CrystalNode Group {}
    ]
  }
  
  

timesensors


  
  
  
  
  DEF TimerLoop TimeSensor {
    cycleInterval 1
    loop          TRUE
    enabled       FALSE
    startTime     1
    stopTime      0
  }
  
  DEF TimerSingle TimeSensor {
    cycleInterval 1
    loop          TRUE
    enabled       FALSE
    startTime     1
    stopTime      0
  }
  
  

script


  
  
  
  
  DEF Manager Script {
    directOutput TRUE
    field SFNode CrystalNode USE CrystalNode
    field SFFloat seed 432113
    field SFInt32 ndxN -1
    field SFString tstr ""
    field SFString gstr ""
    eventIn SFTime loopCrystal
    eventIn SFTime singleCrystal
  
  

functions


  
  url "vrmlscript:
        function loopCrystal (value, time) {
          addCrystal (value, time, 'TRUE');
        }
        function singleCrystal (value, time) {
          addCrystal (value, time, 'FALSE');
        }
  
  

coordinates


  //      Compute coordinates.  Allowable range is a box
  //      that sits above the X-Z plane and extends +/-5
  //      in X or Z and 20 in Y.  The color is determined
  //      randomly in red, green, and blue.  The cycle time
  //      is also determined randomly from 1 to 20 seconds.
  
        function addCrystal (value, time, loop) {
          sx = Math.floor (Math.random(seed) * 20) + 1;
          sz = Math.floor (Math.random(seed) * 10) + 1;
          sy = Math.floor (Math.random(seed) * 20)/10 + .1;
          xc = (Math.floor (Math.random(seed) * 20 - 10) * 100) / 100;
          zc = (Math.floor (Math.random(seed) * 20 - 10) * 100) / 100;
          yc = (Math.floor (Math.random(seed) * 12 + 1) * 100) / 100;
          rd = (Math.floor (Math.random(seed) * 100)) / 100;
          gn = (Math.floor (Math.random(seed) * 100)) / 100;
          bl = (Math.floor (Math.random(seed) * 100)) / 100;
          tt = (Math.floor (Math.random(seed) * 190)) / 10 + 1;
  
  

create crystals


  // For sphere crystals, use the rotation as the color
  
          ndxN ++;
          gstr = 'SphereCrystal{startTime ' + time + ' cycleInterval ' + tt + ' loop ' + loop + ' color ' + rd + ' ' + gn + ' ' + bl + '}';
          tstr = 'EXTERNPROTO SphereCrystal [exposedField SFTime startTime exposedField SFTime cycleInterval exposedField SFBool loop exposedField SFColor color] ';
          // tstr = tstr + '\"<a href=http://www.realism.co>/vrml/Example/Bubbles/CubeCrystal.wrl#SphereCrystal\" ';
          tstr = tstr + '\"@vr-web3d-x-crystal.wrl#SphereCrystal\" ';
          tstr = tstr + 'DEF N' + ndxN + ' Transform {translation ' + xc + ' ' + yc + ' ' + zc;
          tstr = tstr + ' children [ ' + gstr + '] }';
  
  

comments


  // To see the VRML string used to create the node, uncomment
  // the 'print' line and look in the console window.
  
  // 'xform' contains the parsed version of the VRML string.
  // createVrmlFromString is a method in the Browser object.
  // Once the VRML is parsed, it can be added to the scene
  // geometry with the addChildren method (event) of the
  // appropriate grouping node (CrystalNode in this case).
  
  //      print (tstr);
          xform = new MFNode();
          xform = Browser.createVrmlFromString(tstr);
          CrystalNode.addChildren = xform;
        }
    "
  }
  
  

event routing


  
  
  
  ROUTE   TouchSingle.isActive            TO      TimerSingle.enabled
  ROUTE   TimerSingle.cycleTime           TO      Manager.singleCrystal
  ROUTE   TouchLoop.isActive              TO      TimerLoop.enabled
  ROUTE   TimerLoop.cycleTime             TO      Manager.loopCrystal
  
  


(C) A. Eliëns 21/5/2007

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.