Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _SnowTex ("SnowTexture(RGB)",2D) = "white"{} _SnowCount ("SnowCount", Range (0.0, 1)) = 0.078 }
struct appdata_t { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; float3 normal : NORMAL; }; struct v2f { float4 vertex : SV_POSITION; half2 texcoord : TEXCOORD0; half2 snowUV : TEXCOORD1 ; fixed4 snow : COLOR;//定义一个四元数用来传递将积雪位置到frag函数 UNITY_FOG_COORDS(1) };
float3 worldNormal = mul((float3x3)_Object2World ,v.normal );//将法线转换到世界空间 float rim = 1-saturate(dot(float3(0,1,0),worldNormal ));//将世界空间的法线方向与Y轴做点积运算 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); o.snowUV = TRANSFORM_TEX(v.texcoord , _SnowTex); o.snow = pow(rim,_SnowCount*64);//控制积雪的量,这个值将传入到frag函数里面用来混合两张贴图
Shader "PengLu/Unlit/SnowVF" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _SnowTex ("SnowTexture(RGB)",2D) = "white"{} _SnowCount ("SnowCount", Range (0.0, 1)) = 0.078 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; float3 normal : NORMAL; }; struct v2f { float4 vertex : SV_POSITION; half2 texcoord : TEXCOORD0; half2 snowUV : TEXCOORD1 ; fixed4 snow : COLOR; UNITY_FOG_COORDS(1) }; sampler2D _MainTex; sampler2D _SnowTex; float4 _MainTex_ST,_SnowTex_ST; float _SnowCount; v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); float3 worldNormal = mul((float3x3)_Object2World ,v.normal ); float rim = 1-saturate(dot(float3(0,1,0),worldNormal )); o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); o.snowUV = TRANSFORM_TEX(v.texcoord , _SnowTex); o.snow = pow(rim,_SnowCount*64); UNITY_TRANSFER_FOG(o,o.vertex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.texcoord); fixed4 snow = tex2D(_SnowTex,i.snowUV); col = lerp(snow,col,saturate(i.snow)); UNITY_APPLY_FOG(i.fogCoord, col); UNITY_OPAQUE_ALPHA(col.a); return col; } ENDCG } } }
Shader "PengLu/Unlit/SnowBumpVF" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("BaseBump", 2D) = "bump" {} _SnowTex ("SnowTexture(RGB)",2D) = "white"{} _SnowCount ("SnowCount", Range (0.01, 1)) = 0.078 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #include "UnityCG.cginc" struct v2f { float4 vertex : SV_POSITION; half2 texcoord : TEXCOORD0; half2 snowUV : TEXCOORD1 ; // fixed4 snow : COLOR; float3 tangent:TEXCOORD2; float3 binormal : TEXCOORD3; float3 normal : TEXCOORD4; UNITY_FOG_COORDS(1) }; sampler2D _MainTex,_BumpMap; sampler2D _SnowTex; float4 _MainTex_ST,_SnowTex_ST; float _SnowCount; v2f vert (appdata_full v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.tangent = v.tangent.xyz; o.normal = v.normal; o.binormal=cross(v.normal,v.tangent.xyz)*v.tangent.w; o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); o.snowUV = TRANSFORM_TEX(v.texcoord , _SnowTex); UNITY_TRANSFER_FOG(o,o.vertex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.texcoord); fixed4 snow = tex2D(_SnowTex,i.snowUV); float3x3 rotation=float3x3 (i.tangent.xyz,i.binormal,i.normal); float3 N = UnpackNormal(tex2D(_BumpMap,i.texcoord)); N=normalize(mul(N,rotation)); N = mul((float3x3)_Object2World ,N ); float rim = 1-saturate(dot(float3(0,1,0),N)); float4 lerpsnow = pow(rim,_SnowCount*16); col = lerp(snow,col,saturate(lerpsnow)); UNITY_APPLY_FOG(i.fogCoord, col); UNITY_OPAQUE_ALPHA(col.a); return col; } ENDCG } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011047171/article/details/47339673