CanvasExtensions.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #if UNITY_2021_3_0 || UNITY_2021_3_1 || UNITY_2021_3_2 || UNITY_2021_3_3 || UNITY_2021_3_4 || UNITY_2021_3_5 || UNITY_2021_3_6 || UNITY_2021_3_7 || UNITY_2021_3_8 || UNITY_2021_3_9
  2. #elif UNITY_2021_3_10 || UNITY_2021_3_11 || UNITY_2021_3_12 || UNITY_2021_3_13 || UNITY_2021_3_14 || UNITY_2021_3_15 || UNITY_2021_3_16 || UNITY_2021_3_17 || UNITY_2021_3_18 || UNITY_2021_3_19
  3. #elif UNITY_2021_3_20 || UNITY_2021_3_21 || UNITY_2021_3_22 || UNITY_2021_3_23 || UNITY_2021_3_24 || UNITY_2021_3_25 || UNITY_2021_3_26 || UNITY_2021_3_27 || UNITY_2021_3_28 || UNITY_2021_3_29
  4. #elif UNITY_2021_3_30 || UNITY_2021_3_31 || UNITY_2021_3_32 || UNITY_2021_3_33
  5. #elif UNITY_2022_2_0 || UNITY_2022_2_1 || UNITY_2022_2_2 || UNITY_2022_2_3 || UNITY_2022_2_4 || UNITY_2022_2_5 || UNITY_2022_2_6 || UNITY_2022_2_7 || UNITY_2022_2_8 || UNITY_2022_2_9
  6. #elif UNITY_2022_2_10 || UNITY_2022_2_11 || UNITY_2022_2_12 || UNITY_2022_2_13 || UNITY_2022_2_14
  7. #elif UNITY_2021_3 || UNITY_2022_2 || UNITY_2022_3 || UNITY_2023_2_OR_NEWER
  8. #define CANVAS_SUPPORT_ALWAYS_GAMMA
  9. #endif
  10. using UnityEngine;
  11. using UnityEngine.Profiling;
  12. #if UNITY_MODULE_VR
  13. using UnityEngine.XR;
  14. #endif
  15. namespace Coffee.UIParticleInternal
  16. {
  17. internal static class CanvasExtensions
  18. {
  19. public static bool ShouldGammaToLinearInShader(this Canvas canvas)
  20. {
  21. return QualitySettings.activeColorSpace == ColorSpace.Linear &&
  22. #if CANVAS_SUPPORT_ALWAYS_GAMMA
  23. canvas.vertexColorAlwaysGammaSpace;
  24. #else
  25. false;
  26. #endif
  27. }
  28. public static bool ShouldGammaToLinearInMesh(this Canvas canvas)
  29. {
  30. return QualitySettings.activeColorSpace == ColorSpace.Linear &&
  31. #if CANVAS_SUPPORT_ALWAYS_GAMMA
  32. !canvas.vertexColorAlwaysGammaSpace;
  33. #else
  34. true;
  35. #endif
  36. }
  37. public static bool IsStereoCanvas(this Canvas canvas)
  38. {
  39. #if UNITY_MODULE_VR
  40. if (FrameCache.TryGet<bool>(canvas, nameof(IsStereoCanvas), out var stereo)) return stereo;
  41. stereo =
  42. canvas != null && canvas.renderMode != RenderMode.ScreenSpaceOverlay && canvas.worldCamera != null
  43. && XRSettings.enabled && !string.IsNullOrEmpty(XRSettings.loadedDeviceName);
  44. FrameCache.Set(canvas, nameof(IsStereoCanvas), stereo);
  45. return stereo;
  46. #else
  47. return false;
  48. #endif
  49. }
  50. /// <summary>
  51. /// Gets the view-projection matrix for a Canvas.
  52. /// </summary>
  53. public static void GetViewProjectionMatrix(this Canvas canvas, out Matrix4x4 vpMatrix)
  54. {
  55. canvas.GetViewProjectionMatrix(Camera.MonoOrStereoscopicEye.Mono, out vpMatrix);
  56. }
  57. /// <summary>
  58. /// Gets the view-projection matrix for a Canvas.
  59. /// </summary>
  60. public static void GetViewProjectionMatrix(this Canvas canvas, Camera.MonoOrStereoscopicEye eye,
  61. out Matrix4x4 vpMatrix)
  62. {
  63. if (FrameCache.TryGet(canvas, nameof(GetViewProjectionMatrix), out vpMatrix)) return;
  64. canvas.GetViewProjectionMatrix(eye, out var viewMatrix, out var projectionMatrix);
  65. vpMatrix = viewMatrix * projectionMatrix;
  66. FrameCache.Set(canvas, nameof(GetViewProjectionMatrix), vpMatrix);
  67. }
  68. /// <summary>
  69. /// Gets the view and projection matrices for a Canvas.
  70. /// </summary>
  71. public static void GetViewProjectionMatrix(this Canvas canvas, out Matrix4x4 vMatrix, out Matrix4x4 pMatrix)
  72. {
  73. canvas.GetViewProjectionMatrix(Camera.MonoOrStereoscopicEye.Mono, out vMatrix, out pMatrix);
  74. }
  75. /// <summary>
  76. /// Gets the view and projection matrices for a Canvas.
  77. /// </summary>
  78. public static void GetViewProjectionMatrix(this Canvas canvas, Camera.MonoOrStereoscopicEye eye,
  79. out Matrix4x4 vMatrix, out Matrix4x4 pMatrix)
  80. {
  81. if (FrameCache.TryGet(canvas, "GetViewMatrix", (int)eye, out vMatrix) &&
  82. FrameCache.TryGet(canvas, "GetProjectionMatrix", (int)eye, out pMatrix))
  83. {
  84. return;
  85. }
  86. // Get view and projection matrices.
  87. Profiler.BeginSample("(COF)[CanvasExt] GetViewProjectionMatrix");
  88. var rootCanvas = canvas.rootCanvas;
  89. var cam = rootCanvas.worldCamera;
  90. if (rootCanvas && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay && cam)
  91. {
  92. if (eye == Camera.MonoOrStereoscopicEye.Mono)
  93. {
  94. vMatrix = cam.worldToCameraMatrix;
  95. pMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false);
  96. }
  97. else
  98. {
  99. pMatrix = cam.GetStereoProjectionMatrix((Camera.StereoscopicEye)eye);
  100. vMatrix = cam.GetStereoViewMatrix((Camera.StereoscopicEye)eye);
  101. pMatrix = GL.GetGPUProjectionMatrix(pMatrix, false);
  102. }
  103. }
  104. else
  105. {
  106. var pos = rootCanvas.transform.position;
  107. vMatrix = Matrix4x4.TRS(
  108. new Vector3(-pos.x, -pos.y, -1000),
  109. Quaternion.identity,
  110. new Vector3(1, 1, -1f));
  111. pMatrix = Matrix4x4.TRS(
  112. new Vector3(0, 0, -1),
  113. Quaternion.identity,
  114. new Vector3(1 / pos.x, 1 / pos.y, -2 / 10000f));
  115. }
  116. FrameCache.Set(canvas, "GetViewMatrix", (int)eye, vMatrix);
  117. FrameCache.Set(canvas, "GetProjectionMatrix", (int)eye, pMatrix);
  118. Profiler.EndSample();
  119. }
  120. }
  121. }