How to create color balance shader?
Hi guys. I have been trying to implement my own color balance that i will apply on camera, For that i used this formula:
R = 255 / Rw * R1
G = 255 / Gw * G1
B = 255 / Bw * B1
Where Rw Gw and Bw represents the value of a colour component believed to represent White and R1 G1 and B1 representing the original value of a colour component before implementing the colour balance formula.
I tried to implement it like this:
public class ColorBalance : MonoBehaviour
{
[Range(0,255)]
public int R = 0;
[Range(0, 255)]
public int G = 0;
[Range(0, 255)]
public int B = 0;
[Range(0, 255)]
public int Alpha = 255;
public Material material;
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
material.SetVector("_RGB", new Vector4(255f/R, 255f/G ,255f/ B, 255f/Alpha));
Graphics.Blit(source, destination, material);
}
}
Where material had shader like this:
Shader "PostProcessing/ColorBalance"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_RGB("_RGB", Float) = (0.01,0.01,0.01, 1)
}
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
Tags {"Queue" = "Transparent" "RenderType" = "Transparent" }
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform float4 _RGB;
sampler2D _MainTex;
half4 _MainTex_ST;
fixed4 frag (v2f_img i) : Color
{
fixed4 col = (1.0/ _RGB) * tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST));
return col;
}
ENDCG
}
}
Fallback off
}
But the end result was not correct.
Original:
Red 255 from my method: So what am i doing wrong? Is it correct way of doing it? Please help.
Your problems are probably stem$$anonymous$$g from the colour divisions; in Unity colours range from 0-1, rather than 0-255. When you inverse the division in the shader, you end up multiplying by a number that could be as big as 255; meaning the colours would be blown way out of proportion. I might be wrong, but that's what stands out to me at first glance.
Your answer
Follow this Question
Related Questions
Post process layer doesnt scale to the camera size 1 Answer
Compatibility between stencil buffer and SSAO 0 Answers
Would it be possible to make a post process cell shader like BOTW in Unity? 0 Answers
missing shader. postprocess pass render pass will not execute Error [Solution] 0 Answers
2D glow post possessing effect. 1 Answer