You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.0 KiB

  1. namespace UnityEngine.PostProcessing
  2. {
  3. using UnityObject = Object;
  4. public static class GraphicsUtils
  5. {
  6. public static bool isLinearColorSpace
  7. {
  8. get { return QualitySettings.activeColorSpace == ColorSpace.Linear; }
  9. }
  10. public static bool supportsDX11
  11. {
  12. #if UNITY_WEBGL
  13. get { return false; }
  14. #else
  15. get { return SystemInfo.graphicsShaderLevel >= 50 && SystemInfo.supportsComputeShaders; }
  16. #endif
  17. }
  18. static Texture2D s_WhiteTexture;
  19. public static Texture2D whiteTexture
  20. {
  21. get
  22. {
  23. if (s_WhiteTexture != null)
  24. return s_WhiteTexture;
  25. s_WhiteTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  26. s_WhiteTexture.SetPixel(0, 0, new Color(1f, 1f, 1f, 1f));
  27. s_WhiteTexture.Apply();
  28. return s_WhiteTexture;
  29. }
  30. }
  31. static Mesh s_Quad;
  32. public static Mesh quad
  33. {
  34. get
  35. {
  36. if (s_Quad != null)
  37. return s_Quad;
  38. var vertices = new[]
  39. {
  40. new Vector3(-1f, -1f, 0f),
  41. new Vector3( 1f, 1f, 0f),
  42. new Vector3( 1f, -1f, 0f),
  43. new Vector3(-1f, 1f, 0f)
  44. };
  45. var uvs = new[]
  46. {
  47. new Vector2(0f, 0f),
  48. new Vector2(1f, 1f),
  49. new Vector2(1f, 0f),
  50. new Vector2(0f, 1f)
  51. };
  52. var indices = new[] { 0, 1, 2, 1, 0, 3 };
  53. s_Quad = new Mesh
  54. {
  55. vertices = vertices,
  56. uv = uvs,
  57. triangles = indices
  58. };
  59. s_Quad.RecalculateNormals();
  60. s_Quad.RecalculateBounds();
  61. return s_Quad;
  62. }
  63. }
  64. // Useful when rendering to MRT
  65. public static void Blit(Material material, int pass)
  66. {
  67. GL.PushMatrix();
  68. {
  69. GL.LoadOrtho();
  70. material.SetPass(pass);
  71. GL.Begin(GL.TRIANGLE_STRIP);
  72. {
  73. GL.TexCoord2(0f, 0f); GL.Vertex3(0f, 0f, 0.1f);
  74. GL.TexCoord2(1f, 0f); GL.Vertex3(1f, 0f, 0.1f);
  75. GL.TexCoord2(0f, 1f); GL.Vertex3(0f, 1f, 0.1f);
  76. GL.TexCoord2(1f, 1f); GL.Vertex3(1f, 1f, 0.1f);
  77. }
  78. GL.End();
  79. }
  80. GL.PopMatrix();
  81. }
  82. public static void ClearAndBlit(Texture source, RenderTexture destination, Material material, int pass, bool clearColor = true, bool clearDepth = false)
  83. {
  84. var oldRT = RenderTexture.active;
  85. RenderTexture.active = destination;
  86. GL.Clear(false, clearColor, Color.clear);
  87. GL.PushMatrix();
  88. {
  89. GL.LoadOrtho();
  90. material.SetTexture("_MainTex", source);
  91. material.SetPass(pass);
  92. GL.Begin(GL.TRIANGLE_STRIP);
  93. {
  94. GL.TexCoord2(0f, 0f); GL.Vertex3(0f, 0f, 0.1f);
  95. GL.TexCoord2(1f, 0f); GL.Vertex3(1f, 0f, 0.1f);
  96. GL.TexCoord2(0f, 1f); GL.Vertex3(0f, 1f, 0.1f);
  97. GL.TexCoord2(1f, 1f); GL.Vertex3(1f, 1f, 0.1f);
  98. }
  99. GL.End();
  100. }
  101. GL.PopMatrix();
  102. RenderTexture.active = oldRT;
  103. }
  104. public static void Destroy(UnityObject obj)
  105. {
  106. if (obj != null)
  107. {
  108. #if UNITY_EDITOR
  109. if (Application.isPlaying)
  110. UnityObject.Destroy(obj);
  111. else
  112. UnityObject.DestroyImmediate(obj);
  113. #else
  114. UnityObject.Destroy(obj);
  115. #endif
  116. }
  117. }
  118. public static void Dispose()
  119. {
  120. Destroy(s_Quad);
  121. }
  122. }
  123. }