Skip to content

Instantly share code, notes, and snippets.

@yCatDev
Forked from CptAsgard/Greyscale.cs
Created September 28, 2020 19:24
Show Gist options
  • Save yCatDev/a5f75e910ffd035df37e0b9dde51cb07 to your computer and use it in GitHub Desktop.
Save yCatDev/a5f75e910ffd035df37e0b9dde51cb07 to your computer and use it in GitHub Desktop.
Unity Greyscale image effect
using UnityEngine;
using System.Collections;
public class Greyscale : MonoBehaviour {
public Material mat;
void Start() {
mat.SetFloat( "_Power", 0.0f );
}
void OnRenderImage( RenderTexture source, RenderTexture destination ) {
Graphics.Blit( source, destination, mat );
}
}
Shader "Custom/Greyscale" {
Properties {
_MainTex ("", 2D) = "white" {}
_Power ("", Float) = 1.0
}
SubShader {
ZTest Always Cull Off ZWrite Off Fog { Mode Off } //Rendering settings
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
//we include "UnityCG.cginc" to use the appdata_img struct
struct v2f {
float4 pos : POSITION;
half2 uv : TEXCOORD0;
};
//Our Vertex Shader
v2f vert (appdata_img v){
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
return o;
}
sampler2D _MainTex; //Reference in Pass is necessary to let us use this variable in shaders
float _Power;
//Our Fragment Shader
fixed4 frag (v2f i) : COLOR{
fixed4 orgCol = tex2D(_MainTex, i.uv); //Get the orginal rendered color
//Make changes on the color
float avg = (orgCol.r + orgCol.g + orgCol.b)/3;
fixed4 col = lerp( orgCol, fixed4(avg, avg, avg, 1), _Power );
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment