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::utils as Utils
4#import bevy_pbr::mesh_view_types as ViewTypes
6// From the Filament design doc
7// https://google.github.io/filament/Filament.html#table_symbols
8// Symbol Definition
9// v View unit vector
10// l Incident light unit vector
11// n Surface normal unit vector
12// h Half unit vector between l and v
13// f BRDF
14// f_d Diffuse component of a BRDF
15// f_r Specular component of a BRDF
16// α Roughness, remapped from using input perceptualRoughness
17// σ Diffuse reflectance
18// Ω Spherical domain
19// f0 Reflectance at normal incidence
20// f90 Reflectance at grazing angle
21// χ+(a) Heaviside function (1 if a>0 and 0 otherwise)
22// nior Index of refraction (IOR) of an interface
23// ⟨n⋅l⟩ Dot product clamped to [0..1]
24// ⟨a⟩ Saturated value (clamped to [0..1])
26// The Bidirectional Reflectance Distribution Function (BRDF) describes the surface response of a standard material
27// and consists of two components, the diffuse component (f_d) and the specular component (f_r):
28// f(v,l) = f_d(v,l) + f_r(v,l)
29//
30// The form of the microfacet model is the same for diffuse and specular
31// 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
32//
33// In which:
34// D, also called the Normal Distribution Function (NDF) models the distribution of the microfacets
35// G models the visibility (or occlusion or shadow-masking) of the microfacets
36// f_m is the microfacet BRDF and differs between specular and diffuse components
37//
38// The above integration needs to be approximated.
40// distanceAttenuation is simply the square falloff of light intensity
41// combined with a smooth attenuation at the edge of the light radius
42//
43// light radius is a non-physical construct for efficiency purposes,
44// because otherwise every light affects every fragment in the scene
45fn getDistanceAttenuation(distanceSquare: f32, inverseRangeSquared: f32) -> f32 {
46 let factor = distanceSquare * inverseRangeSquared;
47 let smoothFactor = Utils::saturate(1.0 - factor * factor);
48 let attenuation = smoothFactor * smoothFactor;
49 return attenuation * 1.0 / max(distanceSquare, 0.0001);
52// Normal distribution function (specular D)
53// Based on https://google.github.io/filament/Filament.html#citation-walter07
55// D_GGX(h,α) = α^2 / { π ((n⋅h)^2 (α2−1) + 1)^2 }
57// Simple implementation, has precision problems when using fp16 instead of fp32
58// see https://google.github.io/filament/Filament.html#listing_speculardfp16
59fn D_GGX(roughness: f32, NoH: f32, h: vec3<f32>) -> f32 {
60 let oneMinusNoHSquared = 1.0 - NoH * NoH;
61 let a = NoH * roughness;
62 let k = roughness / (oneMinusNoHSquared + a * a);
63 let d = k * k * (1.0 / Utils::PI);
64 return d;
67// Visibility function (Specular G)
68// V(v,l,a) = G(v,l,α) / { 4 (n⋅v) (n⋅l) }
69// such that f_r becomes
70// f_r(v,l) = D(h,α) V(v,l,α) F(v,h,f0)
71// where
72// V(v,l,α) = 0.5 / { n⋅l sqrt((n⋅v)^2 (1−α2) + α2) + n⋅v sqrt((n⋅l)^2 (1−α2) + α2) }
73// Note the two sqrt's, that may be slow on mobile, see https://google.github.io/filament/Filament.html#listing_approximatedspecularv
74fn V_SmithGGXCorrelated(roughness: f32, NoV: f32, NoL: f32) -> f32 {
75 let a2 = roughness * roughness;
76 let lambdaV = NoL * sqrt((NoV - a2 * NoV) * NoV + a2);
77 let lambdaL = NoV * sqrt((NoL - a2 * NoL) * NoL + a2);
78 let v = 0.5 / (lambdaV + lambdaL);
79 return v;
82// Fresnel function
83// see https://google.github.io/filament/Filament.html#citation-schlick94
84// F_Schlick(v,h,f_0,f_90) = f_0 + (f_90 − f_0) (1 − v⋅h)^5
85fn F_Schlick_vec(f0: vec3<f32>, f90: f32, VoH: f32) -> vec3<f32> {
86 // not using mix to keep the vec3 and float versions identical
87 return f0 + (f90 - f0) * pow(1.0 - VoH, 5.0);
90fn F_Schlick(f0: f32, f90: f32, VoH: f32) -> f32 {
91 // not using mix to keep the vec3 and float versions identical
92 return f0 + (f90 - f0) * pow(1.0 - VoH, 5.0);
95fn fresnel(f0: vec3<f32>, LoH: f32) -> vec3<f32> {
96 // f_90 suitable for ambient occlusion
97 // see https://google.github.io/filament/Filament.html#lighting/occlusion
98 let f90 = Utils::saturate(dot(f0, vec3<f32>(50.0 * 0.33)));
99 return F_Schlick_vec(f0, f90, LoH);
102// Specular BRDF
103// https://google.github.io/filament/Filament.html#materialsystem/specularbrdf
105// Cook-Torrance approximation of the microfacet model integration using Fresnel law F to model f_m
106// f_r(v,l) = { D(h,α) G(v,l,α) F(v,h,f0) } / { 4 (n⋅v) (n⋅l) }
107fn specular(f0: vec3<f32>, roughness: f32, h: vec3<f32>, NoV: f32, NoL: f32,
108 NoH: f32, LoH: f32, specularIntensity: f32) -> vec3<f32> {
109 let D = D_GGX(roughness, NoH, h);
110 let V = V_SmithGGXCorrelated(roughness, NoV, NoL);
111 let F = fresnel(f0, LoH);
113 return (specularIntensity * D * V) * F;
116// Diffuse BRDF
117// https://google.github.io/filament/Filament.html#materialsystem/diffusebrdf
118// fd(v,l) = σ/π * 1 / { |n⋅v||n⋅l| } ∫Ω D(m,α) G(v,l,m) (v⋅m) (l⋅m) dm
119//
120// simplest approximation
121// float Fd_Lambert() {
122// return 1.0 / PI;
123// }
124//
125// vec3 Fd = diffuseColor * Fd_Lambert();
126//
127// Disney approximation
128// See https://google.github.io/filament/Filament.html#citation-burley12
129// minimal quality difference
130fn Fd_Burley(roughness: f32, NoV: f32, NoL: f32, LoH: f32) -> f32 {
131 let f90 = 0.5 + 2.0 * roughness * LoH * LoH;
132 let lightScatter = F_Schlick(1.0, f90, NoL);
133 let viewScatter = F_Schlick(1.0, f90, NoV);
134 return lightScatter * viewScatter * (1.0 / Utils::PI);
137// From https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
138fn EnvBRDFApprox(f0: vec3<f32>, perceptual_roughness: f32, NoV: f32) -> vec3<f32> {
139 let c0 = vec4<f32>(-1.0, -0.0275, -0.572, 0.022);
140 let c1 = vec4<f32>(1.0, 0.0425, 1.04, -0.04);
141 let r = perceptual_roughness * c0 + c1;
142 let a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
143 let AB = vec2<f32>(-1.04, 1.04) * a004 + r.zw;
144 return f0 * AB.x + AB.y;
147fn perceptualRoughnessToRoughness(perceptualRoughness: f32) -> f32 {
148 // clamp perceptual roughness to prevent precision problems
149 // According to Filament design 0.089 is recommended for mobile
150 // Filament uses 0.045 for non-mobile
151 let clampedPerceptualRoughness = clamp(perceptualRoughness, 0.089, 1.0);
152 return clampedPerceptualRoughness * clampedPerceptualRoughness;
155// from https://64.github.io/tonemapping/
156// reinhard on RGB oversaturates colors
157fn reinhard(color: vec3<f32>) -> vec3<f32> {
158 return color / (1.0 + color);
161fn reinhard_extended(color: vec3<f32>, max_white: f32) -> vec3<f32> {
162 let numerator = color * (1.0 + (color / vec3<f32>(max_white * max_white)));
163 return numerator / (1.0 + color);
166// luminance coefficients from Rec. 709.
167// https://en.wikipedia.org/wiki/Rec._709
168fn luminance(v: vec3<f32>) -> f32 {
169 return dot(v, vec3<f32>(0.2126, 0.7152, 0.0722));
172fn change_luminance(c_in: vec3<f32>, l_out: f32) -> vec3<f32> {
173 let l_in = luminance(c_in);
174 return c_in * (l_out / l_in);
177fn reinhard_luminance(color: vec3<f32>) -> vec3<f32> {
178 let l_old = luminance(color);
179 let l_new = l_old / (1.0 + l_old);
180 return change_luminance(color, l_new);
183fn reinhard_extended_luminance(color: vec3<f32>, max_white_l: f32) -> vec3<f32> {
184 let l_old = luminance(color);
185 let numerator = l_old * (1.0 + (l_old / (max_white_l * max_white_l)));
186 let l_new = numerator / (1.0 + l_old);
187 return change_luminance(color, l_new);
190fn point_light(
191 world_position: vec3<f32>, light: ViewTypes::PointLight, roughness: f32, NdotV: f32, N: vec3<f32>, V: vec3<f32>,
192 R: vec3<f32>, F0: vec3<f32>, diffuseColor: vec3<f32>
193) -> vec3<f32> {
194 let light_to_frag = light.position_radius.xyz - world_position.xyz;
195 let distance_square = dot(light_to_frag, light_to_frag);
196 let rangeAttenuation =
197 getDistanceAttenuation(distance_square, light.color_inverse_square_range.w);
199 // Specular.
200 // Representative Point Area Lights.
201 // see http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf p14-16
202 let a = roughness;
203 let centerToRay = dot(light_to_frag, R) * R - light_to_frag;
204 let closestPoint = light_to_frag + centerToRay * Utils::saturate(light.position_radius.w * inverseSqrt(dot(centerToRay, centerToRay)));
205 let LspecLengthInverse = inverseSqrt(dot(closestPoint, closestPoint));
206 let normalizationFactor = a / Utils::saturate(a + (light.position_radius.w * 0.5 * LspecLengthInverse));
207 let specularIntensity = normalizationFactor * normalizationFactor;
209 var L: vec3<f32> = closestPoint * LspecLengthInverse; // normalize() equivalent?
210 var H: vec3<f32> = normalize(L + V);
211 var NoL: f32 = Utils::saturate(dot(N, L));
212 var NoH: f32 = Utils::saturate(dot(N, H));
213 var LoH: f32 = Utils::saturate(dot(L, H));
215 let specular_light = specular(F0, roughness, H, NdotV, NoL, NoH, LoH, specularIntensity);
217 // Diffuse.
218 // Comes after specular since its NoL is used in the lighting equation.
219 L = normalize(light_to_frag);
220 H = normalize(L + V);
221 NoL = Utils::saturate(dot(N, L));
222 NoH = Utils::saturate(dot(N, H));
223 LoH = Utils::saturate(dot(L, H));
225 let diffuse = diffuseColor * Fd_Burley(roughness, NdotV, NoL, LoH);
227 // See https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminanceEquation
228 // Lout = f(v,l) Φ / { 4 π d^2 }⟨n⋅l⟩
229 // where
230 // f(v,l) = (f_d(v,l) + f_r(v,l)) * light_color
231 // Φ is luminous power in lumens
232 // our rangeAttentuation = 1 / d^2 multiplied with an attenuation factor for smoothing at the edge of the non-physical maximum light radius
234 // For a point light, luminous intensity, I, in lumens per steradian is given by:
235 // I = Φ / 4 π
236 // The derivation of this can be seen here: https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminousPower
238 // NOTE: light.color.rgb is premultiplied with light.intensity / 4 π (which would be the luminous intensity) on the CPU
240 // TODO compensate for energy loss https://google.github.io/filament/Filament.html#materialsystem/improvingthebrdfs/energylossinspecularreflectance
242 return ((diffuse + specular_light) * light.color_inverse_square_range.rgb) * (rangeAttenuation * NoL);
245fn spot_light(
246 world_position: vec3<f32>, light: ViewTypes::PointLight, roughness: f32, NdotV: f32, N: vec3<f32>, V: vec3<f32>,
247 R: vec3<f32>, F0: vec3<f32>, diffuseColor: vec3<f32>
248) -> vec3<f32> {
249 // reuse the point light calculations
250 let point_light = point_light(world_position, light, roughness, NdotV, N, V, R, F0, diffuseColor);
252 // reconstruct spot dir from x/z and y-direction flag
253 var spot_dir = vec3<f32>(light.light_custom_data.x, 0.0, light.light_custom_data.y);
254 spot_dir.y = sqrt(1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z);
255 if ((light.flags & ViewTypes::POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u) {
256 spot_dir.y = -spot_dir.y;
257 }
258 let light_to_frag = light.position_radius.xyz - world_position.xyz;
260 // calculate attenuation based on filament formula https://google.github.io/filament/Filament.html#listing_glslpunctuallight
261 // spot_scale and spot_offset have been precomputed
262 // note we normalize here to get "l" from the filament listing. spot_dir is already normalized
263 let cd = dot(-spot_dir, normalize(light_to_frag));
264 let attenuation = Utils::saturate(cd * light.light_custom_data.z + light.light_custom_data.w);
265 let spot_attenuation = attenuation * attenuation;
267 return point_light * spot_attenuation;
270fn directional_light(light: ViewTypes::DirectionalLight, roughness: f32, NdotV: f32, normal: vec3<f32>, view: vec3<f32>, R: vec3<f32>, F0: vec3<f32>, diffuseColor: vec3<f32>) -> vec3<f32> {
271 let incident_light = light.direction_to_light.xyz;
273 let half_vector = normalize(incident_light + view);
274 let NoL = Utils::saturate(dot(normal, incident_light));
275 let NoH = Utils::saturate(dot(normal, half_vector));
276 let LoH = Utils::saturate(dot(incident_light, half_vector));
278 let diffuse = diffuseColor * Fd_Burley(roughness, NdotV, NoL, LoH);
279 let specularIntensity = 1.0;
280 let specular_light = specular(F0, roughness, half_vector, NdotV, NoL, NoH, LoH, specularIntensity);
282 return (specular_light + diffuse) * light.color.rgb * NoL;
Contents