#VRML V2.0 utf8 # File Name : JSEx2.wrl # Author : H. Timucin Ozdemir # Purpose : Java Script Node Example Transform { children [ DEF TIME1 TimeSensor { cycleInterval 10 } DEF T1 Transform { children [ Shape { appearance Appearance { material Material { diffuseColor 1.0 0.0 0.0 } } geometry Sphere{ radius 0.2 } }, DEF GO TouchSensor {} ] translation 1.0 0.0 0.0 } DEF T2 Transform { children [ DEF GO1 TouchSensor {} Shape { appearance Appearance { material Material { diffuseColor 0.0 1.0 0.0 } } geometry Box{} } ] translation -3.0 0.0 0.0 rotation 1.0 0.0 1.0 0.3 } DEF Action1 Script { eventIn SFFloat clicked eventOut SFVec3f set_position url "Example4.class" } ] } ROUTE GO.touchTime TO TIME1.startTime ROUTE GO1.touchTime TO TIME1.startTime ROUTE TIME1.fraction_changed TO Action1.clicked ROUTE Action1.set_position TO T1.set_translation
/* */ import vrml.*; import vrml.field.*; import vrml.node.*; public class Example4 extends Script { // private SFVec3f set_position; // private float valBuf[] = new float[3]; private float count = 0.0f; private float step = 0.2f; public void initialize() { set_position = (SFVec3f) getEventOut("set_position"); } //... // This method is called when any event is received public void processEvent(Event e) { System.out.println(" I received "+e.getName()+" time "+e.getTimeStamp()); System.out.println(" I received "+e.getName()); if(e.getName().equals("clicked")) { valBuf[0] = (float) (1.0+count*1.0); valBuf[1] = (float) (Math.sin(count*6.28)); valBuf[2] = 0.0f; set_position.setValue(valBuf); count += step; if(count>3.14 || count<0) { step = -1.0f * step; count += step; }// end of if }// end of if }// end of processEvent() }// end of Example4