GraphicsUtils.cs 2.9 KB

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