Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Created October 13, 2015 02:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save radiatoryang/7876453e6e2f4e2e89ed to your computer and use it in GitHub Desktop.
Save radiatoryang/7876453e6e2f4e2e89ed to your computer and use it in GitHub Desktop.
taken from http://armedunity.com/topic/4950-brightnesscontrastsaturation-shader/ by "willywill" but archived here
Shader "Custom/Gamma Image Effect" {
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_SaturationAmount ("Saturation Amount", Range(0.0, 1.0)) = 1.0
_BrightnessAmount ("Brightness Amount", Range(0.0, 1.0)) = 1.0
_ContrastAmount ("Contrast Amount", Range(0.0,1.0)) = 1.0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _SaturationAmount;
uniform float _BrightnessAmount;
uniform float _ContrastAmount;
float3 ContrastSaturationBrightness( float3 color, float brt, float sat, float con)
{
//RGB Color Channels
float AvgLumR = 0.5;
float AvgLumG = 0.5;
float AvgLumB = 0.5;
//Luminace Coefficients for brightness of image
float3 LuminaceCoeff = float3(0.2125,0.7154,0.0721);
//Brigntess calculations
float3 AvgLumin = float3(AvgLumR,AvgLumG,AvgLumB);
float3 brtColor = color * brt;
float intensityf = dot(brtColor, LuminaceCoeff);
float3 intensity = float3(intensityf, intensityf, intensityf);
//Saturation calculation
float3 satColor = lerp(intensity, brtColor, sat);
//Contrast calculations
float3 conColor = lerp(AvgLumin, satColor, con);
return conColor;
}
float4 frag (v2f_img i) : COLOR
{
float4 renderTex = tex2D(_MainTex, i.uv);
renderTex.rgb = ContrastSaturationBrightness(renderTex.rgb, _BrightnessAmount, _SaturationAmount, _ContrastAmount);
return renderTex;
}
ENDCG
}
}
}
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class GammaControl : MonoBehaviour {
#region Variables
public Shader curShader;
public float brightnessAmount = 1.0f;
public float saturationAmount = 1.0f;
public float contrastAmount = 1.0f;
private Material curMaterial;
#endregion
#region Properties
Material material
{
get
{
if(curMaterial == null)
{
curMaterial = new Material(curShader);
curMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return curMaterial;
}
}
#endregion
// Use this for initialization
void Start ()
{
if(!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
}
void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
{
if(curShader != null)
{
material.SetFloat("_BrightnessAmount", brightnessAmount);
material.SetFloat("_SaturationAmount", saturationAmount);
material.SetFloat("_ContrastAmount", contrastAmount);
Graphics.Blit(sourceTexture, destTexture, material);
}
else
{
Graphics.Blit(sourceTexture, destTexture);
}
}
// Update is called once per frame
void Update ()
{
brightnessAmount = Mathf.Clamp(brightnessAmount, 0.0f, 1.5f);
saturationAmount = Mathf.Clamp(saturationAmount, 0.0f, 2.0f);
contrastAmount = Mathf.Clamp(contrastAmount, 0.0f, 3.0f);
}
void OnDisable ()
{
if(curMaterial)
{
DestroyImmediate(curMaterial);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment