void Start() { Application.targetFrameRate = 90; if (_material == null) { _material = new Material(Shader.Find("Hidden/DrawTextureCloseLight")); } SetCameraPos(); StartCoroutine(ThreeGlassesUtils.DelayedRun(() => { if (_leftRenderTexture != null && _rightRenderTexture != null) { return; } _leftRenderTexture = new RenderTexture(RenderWidth / 2, RenderHeight, 24, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Default); _rightRenderTexture = new RenderTexture(RenderWidth / 2, RenderHeight, 24, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Default); leftCamera.SetRenderTarget(_leftRenderTexture); rightCamera.SetRenderTarget(_rightRenderTexture); upTexture = false; }, new WaitForEndOfFrame())); }
void Start() { ThreeGlassesUtils.Log("MainCamera init"); // check hmd status bool result = false; if (0 != ThreeGlassesDllInterface.SZVR_GetHMDConnectionStatus(ref result) || !result) { Debug.LogWarning("The Helmet Mounted Display is not connect"); } // get hmd name strPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(64); if (0 != ThreeGlassesDllInterface.SZVR_GetHMDDevName(strPtr)) { hmdName = Marshal.PtrToStringAnsi(strPtr, 64); } if (hmdName == null || hmdName.Length <= 0) { hmdName = "no name"; Debug.LogWarning("can not get HMD's name"); } // init RenderTexture if (renderTexture[0] == null && renderTexture[1] == null) { for (var i = 0; i < CAMERA_NUM; i++) { renderTexture[i] = new RenderTexture( (int)ThreeGlassesHeadDisplayLife.renderWidth / 2, (int)ThreeGlassesHeadDisplayLife.renderHeight, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default); renderTexture[i].antiAliasing = (int)hmdAntiAliasingLevel; renderTexture[i].Create(); } } // init camera VRCameraInit(); if (enableJoypad) { // init wand ThreeGlassesUtils.Log("init joypad"); joyPad[0] = new ThreeGlassesWand(InputType.LeftWand); joyPad[1] = new ThreeGlassesWand(InputType.RightWand); } StopAllCoroutines(); StartCoroutine(CallPluginAtEndOfFrames()); }
void Awake() { renderWidth = 2048; renderHeight = 1024; #if !UNITY_EDITOR var threadId = GetCurrentThreadId(); EnumThreadWindows(threadId, (hWnd, lParam) => { var classText = new StringBuilder(UnityWindowClassName.Length + 1); GetClassName(hWnd, classText, classText.Capacity); if (classText.ToString() != UnityWindowClassName) { return(true); } _windowHandle = hWnd; return(false); }, IntPtr.Zero); #endif bool result = false; HMDPresent(ref result); ThreeGlassesUtils.Log("ThreeGlassesHeadDisplayLife init"); uint[] buffsize = { renderWidth, renderHeight }; ThreeGlassesDllInterface.GetNativeRenderSize(buffsize); renderWidth = (uint)(scaleRenderSize * buffsize[0]); renderHeight = (uint)(scaleRenderSize * buffsize[1]); renderWidth = renderWidth - (renderWidth % 16); renderHeight = renderHeight - (renderHeight % 16); ThreeGlassesDllInterface.SZVRPluginInit( (uint)(AsynchronousProjection ? 0 : 1), renderWidth, renderHeight); #if !UNITY_EDITOR if (_windowHandle != IntPtr.Zero) { ShowWindow(_windowHandle, SW_SHOWNORMAL); SetForegroundWindow(_windowHandle); } #endif }
void Start() { StartCoroutine(ThreeGlassesUtils.DelayedRun(() => { cam.rect = new Rect(0, 0, 1.0f, 1.0f); var cams = gameObject.GetComponentsInChildren <Camera>(); if (cams == null) { return; } foreach (var l_cam in cams) { l_cam.rect = cam.rect; l_cam.fieldOfView = cam.fieldOfView; l_cam.nearClipPlane = cam.nearClipPlane; l_cam.farClipPlane = cam.farClipPlane; } }, new WaitForEndOfFrame())); }
void UpdateHMD() { // update hmd float[] pos = { 0, 0, 0 }; ThreeGlassesDllInterface.SZVR_GetHMDPos(pos); var hmdPosition = new Vector3(pos[0], pos[1], -pos[2]) / 1000f; if (!freezePosition && ThreeGlassesUtils.CheckNaN(hmdPosition)) { thisCam.transform.localPosition = hmdPosition; } float[] rotate = { 0, 0, 0, 1 }; ThreeGlassesDllInterface.SZVR_GetHMDRotate(rotate); hmdRotation = new Quaternion(rotate[0], rotate[1], -rotate[2], -rotate[3]); if (!freezeRotation) { thisCam.transform.localRotation = hmdRotation; } ThreeGlassesDllInterface.StereoRenderBegin(); bool[] button = { false, false }; ThreeGlassesDllInterface.SZVR_GetHMDMenuButton(ref button[0]); ThreeGlassesDllInterface.SZVR_GetHMDExitButton(ref button[1]); for (int i = 0; i < 2; i++) { if (button[i]) { hmdKeyStatus |= 1 << i; } } // touchpad byte[] touchPos = { 0, 0 }; ThreeGlassesDllInterface.SZVR_GetHMDTouchpad(touchPos); hmdTouchPad[0] = ((touchPos[0] / (float)255.0) - 0.5f) * 2.0f; hmdTouchPad[1] = (-(touchPos[1] / (float)255.0) + 0.5f) * 2.0f; }
void OnApplicationQuit() { ThreeGlassesUtils.Log("ThreeGlassesHeadDisplayLife application quit"); ThreeGlassesDllInterface.SZVRPluginDestroy(); }
void UpdateWand() { // update wand info if (!enableJoypad) { return; } byte[] connect = { 0, 0 }; if (0 == ThreeGlassesDllInterface.SZVR_GetWandConnectionStatus(connect)) { if (connect[0] != 0 || connect[1] != 0) { bool getRotate = false, getPos = false, getTrigger = false, getStick = false, getButton = false; float[] wandRotate = { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; float[] wandPos = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; byte[] trigger = { 0, 0 }; byte[] stick = { 0, 0, 0, 0 }; byte[] wandButton = new byte[12]; if (0 == ThreeGlassesDllInterface.SZVR_GetWandRotate(wandRotate)) { getRotate = true; } if (0 == ThreeGlassesDllInterface.SZVR_GetWandPos(wandPos)) { getPos = true; } if (0 == ThreeGlassesDllInterface.SZVR_GetWandTriggerProcess(trigger)) { getTrigger = true; } if (0 == ThreeGlassesDllInterface.SZVR_GetWandStick(stick)) { ThreeGlassesUtils.Log("lwand=" + stick[0] + " " + stick[1]); ThreeGlassesUtils.Log("rwand=" + stick[2] + " " + stick[3]); getStick = true; } if (0 == ThreeGlassesDllInterface.SZVR_GetWandButton(wandButton)) { getButton = true; } for (var i = 0; i < JOYPAD_NUM; i++) { if (connect[i] != 0) { if (getRotate) { joyPad[i].UpdateRotate(wandRotate); } if (getPos) { joyPad[i].UpdatePos(wandPos); } if (getTrigger) { joyPad[i].UpdateTrigger(trigger); } if (getStick) { joyPad[i].UpdateStick(stick); } if (getButton) { joyPad[i].UpdateButton(wandButton); } } } } } }
void VRCameraInit() { // get maincamera's nearClip and farClip thisCam = GetComponent <Camera>(); near = thisCam.nearClipPlane; far = thisCam.farClipPlane; // get components ArrayList needAdd = new ArrayList(); System.Type[] needAddTypes = new System.Type[] { typeof(GUILayer), typeof(FlareLayer) }; Component[] coms = gameObject.GetComponents <Component>(); // non-script component foreach (var com in coms) { foreach (var type in needAddTypes) { if (com.GetType() == type) { needAdd.Add(com); } } } // script component foreach (var com in coms) { if (com is MonoBehaviour) { if (com != this) { System.Type t = com.GetType(); MemberInfo meth = t.GetMethod("OnRenderImage", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (meth != null) { needAdd.Add(com); } } } } // create and set camera for (int i = 0; i < CAMERA_NUM; i++) { // create camera subCamera[i] = new GameObject(); // rename,add ThreeGlassesSubCamera subCamera[i].name = cameraName[i]; subCameraCam[i] = subCamera[i].AddComponent <Camera>(); subCameraCam[i].nearClipPlane = near; subCameraCam[i].farClipPlane = far; subCameraCam[i].cullingMask = layerMask; subCameraCam[i].depth = thisCam.depth; subCamera[i].transform.SetParent(this.transform); // add the components foreach (var item in needAdd) { ThreeGlassesUtils.CopyComponent((Component)item, subCamera[i]); } // add subCamera script after add all component subCameraScript[i] = subCamera[i].AddComponent <ThreeGlassesSubCamera>(); subCameraScript[i].CameraType = (ThreeGlassesSubCamera.CameraTypes)i; } var eyeDis = new Vector3(eyeDistance / 2, 0, 0); subCamera[0].transform.localPosition = -eyeDis; subCamera[1].transform.localPosition = eyeDis; // set the camera who bind ThreeGlassesSubCamera ThreeGlassesSubCamera[] cams = GameObject.FindObjectsOfType(typeof(ThreeGlassesSubCamera)) as ThreeGlassesSubCamera[]; foreach (var cam in cams) { Camera tempCamera = cam.gameObject.GetComponent <Camera>(); if (ThreeGlassesSubCamera.CameraTypes.Screen == cam.CameraType) { tempCamera.targetTexture = null; continue; } tempCamera.targetTexture = renderTexture[(int)cam.CameraType]; } thisCam.enabled = !onlyHeadDisplay; }