源战役客户端
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.

243 lines
6.7 KiB

  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 (!SystemInfo.supportsImageEffects || !SystemInfo.supportsRenderTextures)
  82. {
  83. NotSupported ();
  84. return false;
  85. }
  86. if (needDepth && !SystemInfo.SupportsRenderTextureFormat (RenderTextureFormat.Depth))
  87. {
  88. NotSupported ();
  89. return false;
  90. }
  91. if (needDepth)
  92. GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;
  93. return true;
  94. }
  95. protected bool CheckSupport (bool needDepth, bool needHdr)
  96. {
  97. if (!CheckSupport(needDepth))
  98. return false;
  99. if (needHdr && !supportHDRTextures)
  100. {
  101. NotSupported ();
  102. return false;
  103. }
  104. return true;
  105. }
  106. public bool Dx11Support ()
  107. {
  108. return supportDX11;
  109. }
  110. protected void ReportAutoDisable ()
  111. {
  112. Debug.LogWarning ("The image effect " + ToString() + " has been disabled as it's not supported on the current platform.");
  113. }
  114. // deprecated but needed for old effects to survive upgrading
  115. bool CheckShader (Shader s)
  116. {
  117. 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.");
  118. if (!s.isSupported)
  119. {
  120. NotSupported ();
  121. return false;
  122. }
  123. else
  124. {
  125. return false;
  126. }
  127. }
  128. protected void NotSupported ()
  129. {
  130. enabled = false;
  131. isSupported = false;
  132. return;
  133. }
  134. protected void DrawBorder (RenderTexture dest, Material material)
  135. {
  136. float x1;
  137. float x2;
  138. float y1;
  139. float y2;
  140. RenderTexture.active = dest;
  141. bool invertY = true; // source.texelSize.y < 0.0ff;
  142. // Set up the simple Matrix
  143. GL.PushMatrix();
  144. GL.LoadOrtho();
  145. for (int i = 0; i < material.passCount; i++)
  146. {
  147. material.SetPass(i);
  148. float y1_; float y2_;
  149. if (invertY)
  150. {
  151. y1_ = 1.0f; y2_ = 0.0f;
  152. }
  153. else
  154. {
  155. y1_ = 0.0f; y2_ = 1.0f;
  156. }
  157. // left
  158. x1 = 0.0f;
  159. x2 = 0.0f + 1.0f/(dest.width*1.0f);
  160. y1 = 0.0f;
  161. y2 = 1.0f;
  162. GL.Begin(GL.QUADS);
  163. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  164. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  165. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  166. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  167. // right
  168. x1 = 1.0f - 1.0f/(dest.width*1.0f);
  169. x2 = 1.0f;
  170. y1 = 0.0f;
  171. y2 = 1.0f;
  172. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  173. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  174. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  175. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  176. // top
  177. x1 = 0.0f;
  178. x2 = 1.0f;
  179. y1 = 0.0f;
  180. y2 = 0.0f + 1.0f/(dest.height*1.0f);
  181. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  182. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  183. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  184. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  185. // bottom
  186. x1 = 0.0f;
  187. x2 = 1.0f;
  188. y1 = 1.0f - 1.0f/(dest.height*1.0f);
  189. y2 = 1.0f;
  190. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  191. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  192. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  193. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  194. GL.End();
  195. }
  196. GL.PopMatrix();
  197. }
  198. }
  199. }