最终效果
Shader "Hidden/屏幕黑白"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_bwBlend("_bwBlend",Range(0,1))=0 //混合系数
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
//vert_img 使用系统的顶点着色器 把顶点空间坐标转到屏幕坐标
/*
v2f_img vert_img( appdata_img v )
{
v2f_img o;
//UNITY_MATRIX_MVP 把顶点从模型空间转换到裁剪空间用到的矩阵
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord;
return o;
}
*/
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
//uniform pass 内的全局变量
uniform sampler2D _MainTex;
uniform float _bwBlend;
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// just invert the colors
//col.rgb = 1 - col.rgb;
//得到颜色亮度值 灰色图片算法,通用
float lun=col.r*0.3+col.g*0.59+col.b*0.11;
//从颜色亮度值,构建一个新的颜色 , 注:r=g=b=灰色
fixed4 bw=fixed4(lun,lun,lun,1);
//插值运算
fixed4 result= lerp(col,bw,_bwBlend);
return result;
}
ENDCG
}
}
}
调用
```csharp
//挂在摄像机上
public class test : MonoBehaviour
{
[Range(0,1)]
public float BwBlend=1f;
private Material material;
// Start is called before the first frame update
private void Awake()
{
material = new Material(Shader.Find("Hidden/屏幕黑白"));
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
material.SetFloat("_bwBlend", BwBlend);
//Graphics.Blit 是一个屏幕处理函数,默认会用当前摄像机渲染的图形为 _MainTex赋值
//所以再这里我们只需要为_bwBlend 赋值
Graphics.Blit(source, destination, material);
}
}