I’m fiddling about with image effects in the hopes that I can make a brightness/gamma image effect. The grayscale image effect seemed like the simplest image effect so I duplicated and renamed it. But when I apply my new image effect to the camera it is automatically assigned the wrong shader. I have no idea where Unity gets this connection from. Normally you have to manually assign public variables. How and why does Unity automatically assign this one?
I’ve tried reimporting all my assets and renaming the scripts to try and break the connection, but it makes no difference. The only thing that works is to rename the variable from “shader” to something else. Then when I add the script to my camera the shader slot is empty. But why does it automatically assign it if it’s called “shader” and why does it automatically assign the wrong (old) shader in the slot? I see no references between these two.
I even looked in UnityCG.cginc to see if there was anything there, but I found nothing. I don’t get it. ![]()
Here’s my script. It’s just a clone of the grayscale script, but I’ve tried to remove everything I thought could cause this behavior. Things like using statements, namespaces etc.
using System;
using UnityEngine;
public class Gamma : MonoBehaviour {
public Shader shader;
Material m_Material;
Material material {
get {
if (m_Material == null) {
m_Material = new Material(shader);
m_Material.hideFlags = HideFlags.HideAndDontSave;
}
return m_Material;
}
}
void OnRenderImage(RenderTexture source, RenderTexture destination) {
Graphics.Blit(source, destination, material);
}
}
And here’s my shader. It’s just a straight clone of the grayscale shader, renamed.
Shader "Hidden/Gamma Effect" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_RampTex ("Base (RGB)", 2D) = "grayscaleRamp" {}
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _RampTex;
uniform half _RampOffset;
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 original = tex2D(_MainTex, i.uv);
fixed grayscale = Luminance(original.rgb);
half2 remap = half2 (grayscale + _RampOffset, .5);
fixed4 output = tex2D(_RampTex, remap);
output.a = original.a;
return output;
}
ENDCG
}
}
Fallback off
}