-
Notifications
You must be signed in to change notification settings - Fork 13
/
Lighting.hlsli
308 lines (230 loc) · 9.11 KB
/
Lighting.hlsli
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
// Include guard
#ifndef _LIGHTING_HLSL
#define _LIGHTING_HLSL
#define LIGHT_TYPE_DIRECTIONAL 0
#define LIGHT_TYPE_POINT 1
#define LIGHT_TYPE_SPOT 2
struct Light
{
int Type;
float3 Direction; // 16 bytes
float Range;
float3 Position; // 32 bytes
float Intensity;
float3 Color; // 48 bytes
float SpotFalloff;
float3 Padding; // 64 bytes
};
// === UTILITY FUNCTIONS ============================================
// Basic sample and unpack
float3 SampleAndUnpackNormalMap(Texture2D map, SamplerState samp, float2 uv)
{
return map.Sample(samp, uv).rgb * 2.0f - 1.0f;
}
// Handle converting tangent-space normal map to world space normal
float3 NormalMapping(Texture2D map, SamplerState samp, float2 uv, float3 normal, float3 tangent)
{
// Grab the normal from the map
float3 normalFromMap = SampleAndUnpackNormalMap(map, samp, uv);
// Gather the required vectors for converting the normal
float3 N = normal;
float3 T = normalize(tangent - N * dot(tangent, N));
float3 B = cross(T, N);
// Create the 3x3 matrix to convert from TANGENT-SPACE normals to WORLD-SPACE normals
float3x3 TBN = float3x3(T, B, N);
// Adjust the normal from the map and simply use the results
return normalize(mul(normalFromMap, TBN));
}
// Range-based attenuation function
float Attenuate(Light light, float3 worldPos)
{
float dist = distance(light.Position, worldPos);
// Ranged-based attenuation
float att = saturate(1.0f - (dist * dist / (light.Range * light.Range)));
// Soft falloff
return att * att;
}
// === BASIC LIGHTING ===============================================
// Lambert diffuse BRDF
float Diffuse(float3 normal, float3 dirToLight)
{
return saturate(dot(normal, dirToLight));
}
// Blinn-Phong (specular) BRDF
float SpecularBlinnPhong(float3 normal, float3 dirToLight, float3 toCamera, float shininess)
{
// Calculate halfway vector
float3 halfwayVector = normalize(dirToLight + toCamera);
// Compare halflway vector and normal and raise to a power
return shininess == 0 ? 0.0f : pow(max(dot(halfwayVector, normal), 0), shininess);
}
// === LIGHT TYPES FOR BASIC LIGHTING ===============================
float3 DirLight(Light light, float3 normal, float3 worldPos, float3 camPos, float shininess, float3 surfaceColor)
{
// Get normalize direction to the light
float3 toLight = normalize(-light.Direction);
float3 toCam = normalize(camPos - worldPos);
// Calculate the light amounts
float diff = Diffuse(normal, toLight);
float spec = SpecularBlinnPhong(normal, toLight, toCam, shininess);
// Combine
return (diff * surfaceColor + spec) * light.Intensity * light.Color;
}
float3 PointLight(Light light, float3 normal, float3 worldPos, float3 camPos, float shininess, float3 surfaceColor)
{
// Calc light direction
float3 toLight = normalize(light.Position - worldPos);
float3 toCam = normalize(camPos - worldPos);
// Calculate the light amounts
float atten = Attenuate(light, worldPos);
float diff = Diffuse(normal, toLight);
float spec = SpecularBlinnPhong(normal, toLight, toCam, shininess);
// Combine
return (diff * surfaceColor + spec) * atten * light.Intensity * light.Color;
}
float3 SpotLight(Light light, float3 normal, float3 worldPos, float3 camPos, float shininess, float3 surfaceColor)
{
// Calculate the spot falloff
float3 toLight = normalize(light.Position - worldPos);
float penumbra = pow(saturate(dot(-toLight, light.Direction)), light.SpotFalloff);
// Combine with the point light calculation
// Note: This could be optimized a bit
return PointLight(light, normal, worldPos, camPos, shininess, surfaceColor) * penumbra;
}
// === PHYSICALLY BASED LIGHTING ====================================
// PBR Constants:
// The fresnel value for non-metals (dielectrics)
// Page 9: "F0 of nonmetals is now a constant 0.04"
// http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
// Also slide 65 of http://blog.selfshadow.com/publications/s2014-shading-course/hoffman/s2014_pbs_physics_math_slides.pdf
static const float F0_NON_METAL = 0.04f;
// Need a minimum roughness for when spec distribution function denominator goes to zero
static const float MIN_ROUGHNESS = 0.0000001f; // 6 zeros after decimal
static const float PI = 3.14159265359f;
// Lambert diffuse BRDF - Same as the basic lighting!
float DiffusePBR(float3 normal, float3 dirToLight)
{
return saturate(dot(normal, dirToLight));
}
// GGX (Trowbridge-Reitz)
//
// a - Roughness
// h - Half vector
// n - Normal
//
// D(h, n) = a^2 / pi * ((n dot h)^2 * (a^2 - 1) + 1)^2
float SpecDistribution(float3 n, float3 h, float roughness)
{
// Pre-calculations
float NdotH = saturate(dot(n, h));
float NdotH2 = NdotH * NdotH;
float a = roughness * roughness;
float a2 = max(a * a, MIN_ROUGHNESS); // Applied after remap!
// ((n dot h)^2 * (a^2 - 1) + 1)
float denomToSquare = NdotH2 * (a2 - 1) + 1;
// Can go to zero if roughness is 0 and NdotH is 1
// Final value
return a2 / (PI * denomToSquare * denomToSquare);
}
// Fresnel term - Schlick approx.
//
// v - View vector
// h - Half vector
// f0 - Value when l = n (full specular color)
//
// F(v,h,f0) = f0 + (1-f0)(1 - (v dot h))^5
float3 Fresnel(float3 v, float3 h, float3 f0)
{
// Pre-calculations
float VdotH = saturate(dot(v, h));
// Final value
return f0 + (1 - f0) * pow(1 - VdotH, 5);
}
// Geometric Shadowing - Schlick-GGX (based on Schlick-Beckmann)
// - k is remapped to a / 2, roughness remapped to (r+1)/2
//
// n - Normal
// v - View vector
//
// G(l,v,h)
float GeometricShadowing(float3 n, float3 v, float roughness)
{
// End result of remapping:
float k = pow(roughness + 1, 2) / 8.0f;
float NdotV = saturate(dot(n, v));
// Final value
return NdotV / (NdotV * (1 - k) + k);
}
// Microfacet BRDF (Specular)
//
// f(l,v) = D(h)F(v,h)G(l,v,h) / 4(n dot l)(n dot v)
// - part of the denominator are canceled out by numerator (see below)
//
// D() - Spec Dist - Trowbridge-Reitz (GGX)
// F() - Fresnel - Schlick approx
// G() - Geometric Shadowing - Schlick-GGX
float3 MicrofacetBRDF(float3 n, float3 l, float3 v, float roughness, float metalness, float3 specColor)
{
// Other vectors
float3 h = normalize(v + l);
// Grab various functions
float D = SpecDistribution(n, h, roughness);
float3 F = Fresnel(v, h, specColor); // This ranges from F0_NON_METAL to actual specColor based on metalness
float G = GeometricShadowing(n, v, roughness) * GeometricShadowing(n, l, roughness);
// Final formula
// Denominator dot products partially canceled by G()!
// See page 16: http://blog.selfshadow.com/publications/s2012-shading-course/hoffman/s2012_pbs_physics_math_notes.pdf
return (D * F * G) / (4 * max(dot(n,v), dot(n,l)));
}
// Calculates diffuse amount based on energy conservation
//
// diffuse - Diffuse amount
// specular - Specular color (including light color)
// metalness - surface metalness amount
//
// Metals should have an albedo of (0,0,0)...mostly
// See slide 65: http://blog.selfshadow.com/publications/s2014-shading-course/hoffman/s2014_pbs_physics_math_slides.pdf
float3 DiffuseEnergyConserve(float diffuse, float3 specular, float metalness)
{
return diffuse * ((1 - saturate(specular)) * (1 - metalness));
}
// === LIGHT TYPES FOR PBR LIGHTING =================================
float3 DirLightPBR(Light light, float3 normal, float3 worldPos, float3 camPos, float roughness, float metalness, float3 surfaceColor, float3 specularColor)
{
// Get normalize direction to the light
float3 toLight = normalize(-light.Direction);
float3 toCam = normalize(camPos - worldPos);
// Calculate the light amounts
float diff = DiffusePBR(normal, toLight);
float3 spec = MicrofacetBRDF(normal, toLight, toCam, roughness, metalness, specularColor);
// Calculate diffuse with energy conservation
// (Reflected light doesn't get diffused)
float3 balancedDiff = DiffuseEnergyConserve(diff, spec, metalness);
// Combine amount with
return (balancedDiff * surfaceColor + spec) * light.Intensity * light.Color;
}
float3 PointLightPBR(Light light, float3 normal, float3 worldPos, float3 camPos, float roughness, float metalness, float3 surfaceColor, float3 specularColor)
{
// Calc light direction
float3 toLight = normalize(light.Position - worldPos);
float3 toCam = normalize(camPos - worldPos);
// Calculate the light amounts
float atten = Attenuate(light, worldPos);
float diff = DiffusePBR(normal, toLight);
float3 spec = MicrofacetBRDF(normal, toLight, toCam, roughness, metalness, specularColor);
// Calculate diffuse with energy conservation
// (Reflected light doesn't diffuse)
float3 balancedDiff = DiffuseEnergyConserve(diff, spec, metalness);
// Combine
return (balancedDiff * surfaceColor + spec) * atten * light.Intensity * light.Color;
}
float3 SpotLightPBR(Light light, float3 normal, float3 worldPos, float3 camPos, float roughness, float metalness, float3 surfaceColor, float3 specularColor)
{
// Calculate the spot falloff
float3 toLight = normalize(light.Position - worldPos);
float penumbra = pow(saturate(dot(-toLight, light.Direction)), light.SpotFalloff);
// Combine with the point light calculation
// Note: This could be optimized a bit
return PointLightPBR(light, normal, worldPos, camPos, roughness, metalness, surfaceColor, specularColor) * penumbra;
}
#endif