Unity built-in Grayscal shader with smooth transition - help

Hi everyone,

I have been trying, so far with no success, to create a modified version of the Grayscale camera effect so that I have a range in order to have a smooth transition from the original - coloured - scene to a grayscale one.

Can anyone help?

Many thanks

Something like:

color = lerp(color, Luminance(color), _Grayness);

Here is the shader:

Shader "Hidden/GreyScale" {
Properties {
	_MainTex ("Base (RGB)", 2D) = "white" {}
}

SubShader {
	Pass {
		ZTest Always Cull Off ZWrite Off
		Fog { Mode off }
				
		CGPROGRAM
			#pragma vertex vert_img
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest 
			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			uniform float _Strength;

			float4 frag (v2f_img i) : COLOR {
				float4 c = tex2D(_MainTex, i.uv);
				float4 g = (c.r+c.g+c.b)/3.0;
				c.rgb = lerp(c, g, _Strength);
				return c;
			}
		ENDCG

	}
}

Fallback off

}

here is the c# for your camera

using UnityEngine;

[ExecuteInEditMode]
[AddComponentMenu("Image Effects/GreyScale")]
public class GreyScale : ImageEffectBase {
	
	public float _Strength = 1.0f;

	// Called by camera to apply image effect
	void OnRenderImage (RenderTexture source, RenderTexture destination) {
		Shader.SetGlobalFloat ("_Strength", _Strength);
		Graphics.Blit (source, destination, material);
	}
}

strength value should be between 0.0 and 1.0

eitherway.

float4 g = (c.r+c.g+c.b)/3.0;
float4 g = dot(c, 0.33333333f);
float4 g = dot( c, fixed3(0.22, 0.707, 0.071) );

Thank you so much for your valuable help. Just tried and works like a charm.

About the last remark on using Luminance(), why should it be used instead of the first shader code (not sure what perceptual weighting is…)?