PostEffectsBase.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.ImageEffects
  4. {
  5. [ExecuteInEditMode]
  6. [RequireComponent (typeof(Camera))]
  7. public class PostEffectsBase : MonoBehaviour
  8. {
  9. protected bool supportHDRTextures = true;
  10. protected bool supportDX11 = false;
  11. protected bool isSupported = true;
  12. protected Material CheckShaderAndCreateMaterial ( Shader s, Material m2Create)
  13. {
  14. if (!s)
  15. {
  16. Debug.Log("Missing shader in " + ToString ());
  17. enabled = false;
  18. return null;
  19. }
  20. if (s.isSupported && m2Create && m2Create.shader == s)
  21. return m2Create;
  22. if (!s.isSupported)
  23. {
  24. NotSupported ();
  25. Debug.Log("The shader " + s.ToString() + " on effect "+ToString()+" is not supported on this platform!");
  26. return null;
  27. }
  28. else
  29. {
  30. m2Create = new Material (s);
  31. m2Create.hideFlags = HideFlags.DontSave;
  32. if (m2Create)
  33. return m2Create;
  34. else return null;
  35. }
  36. }
  37. protected Material CreateMaterial (Shader s, Material m2Create)
  38. {
  39. if (!s)
  40. {
  41. Debug.Log ("Missing shader in " + ToString ());
  42. return null;
  43. }
  44. if (m2Create && (m2Create.shader == s) && (s.isSupported))
  45. return m2Create;
  46. if (!s.isSupported)
  47. {
  48. return null;
  49. }
  50. else
  51. {
  52. m2Create = new Material (s);
  53. m2Create.hideFlags = HideFlags.DontSave;
  54. if (m2Create)
  55. return m2Create;
  56. else return null;
  57. }
  58. }
  59. void OnEnable ()
  60. {
  61. isSupported = true;
  62. }
  63. protected bool CheckSupport ()
  64. {
  65. return CheckSupport (false);
  66. }
  67. public virtual bool CheckResources ()
  68. {
  69. Debug.LogWarning ("CheckResources () for " + ToString() + " should be overwritten.");
  70. return isSupported;
  71. }
  72. protected void Start ()
  73. {
  74. CheckResources ();
  75. }
  76. protected bool CheckSupport (bool needDepth)
  77. {
  78. isSupported = true;
  79. supportHDRTextures = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf);
  80. supportDX11 = SystemInfo.graphicsShaderLevel >= 50 && SystemInfo.supportsComputeShaders;
  81. if (needDepth && !SystemInfo.SupportsRenderTextureFormat (RenderTextureFormat.Depth))
  82. {
  83. NotSupported ();
  84. return false;
  85. }
  86. if (needDepth)
  87. GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;
  88. return true;
  89. }
  90. protected bool CheckSupport (bool needDepth, bool needHdr)
  91. {
  92. if (!CheckSupport(needDepth))
  93. return false;
  94. if (needHdr && !supportHDRTextures)
  95. {
  96. NotSupported ();
  97. return false;
  98. }
  99. return true;
  100. }
  101. public bool Dx11Support ()
  102. {
  103. return supportDX11;
  104. }
  105. protected void ReportAutoDisable ()
  106. {
  107. Debug.LogWarning ("The image effect " + ToString() + " has been disabled as it's not supported on the current platform.");
  108. }
  109. // deprecated but needed for old effects to survive upgrading
  110. bool CheckShader (Shader s)
  111. {
  112. Debug.Log("The shader " + s.ToString () + " on effect "+ ToString () + " is not part of the Unity 3.2+ effects suite anymore. For best performance and quality, please ensure you are using the latest Standard Assets Image Effects (Pro only) package.");
  113. if (!s.isSupported)
  114. {
  115. NotSupported ();
  116. return false;
  117. }
  118. else
  119. {
  120. return false;
  121. }
  122. }
  123. protected void NotSupported ()
  124. {
  125. enabled = false;
  126. isSupported = false;
  127. return;
  128. }
  129. protected void DrawBorder (RenderTexture dest, Material material)
  130. {
  131. float x1;
  132. float x2;
  133. float y1;
  134. float y2;
  135. RenderTexture.active = dest;
  136. bool invertY = true; // source.texelSize.y < 0.0ff;
  137. // Set up the simple Matrix
  138. GL.PushMatrix();
  139. GL.LoadOrtho();
  140. for (int i = 0; i < material.passCount; i++)
  141. {
  142. material.SetPass(i);
  143. float y1_; float y2_;
  144. if (invertY)
  145. {
  146. y1_ = 1.0f; y2_ = 0.0f;
  147. }
  148. else
  149. {
  150. y1_ = 0.0f; y2_ = 1.0f;
  151. }
  152. // left
  153. x1 = 0.0f;
  154. x2 = 0.0f + 1.0f/(dest.width*1.0f);
  155. y1 = 0.0f;
  156. y2 = 1.0f;
  157. GL.Begin(GL.QUADS);
  158. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  159. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  160. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  161. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  162. // right
  163. x1 = 1.0f - 1.0f/(dest.width*1.0f);
  164. x2 = 1.0f;
  165. y1 = 0.0f;
  166. y2 = 1.0f;
  167. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  168. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  169. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  170. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  171. // top
  172. x1 = 0.0f;
  173. x2 = 1.0f;
  174. y1 = 0.0f;
  175. y2 = 0.0f + 1.0f/(dest.height*1.0f);
  176. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  177. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  178. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  179. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  180. // bottom
  181. x1 = 0.0f;
  182. x2 = 1.0f;
  183. y1 = 1.0f - 1.0f/(dest.height*1.0f);
  184. y2 = 1.0f;
  185. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  186. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  187. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  188. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  189. GL.End();
  190. }
  191. GL.PopMatrix();
  192. }
  193. }
  194. }