Пример #1
0
        private void Update()
        {
            if (Time.frameCount == prevFrameCount)
            {
                return;
            }

            prevFrameCount = Time.frameCount;

            if (videostream.handle == 0)
            {
                return;
            }

            var vr = SteamVR.instance;

            if (vr == null)
            {
                return;
            }

            var trackedCamera = OpenVR.TrackedCamera;

            if (trackedCamera == null)
            {
                return;
            }

            var nativeTex     = IntPtr.Zero;
            var deviceTexture = (_texture != null) ? _texture : new Texture2D(2, 2);
            var headerSize    = (uint)Marshal.SizeOf(header.GetType());

            if (vr.textureType == ETextureType.OpenGL)
            {
                if (glTextureId != 0)
                {
                    trackedCamera.ReleaseVideoStreamTextureGL(videostream.handle, glTextureId);
                }

                if (
                    trackedCamera.GetVideoStreamTextureGL(videostream.handle, frameType, ref glTextureId, ref header, headerSize) !=
                    EVRTrackedCameraError.None)
                {
                    return;
                }

                nativeTex = (IntPtr)glTextureId;
            }
            else if (vr.textureType == ETextureType.DirectX)
            {
                if (
                    trackedCamera.GetVideoStreamTextureD3D11(videostream.handle, frameType, deviceTexture.GetNativeTexturePtr(),
                                                             ref nativeTex, ref header, headerSize) != EVRTrackedCameraError.None)
                {
                    return;
                }
            }

            if (_texture == null)
            {
                _texture = Texture2D.CreateExternalTexture((int)header.nWidth, (int)header.nHeight, TextureFormat.RGBA32,
                                                           false, false, nativeTex);

                uint width = 0, height = 0;
                var  frameBounds = new VRTextureBounds_t();
                if (trackedCamera.GetVideoStreamTextureSize(deviceIndex, frameType, ref frameBounds, ref width, ref height) ==
                    EVRTrackedCameraError.None)
                {
                    // Account for textures being upside-down in Unity.
                    frameBounds.vMin = 1.0f - frameBounds.vMin;
                    frameBounds.vMax = 1.0f - frameBounds.vMax;
                    this.frameBounds = frameBounds;
                }
            }
            else
            {
                _texture.UpdateExternalTexture(nativeTex);
            }
        }