void Start()
 {
     index = global_count;
     global_count++;
     global_index    = global_count;
     state_string    = new string[4];
     state_string[0] = "fov";
     state_string[1] = "rotate";
     state_string[2] = "xy";
     state_string[3] = "xz";
     tracker         = (VRPNTracker)GetComponent(typeof(VRPNTracker));
     sensorPosition  = tracker.SensorOffset.localPosition;
     sensorRotation  = tracker.SensorOffset.localRotation;
     if (Camera.main == null)
     {
         Debug.Log("No camera found");
     }
     else
     {
         fieldOfView = Camera.main.fieldOfView;
         Component planeRender = GetComponentInChildren(typeof(Renderer));
         if (planeRender != null)
         {
             plane = planeRender.gameObject.transform;
         }
     }
     if (tracker == null)
     {
         Debug.Log("No tracker found");
     }
     else
     {
         savedApply = tracker.ApplyTracking;
         savedDebug = tracker.ShowDebug;
     }
 }
    void Update () {
    	//avoid capturing key events twice
    	if (global_index != last_global_index)
    	{
    		last_global_index = global_index;
    		return;
    	}
    	if (index == global_index || (index == global_count-1 && global_index == global_count))
    	{
    		//Select Tracker
    		if (Input.GetKeyDown(KeyCode.Alpha8))
    		{
    			global_index++;
    			if (global_index > global_count)
    				global_index = 0;
    			tracker.ApplyTracking = savedApply;
    			tracker.ShowDebug = savedDebug;
    		}
    	}
 	   	if (index == global_index && tracker != null)
    	{
    		Action_Type priorState = state;
    		//Save to prefab
    		if (Input.GetKeyDown(KeyCode.Alpha9))
    		{
    			sensorPosition = tracker.SensorOffset.localPosition;
    			sensorRotation = tracker.SensorOffset.localRotation;
    			if (Camera.main != null)
    				fieldOfView = Camera.main.fieldOfView;
    		}
    		else if (Input.GetKeyDown(KeyCode.Alpha0))
    		{
    			tracker.SensorOffset.localPosition = sensorPosition;
    			tracker.SensorOffset.localRotation = sensorRotation;
    			if (Camera.main != null)
    				Camera.main.fieldOfView = fieldOfView;
    		}
    		else if (Input.GetKeyDown(KeyCode.Minus))
    		{
    			gain -= 1;
    			if (gain <= 0)
    				gain = 1;
    		}
    		else if (Input.GetKeyDown(KeyCode.Equals))
    		{
    			gain += 1;
    		}
    		else if (Input.GetKeyDown(KeyCode.I))
    		{
    			if (state == Action_Type.FieldOfView)
    				state = Action_Type.None;
    			else
    				state = Action_Type.FieldOfView;
    		}
    		else if (Input.GetKeyDown(KeyCode.O))
    		{
    			if (state == Action_Type.Rotate)
    				state = Action_Type.None;
    			else
    				state = Action_Type.Rotate;
    		}
    		else if (Input.GetKeyDown(KeyCode.P))
    		{
    			if (state == Action_Type.XYAxis)
    				state = Action_Type.None;
    			else
    				state = Action_Type.XYAxis;
    		}
			else if (Input.GetKeyDown(KeyCode.LeftBracket))
    		{
    			if (state == Action_Type.XZAxis)
    				state = Action_Type.None;
    			else
    				state = Action_Type.XZAxis;
    		}
    		if (priorState == Action_Type.None && (int)state > 0)
    		{
    			savedApply = tracker.ApplyTracking;
    			savedDebug = tracker.ShowDebug;
    			//if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
    			//	tracker.ApplyTracking = VRPNTracker.Transform_Type.None;
    			trackerPosition = tracker.GetPosition();
    			trackerRotation = tracker.GetRotation();
    			tracker.ShowDebug = true;
    		}
    		else if (state != priorState)
    		{
    			tracker.ApplyTracking = savedApply;
    			tracker.ShowDebug = savedDebug;
    		}
    		if (state == Action_Type.FieldOfView)
    		{
   				float fov = Camera.main.fieldOfView;
   				fov += gain*0.01f*Input.GetAxis("Mouse X");
   				if (Camera.main != null)
   					{
   					Camera.main.fieldOfView = fov;
   					if (plane != null)
   						{
   						Vector3 pos = plane.localPosition;
   						pos.z = (float)(5.0*plane.localScale.z/Math.Tan(Math.PI*(double)fov/360.0f));
   						plane.localPosition = pos;
   						}
   					}
    		}
    		else if (state == Action_Type.Rotate)
    		{
    			//if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
    			//{
    			//	tracker.SensorOffset.localRotation = Quaternion.Inverse(trackerRotation)*tracker.GetRotation();
    			//	tracker.SensorOffset.localRotation *= sensorRotation;
    			//}
    			//else
    			//{
    			//	Vector3 right = tracker.SensorOffset.localRotation * Vector3.right;
    			//	Vector3 up = tracker.SensorOffset.localRotation * Vector3.up;
    			//	Quaternion mouseXRotation = Quaternion.AngleAxis(-gain*0.01f*Input.GetAxis("Mouse X"),up);
    			//	Quaternion mouseYRotation = Quaternion.AngleAxis(-gain*0.01f*Input.GetAxis("Mouse Y"),right);
    			//	tracker.SensorOffset.localRotation *= mouseXRotation * mouseYRotation;
    			//}
    		}
    		else if (state == Action_Type.XYAxis)
    		{
    			//if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
    			//{
    			//	Vector3 pos = tracker.SensorOffset.localPosition;
    			//	pos.x += gain*0.01f*Input.GetAxis("Mouse X");
    			//	pos.y += gain*0.01f*Input.GetAxis("Mouse Y");
    			//	tracker.SensorOffset.localPosition = pos;
    			//}
    			//else
    			//	tracker.SensorOffset.localPosition = tracker.GetPosition() - trackerPosition;
    		}
    		else if (state == Action_Type.XZAxis)
    		{
    			//if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
    			//{
    			//	Vector3 pos = tracker.SensorOffset.localPosition;
    			//	pos.x += gain*0.01f*Input.GetAxis("Mouse X");
    			//	pos.z += gain*0.01f*Input.GetAxis("Mouse Y");
    			//	tracker.SensorOffset.localPosition = pos;
    			//}
    			//else
    			//	tracker.SensorOffset.localPosition = tracker.GetPosition() - trackerPosition;
    		}
    	}
    }
 void Update()
 {
     //avoid capturing key events twice
     if (global_index != last_global_index)
     {
         last_global_index = global_index;
         return;
     }
     if (index == global_index || (index == global_count - 1 && global_index == global_count))
     {
         //Select Tracker
         if (Input.GetKeyDown(KeyCode.Alpha8))
         {
             global_index++;
             if (global_index > global_count)
             {
                 global_index = 0;
             }
             tracker.ApplyTracking = savedApply;
             tracker.ShowDebug     = savedDebug;
         }
     }
     if (index == global_index && tracker != null)
     {
         Action_Type priorState = state;
         //Save to prefab
         if (Input.GetKeyDown(KeyCode.Alpha9))
         {
             sensorPosition = tracker.SensorOffset.localPosition;
             sensorRotation = tracker.SensorOffset.localRotation;
             if (Camera.main != null)
             {
                 fieldOfView = Camera.main.fieldOfView;
             }
         }
         else if (Input.GetKeyDown(KeyCode.Alpha0))
         {
             tracker.SensorOffset.localPosition = sensorPosition;
             tracker.SensorOffset.localRotation = sensorRotation;
             if (Camera.main != null)
             {
                 Camera.main.fieldOfView = fieldOfView;
             }
         }
         else if (Input.GetKeyDown(KeyCode.Minus))
         {
             gain -= 1;
             if (gain <= 0)
             {
                 gain = 1;
             }
         }
         else if (Input.GetKeyDown(KeyCode.Equals))
         {
             gain += 1;
         }
         else if (Input.GetKeyDown(KeyCode.I))
         {
             if (state == Action_Type.FieldOfView)
             {
                 state = Action_Type.None;
             }
             else
             {
                 state = Action_Type.FieldOfView;
             }
         }
         else if (Input.GetKeyDown(KeyCode.O))
         {
             if (state == Action_Type.Rotate)
             {
                 state = Action_Type.None;
             }
             else
             {
                 state = Action_Type.Rotate;
             }
         }
         else if (Input.GetKeyDown(KeyCode.P))
         {
             if (state == Action_Type.XYAxis)
             {
                 state = Action_Type.None;
             }
             else
             {
                 state = Action_Type.XYAxis;
             }
         }
         else if (Input.GetKeyDown(KeyCode.LeftBracket))
         {
             if (state == Action_Type.XZAxis)
             {
                 state = Action_Type.None;
             }
             else
             {
                 state = Action_Type.XZAxis;
             }
         }
         if (priorState == Action_Type.None && (int)state > 0)
         {
             savedApply = tracker.ApplyTracking;
             savedDebug = tracker.ShowDebug;
             if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
             {
                 tracker.ApplyTracking = VRPNTracker.Transform_Type.None;
             }
             trackerPosition   = tracker.GetPosition();
             trackerRotation   = tracker.GetRotation();
             tracker.ShowDebug = true;
         }
         else if (state != priorState)
         {
             tracker.ApplyTracking = savedApply;
             tracker.ShowDebug     = savedDebug;
         }
         if (state == Action_Type.FieldOfView)
         {
             float fov = Camera.main.fieldOfView;
             fov += gain * 0.01f * Input.GetAxis("Mouse X");
             if (Camera.main != null)
             {
                 Camera.main.fieldOfView = fov;
                 if (plane != null)
                 {
                     Vector3 pos = plane.localPosition;
                     pos.z = (float)(5.0 * plane.localScale.z / Math.Tan(Math.PI * (double)fov / 360.0f));
                     plane.localPosition = pos;
                 }
             }
         }
         else if (state == Action_Type.Rotate)
         {
             if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
             {
                 tracker.SensorOffset.localRotation  = Quaternion.Inverse(trackerRotation) * tracker.GetRotation();
                 tracker.SensorOffset.localRotation *= sensorRotation;
             }
             else
             {
                 Vector3    right          = tracker.SensorOffset.localRotation * Vector3.right;
                 Vector3    up             = tracker.SensorOffset.localRotation * Vector3.up;
                 Quaternion mouseXRotation = Quaternion.AngleAxis(-gain * 0.01f * Input.GetAxis("Mouse X"), up);
                 Quaternion mouseYRotation = Quaternion.AngleAxis(-gain * 0.01f * Input.GetAxis("Mouse Y"), right);
                 tracker.SensorOffset.localRotation *= mouseXRotation * mouseYRotation;
             }
         }
         else if (state == Action_Type.XYAxis)
         {
             if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
             {
                 Vector3 pos = tracker.SensorOffset.localPosition;
                 pos.x += gain * 0.01f * Input.GetAxis("Mouse X");
                 pos.y += gain * 0.01f * Input.GetAxis("Mouse Y");
                 tracker.SensorOffset.localPosition = pos;
             }
             else
             {
                 tracker.SensorOffset.localPosition = tracker.GetPosition() - trackerPosition;
             }
         }
         else if (state == Action_Type.XZAxis)
         {
             if (tracker.TrackerType == VRPNManager.Tracker_Types.vrpn_Tracker_MotionNode)
             {
                 Vector3 pos = tracker.SensorOffset.localPosition;
                 pos.x += gain * 0.01f * Input.GetAxis("Mouse X");
                 pos.z += gain * 0.01f * Input.GetAxis("Mouse Y");
                 tracker.SensorOffset.localPosition = pos;
             }
             else
             {
                 tracker.SensorOffset.localPosition = tracker.GetPosition() - trackerPosition;
             }
         }
     }
 }
    void Start () {
    	index = global_count;
    	global_count++;
    	global_index = global_count;
    	state_string = new string[4];
    	state_string[0] = "fov";
    	state_string[1] = "rotate";
    	state_string[2] = "xy";
    	state_string[3] = "xz";
		tracker = (VRPNTracker)GetComponent(typeof(VRPNTracker));
    	sensorPosition = tracker.SensorOffset.localPosition;
    	sensorRotation = tracker.SensorOffset.localRotation;
    	if (Camera.main == null)
    		Debug.Log("No camera found");
    	else
    	{
    		fieldOfView = Camera.main.fieldOfView;
    		Component planeRender = GetComponentInChildren(typeof(Renderer));
    		if (planeRender != null)
    			plane = planeRender.gameObject.transform;
    	}
    	if (tracker == null)
    		Debug.Log("No tracker found");
    	else
    		{
    		savedApply = tracker.ApplyTracking;
    		savedDebug = tracker.ShowDebug;
    		}
    }