Overlay3DTexture.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SXR;
  5. namespace Ximmerse.XR.Rendering
  6. {
  7. public class Overlay3DTexture : MonoBehaviour
  8. {
  9. [Tooltip("The texture to be displayed at world space.")]
  10. public Texture2D texture;
  11. [Tooltip("The world anchor transform to display the texture")]
  12. public Transform anchor;
  13. [Range(0, 3)]
  14. [Tooltip("Layer index to display the texture, must be 0-3")]
  15. public int layerIndex = 1;
  16. [Tooltip("Size of the displayed texture.")]
  17. public Vector2 size = Vector2.one;
  18. Camera m_MainCamera;
  19. float[] anchorLL = new float[4];//mvp(4) + uv(2)
  20. float[] anchorLT = new float[4];//mvp(4) + uv(2)
  21. float[] anchorRT = new float[4];//mvp(4) + uv(2)
  22. float[] anchorRB = new float[4];//mvp(4) + uv(2)
  23. float[] matrixInFloats = new float[16];
  24. private void LateUpdate()
  25. {
  26. SetLayerData();
  27. //SetLayerData();
  28. }
  29. void SetLayerData()
  30. {
  31. bool isAndroid = Application.platform == RuntimePlatform.Android;
  32. if (!isAndroid || !anchor || !texture)
  33. {
  34. return;
  35. }
  36. if (!m_MainCamera)
  37. {
  38. m_MainCamera = Camera.main;
  39. }
  40. if (!m_MainCamera)
  41. {
  42. Debug.LogError("OverlayTexture : no main camera !");
  43. return;
  44. }
  45. Matrix4x4 projectionMatrix = default(Matrix4x4);
  46. float ipd = 0.062f;
  47. float l = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Left_FLOAT);
  48. float r = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Right_FLOAT);
  49. float t = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Top_FLOAT);
  50. float b = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Bottom_FLOAT);
  51. float n = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Near_FLOAT);
  52. float f = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Far_FLOAT);
  53. projectionMatrix = GetPerspectiveProjectionMatrix(l, r, b, t, n, f);
  54. Matrix4x4 mvp = projectionMatrix * m_MainCamera.worldToCameraMatrix * anchor.localToWorldMatrix;
  55. Vector4 clipLL = mvp * new Vector4(-size.x * 0.5f, -size.y * 0.5f, 0, 1);
  56. Vector4 clipLT = mvp * new Vector4(-size.x * 0.5f, size.y * 0.5f, 0, 1);
  57. Vector4 clipRT = mvp * new Vector4(size.x * 0.5f, size.y * 0.5f, 0, 1);
  58. Vector4 clipRB = mvp * new Vector4(size.x * 0.5f, -size.y * 0.5f, 0, 1);
  59. anchorLL[0] = clipLL[0];
  60. anchorLL[1] = clipLL[1];
  61. anchorLL[2] = clipLL[2];
  62. anchorLL[3] = clipLL[3];
  63. //anchorLL[4] = 0;
  64. //anchorLL[5] = 0;
  65. anchorLT[0] = clipLT[0];
  66. anchorLT[1] = clipLT[1];
  67. anchorLT[2] = clipLT[2];
  68. anchorLT[3] = clipLT[3];
  69. //anchorLT[4] = 0;
  70. //anchorLT[5] = 1;
  71. anchorRT[0] = clipRT[0];
  72. anchorRT[1] = clipRT[1];
  73. anchorRT[2] = clipRT[2];
  74. anchorRT[3] = clipRT[3];
  75. //anchorRT[4] = 1;
  76. //anchorRT[5] = 1;
  77. anchorRB[0] = clipRB[0];
  78. anchorRB[1] = clipRB[1];
  79. anchorRB[2] = clipRB[2];
  80. anchorRB[3] = clipRB[3];
  81. //anchorRB[4] = 1;
  82. //anchorRB[5] = 0;
  83. //Call SVR api :
  84. {
  85. var mtx = mvp;
  86. for (int i = 0; i < 16; i++)
  87. {
  88. matrixInFloats[i] = mtx[i];
  89. }
  90. SvrPluginAndroid.Unity_setWorldOverlayTexture(true, this.layerIndex, this.texture.GetNativeTexturePtr().ToInt32(),
  91. this.size.x, this.size.y, matrixInFloats, anchorLL, anchorLT, anchorRT, anchorRB
  92. );
  93. }
  94. }
  95. static Matrix4x4 GetPerspectiveProjectionMatrix(float left, float right, float bottom, float top, float near, float far)
  96. {
  97. float x = 2.0F * near / (right - left);
  98. float y = 2.0F * near / (top - bottom);
  99. float a = (right + left) / (right - left);
  100. float b = (top + bottom) / (top - bottom);
  101. float c = -(far + near) / (far - near);
  102. float d = -(2.0F * far * near) / (far - near);
  103. float e = -1.0F;
  104. Matrix4x4 m = new Matrix4x4();
  105. m[0, 0] = x;
  106. m[0, 1] = 0;
  107. m[0, 2] = a;
  108. m[0, 3] = 0;
  109. m[1, 0] = 0;
  110. m[1, 1] = y;
  111. m[1, 2] = b;
  112. m[1, 3] = 0;
  113. m[2, 0] = 0;
  114. m[2, 1] = 0;
  115. m[2, 2] = c;
  116. m[2, 3] = d;
  117. m[3, 0] = 0;
  118. m[3, 1] = 0;
  119. m[3, 2] = e;
  120. m[3, 3] = 0;
  121. return m;
  122. }
  123. }
  124. }