Unity ShaderLab特效教程 适用于sprite和ugui的2d着色器实例 代码+详解注释 【将sprite或ugui变为可调节的灰色图像】

本文详细介绍了如何使用Unity3D创建一个可调节亮度和透明度的灰度图Shader。通过具体代码示例,解释了Shader基础知识和坐标系统,并展示了如何实现灰度效果和透明度调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果代码中有什么不清楚请查看以下基础知识

Shader基础知识
unity3d 中 七种坐标知识详解
一个可调节亮度和透明度的灰度图shader

灰度可调,透明可调
原图
在这里插入图片描述
效果图
在这里插入图片描述

Shader "Custom/灰度"
{
    Properties
    {
        _MainTex ("贴图", 2D) = "black" {}
        _Color ("颜色", Color) = (0.33, 0.33, 0.33,1)
    }
    
    SubShader
    {
        LOD 200
        Tags
        {
            //透明队列
            "Queue" = "Transparent"
            //不被任何投影或贴图影响。一般应用在sprite和GUI
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }
        //第一个代码块
        Pass
        {
            //2d关闭
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            // Offset -1, -1
            //正常透明度混合
            Blend SrcAlpha OneMinusSrcAlpha
            //ColorMaterial AmbientAndDiffuse | Emission 颜色材质 环境漫反射光照 及放射光
            // ColorMaterial AmbientAndDiffuse

            CGPROGRAM
                //顶点与片元着色器
                #pragma vertex vert
                #pragma fragment frag        
                //unity宏    
                #include "UnityCG.cginc"

                sampler2D _MainTex;
                fixed4 _Color;

                struct a2v
                {
                    //顶点位置
                    float4 vertex : POSITION;
                    //贴图
                    float2 texcoord : TEXCOORD0;
                    //颜色
                    fixed4 color : COLOR;
                };
        
                struct v2f
                {
                    //像素位置
                    float4 vertex : SV_POSITION;
                    //贴图
                    half2 texcoord : TEXCOORD0;
                    //颜色
                    fixed4 color : COLOR;
                };
                //顶点着色器
                v2f vert (a2v v)
                {
                    v2f o;
                    //将顶点从模型空间转换为裁剪空间,因为2d就是平面映射。
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.texcoord = v.texcoord;
                    o.color = v.color;
                    return o;
                }
                // 片元着色器
                fixed4 frag (v2f v) : COLOR
                {
                    //像素颜色
                    fixed4 color = tex2D(_MainTex, v.texcoord) ;
                    //像素颜色 点乘 灰度因子
                    color.rgb = dot(color.rgb, _Color.rgb);
                    //透明度
                    color.a = _Color.a;
                    return color;
                }
            ENDCG
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千年奇葩

从来没受过打赏,这玩意好吃吗?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值