Unity Shader 小功能之 透明

来源:互联网 发布:股票 潜伏日历 软件 编辑:程序博客网 时间:2024/05/01 14:49

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 0