context: /* VRManagerScript
   * MiddleVR
   * (c) i'm in VR
   */
  
  using UnityEngine;
  using System.Collections;
  using MiddleVR_Unity3D;
  using System;
  
  public class VRManagerScript : MonoBehaviour
  {
      // Public readable parameters
      [HideInInspector]
      public float WandAxisHorizontal = 0.0f;
  
      [HideInInspector]
      public float WandAxisVertical = 0.0f;
  
      [HideInInspector]
      public bool WandButton0 = false;
  
      [HideInInspector]
      public bool WandButton1 = false;
  
      [HideInInspector]
      public bool WandButton2 = false;
  
      [HideInInspector]
      public bool WandButton3 = false;
  
      [HideInInspector]
      public bool WandButton4 = false;
  
      [HideInInspector]
      public bool WandButton5 = false;
  
      [HideInInspector]
      public double DeltaTime = 0.0f;
  
      // Exposed parameters:
      public string ConfigFile = "c:\\config.vrx";
      public GameObject RootNode = null;
      public GameObject TemplateCamera = null;
  
      public bool  ShowWand                 = true;
      public bool  ShowFPS                  = true;
      public bool  DisableExistingCameras   = true;
      public bool  GrabExistingNodes        = false;
      public bool  DebugNodes               = false;
      public bool  DebugScreens             = false;
      public bool  QuitOnEsc                = true;
      public bool  DontChangeWindowGeometry = false;
      public bool  SimpleCluster            = true;
      public bool  ForceQuality             = false;
      public int   ForceQualityIndex        = 3;
      public bool  ChangeWorldScale         = false;
      public float WorldScale               = 1.0f;
  
      // Private members
      private vrKernel         kernel     = null;
      private vrDisplayManager displayMgr = null;
  
      private bool m_isInit        = false;
      private bool m_isGeometrySet = false;
      private bool m_displayLog    = false;
  
      //private int  m_AntiAliasingLevel = 0;
      
      private bool m_NeedDelayedRenderingReset = false;
      private int  m_RenderingResetDelay       = 1;
  
      private GUIText    m_GUI  = null;
      private GameObject m_Wand = null;
  
      private bool[] mouseButtons = new bool[3];
      
      private bool m_AllowRenderTargetAA = false;
  
      private uint m_FirstFrameAfterReset = 0;
  
      // Public methods
  
      public void Log(string text)
      {
          MiddleVRTools.Log(text);
      }
  
      public bool IsKeyPressed(uint iKey)
      {
          if (MiddleVR.VRDeviceMgr != null && MiddleVR.VRDeviceMgr.IsKeyPressed(iKey))
          {
              return true;
          }
  
          return false;
      }
  
      public bool IsMouseButtonPressed(uint iButtonIndex)
      {
          if (MiddleVR.VRDeviceMgr != null && MiddleVR.VRDeviceMgr.IsMouseButtonPressed(iButtonIndex))
          {
              return true;
          }
  
          return false;
      }
  
      public float GetMouseAxisValue(uint iAxisIndex)
      {
          if (MiddleVR.VRDeviceMgr != null)
          {
              return MiddleVR.VRDeviceMgr.GetMouseAxisValue(iAxisIndex);
          }
  
          return 0.0f;
      }
      
  
      // Private methods
  
      void InitializeVR()
      {
          mouseButtons[0] = mouseButtons[1] = mouseButtons[2] = false;
  
          if (m_displayLog)
          {
              GameObject gui = new GameObject();
              m_GUI = gui.AddComponent("GUIText") as GUIText;
              gui.transform.localPosition = new UnityEngine.Vector3(0.5f, 0.0f, 0.0f);
              m_GUI.pixelOffset = new UnityEngine.Vector2(15, 0);
              m_GUI.anchor = TextAnchor.LowerCenter;
          }
  
          MiddleVRTools.IsEditor = Application.isEditor;
  
          if( MiddleVR.VRKernel != null )
          {
              MiddleVRTools.Log(3,"[ ] VRKernel already alive, reset Unity Manager.");
              MiddleVRTools.VRReset();
              m_isInit = true;
              // Not needed because this is the first execution of this script instance
              // m_isGeometrySet = false; 
              m_FirstFrameAfterReset = MiddleVR.VRKernel.GetFrame();
          }
          else
          {
              m_isInit = MiddleVRTools.VRInitialize(ConfigFile);
          }
  
          // Get AA from vrx configuration file
          //m_AntiAliasingLevel = (int)MiddleVR.VRDisplayMgr.GetAntiAliasing();
  
          DumpOptions();
  
          kernel = MiddleVR.VRKernel;
          displayMgr = MiddleVR.VRDisplayMgr;
  
          if (!m_isInit)
          {
              GameObject gui = new GameObject();
              m_GUI = gui.AddComponent("GUIText") as GUIText;
              gui.transform.localPosition = new UnityEngine.Vector3(0.2f, 0.0f, 0.0f);
              m_GUI.pixelOffset = new UnityEngine.Vector2(0, 0);
              m_GUI.anchor = TextAnchor.LowerLeft;
  
              string txt = kernel.GetLogString(true);
              print(txt);
              m_GUI.text = txt;
  
              return;
          }
  
          if (SimpleCluster)
          {
              SetupSimpleCluster();
          }
  
          if (ChangeWorldScale)
          {
              displayMgr.SetChangeWorldScale(true);
              displayMgr.SetWorldScale(WorldScale);
          }
  
          if (DisableExistingCameras)
          {
              Camera[] cameras = GameObject.FindObjectsOfType(typeof(Camera)) as Camera[];
  
              foreach (Camera cam in cameras)
              {
                  if (cam.targetTexture == null)
                  {
                      cam.enabled = false;
                  }
              }
          }
  
          MiddleVRTools.CreateNodes(RootNode, DebugNodes, DebugScreens, GrabExistingNodes,TemplateCamera);
          MiddleVRTools.CreateViewportsAndCameras(DontChangeWindowGeometry, m_AllowRenderTargetAA);
  
          //AttachCameraCB();
  
          MiddleVRTools.Log(4, "[<] End of VR initialization script");
      }
  
      void AttachCameraCB()
      {
          Camera[] cameras = GameObject.FindObjectsOfType(typeof(Camera)) as Camera[];
      
          foreach (Camera cam in cameras)
          {
              if (cam.targetTexture != null)
              {
                  cam.gameObject.AddComponent<VRCameraCB>();
              }
          }
      }
  
      void Start () {
          MiddleVRTools.Log(4,"[>] VR Manager Start.");
  
  #if !UNITY_3_4 && !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1
          m_AllowRenderTargetAA = true;
  #endif
  
          InitializeVR();
  
          // Reset Manager's position so text display is correct.
          transform.position = new UnityEngine.Vector3(0, 0, 0);
          transform.rotation = new Quaternion();
          transform.localScale = new UnityEngine.Vector3(1, 1, 1);
  
          m_Wand = GameObject.Find("VRWand");
  
          if (ShowFPS) {
              guiText.enabled = true;
          } else {
              guiText.enabled = false;
          }
  
          if(ShowWand) {
              ShowWandGeometry(true);
          } else {
              ShowWandGeometry(false);
          }
  
          if (ForceQuality)
          {
  #if UNITY_3_4
              QualitySettings.currentLevel = (QualityLevel)ForceQualityIndex;
  #else
              QualitySettings.SetQualityLevel(ForceQualityIndex);
  #endif
              bool useOpenGLQuadbuffer = MiddleVR.VRDisplayMgr.GetActiveViewport(0).GetStereo() && (MiddleVR.VRDisplayMgr.GetActiveViewport(0).GetStereoMode()==0); //VRStereoMode_QuadBuffer = 0
              if( !Application.isEditor && ( useOpenGLQuadbuffer || MiddleVR.VRClusterMgr.GetForceOpenGLConversion() ) )
              {
                  m_NeedDelayedRenderingReset = true;
                  m_RenderingResetDelay = 1;
              }
          }
  
          // Manage VSync after the quality settings 
          MiddleVRTools.ManageVSync();
  
          // Set AA from vrx configuration file
          //QualitySettings.antiAliasing = m_AntiAliasingLevel;
  
  
gt:         MiddleVRTools.Log(4, "[<] End of VR Manager Start.");