原理:
移除场景中除了黑白灰以外所有的颜色,让整个图像灰度化(Grayscale)。很简单的实现方式是,取所有的颜色分量,将它们平均化,但是但人眼会对绿色更加敏感一些,而对蓝色不那么敏感,所以为了获取物理上更精确的效果,我们需要使用加权的(Weighted)通道:
脚本如下:
public Shader shader;//相应的shader
private Material mat = null;//相应的材料
public Material material
{
get
{
mat = CheckShaderAndCreateMaterial(shader, mat);
return mat;
}
}
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (material != null)
{
Graphics.Blit(src, dest, material);
}
else
{
Graphics.Blit(src, dest);
}
}
Shader实现如下:
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}//渲染纹理
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv:TEXCOORD0;
};
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 FragColor = tex2D(_MainTex,i.uv);
fixed average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b;
FragColor = fixed4(average,average,average,1.0);
return FragColor;
}
ENDCG
}
}
FallBack Off
效果如下:
来源:CSDN
作者:vvc223c
链接:https://blog.csdn.net/qq_44800780/article/details/103480395