UpdateStereoMaterial.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace RenderHeads.Media.AVProVideo
  5. {
  6. [AddComponentMenu("AVPro Video/Update Stereo Material", 400)]
  7. [HelpURL("http://renderheads.com/product/avpro-video/")]
  8. public class UpdateStereoMaterial : MonoBehaviour
  9. {
  10. private void Awake()
  11. {
  12. this._cameraPositionId = Shader.PropertyToID("_cameraPosition");
  13. this._viewMatrixId = Shader.PropertyToID("_ViewMatrix");
  14. if (this._camera == null)
  15. {
  16. Debug.LogWarning("[AVProVideo] No camera set for UpdateStereoMaterial component. If you are rendering in stereo then it is recommended to set this.");
  17. }
  18. }
  19. private void SetupMaterial(Material m, Camera camera)
  20. {
  21. m.SetVector(this._cameraPositionId, camera.transform.position);
  22. m.SetMatrix(this._viewMatrixId, camera.worldToCameraMatrix.transpose);
  23. }
  24. private void LateUpdate()
  25. {
  26. Camera camera = this._camera;
  27. if (camera == null)
  28. {
  29. camera = Camera.main;
  30. }
  31. if (this._renderer == null && this._material == null)
  32. {
  33. this._renderer = base.gameObject.GetComponent<MeshRenderer>();
  34. }
  35. if (camera != null)
  36. {
  37. if (this._renderer != null)
  38. {
  39. this.SetupMaterial(this._renderer.material, camera);
  40. }
  41. if (this._material != null)
  42. {
  43. this.SetupMaterial(this._material, camera);
  44. }
  45. if (this._uGuiComponent != null)
  46. {
  47. this.SetupMaterial(this._uGuiComponent.material, camera);
  48. }
  49. }
  50. }
  51. [Header("Stereo camera")]
  52. public Camera _camera;
  53. [Header("Rendering elements")]
  54. public MeshRenderer _renderer;
  55. public Graphic _uGuiComponent;
  56. public Material _material;
  57. private int _cameraPositionId;
  58. private int _viewMatrixId;
  59. }
  60. }