設(shè)置3D模型透明 alpha
目錄
1. 普通透明設(shè)置準(zhǔn)備
2. 普通透明設(shè)置實(shí)現(xiàn)
3. 普通透明設(shè)置說(shuō)明
4. cutoff準(zhǔn)備
5. cutoff實(shí)現(xiàn)
6. cutoff說(shuō)明
Unity默認(rèn)使用的shader不能設(shè)置3D對(duì)象的透明和和半透明瘟裸,我們只需要做簡(jiǎn)單的修改就可以實(shí)現(xiàn)這個(gè)功能。
普通透明設(shè)置準(zhǔn)備
- 新建一個(gè) Stander surface shader 和一個(gè)材質(zhì)material,命名為NormalAlpha枪眉,把新建的shader綁定在這個(gè)材質(zhì)上甸饱。
- 準(zhǔn)備一個(gè)簡(jiǎn)單的模型和對(duì)應(yīng)的貼圖惨远,這里是從asset store下載了一個(gè)卡通車(chē)模型偷厦。
- 把新建的material 賦給卡通車(chē)模型霹肝。
普通透明設(shè)置實(shí)現(xiàn)
-
添加渲染隊(duì)列標(biāo)簽申钩,把這個(gè)標(biāo)簽添加到tags{}中。
"Queue"="Transparent"
-
在#pragma編譯指令后添加參數(shù) alpha
#pragma surface surf Lambert alpha
使用顏色值的最后一位設(shè)置顏色值痹升,附上surf函數(shù)建炫。
void surf (Input IN, inout SurfaceOutput o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
可以通過(guò)屬相面板的顏色值來(lái)控制透明度,因?yàn)?code>o.Alpha = c.a;疼蛾。也可以直接設(shè)定透明度,比如o.Alpha = 0.5;
艺配。
完整代碼
代碼已經(jīng)上傳到github,https://github.com/superzhan/shaderProj察郁。
透明控制的代碼在A(yíng)lpha文件夾下。
Shader "Custom/Alpha/NormalAlpha" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
LOD 200
CGPROGRAM
//再最后添加alpha參數(shù)转唉,就可以實(shí)現(xiàn)模型的透明設(shè)置
#pragma surface surf Lambert alpha
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutput o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
普通透明設(shè)置說(shuō)明
透明的設(shè)置很簡(jiǎn)單皮钠,主要是添加渲染隊(duì)列的tag和在編譯指令后添加alpha參數(shù),最后在surf函數(shù)中設(shè)置輸出的SurfaceOutput的alpha值赠法。這里本來(lái)使用unity5 的默認(rèn)光照模型麦轰,但設(shè)置透明的時(shí)候出來(lái)點(diǎn)問(wèn)題,然后就改成了Lambert光照模型砖织,surf函數(shù)的輸出結(jié)構(gòu)也改成了SurfaceOutput款侵。
.
.
.
.
.
cutoff alpha 通道透明設(shè)置
cutoff alpha的功能是當(dāng)alpha值小于某個(gè)值的時(shí)候,就把這個(gè)像素點(diǎn)設(shè)置成完全透明侧纯。用來(lái)實(shí)現(xiàn)某些效果新锈,比如腐爛消失等。
cutoff準(zhǔn)備
- 新建一個(gè) Stander surface shader 和一個(gè)材質(zhì)material,命名為CutoffAlpha眶熬,把新建的shader綁定在這個(gè)材質(zhì)上妹笆。
- 準(zhǔn)備一個(gè)簡(jiǎn)單的模型和對(duì)應(yīng)的貼圖,這里是從asset store下載了一個(gè)卡通車(chē)模型娜氏。
- 把新建的material 賦給卡通車(chē)模型拳缠。
cutoff實(shí)現(xiàn)
-
添加渲染隊(duì)列標(biāo)簽,把這個(gè)標(biāo)簽添加到tags{}中贸弥。
"Queue"="Transparent"
-
在Properties添加一個(gè)屬性,用于控制alpha的范圍窟坐,當(dāng)alpha小于這個(gè)數(shù)值時(shí),就會(huì)把像素點(diǎn)設(shè)置透明茂腥。
_Cutoff("Cutoff Value",Range(0,1.1))=0.5
-
在#pragma編譯指令后添加參數(shù) alphatest:_Cutoff 狸涌。 冒號(hào)后的_Cutoff就是屬性中的數(shù)值。
#pragma surface surf Lambert alphatest:_Cutoff
在surf函數(shù)中設(shè)置要變更的顏色通道最岗。
void surf (Input IN, inout SurfaceOutput o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
//這里使用藍(lán)色通道
o.Alpha = c.b;
}
附上完整代碼
代碼已經(jīng)上傳到github,https://github.com/superzhan/shaderProj帕胆。
透明控制的代碼在A(yíng)lpha文件夾下。
Shader "Custom/Alpha/CutoffAlpha" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Cutoff("Cutoff Value",Range(0,1.1))=0.5
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutput o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.b;
}
ENDCG
}
FallBack "Diffuse"
}
cutoff說(shuō)明
當(dāng)設(shè)置alphatest:_Cutoff
后般渡,o.Alpha
輸出結(jié)構(gòu)的透明值小于_Cutoff懒豹,這個(gè)像素點(diǎn)就會(huì)完全透明芙盘。在surf函數(shù)中的o.Alpha = c.b
可以設(shè)置為o.Alpha = c.r
或者o.Alpha = c.b
,不同的貼圖顏色會(huì)有不同的效果,需要根據(jù)具體的情況做調(diào)整脸秽。