IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SteamVR_TestTrackedCamera.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 using UnityEngine;
3 
4 public class SteamVR_TestTrackedCamera : MonoBehaviour
5 {
6  public Material material;
7  public Transform target;
8  public bool undistorted = true;
9  public bool cropped = true;
10 
11  void OnEnable()
12  {
13  // The video stream must be symmetrically acquired and released in
14  // order to properly disable the stream once there are no consumers.
15  var source = SteamVR_TrackedCamera.Source(undistorted);
16  source.Acquire();
17 
18  // Auto-disable if no camera is present.
19  if (!source.hasCamera)
20  enabled = false;
21  }
22 
23  void OnDisable()
24  {
25  // Clear the texture when no longer active.
26  material.mainTexture = null;
27 
28  // The video stream must be symmetrically acquired and released in
29  // order to properly disable the stream once there are no consumers.
30  var source = SteamVR_TrackedCamera.Source(undistorted);
31  source.Release();
32  }
33 
34  void Update()
35  {
36  var source = SteamVR_TrackedCamera.Source(undistorted);
37  var texture = source.texture;
38  if (texture == null)
39  {
40  return;
41  }
42 
43  // Apply the latest texture to the material. This must be performed
44  // every frame since the underlying texture is actually part of a ring
45  // buffer which is updated in lock-step with its associated pose.
46  // (You actually really only need to call any of the accessors which
47  // internally call Update on the SteamVR_TrackedCamera.VideoStreamTexture).
48  material.mainTexture = texture;
49 
50  // Adjust the height of the quad based on the aspect to keep the texels square.
51  var aspect = (float)texture.width / texture.height;
52 
53  // The undistorted video feed has 'bad' areas near the edges where the original
54  // square texture feed is stretched to undo the fisheye from the lens.
55  // Therefore, you'll want to crop it to the specified frameBounds to remove this.
56  if (cropped)
57  {
58  var bounds = source.frameBounds;
59  material.mainTextureOffset = new Vector2(bounds.uMin, bounds.vMin);
60 
61  var du = bounds.uMax - bounds.uMin;
62  var dv = bounds.vMax - bounds.vMin;
63  material.mainTextureScale = new Vector2(du, dv);
64 
65  aspect *= Mathf.Abs(du / dv);
66  }
67  else
68  {
69  material.mainTextureOffset = Vector2.zero;
70  material.mainTextureScale = new Vector2(1, -1);
71  }
72 
73  target.localScale = new Vector3(1, 1.0f / aspect, 1);
74 
75  // Apply the pose that this frame was recorded at.
76  if (source.hasTracking)
77  {
78  var t = source.transform;
79  target.localPosition = t.pos;
80  target.localRotation = t.rot;
81  }
82  }
83 }
84