コード例 #1
0
        private void InternalSetQuadViewActive(bool value)
        {
            if (value && m_externalCamera == null && !string.IsNullOrEmpty(m_configPath) && File.Exists(m_configPath))
            {
                // don't know why SteamVR_ExternalCamera must be instantiated from the prefab
                // when create SteamVR_ExternalCamera using AddComponent, errors came out when disabling
                var prefab = Resources.Load <GameObject>("SteamVR_ExternalCamera");
                if (prefab == null)
                {
                    Debug.LogError("SteamVR_ExternalCamera prefab not found!");
                }
                else
                {
                    var ctrlMgr = Instantiate(prefab);
                    var extCam  = ctrlMgr.transform.GetChild(0);
                    extCam.gameObject.name = "SteamVR External Camera";
                    extCam.SetParent(transform, false);
                    DestroyImmediate(extCam.GetComponent <SteamVR_TrackedObject>());
                    DestroyImmediate(ctrlMgr);

                    m_externalCamera = extCam.GetComponent <SteamVR_ExternalCamera>();
                    SteamVR_Render.instance.externalCamera = m_externalCamera;

                    // resolve config file
                    m_externalCamera.enabled    = false;
                    m_externalCamera.configPath = m_configPath;
                    m_externalCamera.ReadConfig();
                    m_externalCamera.enabled = true; // to preserve sceneResolutionScale on enabled

                    // resolve RenderTexture
                    m_externalCamera.AttachToCamera(SteamVR_Render.Top());
                    var w   = Screen.width / 2;
                    var h   = Screen.height / 2;
                    var cam = m_externalCamera.GetComponentInChildren <Camera>();
                    if (cam.targetTexture == null || cam.targetTexture.width != w || cam.targetTexture.height != h)
                    {
                        var tex = new RenderTexture(w, h, 24, RenderTextureFormat.ARGB32, QualitySettings.activeColorSpace == ColorSpace.Linear ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.Default);
                        tex.antiAliasing  = QualitySettings.antiAliasing == 0 ? 1 : QualitySettings.antiAliasing;
                        cam.targetTexture = tex;
                    }
                }
            }

            if (m_externalCamera != null)
            {
                m_externalCamera.gameObject.SetActive(value);

                if (value)
                {
                    VivePose.AddNewPosesListener(this);
                }
                else
                {
                    VivePose.RemoveNewPosesListener(this);
                }
            }
        }
        private bool TryGetTargetExCam(out SteamVR_ExternalCamera excam, out Camera camera)
        {
            if (!TryGetTargetExCam(out excam))
            {
                camera = null;
                return(false);
            }

            excam = ExternalCameraHook.Instance.externalCamera;
            var excamTrans = excam.transform;

            if (excamTrans.childCount <= 1)
            {
                camera = excam.GetComponentInChildren <Camera>();
            }
            else
            {
                // Locate the camera component on the last child and clean up other duplicated cameras
                // Note that SteamVR_ExternalCamera.ReadConfig triggers making a new clone from head camera
                // And ReadConfig is called when externalcamera.cfg is changed on disk
                var duplicateCamsObj = ListPool <GameObject> .Get();

                camera = null;
                for (int i = excamTrans.childCount - 1; i >= 0; --i)
                {
                    var cam = excamTrans.GetChild(i).GetComponent <Camera>();
                    if (cam == null)
                    {
                        continue;
                    }

                    if (camera == null)
                    {
                        camera = cam;
                    }
                    else
                    {
                        duplicateCamsObj.Add(cam.gameObject);
                    }
                }

                for (int i = duplicateCamsObj.Count - 1; i >= 0; --i)
                {
                    Destroy(duplicateCamsObj[i]);
                }

                ListPool <GameObject> .Release(duplicateCamsObj);
            }

            return(true);
        }