1
0

SteamVR_TestTrackedCamera.cs 3.3 KB

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