-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSnowAccumulate.shader
112 lines (96 loc) · 3.93 KB
/
SnowAccumulate.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
Shader "LX/SnowAccumulate"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_SnowTex("SnowTex",2D)="white" {}
_MainBumpTex("MainBumpTex",2D) = "white" {}
_SnowBumpTex("SnowBumpTex",2D) = "white" {}
_SnowDir("SnowDir",vector)=(0,1,0)
_SnowAmount("SnowAmount",float)=1
_BumpScale("BumpScale",float)=1
}
SubShader
{
Tags
{
"RenderType"="Opaque"
}
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
float4 tangent:TANGENT;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldNormal:TEXCOORD1;
float4 ToW0:TEXCOORD2;
float4 ToW1:TEXCOORD3;
float4 ToW2:TEXCOORD4;
float3 worldPos:TEXCOORD5;
};
sampler2D _MainTex;
sampler2D _SnowTex;
sampler2D _MainBumpTex;
sampler2D _SnowBumpTex;
float4 _MainTex_ST;
float3 _SnowDir;
float _SnowAmount;
float _BumpScale;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
o.worldPos = worldPos;
fixed3 worldNormal = o.worldNormal;
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
o.ToW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.ToW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.ToW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
//计算法线
fixed3 mainNormal = UnpackNormal(tex2D(_MainBumpTex, i.uv));
mainNormal.xy *= _BumpScale;
mainNormal.z = sqrt(1.0 - saturate(dot(mainNormal.xy, mainNormal.xy)));
mainNormal = normalize(half3(dot(i.ToW0.xyz, mainNormal), dot(i.ToW1.xyz, mainNormal),
dot(i.ToW2.xyz, mainNormal)));
fixed3 snowNormal = UnpackNormal(tex2D(_SnowBumpTex, i.uv));
snowNormal.xy *= _BumpScale;
snowNormal.z = sqrt(1.0 - saturate(dot(snowNormal.xy, snowNormal.xy)));
snowNormal = normalize(half3(dot(i.ToW0.xyz, snowNormal), dot(i.ToW1.xyz, snowNormal),
dot(i.ToW2.xyz, snowNormal)));
//根据下雪方向和法线的点积来确定混合程度
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 snowColor = tex2D(_SnowTex, i.uv);
float snowValue = saturate(dot(mainNormal, _SnowDir) * _SnowAmount);
fixed4 finalColor = lerp(col, snowColor, snowValue);
//混合法线
fixed3 finalNormal = lerp(mainNormal, snowNormal, snowValue);
//简易lambert光照
float lightDir = UnityWorldSpaceLightDir(i.worldPos);
float diffuse = saturate(dot(lightDir, finalNormal)) * finalColor;
float4 ambient = UNITY_LIGHTMODEL_AMBIENT.rgba * finalColor;
return diffuse + ambient;
}
ENDCG
}
}
}