Mirror_2DandOculus.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using UnityEngine;
  3. public class Mirror_2DandOculus : MonoBehaviour
  4. {
  5. private void Start()
  6. {
  7. this.m_My_Render = base.GetComponent<Renderer>();
  8. this.m_My_Render.sharedMaterial.shader = Resources.Load<Shader>("Shaders/Mirror_2DandOculus");
  9. int antiAliasing = (QualitySettings.antiAliasing != 0) ? QualitySettings.antiAliasing : 1;
  10. this.m_Reflect_Texture = new RenderTexture(this.m_TextureSize, this.m_TextureSize, 16);
  11. this.m_Reflect_Texture.name = "_MirrorTexture";
  12. this.m_Reflect_Texture.isPowerOfTwo = true;
  13. this.m_Reflect_Texture.antiAliasing = antiAliasing;
  14. this.CreateMirrorObjects();
  15. }
  16. private void OnWillRenderObject()
  17. {
  18. if (this.m_SelfRendering)
  19. {
  20. return;
  21. }
  22. if (SteamVR.instance != null)
  23. {
  24. base.enabled = false;
  25. return;
  26. }
  27. this.m_SelfRendering = true;
  28. this.Reflection(Camera.current, this.m_Reflect_Texture);
  29. this.m_SelfRendering = false;
  30. }
  31. private void Reflection(Camera current_cam, RenderTexture reflect_Tex)
  32. {
  33. Vector3 position = base.transform.position;
  34. Vector3 vector = base.transform.TransformDirection(Vector3.back);
  35. this.UpdateCameraModes(current_cam, this.m_Reflect_Camera);
  36. float w = -Vector3.Dot(vector, position) - this.m_ClipPlaneOffset;
  37. Vector4 plane = new Vector4(vector.x, vector.y, vector.z, w);
  38. Matrix4x4 zero = Matrix4x4.zero;
  39. this.CalculateReflectionMatrix(ref zero, plane);
  40. Vector3 position2 = Camera.current.transform.position;
  41. Vector3 position3 = zero.MultiplyPoint(position2);
  42. this.m_Reflect_Camera.transform.position = position3;
  43. Vector3 eulerAngles = Camera.current.transform.eulerAngles;
  44. this.m_Reflect_Camera.transform.eulerAngles = eulerAngles;
  45. this.m_Reflect_Camera.worldToCameraMatrix = current_cam.worldToCameraMatrix * zero;
  46. Vector4 clipPlane = this.CameraSpacePlane(this.m_Reflect_Camera, position, vector, 1f);
  47. Matrix4x4 projectionMatrix = current_cam.CalculateObliqueMatrix(clipPlane);
  48. this.m_Reflect_Camera.projectionMatrix = projectionMatrix;
  49. this.m_Reflect_Camera.cullingMask = this.m_ReflectLayers.value;
  50. GL.invertCulling = true;
  51. this.m_Reflect_Camera.targetTexture = reflect_Tex;
  52. this.m_Reflect_Camera.Render();
  53. this.m_My_Render.sharedMaterial.SetTexture("_MainTex", reflect_Tex);
  54. GL.invertCulling = false;
  55. }
  56. private void UpdateCameraModes(Camera src, Camera dest)
  57. {
  58. if (dest == null)
  59. {
  60. return;
  61. }
  62. dest.clearFlags = src.clearFlags;
  63. dest.backgroundColor = src.backgroundColor;
  64. dest.farClipPlane = src.farClipPlane;
  65. dest.nearClipPlane = src.nearClipPlane;
  66. dest.orthographic = src.orthographic;
  67. dest.fieldOfView = src.fieldOfView;
  68. dest.aspect = src.aspect;
  69. dest.orthographicSize = src.orthographicSize;
  70. }
  71. private void CreateMirrorObjects()
  72. {
  73. GameObject gameObject = new GameObject("Mirror Refl Camera", new Type[]
  74. {
  75. typeof(Camera)
  76. });
  77. this.m_Reflect_Camera = gameObject.GetComponent<Camera>();
  78. this.m_Reflect_Camera.enabled = false;
  79. this.m_Reflect_Camera.transform.position = base.transform.position;
  80. this.m_Reflect_Camera.transform.rotation = base.transform.rotation;
  81. gameObject.transform.parent = base.transform;
  82. }
  83. private Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float sideSign)
  84. {
  85. Vector3 v = pos + normal * this.m_ClipPlaneOffset;
  86. Matrix4x4 worldToCameraMatrix = cam.worldToCameraMatrix;
  87. Vector3 lhs = worldToCameraMatrix.MultiplyPoint(v);
  88. Vector3 rhs = worldToCameraMatrix.MultiplyVector(normal).normalized * sideSign;
  89. return new Vector4(rhs.x, rhs.y, rhs.z, -Vector3.Dot(lhs, rhs));
  90. }
  91. private void CalculateReflectionMatrix(ref Matrix4x4 reflectionMat, Vector4 plane)
  92. {
  93. reflectionMat.m00 = 1f - 2f * plane[0] * plane[0];
  94. reflectionMat.m01 = -2f * plane[0] * plane[1];
  95. reflectionMat.m02 = -2f * plane[0] * plane[2];
  96. reflectionMat.m03 = -2f * plane[3] * plane[0];
  97. reflectionMat.m10 = -2f * plane[1] * plane[0];
  98. reflectionMat.m11 = 1f - 2f * plane[1] * plane[1];
  99. reflectionMat.m12 = -2f * plane[1] * plane[2];
  100. reflectionMat.m13 = -2f * plane[3] * plane[1];
  101. reflectionMat.m20 = -2f * plane[2] * plane[0];
  102. reflectionMat.m21 = -2f * plane[2] * plane[1];
  103. reflectionMat.m22 = 1f - 2f * plane[2] * plane[2];
  104. reflectionMat.m23 = -2f * plane[3] * plane[2];
  105. reflectionMat.m30 = 0f;
  106. reflectionMat.m31 = 0f;
  107. reflectionMat.m32 = 0f;
  108. reflectionMat.m33 = 1f;
  109. }
  110. private const string m_Shader_Path = "Shaders/Mirror_2DandOculus";
  111. [SerializeField]
  112. private int m_TextureSize = 2048;
  113. [SerializeField]
  114. private float m_ClipPlaneOffset = 0.07f;
  115. [SerializeField]
  116. private LayerMask m_ReflectLayers = -1;
  117. private bool m_SelfRendering;
  118. private Renderer m_My_Render;
  119. private Camera m_Reflect_Camera;
  120. private RenderTexture m_Reflect_Texture;
  121. }