unity屏幕渐变黑白效果

最终效果
请添加图片描述

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);
    }
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值