Unity3D_NGUI 灰度shader

本文提供了一种使用代码将图片颜色转换为黑白的方法,通过调整颜色通道来实现图片的灰度效果。具体步骤包括设置UISprite.color为黑色,并在着色阶段根据像素的颜色值决定是否应用灰度效果。
<pre name="code" class="plain">


Unlit/Transparent Colored

struct v2f 

{ 
    float4 vertex : SV_POSITION; 
    half2 texcoord : TEXCOORD0; 
    fixed4 color : COLOR; 
    fixed gray : TEXCOORD1; 
}; 
sampler2D _MainTex; 
float4 _MainTex_ST; 
v2f vert (appdata_t v) 
{ 
    v2f o; 
    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); 
    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); 
    o.color = v.color; 
    o.gray = dot(v.color, fixed4(1,1,1,0)); 
    return o; 
} 
fixed4 frag (v2f i) : COLOR 
{ 
    fixed4 col; 
    if (i.gray == 0) 
    { 
        col = tex2D(_MainTex, i.texcoord); 
        col.rgb = dot(col.rgb, fixed3(.222,.707,.071)); 
    } 
    else 
    { 
        col = tex2D(_MainTex, i.texcoord) * i.color; 
    } 
    return col; 
}


设置UISprite.color = Color.black;使图片黑白

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值