|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 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 | + |
| 13 | + private List<Material> createdMaterials = new List<Material> (); |
| 14 | + |
| 15 | + protected Material CheckShaderAndCreateMaterial ( Shader s, Material m2Create) |
| 16 | + { |
| 17 | + if (!s) |
| 18 | + { |
| 19 | + Debug.Log("Missing shader in " + ToString ()); |
| 20 | + enabled = false; |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + if (s.isSupported && m2Create && m2Create.shader == s) |
| 25 | + return m2Create; |
| 26 | + |
| 27 | + if (!s.isSupported) |
| 28 | + { |
| 29 | + NotSupported (); |
| 30 | + Debug.Log("The shader " + s.ToString() + " on effect "+ToString()+" is not supported on this platform!"); |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + m2Create = new Material (s); |
| 35 | + createdMaterials.Add (m2Create); |
| 36 | + m2Create.hideFlags = HideFlags.DontSave; |
| 37 | + |
| 38 | + return m2Create; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + protected Material CreateMaterial (Shader s, Material m2Create) |
| 43 | + { |
| 44 | + if (!s) |
| 45 | + { |
| 46 | + Debug.Log ("Missing shader in " + ToString ()); |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + if (m2Create && (m2Create.shader == s) && (s.isSupported)) |
| 51 | + return m2Create; |
| 52 | + |
| 53 | + if (!s.isSupported) |
| 54 | + { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + m2Create = new Material (s); |
| 59 | + createdMaterials.Add (m2Create); |
| 60 | + m2Create.hideFlags = HideFlags.DontSave; |
| 61 | + |
| 62 | + return m2Create; |
| 63 | + } |
| 64 | + |
| 65 | + void OnEnable () |
| 66 | + { |
| 67 | + isSupported = true; |
| 68 | + } |
| 69 | + |
| 70 | + void OnDestroy () |
| 71 | + { |
| 72 | + RemoveCreatedMaterials (); |
| 73 | + } |
| 74 | + |
| 75 | + private void RemoveCreatedMaterials () |
| 76 | + { |
| 77 | + while (createdMaterials.Count > 0) |
| 78 | + { |
| 79 | + Material mat = createdMaterials[0]; |
| 80 | + createdMaterials.RemoveAt (0); |
| 81 | +#if UNITY_EDITOR |
| 82 | + DestroyImmediate (mat); |
| 83 | +#else |
| 84 | + Destroy(mat); |
| 85 | +#endif |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + protected bool CheckSupport () |
| 90 | + { |
| 91 | + return CheckSupport (false); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + public virtual bool CheckResources () |
| 96 | + { |
| 97 | + Debug.LogWarning ("CheckResources () for " + ToString() + " should be overwritten."); |
| 98 | + return isSupported; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + protected void Start () |
| 103 | + { |
| 104 | + CheckResources (); |
| 105 | + } |
| 106 | + |
| 107 | + protected bool CheckSupport (bool needDepth) |
| 108 | + { |
| 109 | + isSupported = true; |
| 110 | + supportHDRTextures = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf); |
| 111 | + supportDX11 = SystemInfo.graphicsShaderLevel >= 50 && SystemInfo.supportsComputeShaders; |
| 112 | + |
| 113 | +//With Unity 2019.2 and up, supportsImageEffects always returns true |
| 114 | +#if !UNITY_2019_2_OR_NEWER |
| 115 | + if (!SystemInfo.supportsImageEffects) |
| 116 | + { |
| 117 | + NotSupported (); |
| 118 | + return false; |
| 119 | + } |
| 120 | +#endif |
| 121 | + |
| 122 | + if (needDepth && !SystemInfo.SupportsRenderTextureFormat (RenderTextureFormat.Depth)) |
| 123 | + { |
| 124 | + NotSupported (); |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + if (needDepth) |
| 129 | + GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth; |
| 130 | + |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + protected bool CheckSupport (bool needDepth, bool needHdr) |
| 135 | + { |
| 136 | + if (!CheckSupport(needDepth)) |
| 137 | + return false; |
| 138 | + |
| 139 | + if (needHdr && !supportHDRTextures) |
| 140 | + { |
| 141 | + NotSupported (); |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + public bool Dx11Support () |
| 150 | + { |
| 151 | + return supportDX11; |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + protected void ReportAutoDisable () |
| 156 | + { |
| 157 | + Debug.LogWarning ("The image effect " + ToString() + " has been disabled as it's not supported on the current platform."); |
| 158 | + } |
| 159 | + |
| 160 | + // deprecated but needed for old effects to survive upgrading |
| 161 | + bool CheckShader (Shader s) |
| 162 | + { |
| 163 | + 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."); |
| 164 | + if (!s.isSupported) |
| 165 | + { |
| 166 | + NotSupported (); |
| 167 | + return false; |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + return false; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + protected void NotSupported () |
| 177 | + { |
| 178 | + enabled = false; |
| 179 | + isSupported = false; |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + protected void DrawBorder (RenderTexture dest, Material material) |
| 185 | + { |
| 186 | + float x1; |
| 187 | + float x2; |
| 188 | + float y1; |
| 189 | + float y2; |
| 190 | + |
| 191 | + RenderTexture.active = dest; |
| 192 | + bool invertY = true; // source.texelSize.y < 0.0ff; |
| 193 | + // Set up the simple Matrix |
| 194 | + GL.PushMatrix(); |
| 195 | + GL.LoadOrtho(); |
| 196 | + |
| 197 | + for (int i = 0; i < material.passCount; i++) |
| 198 | + { |
| 199 | + material.SetPass(i); |
| 200 | + |
| 201 | + float y1_; float y2_; |
| 202 | + if (invertY) |
| 203 | + { |
| 204 | + y1_ = 1.0f; y2_ = 0.0f; |
| 205 | + } |
| 206 | + else |
| 207 | + { |
| 208 | + y1_ = 0.0f; y2_ = 1.0f; |
| 209 | + } |
| 210 | + |
| 211 | + // left |
| 212 | + x1 = 0.0f; |
| 213 | + x2 = 0.0f + 1.0f/(dest.width*1.0f); |
| 214 | + y1 = 0.0f; |
| 215 | + y2 = 1.0f; |
| 216 | + GL.Begin(GL.QUADS); |
| 217 | + |
| 218 | + GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f); |
| 219 | + GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f); |
| 220 | + GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f); |
| 221 | + GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f); |
| 222 | + |
| 223 | + // right |
| 224 | + x1 = 1.0f - 1.0f/(dest.width*1.0f); |
| 225 | + x2 = 1.0f; |
| 226 | + y1 = 0.0f; |
| 227 | + y2 = 1.0f; |
| 228 | + |
| 229 | + GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f); |
| 230 | + GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f); |
| 231 | + GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f); |
| 232 | + GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f); |
| 233 | + |
| 234 | + // top |
| 235 | + x1 = 0.0f; |
| 236 | + x2 = 1.0f; |
| 237 | + y1 = 0.0f; |
| 238 | + y2 = 0.0f + 1.0f/(dest.height*1.0f); |
| 239 | + |
| 240 | + GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f); |
| 241 | + GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f); |
| 242 | + GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f); |
| 243 | + GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f); |
| 244 | + |
| 245 | + // bottom |
| 246 | + x1 = 0.0f; |
| 247 | + x2 = 1.0f; |
| 248 | + y1 = 1.0f - 1.0f/(dest.height*1.0f); |
| 249 | + y2 = 1.0f; |
| 250 | + |
| 251 | + GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f); |
| 252 | + GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f); |
| 253 | + GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f); |
| 254 | + GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f); |
| 255 | + |
| 256 | + GL.End(); |
| 257 | + } |
| 258 | + |
| 259 | + GL.PopMatrix(); |
| 260 | + } |
| 261 | + } |
0 commit comments