unity 透明度算法_UnityShader 学习笔记 4 透明度测试

原图:

75bade92e32e3e3db11373e5189fd12a.png

效果图:

6c7cccf6aa47b0d186dcce2aea545457.png

dd9bec81102d45bd060ab20d52fee764.png

源码:

Shader "_MyShader/5_Alpha/0_AlphaTest"

{

Properties

{

_Color ("Color ", COLOR) = (1,1,1,1)

_MainTex ("Texture", 2D) = "white" {}

_CutOff ("CutOff", Range(0,0.9)) = 0.6

}

SubShader

{

Tags {"Queue" = "AlphaTest" "IgnoreProjecter" = "true" "RenderType" = "TransparentCutout"}

pass {

Tags {"LightMode" = "ForwardBase"}

CGPROGRAM

#pragma vertex vert

#pragma fragment frag

#include "Lighting.cginc"

#include "UnityCG.cginc"

fixed4 _Color;

sampler2D _MainTex;

float4 _MainTex_ST;

fixed _CutOff;

struct a2v {

float4 vertex: POSITION;

float3 normal: NORMAL;

float4 texcoord: TEXCOORD0;

};

struct v2f {

float4 pos: SV_POSITION;

float3 worldNormalDir: POSITION1;

float3 worldLightDir: POSITION2;

float2 uv: TEXCOORD0;

};

v2f vert(a2v v){

v2f o;

o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

o.worldNormalDir = UnityObjectToWorldNormal(v.normal);

o.worldLightDir = UnityWorldSpaceLightDir(v.vertex);

o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);

return o;

}

fixed4 frag(v2f i):SV_Target{

fixed3 normalDir = normalize(i.worldNormalDir);

fixed3 lightDir = normalize(i.worldLightDir);

fixed4 tex = tex2D(_MainTex,i.uv);

//AlphaTest

//if(tex.a - _CutOff <= 0)

//    discard;

clip(tex.a - _CutOff);

fixed3 albedo = tex.rgb *_Color.rgb;

fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

fixed3 diffuseC = _LightColor0.rgb * albedo * saturate(dot(normalDir,lightDir));

fixed4 col = fixed4(ambient + diffuseC,1);

return col;

}

ENDCG

}

}

FallBack "Transparent/Cutout/VertexLit"

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值