ARRender.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using EZXR.Glass.Core;
  2. using EZXR.Glass.Device;
  3. using EZXR.Glass.SixDof;
  4. using System;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. namespace EZXR.Glass.Rendering
  8. {
  9. public class ARRender : MonoBehaviour
  10. {
  11. #region params
  12. private bool m_ConfigCamera = false;
  13. private Material m_BackgroundMaterial;
  14. private CommandBuffer m_VideoCommandBuffer;
  15. private Camera m_Camera;
  16. private Texture2D m_VideoTexture;
  17. private bool m_enableRendering = true;
  18. private static int screenWidth;
  19. private static int screenHeight;
  20. private static float imageWidth;
  21. private static float imageHeight;
  22. //int cnt = 0;
  23. public static byte[] bytes;
  24. #endregion
  25. #region unity functions
  26. void Start()
  27. {
  28. CameraResolution cameraResolution = new CameraResolution();
  29. NativeTracking.GetFisheyeCameraResolution(ref cameraResolution);
  30. screenWidth = cameraResolution.width;
  31. screenHeight = cameraResolution.height;
  32. imageWidth = (float)cameraResolution.width;
  33. imageHeight = (float)cameraResolution.height;
  34. }
  35. private void Awake()
  36. {
  37. m_Camera = GetComponent<Camera>();
  38. }
  39. private void OnPreRender()
  40. {
  41. Debug.Log("UNITY LOG ===== heihei");
  42. if (!m_enableRendering) return;
  43. Debug.Log("UNITY LOG ===== xixi");
  44. UpdateInsightARBackground();
  45. }
  46. #endregion
  47. #region custom functions
  48. /// <summary>
  49. /// 控制是否渲染背景
  50. /// </summary>
  51. /// <param name="enabled"></param>
  52. public void SetRenderCamera(bool enabled)
  53. {
  54. m_enableRendering = enabled;
  55. }
  56. private void UpdateInsightARBackground()
  57. {
  58. if (ARFrame.SessionStatus < EZVIOState.EZVIOCameraState_Detecting) return;
  59. //NativeTracking.UpdateOnGLThread();
  60. NativeTracking.UpdateOnGlThread_WithImgUndistorted();
  61. IntPtr texturePtr = (IntPtr)NativeTracking.GetTextureId();
  62. if (texturePtr == IntPtr.Zero) return;
  63. if (!m_ConfigCamera)
  64. {
  65. ConfigARCamera();
  66. }
  67. if (m_VideoTexture == null
  68. || m_VideoTexture.GetNativeTexturePtr().ToInt32() != texturePtr.ToInt32())
  69. {
  70. // Debug.Log("UNITY LOG haaaaaaaa");
  71. //m_VideoTexture = Texture2D.CreateExternalTexture(screenWidth, screenHeight,
  72. // TextureFormat.RGBA32, false, false, texturePtr);
  73. m_VideoTexture = Texture2D.CreateExternalTexture((int)imageWidth, (int)imageHeight,
  74. TextureFormat.RGBA32, false, false, texturePtr);
  75. m_VideoTexture.filterMode = FilterMode.Bilinear;
  76. m_VideoTexture.wrapMode = TextureWrapMode.Repeat;
  77. m_BackgroundMaterial.SetTexture("_MainTex", m_VideoTexture);
  78. }
  79. m_VideoTexture.UpdateExternalTexture(texturePtr);
  80. //bytes = m_VideoTexture.EncodeToPNG();
  81. //File.WriteAllBytes(Application.persistentDataPath + "/image" + cnt++ + ".png", bytes);
  82. int isPortrait = 0;
  83. float rotation = 0;
  84. if (Screen.orientation == ScreenOrientation.Portrait)
  85. {
  86. rotation = -90;
  87. isPortrait = 1;
  88. }
  89. else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
  90. {
  91. rotation = 90;
  92. isPortrait = 1;
  93. }
  94. else if (Screen.orientation == ScreenOrientation.LandscapeRight || Screen.orientation == ScreenOrientation.LandscapeLeft)
  95. {
  96. isPortrait = 0;
  97. }
  98. Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, 0.0f, rotation), Vector3.one);
  99. m_BackgroundMaterial.SetMatrix("_TextureRotation", m);
  100. float imageAspect = (float)imageWidth / (float)imageHeight;
  101. float screenAspect = (float)screenWidth / (float)screenHeight;
  102. float ratio = screenAspect > 1 ? imageAspect / screenAspect : imageAspect * screenAspect;
  103. float s_ShaderScaleX = 1.0f;
  104. float s_ShaderScaleY = 1.0f;
  105. if (isPortrait == 1)
  106. {
  107. if (ratio < 1)
  108. {
  109. s_ShaderScaleX = ratio;
  110. }
  111. else if (ratio > 1)
  112. {
  113. s_ShaderScaleY = 1f / ratio;
  114. }
  115. }
  116. else if (isPortrait == 0)
  117. {
  118. if (ratio < 1f)
  119. {
  120. s_ShaderScaleY = ratio;
  121. }
  122. else if (ratio > 1f)
  123. {
  124. s_ShaderScaleX = 1f / ratio;
  125. }
  126. }
  127. m_BackgroundMaterial.SetFloat("_texCoordScaleX", s_ShaderScaleX);
  128. m_BackgroundMaterial.SetFloat("_texCoordScaleY", s_ShaderScaleY);
  129. m_BackgroundMaterial.SetInt("_isPortrait", isPortrait);
  130. //float DevelopRate = imageHeight / imageWidth;
  131. //float ScreenRate = (float)screenHeight / (float)screenWidth;
  132. //float cameraRectHeightRate = imageHeight / ((imageWidth / screenWidth) * screenHeight);
  133. //float cameraRectWidthRate = imageWidth / ((imageHeight / screenHeight) * screenWidth);
  134. //Debug.Log("=========UNITY LOG : " + screenWidth + " " + screenHeight + " " + imageWidth + " " + imageHeight + " " + DevelopRate + " " + ScreenRate + " " + cameraRectWidthRate + " " + cameraRectHeightRate);
  135. //if (DevelopRate <= ScreenRate)
  136. //{
  137. // m_Camera.rect = new Rect(0, (1 - cameraRectHeightRate) / 2, 1, cameraRectHeightRate);
  138. //}
  139. //else
  140. //{
  141. // m_Camera.rect = new Rect(0, 0, cameraRectWidthRate, 1);
  142. //}
  143. }
  144. private void ConfigARCamera()
  145. {
  146. m_VideoCommandBuffer = new CommandBuffer();
  147. m_BackgroundMaterial = new Material(Shader.Find("ARBackground"));
  148. m_VideoCommandBuffer.Blit(null, BuiltinRenderTextureType.CurrentActive, m_BackgroundMaterial);
  149. m_Camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, m_VideoCommandBuffer);
  150. m_ConfigCamera = true;
  151. }
  152. #endregion
  153. }
  154. }