32 this.undistorted = undistorted;
33 videostream = Stream(deviceIndex);
35 public bool undistorted {
get;
private set; }
36 public uint deviceIndex {
get {
return videostream.deviceIndex; } }
37 public bool hasCamera {
get {
return videostream.hasCamera; } }
38 public bool hasTracking {
get { Update();
return header.standingTrackedDevicePose.bPoseIsValid; } }
39 public uint frameId {
get { Update();
return header.nFrameSequence; } }
41 public EVRTrackedCameraFrameType frameType {
get {
return undistorted ? EVRTrackedCameraFrameType.Undistorted : EVRTrackedCameraFrameType.Distorted; } }
44 public Texture2D texture {
get { Update();
return _texture; } }
47 public Vector3 velocity {
get { Update(); var pose = header.standingTrackedDevicePose;
return new Vector3(pose.vVelocity.v0, pose.vVelocity.v1, -pose.vVelocity.v2); } }
48 public Vector3 angularVelocity {
get { Update(); var pose = header.standingTrackedDevicePose;
return new Vector3(-pose.vAngularVelocity.v0, -pose.vAngularVelocity.v1, pose.vAngularVelocity.v2); } }
52 public ulong Acquire()
54 return videostream.Acquire();
56 public ulong Release()
58 var result = videostream.Release();
60 if (videostream.handle == 0)
62 Object.Destroy(_texture);
69 int prevFrameCount = -1;
72 if (Time.frameCount == prevFrameCount)
75 prevFrameCount = Time.frameCount;
77 if (videostream.handle == 0)
80 var vr = SteamVR.instance;
84 var trackedCamera = OpenVR.TrackedCamera;
85 if (trackedCamera == null)
88 var nativeTex = System.IntPtr.Zero;
89 var deviceTexture = (_texture != null) ? _texture :
new Texture2D(2, 2);
90 var headerSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(header.GetType());
92 if (vr.textureType == ETextureType.OpenGL)
95 trackedCamera.ReleaseVideoStreamTextureGL(videostream.handle, glTextureId);
97 if (trackedCamera.GetVideoStreamTextureGL(videostream.handle, frameType, ref glTextureId, ref header, headerSize) != EVRTrackedCameraError.None)
100 nativeTex = (System.IntPtr)glTextureId;
102 else if (vr.textureType == ETextureType.DirectX)
104 if (trackedCamera.GetVideoStreamTextureD3D11(videostream.handle, frameType, deviceTexture.GetNativeTexturePtr(), ref nativeTex, ref header, headerSize) != EVRTrackedCameraError.None)
108 if (_texture == null)
110 _texture = Texture2D.CreateExternalTexture((int)header.nWidth, (
int)header.nHeight, TextureFormat.RGBA32,
false,
false, nativeTex);
112 uint width = 0, height = 0;
114 if (trackedCamera.GetVideoStreamTextureSize(deviceIndex, frameType, ref frameBounds, ref width, ref height) == EVRTrackedCameraError.None)
117 frameBounds.vMin = 1.0f - frameBounds.vMin;
118 frameBounds.vMax = 1.0f - frameBounds.vMax;
119 this.frameBounds = frameBounds;
124 _texture.UpdateExternalTexture(nativeTex);
129 VideoStream videostream;
133 #region Top level accessors.
137 if (distorted == null)
139 if (distorted[deviceIndex] == null)
141 return distorted[deviceIndex];
144 public static VideoStreamTexture Undistorted(
int deviceIndex = (
int)
OpenVR.k_unTrackedDeviceIndex_Hmd)
146 if (undistorted == null)
147 undistorted =
new VideoStreamTexture[OpenVR.k_unMaxTrackedDeviceCount];
148 if (undistorted[deviceIndex] == null)
149 undistorted[deviceIndex] =
new VideoStreamTexture((uint)deviceIndex,
true);
150 return undistorted[deviceIndex];
153 public static VideoStreamTexture Source(
bool undistorted,
int deviceIndex = (
int)
OpenVR.k_unTrackedDeviceIndex_Hmd)
155 return undistorted ? Undistorted(deviceIndex) : Distorted(deviceIndex);
158 private static VideoStreamTexture[] distorted, undistorted;
162 #region Internal class to manage lifetime of video streams (per device).
166 public VideoStream(uint deviceIndex)
168 this.deviceIndex = deviceIndex;
169 var trackedCamera = OpenVR.TrackedCamera;
170 if (trackedCamera != null)
171 trackedCamera.HasCamera(deviceIndex, ref _hasCamera);
173 public uint deviceIndex {
get;
private set; }
176 public ulong handle {
get {
return _handle; } }
179 public bool hasCamera {
get {
return _hasCamera; } }
182 public ulong Acquire()
184 if (_handle == 0 && hasCamera)
186 var trackedCamera = OpenVR.TrackedCamera;
187 if (trackedCamera != null)
188 trackedCamera.AcquireVideoStreamingService(deviceIndex, ref _handle);
192 public ulong Release()
194 if (refCount > 0 && --refCount == 0 && _handle != 0)
196 var trackedCamera = OpenVR.TrackedCamera;
197 if (trackedCamera != null)
198 trackedCamera.ReleaseVideoStreamingService(_handle);
205 static VideoStream Stream(uint deviceIndex)
207 if (videostreams == null)
208 videostreams =
new VideoStream[OpenVR.k_unMaxTrackedDeviceCount];
209 if (videostreams[deviceIndex] == null)
210 videostreams[deviceIndex] =
new VideoStream(deviceIndex);
211 return videostreams[deviceIndex];
214 static VideoStream[] videostreams;