Advertisement
noradninja

Gamma adjust

Nov 19th, 2022
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityStandardAssets.ImageEffects;
  4.  
  5. [ExecuteInEditMode]
  6. [AddComponentMenu("Image Effects/Color Adjustments/Gamma")]
  7. public class Gamma : PostEffectsBase {
  8.     public enum ColorCorrectionMode {
  9.         Simple = 0,
  10.         Advanced = 1
  11.     }
  12.  
  13.     [HideInInspector]
  14.     public AnimationCurve channels = AnimationCurve.Linear(0f, 0f, 1f, 1f);
  15.  
  16.     public bool useDepthCorrection = true;
  17.  
  18.     private Material ccMaterial;
  19.     private Material ccDepthMaterial;
  20.     private Material selectiveCcMaterial;
  21.  
  22.     private Texture2D rgbChannelTex;
  23.     private Texture2D rgbDepthChannelTex;
  24.     private Texture2D zCurveTex;
  25.  
  26.     public float gamma = 1.0f;
  27.  
  28.     [HideInInspector]
  29.     public ColorCorrectionMode mode;
  30.     [HideInInspector]
  31.     public bool updateTextures = true;
  32.  
  33.     public Shader colorCorrectionCurvesShader = null;
  34.     public Shader simpleColorCorrectionCurvesShader = null;
  35.  
  36.     private bool updateTexturesOnStartup = true;
  37.    
  38.     float oldGamma;
  39.  
  40.  
  41.     new void Start() {
  42.         base.Start();
  43.         updateTexturesOnStartup = true;
  44.         oldGamma = -1f;
  45.     }
  46.  
  47.     void Awake() { }
  48.  
  49.  
  50.     public override bool CheckResources() {
  51.         CheckSupport(mode == ColorCorrectionMode.Advanced);
  52.  
  53.         ccMaterial = CheckShaderAndCreateMaterial(simpleColorCorrectionCurvesShader, ccMaterial);
  54.         ccDepthMaterial = CheckShaderAndCreateMaterial(colorCorrectionCurvesShader, ccDepthMaterial);
  55.  
  56.         if(!rgbChannelTex)
  57.             rgbChannelTex = new Texture2D(256, 4, TextureFormat.ARGB32, false, true);
  58.         if(!rgbDepthChannelTex)
  59.             rgbDepthChannelTex = new Texture2D(256, 4, TextureFormat.ARGB32, false, true);
  60.         if(!zCurveTex)
  61.             zCurveTex = new Texture2D(256, 1, TextureFormat.ARGB32, false, true);
  62.  
  63.         rgbChannelTex.hideFlags = HideFlags.DontSave;
  64.         rgbDepthChannelTex.hideFlags = HideFlags.DontSave;
  65.         zCurveTex.hideFlags = HideFlags.DontSave;
  66.  
  67.         rgbChannelTex.wrapMode = TextureWrapMode.Clamp;
  68.         rgbDepthChannelTex.wrapMode = TextureWrapMode.Clamp;
  69.         zCurveTex.wrapMode = TextureWrapMode.Clamp;
  70.  
  71.         if(!isSupported)
  72.             ReportAutoDisable();
  73.         return isSupported;
  74.     }
  75.  
  76.     public void UpdateParameters() {
  77.         CheckResources(); // textures might not be created if we're tweaking UI while disabled
  78.  
  79.         if(channels != null) {
  80.             for(float i = 0.0f;i <= 1.0f;i += 1.0f / 255.0f) {
  81.                 float ch = Mathf.Clamp(channels.Evaluate(i), 0.0f, 1.0f);
  82.  
  83.                 rgbChannelTex.SetPixel((int)Mathf.Floor(i * 255.0f), 0, new Color(ch, ch, ch) * gamma);
  84.                 rgbChannelTex.SetPixel((int)Mathf.Floor(i * 255.0f), 1, new Color(ch, ch, ch) * gamma);
  85.                 rgbChannelTex.SetPixel((int)Mathf.Floor(i * 255.0f), 2, new Color(ch, ch, ch) * gamma);
  86.  
  87.                 float zC = Mathf.Clamp(channels.Evaluate(i), 0.0f, 1.0f);
  88.  
  89.                 zCurveTex.SetPixel((int)Mathf.Floor(i * 255.0f), 0, new Color(zC, zC, zC) * gamma);
  90.  
  91.                 ch = Mathf.Clamp(channels.Evaluate(i), 0.0f, 1.0f);
  92.  
  93.                 rgbDepthChannelTex.SetPixel((int)Mathf.Floor(i * 255.0f), 0, new Color(ch, ch, ch) * gamma);
  94.                 rgbDepthChannelTex.SetPixel((int)Mathf.Floor(i * 255.0f), 1, new Color(ch, ch, ch) * gamma);
  95.                 rgbDepthChannelTex.SetPixel((int)Mathf.Floor(i * 255.0f), 2, new Color(ch, ch, ch) * gamma);
  96.             }
  97.  
  98.             rgbChannelTex.Apply();
  99.             rgbDepthChannelTex.Apply();
  100.             zCurveTex.Apply();
  101.         }
  102.     }
  103.    
  104.     void Update(){
  105.         if(oldGamma != gamma){
  106.             oldGamma = gamma;
  107.             UpdateParameters();
  108.         }
  109.     }
  110.  
  111.     void OnRenderImage(RenderTexture source, RenderTexture destination) {
  112.         if(CheckResources() == false) {
  113.             Graphics.Blit(source, destination);
  114.             return;
  115.         }
  116.  
  117.         if(updateTexturesOnStartup) {
  118.             UpdateParameters();
  119.             updateTexturesOnStartup = false;
  120.         }
  121.  
  122.         if(useDepthCorrection)
  123.             GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;
  124.  
  125.         RenderTexture renderTarget2Use = destination;
  126.  
  127.         if(useDepthCorrection) {
  128.             ccDepthMaterial.SetTexture("_RgbTex", rgbChannelTex);
  129.             ccDepthMaterial.SetTexture("_ZCurve", zCurveTex);
  130.             ccDepthMaterial.SetTexture("_RgbDepthTex", rgbDepthChannelTex);
  131.             ccDepthMaterial.SetFloat("_Saturation", 1f);
  132.  
  133.             Graphics.Blit(source, renderTarget2Use, ccDepthMaterial);
  134.         }
  135.         else {
  136.             ccMaterial.SetTexture("_RgbTex", rgbChannelTex);
  137.             ccMaterial.SetFloat("_Saturation", 1f);
  138.  
  139.             Graphics.Blit(source, renderTarget2Use, ccMaterial);
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement