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