Unity shader学习之Alpha Blend

Unity shader学习之Alpha Blend,第1张

Unity shader学习之Alpha Blend

通过 ShaderLab 的 AlphaBlend 能够实现真正的半透明效果。


使用 AlphaBlend 时,需要关闭深度写入 ZWrite Off,但得非常小心物体渲染顺序, RenderQueue。


转载请注明出处: http://www.cnblogs.com/jietian331/p/7158938.html

shader如下:

Shader "Custom/Alpha Blend"
{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
_Specular("Specular,", Color) = (,,,)
_Gloss("Gloss", Range(,)) =
} SubShader
{
Tags
{
"RenderType" = "Transparent"
"Queue" = "Transparent"
"IgnoreProjector" = "True"
} Pass
{
Tags
{
"LightMode" = "ForwardBase"
}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc"
#include "Lighting.cginc" sampler2D _MainTex;
fixed4 _Specular;
float _Gloss; struct appdata
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
}; struct v2f
{
float4 pos : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float3 worldLight : TEXCOORD2;
float3 worldView : TEXCOORD3;
}; v2f vert(appdata v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.uv = v.uv;
o.worldNormal = normalize(UnityObjectToWorldNormal(v.normal));
o.worldLight = normalize(WorldSpaceLightDir(v.vertex));
o.worldView = normalize(WorldSpaceViewDir(v.vertex));
return o;
} fixed4 frag(v2f i) : SV_TARGET
{
fixed4 albedo = tex2D(_MainTex, i.uv) * i.color;
fixed3 ambient = albedo.rgb * UNITY_LIGHTMODEL_AMBIENT.rgb;
fixed3 diff = albedo.rgb * _LightColor0.rgb * max(, dot(i.worldNormal, i.worldLight));
float3 halfDir = normalize(i.worldView + i.worldLight);
fixed3 spec = albedo.rgb * _Specular.rgb * pow(max(, dot(halfDir, i.worldNormal)), _Gloss);
fixed3 col = ambient + diff + spec;
return fixed4(col, albedo.a);
} ENDCG
}
} Fallback "Diffuse"
}

效果如下:

但关闭了深度写入之后,会引发一些问题,如:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/587580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存