home *** CD-ROM | disk | FTP | other *** search
/ Sony Community Place / BROWSER / APP / cpb20prD1b.exe / DATA.Z / SpotLight.java < prev    next >
Encoding:
Java Source  |  1997-05-14  |  7.5 KB  |  311 lines

  1. // This file was generated by Community Place Conductor Version 1.0 Alpha 
  2. //
  3. import vrml.*;
  4. import vrml.node.*;
  5. import vrml.field.*;
  6.  
  7. public class SpotLight extends Script {
  8.     // Declare eventOut field(s)
  9.     // CPC_VER_DECL begin
  10.     private SFBool m_evOn;
  11.     private SFFloat m_evIntensity;
  12.     private SFColor m_evColor;
  13.     private SFVec3f m_evLocation;
  14.     private SFVec3f m_evDirection;
  15.     private SFFloat m_evCutOffAngle;
  16.     private MFString m_evMessage;
  17.     // CPC_VER_DECL end
  18.  
  19.     boolean m_setOn;
  20.     float m_setIntensity;
  21.     UpDown m_changerIntensity;
  22.     float[] m_setColor = new float[3];
  23.     UpDown m_changerColor;
  24.     int m_idColor;
  25.     float[] m_setLocation = new float[3];
  26.     Rotate m_changerLocation;
  27.     float[] m_setDirection = new float[3];
  28.     Rotate m_changerDirection;
  29.     float m_setCutOffAngle;
  30.     UpDown m_changerCutOffAngle;
  31.     Disco m_changerDisco;
  32.  
  33.     // mode
  34.     int m_mode;
  35.     final int onMode = 0;
  36.     final int intensityMode = 1;
  37.     final int colorMode = 2;
  38.     final int locationMode = 3;
  39.     final int directionMode = 4;
  40.     final int cutOffAngleMode = 5;
  41.     final int discoMode = 6;
  42.     final int nonMode = 7;
  43.  
  44.     final int topMode = onMode;
  45.     final int lastMode = discoMode;
  46.  
  47.     // for message
  48.     Browser m_browser = null;
  49.  
  50.     final String[] m_modeStr = {
  51.         "On",
  52.         "Intensity",
  53.         "Color",
  54.         "Location",
  55.         "Direction",
  56.         "CutOffAngle",
  57.         "Direction & Color",
  58.                 "nothing..."
  59.     };
  60.  
  61.     // Declare initialize()
  62.     public void initialize() {
  63.         // This method is called ..
  64.         // CPC_INIT begin
  65.         m_evOn = (SFBool)getEventOut("evOn");
  66.         m_evIntensity = (SFFloat)getEventOut("evIntensity");
  67.         m_evColor = (SFColor)getEventOut("evColor");
  68.         m_evLocation = (SFVec3f)getEventOut("evLocation");
  69.         m_evDirection = (SFVec3f)getEventOut("evDirection");
  70.         m_evCutOffAngle = (SFFloat)getEventOut("evCutOffAngle");
  71.         m_evMessage = (MFString)getEventOut("evMessage");
  72.         // CPC_INIT end
  73.         // mode
  74.         m_mode = nonMode;
  75.  
  76.                 m_browser = getBrowser();
  77.         initField();
  78.     }
  79.  
  80.     void initField(){
  81.         // Message
  82.         m_browser.setDescription("Checking " + m_modeStr[m_mode]);
  83.  
  84.         // On
  85.         m_setOn = true;
  86.         m_evOn.setValue(m_setOn);
  87.         // Intensity
  88.         m_setIntensity = 1f;
  89.         m_evIntensity.setValue(m_setIntensity);
  90.         m_changerIntensity = new UpDown(m_setIntensity, 0f, 1f, 0.1f);
  91.         // Color
  92.         m_idColor = 0;
  93.         set3Float(m_setColor, 1f, 1f, 1f);
  94.         m_evColor.setValue(m_setColor);
  95.         m_changerColor = new UpDown(m_setColor[m_idColor], 0f, 1f, 0.1f);
  96.         // Location
  97.         set3Float(m_setLocation, 0f, 0f, 0f);
  98.         m_evLocation.setValue(m_setLocation);
  99.         m_changerLocation = new Rotate(-2f, 0f, (float)(Math.PI/10));
  100.         // Direction
  101.         set3Float(m_setDirection, 0f, 0f, -1f);
  102.         m_evDirection.setValue(m_setDirection);
  103.         m_changerDirection = new Rotate(0f, -1f, (float)(Math.PI/10));
  104.         // CutOffAngle
  105.         m_setCutOffAngle = (float)(Math.PI/4);
  106.         m_evCutOffAngle.setValue(m_setCutOffAngle);
  107.         m_changerCutOffAngle = new UpDown(m_setCutOffAngle, 0f, (float)(Math.PI/4), (float)(Math.PI/40));
  108.         // Direction(random)
  109.         m_changerDisco = new Disco();
  110.     }
  111.  
  112.     void changeOn(){
  113.         m_setOn = !m_setOn;
  114.         m_evOn.setValue(m_setOn);
  115.         //Message
  116.         msgValue((new Boolean(m_setOn)).toString());
  117.     }
  118.     void changeIntensity(){
  119.         m_changerIntensity.next();
  120.         m_setIntensity = m_changerIntensity.p;
  121.         m_evIntensity.setValue(m_setIntensity);
  122.         //Message
  123.         msgValue(Float.toString(m_setIntensity));
  124.     }
  125.     void changeColor(){
  126.         m_changerColor.next();
  127.         m_setColor[m_idColor] = m_changerColor.p;
  128.         m_evColor.setValue(m_setColor);
  129.         if(m_setColor[m_idColor] == 0f){
  130.             m_idColor++;
  131.             if(m_idColor > 2)
  132.                 m_idColor = 0;
  133.             m_changerColor.p = m_setColor[m_idColor];
  134.         }
  135.         //Message
  136.         msgValue( "(" + 
  137.             Float.toString(m_setColor[0]) + ","+
  138.             Float.toString(m_setColor[1]) + ","+
  139.             Float.toString(m_setColor[2]) + ")"
  140.         );
  141.     }
  142.     void changeLocation(){
  143.         m_changerLocation.next();
  144.         m_setLocation[0] = m_changerLocation.p1;
  145.         m_setLocation[1] = m_changerLocation.p2;
  146.         m_evLocation.setValue(m_setLocation);
  147.         //Message
  148.         msgValue( "(" + 
  149.             Float.toString(m_setLocation[0]) + ","+
  150.             Float.toString(m_setLocation[1]) + ","+
  151.             Float.toString(m_setLocation[2]) + ")"
  152.         );
  153.     }
  154.     void changeDirection(){
  155.         m_changerDirection.next();
  156.         m_setDirection[1] = m_changerDirection.p1;
  157.         m_setDirection[2] = m_changerDirection.p2;
  158.         m_evDirection.setValue(m_setDirection);
  159.         //Message
  160.         msgValue( "(" + 
  161.             Float.toString(m_setDirection[0]) + ","+
  162.             Float.toString(m_setDirection[1]) + ","+
  163.             Float.toString(m_setDirection[2]) + ")"
  164.         );
  165.         // location
  166.         float[] location = new float[3];
  167.         set3Float(location, 0f, 1f, -3f);
  168.         m_evLocation.setValue(location);
  169.     }
  170.     void changeCutOffAngle(){
  171.         m_changerCutOffAngle.next();
  172.         m_setCutOffAngle = m_changerCutOffAngle.p;
  173.         m_evCutOffAngle.setValue(m_setCutOffAngle);
  174.         //Message
  175.         msgValue(Float.toString(m_setCutOffAngle));
  176.     }
  177.     void changeDisco(){
  178.         m_changerDisco.next();
  179.         // Direction
  180.         m_evDirection.setValue(m_changerDisco.direction);
  181.         // Color
  182.         m_evColor.setValue(m_changerDisco.color);
  183.         // Loation
  184.         m_setLocation[0] = 0f;
  185.         m_setLocation[1] = 2f;
  186.         m_setLocation[2] = -4f;
  187.         m_evLocation.setValue(m_setLocation);
  188.     }
  189.  
  190.     // CPC_HANDLER begin
  191.     private void _evTimeCB(ConstSFTime ev) {
  192.         switch(m_mode){
  193.         case onMode:
  194.             changeOn();
  195.             break;
  196.         case intensityMode:
  197.             changeIntensity();
  198.             break;
  199.         case colorMode:
  200.             changeColor();
  201.             break;
  202.         case locationMode:
  203.             changeLocation();
  204.             break;
  205.         case directionMode:
  206.             changeDirection();
  207.             break;
  208.         case cutOffAngleMode:
  209.             changeCutOffAngle();
  210.             break;
  211.         case discoMode:
  212.             changeDisco();
  213.             break;
  214.         }
  215.     }
  216.     private void _evTouchTimeCB(ConstSFTime ev) {
  217.         m_mode++;
  218.         if(m_mode > lastMode)
  219.             m_mode = topMode;
  220.         initField();
  221.     }
  222.     // CPC_HANDLER end
  223.  
  224.     void msgValue(String val){
  225.         String[] msg = new String[1];
  226.         msg[0] = m_modeStr[m_mode] + " " + val;
  227. //        m_evMessage.setValue(msg);
  228. //        System.out.println(msg[0]);
  229.     }
  230.     void set3Float(float[] f, float a, float b, float c){
  231.         f[0] = a;
  232.         f[1] = b;
  233.         f[2] = c;
  234.     }
  235.     
  236.     // Declare processEvents field
  237.     public void processEvents(int count, Event events[]) {
  238.         // This method is called 
  239.         for (int i = 0; i < count; i++) {
  240.             // CPC_EVENT begin
  241.             if (events[i].getName().equals("evTime")) {
  242.                 _evTimeCB((ConstSFTime)events[i].getValue());
  243.             }
  244.             if (events[i].getName().equals("evTouchTime")) {
  245.                 _evTouchTimeCB((ConstSFTime)events[i].getValue());
  246.             }
  247.             // CPC_EVENT end
  248.         }
  249.     } // end of processEvents() 
  250. } // end of class SpotLight 
  251.  
  252. class UpDown{
  253.     float min, max, step, p;
  254.  
  255.     UpDown(float p, float min, float max, float step){
  256.         this.p = p;
  257.         this.min = min;
  258.         this.max = max;
  259.         this.step = step;
  260.     }
  261.  
  262.     void next(){
  263.         p += step;
  264.         if(p >= max){
  265.             step *= -1f;
  266.             p = max;
  267.         }else if(p <= min){
  268.             step *= -1f;
  269.             p = min;
  270.         }
  271.     }
  272. }
  273.  
  274. class Rotate{
  275.     float step, p1, p2;
  276.     protected double r, a;
  277.  
  278.     Rotate(float p1, float p2, float step){
  279.         this.p1 = p1;
  280.         this.p2 = p2;
  281.         this.step = step;
  282.         this.r = Math.sqrt(p1*p1 + p2*p2);
  283.         this.a = Math.acos(p1/r);
  284.     }
  285.  
  286.     void next(){
  287.         a += step;
  288.         if( a > 2 * Math.PI )
  289.             a -= 2 * Math.PI;
  290.         p1 = (float)(r * Math.sin(a));
  291.         p2 = (float)(r * Math.cos(a));
  292.     }
  293. }
  294. class Disco{
  295.     float[] direction = new float[3];
  296.     float[] color = new float[3];
  297.     java.util.Random random = new java.util.Random();
  298.  
  299.     void next(){
  300.         // Direction
  301.         direction[0] = random.nextFloat() - 0.5f;
  302.         direction[1] = - random.nextFloat();
  303.         direction[2] = random.nextFloat() - 0.5f;
  304.         // Color
  305.         color[0] = random.nextFloat();
  306.         color[1] = random.nextFloat();
  307.         color[2] = random.nextFloat();
  308.     }
  309. }
  310.  
  311.