forked from ipud2/Unity-Basic-Shader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PBR_BetterThanUnity.shader
308 lines (247 loc) · 11.1 KB
/
PBR_BetterThanUnity.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
Shader "Unlit/JoeyTA101PBR"
{
Properties
{
[Toggle]_EnableBaseColor("Enable BaseColor",Float) = 0
[Toggle]_EnableRoughness("Enable Roughness",Float) = 0
[Toggle]_EnableMetallic("Enable Metallic",Float) = 0
[Toggle]_EnableEmission("Enable Emission",Float) = 0
[Space(10)]
_BaseColor ("_BaseColor",Color) = (0.5,0.3,0.2,1)
_Roughness("_Roughness",Range(0,1)) = 0.5
_Metallic("_Metallic",Range(0,1)) = 0.5
_Emission("_Emission",Color) = (0,0,0,0)
[Space(10)]
_BaseColorTex ("_BaseColorTex", 2D) = "white" {}
_MetallicTex ("_MetallicTex", 2D) = "white" {}
_RoughnessTex ("_RoughnessTex", 2D) = "white" {}
_EmissionTex ("_EmissionTex", 2D) = "black" {}
[Space(10)]
_NormalTex ("_NormalTex", 2D) = "bump" {}
_AOTex ("_AOTex", 2D) = "white" {}
}
SubShader
{
Tags
{
"RenderType"="Opaque" "LightMode"="ForwardBase"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "UnityGlobalIllumination.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 tangent :TANGENT;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float3 tangent : TEXCOORD1;
float3 normal : TEXCOORD3;
float3 worldPosition: TEXCOORD4;
float3 localPostion : TEXCOORD5;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Value, _RangeValue;
float4 _Color, _BaseColor;
float _Metallic, _Roughness;
sampler2D _BaseColorTex, _MetallicTex, _RoughnessTex;
sampler2D _EmissionTex, _AOTex, _NormalTex;
float _EnableBaseColor,_EnableRoughness,_EnableMetallic,_EnableEmission;
float4 _Emission;
#define PI 3.141592654
//D
float D_DistributionGGX(float3 N, float3 H, float Roughness)
{
float a = Roughness * Roughness;
// float a = Roughness;
float a2 = a * a;
float NH = max(dot(N, H), 0);
float NH2 = NH * NH;
float nominator = a2;
float denominator = (NH2 * (a2 - 1.0) + 1.0);
denominator = PI * denominator * denominator;
return nominator / max(denominator, 0.0000001); //防止分母为0
// return nominator/ (denominator) ;//防止分母为0
}
//G
float GeometrySchlickGGX(float NV, float Roughness)
{
float r = Roughness + 1.0;
float k = r * r / 8.0;
float nominator = NV;
float denominator = k + (1.0 - k) * NV;
// return nominator/ max(denominator,0.001) ;//防止分母为0
return nominator / max(denominator, 0.0000001); //防止分母为0
}
float G_GeometrySmith(float3 N, float3 V, float3 L, float Roughness)
{
float NV = max(dot(N, V), 0);
float NL = max(dot(N, L), 0);
float ggx1 = GeometrySchlickGGX(NV, Roughness);
float ggx2 = GeometrySchlickGGX(NL, Roughness);
return ggx1 * ggx2;
}
//F
float3 F_FrenelSchlick(float NV, float3 F0)
{
return F0 + (1.0 - F0) * pow(1.0 - NV, 5);
}
float3 FresnelSchlickRoughness(float NV, float3 F0, float Roughness)
{
return F0 + (max(float3(1.0 - Roughness, 1.0 - Roughness, 1.0 - Roughness), F0) - F0) * pow(
1.0 - NV, 5.0);
}
//UE4 Black Ops II modify version
float2 EnvBRDFApprox(float Roughness, float NoV)
{
// [ Lazarov 2013, "Getting More Physical in Call of Duty: Black Ops II" ]
// Adaptation to fit our G term.
const float4 c0 = {-1, -0.0275, -0.572, 0.022};
const float4 c1 = {1, 0.0425, 1.04, -0.04};
float4 r = Roughness * c0 + c1;
float a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
float2 AB = float2(-1.04, 1.04) * a004 + r.zw;
return AB;
}
// Black Ops II
// float2 EnvBRDFApprox(float Roughness, float NV)
// {
// float g = 1 -Roughness;
// float4 t = float4(1/0.96, 0.475, (0.0275 - 0.25*0.04)/0.96, 0.25);
// t *= float4(g, g, g, g);
// t += float4(0, 0, (0.015 - 0.75*0.04)/0.96, 0.75);
// float A = t.x * min(t.y, exp2(-9.28 * NV)) + t.z;
// float B = t.w;
// return float2 ( t.w-A,A);
// }如果
float3 ACESToneMapping(float3 x)
{
float a = 2.51f;
float b = 0.03f;
float c = 2.43f;
float d = 0.59f;
float e = 0.14f;
return saturate((x * (a * x + b)) / (x * (c * x + d) + e));
}
float4 ACESToneMapping(float4 x)
{
float a = 2.51f;
float b = 0.03f;
float c = 2.43f;
float d = 0.59f;
float e = 0.14f;
return saturate((x * (a * x + b)) / (x * (c * x + d) + e));
}
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.normal = UnityObjectToWorldNormal(v.normal);
o.worldPosition = mul(unity_ObjectToWorld, v.vertex);
o.localPostion = v.vertex.xyz;
o.tangent = UnityObjectToWorldDir(v.tangent)* v.tangent.w;
return o;
}
float4 frag(v2f i) : SV_Target
{
//Variable
float3 T = normalize(i.tangent);
float3 N = normalize(i.normal);
float3 B = normalize(cross(N, T));
// float3 B = normalize( i.bitangent);
float3 L = normalize(UnityWorldSpaceLightDir(i.worldPosition.xyz));
float3 V = normalize(UnityWorldSpaceViewDir(i.worldPosition.xyz));
float3 H = normalize(V + L);
float2 uv = i.uv;
//================== Normal Map ============================================== //
float3 NormalMap = UnpackNormal(tex2D(_NormalTex, uv));
//TBN矩阵:将世界坐标转到Tangent坐标
//TBN是正交矩阵,正交矩阵的逆等于其转置
float3x3 TBN = float3x3(T, B, N);
N = normalize(mul(NormalMap, TBN));
//================== PBR ============================================== //
// float3 BaseColor = _BaseColor;
// float Roughness = _Roughness;
// float Metallic = _Metallic;
float3 BaseColor = tex2D(_BaseColorTex, uv);
// return BaseColor.xyzz;
float Roughness = tex2D(_RoughnessTex, uv).r;
float Metallic = tex2D(_MetallicTex, uv).r;
float3 Emission = tex2D(_EmissionTex, uv);
float3 AO = tex2D(_AOTex, uv);
// float _EnableBaseColor,_EnableRoughness,_EnableMetallic,_EnableEmission;
if(_EnableBaseColor) BaseColor= _BaseColor;
if(_EnableRoughness) Roughness= _Roughness;
if(_EnableMetallic) Metallic= _Metallic;
if(_EnableEmission) Emission= _Emission;
float3 F0 = lerp(0.04, BaseColor, Metallic);
float3 Radiance = _LightColor0.xyz;
//================== Direct Light ============================================== //
//Specular
//Cook-Torrance BRDF
float HV = max(dot(H, V), 0);
float NV = max(dot(N, V), 0);
float NL = max(dot(N, L), 0);
float D = D_DistributionGGX(N, H, Roughness);
float3 F = F_FrenelSchlick(NV, F0);
float G = G_GeometrySmith(N, V, L, Roughness);
float3 KS = F;
float3 KD = 1 - KS;
KD *= 1 - Metallic;
float3 nominator = D * F * G;
float denominator = max(4 * NV * NL, 0.001);
float3 Specular = nominator / denominator;
// Specular =max( Specular,0);
//Diffuse
// float3 Diffuse = KD * BaseColor / PI;
float3 Diffuse = KD * BaseColor; //没有除以 PI
float3 DirectLight = (Diffuse + Specular) * NL * Radiance;
//================== Indirect Light ============================================== //
float3 IndirectLight = 0;
//Specular
float3 R = reflect(-V, N);
float3 F_IndirectLight = FresnelSchlickRoughness(NV, F0, Roughness);
// return F_IndirectLight.xyzz;
// float3 F_IndirectLight = F_FrenelSchlick(NV,F0);
float mip = Roughness * (1.7 - 0.7 * Roughness) * UNITY_SPECCUBE_LOD_STEPS;
float4 rgb_mip = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, R, mip);
//间接光镜面反射采样的预过滤环境贴图
float3 EnvSpecularPrefilted = DecodeHDR(rgb_mip, unity_SpecCube0_HDR);
//数值近似
float2 env_brdf = EnvBRDFApprox(Roughness, NV);
float3 Specular_Indirect = EnvSpecularPrefilted * (F_IndirectLight * env_brdf.r + env_brdf.g);
//Diffuse
float3 KD_IndirectLight = float3(1, 1, 1) - F_IndirectLight;
// return KD_IndirectLight.xyzz;
KD_IndirectLight *= 1 - Metallic;
float3 irradianceSH = ShadeSH9(float4(N, 1));
// return irradianceSH.rgbb;
// float3 Diffuse_Indirect = irradianceSH * BaseColor *KD_IndirectLight / PI;
float3 Diffuse_Indirect = irradianceSH * BaseColor * KD_IndirectLight; //没有除以 PI
IndirectLight = (Diffuse_Indirect + Specular_Indirect) * AO;
float4 FinalColor = 0;
FinalColor.rgb = DirectLight + IndirectLight;
FinalColor.rgb += Emission;
//HDR => LDR aka ToneMapping
// return G;
// FinalColor.rgb = ACESToneMapping(FinalColor.rgb);
//Linear => Gamma
// FinalColor = pow(FinalColor,1/2.2);
return FinalColor;
}
ENDCG
}
}
}