Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#define_import_path bevy_pbr::lighting
3#import bevy_pbr::{
4 mesh_view_types::POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE,
5 mesh_view_bindings as view_bindings,
6}
7#import bevy_render::maths::PI
9const LAYER_BASE: u32 = 0;
10const LAYER_CLEARCOAT: u32 = 1;
12// From the Filament design doc
13// https://google.github.io/filament/Filament.html#table_symbols
14// Symbol Definition
15// v View unit vector
16// l Incident light unit vector
17// n Surface normal unit vector
18// h Half unit vector between l and v
19// f BRDF
20// f_d Diffuse component of a BRDF
21// f_r Specular component of a BRDF
22// α Roughness, remapped from using input perceptualRoughness
23// σ Diffuse reflectance
24// Ω Spherical domain
25// f0 Reflectance at normal incidence
26// f90 Reflectance at grazing angle
27// χ+(a) Heaviside function (1 if a>0 and 0 otherwise)
28// nior Index of refraction (IOR) of an interface
29// ⟨n⋅l⟩ Dot product clamped to [0..1]
30// ⟨a⟩ Saturated value (clamped to [0..1])
32// The Bidirectional Reflectance Distribution Function (BRDF) describes the surface response of a standard material
33// and consists of two components, the diffuse component (f_d) and the specular component (f_r):
34// f(v,l) = f_d(v,l) + f_r(v,l)
35//
36// The form of the microfacet model is the same for diffuse and specular
37// f_r(v,l) = f_d(v,l) = 1 / { |n⋅v||n⋅l| } ∫_Ω D(m,α) G(v,l,m) f_m(v,l,m) (v⋅m) (l⋅m) dm
38//
39// In which:
40// D, also called the Normal Distribution Function (NDF) models the distribution of the microfacets
41// G models the visibility (or occlusion or shadow-masking) of the microfacets
42// f_m is the microfacet BRDF and differs between specular and diffuse components
43//
44// The above integration needs to be approximated.
46// Input to a lighting function for a single layer (either the base layer or the
47// clearcoat layer).
48struct LayerLightingInput {
49 // The normal vector.
50 N: vec3<f32>,
51 // The reflected vector.
52 R: vec3<f32>,
53 // The normal vector ⋅ the view vector.
54 NdotV: f32,
56 // The perceptual roughness of the layer.
57 perceptual_roughness: f32,
58 // The roughness of the layer.
59 roughness: f32,
62// Input to a lighting function (`point_light`, `spot_light`,
63// `directional_light`).
64struct LightingInput {
65#ifdef STANDARD_MATERIAL_CLEARCOAT
66 layers: array<LayerLightingInput, 2>,
67#else // STANDARD_MATERIAL_CLEARCOAT
68 layers: array<LayerLightingInput, 1>,
69#endif // STANDARD_MATERIAL_CLEARCOAT
71 // The world-space position.
72 P: vec3<f32>,
73 // The vector to the view.
74 V: vec3<f32>,
76 // The diffuse color of the material.
77 diffuse_color: vec3<f32>,
79 // Specular reflectance at the normal incidence angle.
80 //
81 // This should be read F₀, but due to Naga limitations we can't name it that.
82 F0_: vec3<f32>,
83 // Constants for the BRDF approximation.
84 //
85 // See `EnvBRDFApprox` in
86 // <https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile>.
87 // What we call `F_ab` they call `AB`.
88 F_ab: vec2<f32>,
90#ifdef STANDARD_MATERIAL_CLEARCOAT
91 // The strength of the clearcoat layer.
92 clearcoat_strength: f32,
93#endif // STANDARD_MATERIAL_CLEARCOAT
95#ifdef STANDARD_MATERIAL_ANISOTROPY
96 // The anisotropy strength, reflecting the amount of increased roughness in
97 // the tangent direction.
98 anisotropy: f32,
99 // The tangent direction for anisotropy: i.e. the direction in which
100 // roughness increases.
101 Ta: vec3<f32>,
102 // The bitangent direction, which is the cross product of the normal with
103 // the tangent direction.
104 Ba: vec3<f32>,
105#endif // STANDARD_MATERIAL_ANISOTROPY
108// Values derived from the `LightingInput` for both diffuse and specular lights.
109struct DerivedLightingInput {
110 // The half-vector between L, the incident light vector, and V, the view
111 // vector.
112 H: vec3<f32>,
113 // The normal vector ⋅ the incident light vector.
114 NdotL: f32,
115 // The normal vector ⋅ the half-vector.
116 NdotH: f32,
117 // The incident light vector ⋅ the half-vector.
118 LdotH: f32,
121// distanceAttenuation is simply the square falloff of light intensity
122// combined with a smooth attenuation at the edge of the light radius
123//
124// light radius is a non-physical construct for efficiency purposes,
125// because otherwise every light affects every fragment in the scene
126fn getDistanceAttenuation(distanceSquare: f32, inverseRangeSquared: f32) -> f32 {
127 let factor = distanceSquare * inverseRangeSquared;
128 let smoothFactor = saturate(1.0 - factor * factor);
129 let attenuation = smoothFactor * smoothFactor;
130 return attenuation * 1.0 / max(distanceSquare, 0.0001);
133// Normal distribution function (specular D)
134// Based on https://google.github.io/filament/Filament.html#citation-walter07
136// D_GGX(h,α) = α^2 / { π ((n⋅h)^2 (α2−1) + 1)^2 }
138// Simple implementation, has precision problems when using fp16 instead of fp32
139// see https://google.github.io/filament/Filament.html#listing_speculardfp16
140fn D_GGX(roughness: f32, NdotH: f32, h: vec3<f32>) -> f32 {
141 let oneMinusNdotHSquared = 1.0 - NdotH * NdotH;
142 let a = NdotH * roughness;
143 let k = roughness / (oneMinusNdotHSquared + a * a);
144 let d = k * k * (1.0 / PI);
145 return d;
148// An approximation of the anisotropic GGX distribution function.
149//
150// 1
151// D(𝐡) = ───────────────────────────────────────────────────
152// παₜα_b((𝐡 ⋅ 𝐭)² / αₜ²) + (𝐡 ⋅ 𝐛)² / α_b² + (𝐡 ⋅ 𝐧)²)²
153//
154// * `T` = 𝐭 = the tangent direction = the direction of increased roughness.
155//
156// * `B` = 𝐛 = the bitangent direction = the direction of decreased roughness.
157//
158// * `at` = αₜ = the alpha-roughness in the tangent direction.
159//
160// * `ab` = α_b = the alpha-roughness in the bitangent direction.
161//
162// This is from the `KHR_materials_anisotropy` spec:
163// <https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_anisotropy/README.md#individual-lights>
164fn D_GGX_anisotropic(at: f32, ab: f32, NdotH: f32, TdotH: f32, BdotH: f32) -> f32 {
165 let a2 = at * ab;
166 let f = vec3(ab * TdotH, at * BdotH, a2 * NdotH);
167 let w2 = a2 / dot(f, f);
168 let d = a2 * w2 * w2 * (1.0 / PI);
169 return d;
172// Visibility function (Specular G)
173// V(v,l,a) = G(v,l,α) / { 4 (n⋅v) (n⋅l) }
174// such that f_r becomes
175// f_r(v,l) = D(h,α) V(v,l,α) F(v,h,f0)
176// where
177// V(v,l,α) = 0.5 / { n⋅l sqrt((n⋅v)^2 (1−α2) + α2) + n⋅v sqrt((n⋅l)^2 (1−α2) + α2) }
178// Note the two sqrt's, that may be slow on mobile, see https://google.github.io/filament/Filament.html#listing_approximatedspecularv
179fn V_SmithGGXCorrelated(roughness: f32, NdotV: f32, NdotL: f32) -> f32 {
180 let a2 = roughness * roughness;
181 let lambdaV = NdotL * sqrt((NdotV - a2 * NdotV) * NdotV + a2);
182 let lambdaL = NdotV * sqrt((NdotL - a2 * NdotL) * NdotL + a2);
183 let v = 0.5 / (lambdaV + lambdaL);
184 return v;
187// The visibility function, anisotropic variant.
188fn V_GGX_anisotropic(
189 at: f32,
190 ab: f32,
191 NdotL: f32,
192 NdotV: f32,
193 BdotV: f32,
194 TdotV: f32,
195 TdotL: f32,
196 BdotL: f32,
197) -> f32 {
198 let GGX_V = NdotL * length(vec3(at * TdotV, ab * BdotV, NdotV));
199 let GGX_L = NdotV * length(vec3(at * TdotL, ab * BdotL, NdotL));
200 let v = 0.5 / (GGX_V + GGX_L);
201 return saturate(v);
204// A simpler, but nonphysical, alternative to Smith-GGX. We use this for
205// clearcoat, per the Filament spec.
206//
207// https://google.github.io/filament/Filament.html#materialsystem/clearcoatmodel#toc4.9.1
208fn V_Kelemen(LdotH: f32) -> f32 {
209 return 0.25 / (LdotH * LdotH);
212// Fresnel function
213// see https://google.github.io/filament/Filament.html#citation-schlick94
214// F_Schlick(v,h,f_0,f_90) = f_0 + (f_90 − f_0) (1 − v⋅h)^5
215fn F_Schlick_vec(f0: vec3<f32>, f90: f32, VdotH: f32) -> vec3<f32> {
216 // not using mix to keep the vec3 and float versions identical
217 return f0 + (f90 - f0) * pow(1.0 - VdotH, 5.0);
220fn F_Schlick(f0: f32, f90: f32, VdotH: f32) -> f32 {
221 // not using mix to keep the vec3 and float versions identical
222 return f0 + (f90 - f0) * pow(1.0 - VdotH, 5.0);
225fn fresnel(f0: vec3<f32>, LdotH: f32) -> vec3<f32> {
226 // f_90 suitable for ambient occlusion
227 // see https://google.github.io/filament/Filament.html#lighting/occlusion
228 let f90 = saturate(dot(f0, vec3<f32>(50.0 * 0.33)));
229 return F_Schlick_vec(f0, f90, LdotH);
232// Given distribution, visibility, and Fresnel term, calculates the final
233// specular light.
234//
235// Multiscattering approximation:
236// <https://google.github.io/filament/Filament.html#listing_energycompensationimpl>
237fn specular_multiscatter(
238 input: ptr<function, LightingInput>,
239 D: f32,
240 V: f32,
241 F: vec3<f32>,
242 specular_intensity: f32,
243) -> vec3<f32> {
244 // Unpack.
245 let F0 = (*input).F0_;
246 let F_ab = (*input).F_ab;
248 var Fr = (specular_intensity * D * V) * F;
249 Fr *= 1.0 + F0 * (1.0 / F_ab.x - 1.0);
250 return Fr;
253// Specular BRDF
254// https://google.github.io/filament/Filament.html#materialsystem/specularbrdf
256// N, V, and L must all be normalized.
257fn derive_lighting_input(N: vec3<f32>, V: vec3<f32>, L: vec3<f32>) -> DerivedLightingInput {
258 var input: DerivedLightingInput;
259 var H: vec3<f32> = normalize(L + V);
260 input.H = H;
261 input.NdotL = saturate(dot(N, L));
262 input.NdotH = saturate(dot(N, H));
263 input.LdotH = saturate(dot(L, H));
264 return input;
267// Returns L in the `xyz` components and the specular intensity in the `w` component.
268fn compute_specular_layer_values_for_point_light(
269 input: ptr<function, LightingInput>,
270 layer: u32,
271 V: vec3<f32>,
272 light_to_frag: vec3<f32>,
273 light_position_radius: f32,
274) -> vec4<f32> {
275 // Unpack.
276 let R = (*input).layers[layer].R;
277 let a = (*input).layers[layer].roughness;
279 // Representative Point Area Lights.
280 // see http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf p14-16
281 let centerToRay = dot(light_to_frag, R) * R - light_to_frag;
282 let closestPoint = light_to_frag + centerToRay * saturate(
283 light_position_radius * inverseSqrt(dot(centerToRay, centerToRay)));
284 let LspecLengthInverse = inverseSqrt(dot(closestPoint, closestPoint));
285 let normalizationFactor = a / saturate(a + (light_position_radius * 0.5 * LspecLengthInverse));
286 let intensity = normalizationFactor * normalizationFactor;
288 let L: vec3<f32> = closestPoint * LspecLengthInverse; // normalize() equivalent?
289 return vec4(L, intensity);
292// Cook-Torrance approximation of the microfacet model integration using Fresnel law F to model f_m
293// f_r(v,l) = { D(h,α) G(v,l,α) F(v,h,f0) } / { 4 (n⋅v) (n⋅l) }
294fn specular(
295 input: ptr<function, LightingInput>,
296 derived_input: ptr<function, DerivedLightingInput>,
297 specular_intensity: f32,
298) -> vec3<f32> {
299 // Unpack.
300 let roughness = (*input).layers[LAYER_BASE].roughness;
301 let NdotV = (*input).layers[LAYER_BASE].NdotV;
302 let F0 = (*input).F0_;
303 let H = (*derived_input).H;
304 let NdotL = (*derived_input).NdotL;
305 let NdotH = (*derived_input).NdotH;
306 let LdotH = (*derived_input).LdotH;
308 // Calculate distribution.
309 let D = D_GGX(roughness, NdotH, H);
310 // Calculate visibility.
311 let V = V_SmithGGXCorrelated(roughness, NdotV, NdotL);
312 // Calculate the Fresnel term.
313 let F = fresnel(F0, LdotH);
315 // Calculate the specular light.
316 let Fr = specular_multiscatter(input, D, V, F, specular_intensity);
317 return Fr;
320// Calculates the specular light for the clearcoat layer. Returns Fc, the
321// Fresnel term, in the first channel, and Frc, the specular clearcoat light, in
322// the second channel.
323//
324// <https://google.github.io/filament/Filament.html#listing_clearcoatbrdf>
325fn specular_clearcoat(
326 input: ptr<function, LightingInput>,
327 derived_input: ptr<function, DerivedLightingInput>,
328 clearcoat_strength: f32,
329 specular_intensity: f32,
330) -> vec2<f32> {
331 // Unpack.
332 let roughness = (*input).layers[LAYER_CLEARCOAT].roughness;
333 let H = (*derived_input).H;
334 let NdotH = (*derived_input).NdotH;
335 let LdotH = (*derived_input).LdotH;
337 // Calculate distribution.
338 let Dc = D_GGX(roughness, NdotH, H);
339 // Calculate visibility.
340 let Vc = V_Kelemen(LdotH);
341 // Calculate the Fresnel term.
342 let Fc = F_Schlick(0.04, 1.0, LdotH) * clearcoat_strength;
343 // Calculate the specular light.
344 let Frc = (specular_intensity * Dc * Vc) * Fc;
345 return vec2(Fc, Frc);
348#ifdef STANDARD_MATERIAL_ANISOTROPY
350fn specular_anisotropy(
351 input: ptr<function, LightingInput>,
352 derived_input: ptr<function, DerivedLightingInput>,
353 L: vec3<f32>,
354 specular_intensity: f32,
355) -> vec3<f32> {
356 // Unpack.
357 let roughness = (*input).layers[LAYER_BASE].roughness;
358 let NdotV = (*input).layers[LAYER_BASE].NdotV;
359 let V = (*input).V;
360 let F0 = (*input).F0_;
361 let anisotropy = (*input).anisotropy;
362 let Ta = (*input).Ta;
363 let Ba = (*input).Ba;
364 let H = (*derived_input).H;
365 let NdotL = (*derived_input).NdotL;
366 let NdotH = (*derived_input).NdotH;
367 let LdotH = (*derived_input).LdotH;
369 let TdotL = dot(Ta, L);
370 let BdotL = dot(Ba, L);
371 let TdotH = dot(Ta, H);
372 let BdotH = dot(Ba, H);
373 let TdotV = dot(Ta, V);
374 let BdotV = dot(Ba, V);
376 let ab = roughness * roughness;
377 let at = mix(ab, 1.0, anisotropy * anisotropy);
379 let Da = D_GGX_anisotropic(at, ab, NdotH, TdotH, BdotH);
380 let Va = V_GGX_anisotropic(at, ab, NdotL, NdotV, BdotV, TdotV, TdotL, BdotL);
381 let Fa = fresnel(F0, LdotH);
383 // Calculate the specular light.
384 let Fr = specular_multiscatter(input, Da, Va, Fa, specular_intensity);
385 return Fr;
388#endif // STANDARD_MATERIAL_ANISOTROPY
390// Diffuse BRDF
391// https://google.github.io/filament/Filament.html#materialsystem/diffusebrdf
392// fd(v,l) = σ/π * 1 / { |n⋅v||n⋅l| } ∫Ω D(m,α) G(v,l,m) (v⋅m) (l⋅m) dm
393//
394// simplest approximation
395// float Fd_Lambert() {
396// return 1.0 / PI;
397// }
398//
399// vec3 Fd = diffuseColor * Fd_Lambert();
400//
401// Disney approximation
402// See https://google.github.io/filament/Filament.html#citation-burley12
403// minimal quality difference
404fn Fd_Burley(
405 input: ptr<function, LightingInput>,
406 derived_input: ptr<function, DerivedLightingInput>,
407) -> f32 {
408 // Unpack.
409 let roughness = (*input).layers[LAYER_BASE].roughness;
410 let NdotV = (*input).layers[LAYER_BASE].NdotV;
411 let NdotL = (*derived_input).NdotL;
412 let LdotH = (*derived_input).LdotH;
414 let f90 = 0.5 + 2.0 * roughness * LdotH * LdotH;
415 let lightScatter = F_Schlick(1.0, f90, NdotL);
416 let viewScatter = F_Schlick(1.0, f90, NdotV);
417 return lightScatter * viewScatter * (1.0 / PI);
420// Scale/bias approximation
421// https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
422// TODO: Use a LUT (more accurate)
423fn F_AB(perceptual_roughness: f32, NdotV: f32) -> vec2<f32> {
424 let c0 = vec4<f32>(-1.0, -0.0275, -0.572, 0.022);
425 let c1 = vec4<f32>(1.0, 0.0425, 1.04, -0.04);
426 let r = perceptual_roughness * c0 + c1;
427 let a004 = min(r.x * r.x, exp2(-9.28 * NdotV)) * r.x + r.y;
428 return vec2<f32>(-1.04, 1.04) * a004 + r.zw;
431fn EnvBRDFApprox(F0: vec3<f32>, F_ab: vec2<f32>) -> vec3<f32> {
432 return F0 * F_ab.x + F_ab.y;
435fn perceptualRoughnessToRoughness(perceptualRoughness: f32) -> f32 {
436 // clamp perceptual roughness to prevent precision problems
437 // According to Filament design 0.089 is recommended for mobile
438 // Filament uses 0.045 for non-mobile
439 let clampedPerceptualRoughness = clamp(perceptualRoughness, 0.089, 1.0);
440 return clampedPerceptualRoughness * clampedPerceptualRoughness;
443fn point_light(
444 light_id: u32,
445 input: ptr<function, LightingInput>,
446 enable_diffuse: bool
447) -> vec3<f32> {
448 // Unpack.
449 let diffuse_color = (*input).diffuse_color;
450 let P = (*input).P;
451 let N = (*input).layers[LAYER_BASE].N;
452 let V = (*input).V;
454 let light = &view_bindings::clusterable_objects.data[light_id];
455 let light_to_frag = (*light).position_radius.xyz - P;
456 let L = normalize(light_to_frag);
457 let distance_square = dot(light_to_frag, light_to_frag);
458 let rangeAttenuation = getDistanceAttenuation(distance_square, (*light).color_inverse_square_range.w);
460 // Base layer
462 let specular_L_intensity = compute_specular_layer_values_for_point_light(
463 input,
464 LAYER_BASE,
465 V,
466 light_to_frag,
467 (*light).position_radius.w,
468 );
469 var specular_derived_input = derive_lighting_input(N, V, specular_L_intensity.xyz);
471 let specular_intensity = specular_L_intensity.w;
473#ifdef STANDARD_MATERIAL_ANISOTROPY
474 let specular_light = specular_anisotropy(input, &specular_derived_input, L, specular_intensity);
475#else // STANDARD_MATERIAL_ANISOTROPY
476 let specular_light = specular(input, &specular_derived_input, specular_intensity);
477#endif // STANDARD_MATERIAL_ANISOTROPY
479 // Clearcoat
481#ifdef STANDARD_MATERIAL_CLEARCOAT
482 // Unpack.
483 let clearcoat_N = (*input).layers[LAYER_CLEARCOAT].N;
484 let clearcoat_strength = (*input).clearcoat_strength;
486 // Perform specular input calculations again for the clearcoat layer. We
487 // can't reuse the above because the clearcoat normal might be different
488 // from the main layer normal.
489 let clearcoat_specular_L_intensity = compute_specular_layer_values_for_point_light(
490 input,
491 LAYER_CLEARCOAT,
492 V,
493 light_to_frag,
494 (*light).position_radius.w,
495 );
496 var clearcoat_specular_derived_input =
497 derive_lighting_input(clearcoat_N, V, clearcoat_specular_L_intensity.xyz);
499 // Calculate the specular light.
500 let clearcoat_specular_intensity = clearcoat_specular_L_intensity.w;
501 let Fc_Frc = specular_clearcoat(
502 input,
503 &clearcoat_specular_derived_input,
504 clearcoat_strength,
505 clearcoat_specular_intensity
506 );
507 let inv_Fc = 1.0 - Fc_Frc.r; // Inverse Fresnel term.
508 let Frc = Fc_Frc.g; // Clearcoat light.
509#endif // STANDARD_MATERIAL_CLEARCOAT
511 // Diffuse.
512 // Comes after specular since its N⋅L is used in the lighting equation.
513 var derived_input = derive_lighting_input(N, V, L);
514 var diffuse = vec3(0.0);
515 if (enable_diffuse) {
516 diffuse = diffuse_color * Fd_Burley(input, &derived_input);
517 }
519 // See https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminanceEquation
520 // Lout = f(v,l) Φ / { 4 π d^2 }⟨n⋅l⟩
521 // where
522 // f(v,l) = (f_d(v,l) + f_r(v,l)) * light_color
523 // Φ is luminous power in lumens
524 // our rangeAttenuation = 1 / d^2 multiplied with an attenuation factor for smoothing at the edge of the non-physical maximum light radius
526 // For a point light, luminous intensity, I, in lumens per steradian is given by:
527 // I = Φ / 4 π
528 // The derivation of this can be seen here: https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminousPower
530 // NOTE: (*light).color.rgb is premultiplied with (*light).intensity / 4 π (which would be the luminous intensity) on the CPU
532 var color: vec3<f32>;
533#ifdef STANDARD_MATERIAL_CLEARCOAT
534 // Account for the Fresnel term from the clearcoat darkening the main layer.
535 //
536 // <https://google.github.io/filament/Filament.html#materialsystem/clearcoatmodel/integrationinthesurfaceresponse>
537 color = (diffuse + specular_light * inv_Fc) * inv_Fc + Frc;
538#else // STANDARD_MATERIAL_CLEARCOAT
539 color = diffuse + specular_light;
540#endif // STANDARD_MATERIAL_CLEARCOAT
542 return color * (*light).color_inverse_square_range.rgb *
543 (rangeAttenuation * derived_input.NdotL);
546fn spot_light(
547 light_id: u32,
548 input: ptr<function, LightingInput>,
549 enable_diffuse: bool
550) -> vec3<f32> {
551 // reuse the point light calculations
552 let point_light = point_light(light_id, input, enable_diffuse);
554 let light = &view_bindings::clusterable_objects.data[light_id];
556 // reconstruct spot dir from x/z and y-direction flag
557 var spot_dir = vec3<f32>((*light).light_custom_data.x, 0.0, (*light).light_custom_data.y);
558 spot_dir.y = sqrt(max(0.0, 1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z));
559 if ((*light).flags & POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u {
560 spot_dir.y = -spot_dir.y;
561 }
562 let light_to_frag = (*light).position_radius.xyz - (*input).P.xyz;
564 // calculate attenuation based on filament formula https://google.github.io/filament/Filament.html#listing_glslpunctuallight
565 // spot_scale and spot_offset have been precomputed
566 // note we normalize here to get "l" from the filament listing. spot_dir is already normalized
567 let cd = dot(-spot_dir, normalize(light_to_frag));
568 let attenuation = saturate(cd * (*light).light_custom_data.z + (*light).light_custom_data.w);
569 let spot_attenuation = attenuation * attenuation;
571 return point_light * spot_attenuation;
574fn directional_light(
575 light_id: u32,
576 input: ptr<function, LightingInput>,
577 enable_diffuse: bool
578) -> vec3<f32> {
579 // Unpack.
580 let diffuse_color = (*input).diffuse_color;
581 let NdotV = (*input).layers[LAYER_BASE].NdotV;
582 let N = (*input).layers[LAYER_BASE].N;
583 let V = (*input).V;
584 let roughness = (*input).layers[LAYER_BASE].roughness;
586 let light = &view_bindings::lights.directional_lights[light_id];
588 let L = (*light).direction_to_light.xyz;
589 var derived_input = derive_lighting_input(N, V, L);
591 var diffuse = vec3(0.0);
592 if (enable_diffuse) {
593 diffuse = diffuse_color * Fd_Burley(input, &derived_input);
594 }
596#ifdef STANDARD_MATERIAL_ANISOTROPY
597 let specular_light = specular_anisotropy(input, &derived_input, L, 1.0);
598#else // STANDARD_MATERIAL_ANISOTROPY
599 let specular_light = specular(input, &derived_input, 1.0);
600#endif // STANDARD_MATERIAL_ANISOTROPY
602#ifdef STANDARD_MATERIAL_CLEARCOAT
603 let clearcoat_N = (*input).layers[LAYER_CLEARCOAT].N;
604 let clearcoat_strength = (*input).clearcoat_strength;
606 // Perform specular input calculations again for the clearcoat layer. We
607 // can't reuse the above because the clearcoat normal might be different
608 // from the main layer normal.
609 var derived_clearcoat_input = derive_lighting_input(clearcoat_N, V, L);
611 let Fc_Frc =
612 specular_clearcoat(input, &derived_clearcoat_input, clearcoat_strength, 1.0);
613 let inv_Fc = 1.0 - Fc_Frc.r;
614 let Frc = Fc_Frc.g;
615#endif // STANDARD_MATERIAL_CLEARCOAT
617 var color: vec3<f32>;
618#ifdef STANDARD_MATERIAL_CLEARCOAT
619 // Account for the Fresnel term from the clearcoat darkening the main layer.
620 //
621 // <https://google.github.io/filament/Filament.html#materialsystem/clearcoatmodel/integrationinthesurfaceresponse>
622 color = (diffuse + specular_light * inv_Fc) * inv_Fc * derived_input.NdotL +
623 Frc * derived_clearcoat_input.NdotL;
624#else // STANDARD_MATERIAL_CLEARCOAT
625 color = (diffuse + specular_light) * derived_input.NdotL;
626#endif // STANDARD_MATERIAL_CLEARCOAT
628 return color * (*light).color.rgb;
Contents