home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-05-14 | 7.5 KB | 311 lines |
- // This file was generated by Community Place Conductor Version 1.0 Alpha
- //
- import vrml.*;
- import vrml.node.*;
- import vrml.field.*;
-
- public class SpotLight extends Script {
- // Declare eventOut field(s)
- // CPC_VER_DECL begin
- private SFBool m_evOn;
- private SFFloat m_evIntensity;
- private SFColor m_evColor;
- private SFVec3f m_evLocation;
- private SFVec3f m_evDirection;
- private SFFloat m_evCutOffAngle;
- private MFString m_evMessage;
- // CPC_VER_DECL end
-
- boolean m_setOn;
- float m_setIntensity;
- UpDown m_changerIntensity;
- float[] m_setColor = new float[3];
- UpDown m_changerColor;
- int m_idColor;
- float[] m_setLocation = new float[3];
- Rotate m_changerLocation;
- float[] m_setDirection = new float[3];
- Rotate m_changerDirection;
- float m_setCutOffAngle;
- UpDown m_changerCutOffAngle;
- Disco m_changerDisco;
-
- // mode
- int m_mode;
- final int onMode = 0;
- final int intensityMode = 1;
- final int colorMode = 2;
- final int locationMode = 3;
- final int directionMode = 4;
- final int cutOffAngleMode = 5;
- final int discoMode = 6;
- final int nonMode = 7;
-
- final int topMode = onMode;
- final int lastMode = discoMode;
-
- // for message
- Browser m_browser = null;
-
- final String[] m_modeStr = {
- "On",
- "Intensity",
- "Color",
- "Location",
- "Direction",
- "CutOffAngle",
- "Direction & Color",
- "nothing..."
- };
-
- // Declare initialize()
- public void initialize() {
- // This method is called ..
- // CPC_INIT begin
- m_evOn = (SFBool)getEventOut("evOn");
- m_evIntensity = (SFFloat)getEventOut("evIntensity");
- m_evColor = (SFColor)getEventOut("evColor");
- m_evLocation = (SFVec3f)getEventOut("evLocation");
- m_evDirection = (SFVec3f)getEventOut("evDirection");
- m_evCutOffAngle = (SFFloat)getEventOut("evCutOffAngle");
- m_evMessage = (MFString)getEventOut("evMessage");
- // CPC_INIT end
- // mode
- m_mode = nonMode;
-
- m_browser = getBrowser();
- initField();
- }
-
- void initField(){
- // Message
- m_browser.setDescription("Checking " + m_modeStr[m_mode]);
-
- // On
- m_setOn = true;
- m_evOn.setValue(m_setOn);
- // Intensity
- m_setIntensity = 1f;
- m_evIntensity.setValue(m_setIntensity);
- m_changerIntensity = new UpDown(m_setIntensity, 0f, 1f, 0.1f);
- // Color
- m_idColor = 0;
- set3Float(m_setColor, 1f, 1f, 1f);
- m_evColor.setValue(m_setColor);
- m_changerColor = new UpDown(m_setColor[m_idColor], 0f, 1f, 0.1f);
- // Location
- set3Float(m_setLocation, 0f, 0f, 0f);
- m_evLocation.setValue(m_setLocation);
- m_changerLocation = new Rotate(-2f, 0f, (float)(Math.PI/10));
- // Direction
- set3Float(m_setDirection, 0f, 0f, -1f);
- m_evDirection.setValue(m_setDirection);
- m_changerDirection = new Rotate(0f, -1f, (float)(Math.PI/10));
- // CutOffAngle
- m_setCutOffAngle = (float)(Math.PI/4);
- m_evCutOffAngle.setValue(m_setCutOffAngle);
- m_changerCutOffAngle = new UpDown(m_setCutOffAngle, 0f, (float)(Math.PI/4), (float)(Math.PI/40));
- // Direction(random)
- m_changerDisco = new Disco();
- }
-
- void changeOn(){
- m_setOn = !m_setOn;
- m_evOn.setValue(m_setOn);
- //Message
- msgValue((new Boolean(m_setOn)).toString());
- }
- void changeIntensity(){
- m_changerIntensity.next();
- m_setIntensity = m_changerIntensity.p;
- m_evIntensity.setValue(m_setIntensity);
- //Message
- msgValue(Float.toString(m_setIntensity));
- }
- void changeColor(){
- m_changerColor.next();
- m_setColor[m_idColor] = m_changerColor.p;
- m_evColor.setValue(m_setColor);
- if(m_setColor[m_idColor] == 0f){
- m_idColor++;
- if(m_idColor > 2)
- m_idColor = 0;
- m_changerColor.p = m_setColor[m_idColor];
- }
- //Message
- msgValue( "(" +
- Float.toString(m_setColor[0]) + ","+
- Float.toString(m_setColor[1]) + ","+
- Float.toString(m_setColor[2]) + ")"
- );
- }
- void changeLocation(){
- m_changerLocation.next();
- m_setLocation[0] = m_changerLocation.p1;
- m_setLocation[1] = m_changerLocation.p2;
- m_evLocation.setValue(m_setLocation);
- //Message
- msgValue( "(" +
- Float.toString(m_setLocation[0]) + ","+
- Float.toString(m_setLocation[1]) + ","+
- Float.toString(m_setLocation[2]) + ")"
- );
- }
- void changeDirection(){
- m_changerDirection.next();
- m_setDirection[1] = m_changerDirection.p1;
- m_setDirection[2] = m_changerDirection.p2;
- m_evDirection.setValue(m_setDirection);
- //Message
- msgValue( "(" +
- Float.toString(m_setDirection[0]) + ","+
- Float.toString(m_setDirection[1]) + ","+
- Float.toString(m_setDirection[2]) + ")"
- );
- // location
- float[] location = new float[3];
- set3Float(location, 0f, 1f, -3f);
- m_evLocation.setValue(location);
- }
- void changeCutOffAngle(){
- m_changerCutOffAngle.next();
- m_setCutOffAngle = m_changerCutOffAngle.p;
- m_evCutOffAngle.setValue(m_setCutOffAngle);
- //Message
- msgValue(Float.toString(m_setCutOffAngle));
- }
- void changeDisco(){
- m_changerDisco.next();
- // Direction
- m_evDirection.setValue(m_changerDisco.direction);
- // Color
- m_evColor.setValue(m_changerDisco.color);
- // Loation
- m_setLocation[0] = 0f;
- m_setLocation[1] = 2f;
- m_setLocation[2] = -4f;
- m_evLocation.setValue(m_setLocation);
- }
-
- // CPC_HANDLER begin
- private void _evTimeCB(ConstSFTime ev) {
- switch(m_mode){
- case onMode:
- changeOn();
- break;
- case intensityMode:
- changeIntensity();
- break;
- case colorMode:
- changeColor();
- break;
- case locationMode:
- changeLocation();
- break;
- case directionMode:
- changeDirection();
- break;
- case cutOffAngleMode:
- changeCutOffAngle();
- break;
- case discoMode:
- changeDisco();
- break;
- }
- }
- private void _evTouchTimeCB(ConstSFTime ev) {
- m_mode++;
- if(m_mode > lastMode)
- m_mode = topMode;
- initField();
- }
- // CPC_HANDLER end
-
- void msgValue(String val){
- String[] msg = new String[1];
- msg[0] = m_modeStr[m_mode] + " " + val;
- // m_evMessage.setValue(msg);
- // System.out.println(msg[0]);
- }
- void set3Float(float[] f, float a, float b, float c){
- f[0] = a;
- f[1] = b;
- f[2] = c;
- }
-
- // Declare processEvents field
- public void processEvents(int count, Event events[]) {
- // This method is called
- for (int i = 0; i < count; i++) {
- // CPC_EVENT begin
- if (events[i].getName().equals("evTime")) {
- _evTimeCB((ConstSFTime)events[i].getValue());
- }
- if (events[i].getName().equals("evTouchTime")) {
- _evTouchTimeCB((ConstSFTime)events[i].getValue());
- }
- // CPC_EVENT end
- }
- } // end of processEvents()
- } // end of class SpotLight
-
- class UpDown{
- float min, max, step, p;
-
- UpDown(float p, float min, float max, float step){
- this.p = p;
- this.min = min;
- this.max = max;
- this.step = step;
- }
-
- void next(){
- p += step;
- if(p >= max){
- step *= -1f;
- p = max;
- }else if(p <= min){
- step *= -1f;
- p = min;
- }
- }
- }
-
- class Rotate{
- float step, p1, p2;
- protected double r, a;
-
- Rotate(float p1, float p2, float step){
- this.p1 = p1;
- this.p2 = p2;
- this.step = step;
- this.r = Math.sqrt(p1*p1 + p2*p2);
- this.a = Math.acos(p1/r);
- }
-
- void next(){
- a += step;
- if( a > 2 * Math.PI )
- a -= 2 * Math.PI;
- p1 = (float)(r * Math.sin(a));
- p2 = (float)(r * Math.cos(a));
- }
- }
- class Disco{
- float[] direction = new float[3];
- float[] color = new float[3];
- java.util.Random random = new java.util.Random();
-
- void next(){
- // Direction
- direction[0] = random.nextFloat() - 0.5f;
- direction[1] = - random.nextFloat();
- direction[2] = random.nextFloat() - 0.5f;
- // Color
- color[0] = random.nextFloat();
- color[1] = random.nextFloat();
- color[2] = random.nextFloat();
- }
- }
-
-