Unity Shader 小功能之 透明

1.在Properties块中添加一个新的property,这使得我们可以全局控制透明度。

    Properties {  
        _MainTex ("Base (RGB)", 2D) = "white" {}  
        _TransVal ("Transparency Value", Range(0,1)) = 0.5  
    }  

2.改变渲染队列

Tags { "Queue"="Transparent" }  

Tags { "Queue"="Transparent" }一步将决定半透明物体和不透明物体之间正确的渲染关系,如果没有正确设置,那么很有可能就会出现后面的物体跑到了透明物体的前面。


3.在#pragma声明中添加一个新的参数:alpha参数。

CGPROGRAM  
#pragma surface surf Lambert alpha 

解释:再解释一遍上面这句声明的意思。使用名为surf的Surface Function,使用内置的Lambert光照函数,开启透明通道。


4.在surf()函数中添加控制透明度的代码。

    void surf (Input IN, inout SurfaceOutput o) {  
        half4 c = tex2D (_MainTex, IN.uv_MainTex);  
        o.Albedo = c.rgb;  
        o.Alpha = c.b * _TransVal;  
    }  

完整代码示例:

    Shader "Custom/SimpleAlpha" {  
        Properties {  
            _MainTex ("Base (RGB)", 2D) = "white" {}  
            _TransVal ("Transparency Value", Range(0,1)) = 0.5  
        }  
        SubShader {  
            Tags { "RenderType"="Opaque" "Queue"="Transparent"}  
            LOD 200  
              
            CGPROGRAM  
            #pragma surface surf Lambert alpha  
      
            sampler2D _MainTex;  
            float _TransVal;  
      
            struct Input {  
                float2 uv_MainTex;  
            };  
      
            void surf (Input IN, inout SurfaceOutput o) {  
                half4 c = tex2D (_MainTex, IN.uv_MainTex);  
                o.Albedo = c.rgb;  
                o.Alpha = c.b * _TransVal;  
            }  
            ENDCG  
        }   
        FallBack "Diffuse"  
    }  

相关链接:http://blog.csdn.net/candycat1992/article/details/28630459

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值