UpdateStereoMaterial.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0)
  2. #define UNITY_HELPATTRIB
  3. #endif
  4. using UnityEngine;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2015-2020 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProVideo
  9. {
  10. /// <summary>
  11. /// This script is needed to send the camera position to the stereo shader so that
  12. /// it can determine which eye it is rendering. In the future this script won't
  13. /// be needed once we have support for single-pass stereo rendering.
  14. /// </summary>
  15. [AddComponentMenu("AVPro Video/Update Stereo Material", 400)]
  16. #if UNITY_HELPATTRIB
  17. [HelpURL("http://renderheads.com/products/avpro-video/")]
  18. #endif
  19. public class UpdateStereoMaterial : MonoBehaviour
  20. {
  21. [Header("Stereo camera")]
  22. public Camera _camera;
  23. [Header("Rendering elements")]
  24. public MeshRenderer _renderer;
  25. public UnityEngine.UI.Graphic _uGuiComponent;
  26. public Material _material;
  27. [SerializeField]
  28. private StereoEye _forceEyeMode;
  29. private static int _cameraPositionId;
  30. private static int _viewMatrixId;
  31. private StereoEye _setForceEyeMode = StereoEye.Both;
  32. public StereoEye ForceEyeMode { get { return _forceEyeMode; } set { _forceEyeMode = value; } }
  33. private Camera _foundCamera;
  34. void Awake()
  35. {
  36. if (_cameraPositionId == 0)
  37. {
  38. _cameraPositionId = Shader.PropertyToID("_cameraPosition");
  39. }
  40. if (_viewMatrixId == 0)
  41. {
  42. _viewMatrixId = Shader.PropertyToID("_ViewMatrix");
  43. }
  44. if (_camera == null)
  45. {
  46. Debug.LogWarning("[AVProVideo] No camera set for UpdateStereoMaterial component. If you are rendering in stereo then it is recommended to set this.");
  47. }
  48. }
  49. private void SetupMaterial(Material m, Camera camera)
  50. {
  51. m.SetVector(_cameraPositionId, camera.transform.position);
  52. m.SetMatrix(_viewMatrixId, camera.worldToCameraMatrix.transpose);
  53. if (_forceEyeMode != _setForceEyeMode)
  54. {
  55. Helper.SetupStereoEyeModeMaterial(m, _forceEyeMode);
  56. _setForceEyeMode = _forceEyeMode;
  57. }
  58. }
  59. // We do a LateUpdate() to allow for any changes in the camera position that may have happened in Update()
  60. private void LateUpdate()
  61. {
  62. if (_camera != null && _foundCamera != _camera)
  63. {
  64. _foundCamera = _camera;
  65. }
  66. if (_foundCamera == null)
  67. {
  68. _foundCamera = Camera.main;
  69. if (_foundCamera == null)
  70. {
  71. Debug.LogWarning("[AVPro Video] Cannot find main camera for UpdateStereoMaterial, this can lead to eyes flickering");
  72. if (Camera.allCameras.Length > 0)
  73. {
  74. _foundCamera = Camera.allCameras[0];
  75. Debug.LogWarning("[AVPro Video] UpdateStereoMaterial using camera " + _foundCamera.name);
  76. }
  77. }
  78. }
  79. if (_renderer == null && _material == null)
  80. {
  81. _renderer = this.gameObject.GetComponent<MeshRenderer>();
  82. }
  83. if (_foundCamera != null)
  84. {
  85. if (_renderer != null)
  86. {
  87. SetupMaterial(_renderer.material, _foundCamera);
  88. }
  89. if (_material != null)
  90. {
  91. SetupMaterial(_material, _foundCamera);
  92. }
  93. if (_uGuiComponent != null)
  94. {
  95. SetupMaterial(_uGuiComponent.material, _foundCamera);
  96. }
  97. }
  98. }
  99. }
  100. }