StereoRenderEffect.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. namespace Nxr.Internal
  16. {
  17. /// @cond
  18. [RequireComponent(typeof(Camera))]
  19. [AddComponentMenu("NXR/Internal/StereoRenderEffect")]
  20. public class StereoRenderEffect : MonoBehaviour
  21. {
  22. private Material material;
  23. private Camera cam;
  24. #if UNITY_5_6_OR_NEWER
  25. private Rect fullRect;
  26. public NxrViewer.Eye eye;
  27. #else
  28. private static readonly Rect fullRect = new Rect(0, 0, 1, 1);
  29. #endif // UNITY_5_6_OR_NEWER
  30. void Awake()
  31. {
  32. cam = GetComponent<Camera>();
  33. }
  34. void Start()
  35. {
  36. material = new Material(Shader.Find("NAR/UnlitTexture"));
  37. #if UNITY_5_6_OR_NEWER
  38. fullRect = (eye == NxrViewer.Eye.Left ? new Rect(0, 0, 0.5f, 1) : new Rect(0.5f, 0, 0.5f, 1));
  39. #endif
  40. }
  41. public void UpdateEye(NxrViewer.Eye eyeTmp)
  42. {
  43. #if UNITY_5_6_OR_NEWER
  44. this.eye = eyeTmp;
  45. fullRect = (eye == NxrViewer.Eye.Left ? new Rect(0, 0, 0.5f, 1) : new Rect(0.5f, 0, 0.5f, 1));
  46. #endif
  47. }
  48. void OnRenderImage(RenderTexture source, RenderTexture dest)
  49. {
  50. GL.PushMatrix();
  51. int width = dest ? dest.width : Screen.width;
  52. int height = dest ? dest.height : Screen.height;
  53. GL.LoadPixelMatrix(0, width, height, 0);
  54. // Camera rects are in screen coordinates (bottom left is origin), but DrawTexture takes a
  55. // rect in GUI coordinates (top left is origin).
  56. Rect blitRect = cam.pixelRect;
  57. blitRect.y = height - blitRect.height - blitRect.y;
  58. RenderTexture oldActive = RenderTexture.active;
  59. RenderTexture.active = dest;
  60. Graphics.DrawTexture(blitRect, source, fullRect, 0, 0, 0, 0, Color.white, material);
  61. RenderTexture.active = oldActive;
  62. GL.PopMatrix();
  63. }
  64. }
  65. }
  66. /// @endcond