|
| 1 | +using System; |
| 2 | +using UnityEngine; |
| 3 | + |
| 4 | +// Usage: Place this file into /Standard Assets/Effects/ImageEffects/Scripts/-folder |
| 5 | +// Requires: https://github.com/UnityCommunity/UnityLibrary/blob/master/Scripts/Editor/ImageEffects/ColorCorrectionCurvesEditorLayers |
| 6 | + |
| 7 | +namespace UnityStandardAssets.ImageEffects |
| 8 | +{ |
| 9 | + [ExecuteInEditMode] |
| 10 | + [AddComponentMenu ("Image Effects/Color Adjustments/Color Correction (Curves, Saturation) Layers")] |
| 11 | + public class ColorCorrectionCurvesLayers : PostEffectsBase |
| 12 | + { |
| 13 | + public enum ColorCorrectionMode |
| 14 | + { |
| 15 | + Simple = 0, |
| 16 | + Advanced = 1 |
| 17 | + } |
| 18 | + |
| 19 | + public AnimationCurve redChannel = new AnimationCurve(new Keyframe(0f,0f), new Keyframe(1f,1f)); |
| 20 | + public AnimationCurve greenChannel = new AnimationCurve(new Keyframe(0f,0f), new Keyframe(1f,1f)); |
| 21 | + public AnimationCurve blueChannel = new AnimationCurve(new Keyframe(0f,0f), new Keyframe(1f,1f)); |
| 22 | + |
| 23 | + public bool useDepthCorrection = false; |
| 24 | + |
| 25 | + public AnimationCurve zCurve = new AnimationCurve(new Keyframe(0f,0f), new Keyframe(1f,1f)); |
| 26 | + public AnimationCurve depthRedChannel = new AnimationCurve(new Keyframe(0f,0f), new Keyframe(1f,1f)); |
| 27 | + public AnimationCurve depthGreenChannel = new AnimationCurve(new Keyframe(0f,0f), new Keyframe(1f,1f)); |
| 28 | + public AnimationCurve depthBlueChannel = new AnimationCurve(new Keyframe(0f,0f), new Keyframe(1f,1f)); |
| 29 | + |
| 30 | + private Material ccMaterial; |
| 31 | + private Material ccDepthMaterial; |
| 32 | + private Material selectiveCcMaterial; |
| 33 | + |
| 34 | + private Texture2D rgbChannelTex; |
| 35 | + private Texture2D rgbDepthChannelTex; |
| 36 | + private Texture2D zCurveTex; |
| 37 | + |
| 38 | + public float saturation = 1.0f; |
| 39 | + |
| 40 | + public bool selectiveCc = false; |
| 41 | + |
| 42 | + public Color selectiveFromColor = Color.white; |
| 43 | + public Color selectiveToColor = Color.white; |
| 44 | + |
| 45 | + public ColorCorrectionMode mode; |
| 46 | + |
| 47 | + public bool updateTextures = true; |
| 48 | + |
| 49 | + public Shader colorCorrectionCurvesShader = null; |
| 50 | + public Shader simpleColorCorrectionCurvesShader = null; |
| 51 | + public Shader colorCorrectionSelectiveShader = null; |
| 52 | + |
| 53 | + private bool updateTexturesOnStartup = true; |
| 54 | + |
| 55 | + public LayerMask excludeLayers = 0; |
| 56 | + |
| 57 | + private GameObject tmpCam = null; |
| 58 | + private Camera _camera; |
| 59 | + |
| 60 | + |
| 61 | + new void Start () |
| 62 | + { |
| 63 | + base.Start (); |
| 64 | + updateTexturesOnStartup = true; |
| 65 | + } |
| 66 | + |
| 67 | + void Awake () { } |
| 68 | + |
| 69 | + |
| 70 | + public override bool CheckResources () |
| 71 | + { |
| 72 | + CheckSupport (mode == ColorCorrectionMode.Advanced); |
| 73 | + |
| 74 | + ccMaterial = CheckShaderAndCreateMaterial (simpleColorCorrectionCurvesShader, ccMaterial); |
| 75 | + ccDepthMaterial = CheckShaderAndCreateMaterial (colorCorrectionCurvesShader, ccDepthMaterial); |
| 76 | + selectiveCcMaterial = CheckShaderAndCreateMaterial (colorCorrectionSelectiveShader, selectiveCcMaterial); |
| 77 | + |
| 78 | + if (!rgbChannelTex) |
| 79 | + rgbChannelTex = new Texture2D (256, 4, TextureFormat.ARGB32, false, true); |
| 80 | + if (!rgbDepthChannelTex) |
| 81 | + rgbDepthChannelTex = new Texture2D (256, 4, TextureFormat.ARGB32, false, true); |
| 82 | + if (!zCurveTex) |
| 83 | + zCurveTex = new Texture2D (256, 1, TextureFormat.ARGB32, false, true); |
| 84 | + |
| 85 | + rgbChannelTex.hideFlags = HideFlags.DontSave; |
| 86 | + rgbDepthChannelTex.hideFlags = HideFlags.DontSave; |
| 87 | + zCurveTex.hideFlags = HideFlags.DontSave; |
| 88 | + |
| 89 | + rgbChannelTex.wrapMode = TextureWrapMode.Clamp; |
| 90 | + rgbDepthChannelTex.wrapMode = TextureWrapMode.Clamp; |
| 91 | + zCurveTex.wrapMode = TextureWrapMode.Clamp; |
| 92 | + |
| 93 | + if (!isSupported) |
| 94 | + ReportAutoDisable (); |
| 95 | + return isSupported; |
| 96 | + } |
| 97 | + |
| 98 | + public void UpdateParameters () |
| 99 | + { |
| 100 | + CheckResources(); // textures might not be created if we're tweaking UI while disabled |
| 101 | + |
| 102 | + if (redChannel != null && greenChannel != null && blueChannel != null) |
| 103 | + { |
| 104 | + for (float i = 0.0f; i <= 1.0f; i += 1.0f / 255.0f) |
| 105 | + { |
| 106 | + float rCh = Mathf.Clamp (redChannel.Evaluate(i), 0.0f, 1.0f); |
| 107 | + float gCh = Mathf.Clamp (greenChannel.Evaluate(i), 0.0f, 1.0f); |
| 108 | + float bCh = Mathf.Clamp (blueChannel.Evaluate(i), 0.0f, 1.0f); |
| 109 | + |
| 110 | + rgbChannelTex.SetPixel ((int) Mathf.Floor(i*255.0f), 0, new Color(rCh,rCh,rCh) ); |
| 111 | + rgbChannelTex.SetPixel ((int) Mathf.Floor(i*255.0f), 1, new Color(gCh,gCh,gCh) ); |
| 112 | + rgbChannelTex.SetPixel ((int) Mathf.Floor(i*255.0f), 2, new Color(bCh,bCh,bCh) ); |
| 113 | + |
| 114 | + float zC = Mathf.Clamp (zCurve.Evaluate(i), 0.0f,1.0f); |
| 115 | + |
| 116 | + zCurveTex.SetPixel ((int) Mathf.Floor(i*255.0f), 0, new Color(zC,zC,zC) ); |
| 117 | + |
| 118 | + rCh = Mathf.Clamp (depthRedChannel.Evaluate(i), 0.0f,1.0f); |
| 119 | + gCh = Mathf.Clamp (depthGreenChannel.Evaluate(i), 0.0f,1.0f); |
| 120 | + bCh = Mathf.Clamp (depthBlueChannel.Evaluate(i), 0.0f,1.0f); |
| 121 | + |
| 122 | + rgbDepthChannelTex.SetPixel ((int) Mathf.Floor(i*255.0f), 0, new Color(rCh,rCh,rCh) ); |
| 123 | + rgbDepthChannelTex.SetPixel ((int) Mathf.Floor(i*255.0f), 1, new Color(gCh,gCh,gCh) ); |
| 124 | + rgbDepthChannelTex.SetPixel ((int) Mathf.Floor(i*255.0f), 2, new Color(bCh,bCh,bCh) ); |
| 125 | + } |
| 126 | + |
| 127 | + rgbChannelTex.Apply (); |
| 128 | + rgbDepthChannelTex.Apply (); |
| 129 | + zCurveTex.Apply (); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + void UpdateTextures () |
| 134 | + { |
| 135 | + UpdateParameters (); |
| 136 | + } |
| 137 | + |
| 138 | + void OnRenderImage (RenderTexture source, RenderTexture destination) |
| 139 | + { |
| 140 | + if (CheckResources()==false) |
| 141 | + { |
| 142 | + Graphics.Blit (source, destination); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + if (updateTexturesOnStartup) |
| 147 | + { |
| 148 | + UpdateParameters (); |
| 149 | + updateTexturesOnStartup = false; |
| 150 | + } |
| 151 | + |
| 152 | + if (useDepthCorrection) |
| 153 | + GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth; |
| 154 | + |
| 155 | + RenderTexture renderTarget2Use = destination; |
| 156 | + |
| 157 | + if (selectiveCc) |
| 158 | + { |
| 159 | + renderTarget2Use = RenderTexture.GetTemporary (source.width, source.height); |
| 160 | + } |
| 161 | + |
| 162 | + if (useDepthCorrection) |
| 163 | + { |
| 164 | + ccDepthMaterial.SetTexture ("_RgbTex", rgbChannelTex); |
| 165 | + ccDepthMaterial.SetTexture ("_ZCurve", zCurveTex); |
| 166 | + ccDepthMaterial.SetTexture ("_RgbDepthTex", rgbDepthChannelTex); |
| 167 | + ccDepthMaterial.SetFloat ("_Saturation", saturation); |
| 168 | + |
| 169 | + Graphics.Blit (source, renderTarget2Use, ccDepthMaterial); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + ccMaterial.SetTexture ("_RgbTex", rgbChannelTex); |
| 174 | + ccMaterial.SetFloat ("_Saturation", saturation); |
| 175 | + |
| 176 | + Graphics.Blit (source, renderTarget2Use, ccMaterial); |
| 177 | + } |
| 178 | + |
| 179 | + if (selectiveCc) |
| 180 | + { |
| 181 | + selectiveCcMaterial.SetColor ("selColor", selectiveFromColor); |
| 182 | + selectiveCcMaterial.SetColor ("targetColor", selectiveToColor); |
| 183 | + Graphics.Blit (renderTarget2Use, destination, selectiveCcMaterial); |
| 184 | + |
| 185 | + RenderTexture.ReleaseTemporary (renderTarget2Use); |
| 186 | + } |
| 187 | + |
| 188 | + // exclude layers |
| 189 | + Camera cam = null; |
| 190 | + if (excludeLayers.value != 0) cam = GetTmpCam(); |
| 191 | + |
| 192 | + if (cam && excludeLayers.value != 0) |
| 193 | + { |
| 194 | + cam.targetTexture = destination; |
| 195 | + cam.cullingMask = excludeLayers; |
| 196 | + cam.Render(); |
| 197 | + } |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + Camera GetTmpCam() |
| 202 | + { |
| 203 | + if (tmpCam == null) |
| 204 | + { |
| 205 | + if (_camera == null) _camera = GetComponent<Camera>(); |
| 206 | + |
| 207 | + string name = "_" + _camera.name + "_ColorCorrectionTmpCam"; |
| 208 | + GameObject go = GameObject.Find(name); |
| 209 | + |
| 210 | + if (null == go) // couldn't find, recreate |
| 211 | + { |
| 212 | + tmpCam = new GameObject(name, typeof(Camera)); |
| 213 | + } else |
| 214 | + { |
| 215 | + tmpCam = go; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + tmpCam.hideFlags = HideFlags.DontSave; |
| 220 | + tmpCam.transform.position = _camera.transform.position; |
| 221 | + tmpCam.transform.rotation = _camera.transform.rotation; |
| 222 | + tmpCam.transform.localScale = _camera.transform.localScale; |
| 223 | + tmpCam.GetComponent<Camera>().CopyFrom(_camera); |
| 224 | + |
| 225 | + tmpCam.GetComponent<Camera>().enabled = false; |
| 226 | + tmpCam.GetComponent<Camera>().depthTextureMode = DepthTextureMode.None; |
| 227 | + tmpCam.GetComponent<Camera>().clearFlags = CameraClearFlags.Nothing; |
| 228 | + |
| 229 | + return tmpCam.GetComponent<Camera>(); |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments