Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Spherical Area Lights #1901

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct PointLight {
pub color: Color,
pub intensity: f32,
pub range: f32,
pub radius: f32,
}

impl Default for PointLight {
Expand All @@ -19,6 +20,7 @@ impl Default for PointLight {
color: Color::rgb(1.0, 1.0, 1.0),
intensity: 200.0,
range: 20.0,
radius: 0.0,
}
}
}
Expand All @@ -39,7 +41,9 @@ impl PointLightUniform {

// premultiply color by intensity
// we don't use the alpha at all, so no reason to multiply only [0..3]
let color: [f32; 4] = (light.color * light.intensity).into();
let mut color: [f32; 4] = (light.color * light.intensity).into();
color[3] = light.radius;
Josh015 marked this conversation as resolved.
Show resolved Hide resolved

PointLightUniform {
pos: [x, y, z, 1.0],
color,
Expand Down
32 changes: 26 additions & 6 deletions crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ vec3 fresnel(vec3 f0, float LoH) {
// Cook-Torrance approximation of the microfacet model integration using Fresnel law F to model f_m
// f_r(v,l) = { D(h,α) G(v,l,α) F(v,h,f0) } / { 4 (n⋅v) (n⋅l) }
vec3 specular(vec3 f0, float roughness, const vec3 h, float NoV, float NoL,
float NoH, float LoH) {
float NoH, float LoH, float specularIntensity) {
float D = D_GGX(roughness, NoH, h);
float V = V_SmithGGXCorrelated(roughness, NoV, NoL);
vec3 F = fresnel(f0, LoH);

return (D * V) * F;
return (specularIntensity * D * V) * F;
}

// Diffuse BRDF
Expand Down Expand Up @@ -339,24 +339,44 @@ void main() {
// Diffuse strength inversely related to metallicity
vec3 diffuseColor = output_color.rgb * (1.0 - metallic);

vec3 R = reflect(-V, N);

// accumulate color
vec3 light_accum = vec3(0.0);
for (int i = 0; i < int(NumLights.x) && i < MAX_LIGHTS; ++i) {
PointLight light = PointLights[i];

vec3 light_to_frag = light.pos.xyz - v_WorldPosition.xyz;
vec3 L = normalize(light_to_frag);
float distance_square = dot(light_to_frag, light_to_frag);

float rangeAttenuation =
getDistanceAttenuation(distance_square, light.inverseRangeSquared);

// Specular.
// Representative Point Area Lights.
// see http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf p14-16
float a = roughness;
float radius = light.color.a;
vec3 centerToRay = dot(light_to_frag, R) * R - light_to_frag;
vec3 closestPoint = light_to_frag + centerToRay * saturate(radius * inversesqrt(dot(centerToRay, centerToRay)));
float LspecLengthInverse = inversesqrt(dot(closestPoint, closestPoint));
float normalizationFactor = a / saturate(a + (radius * 0.5 * LspecLengthInverse));
float specularIntensity = normalizationFactor * normalizationFactor;

vec3 L = normalize(closestPoint * LspecLengthInverse);
vec3 H = normalize(L + V);
float NoL = saturate(dot(N, L));
float NoH = saturate(dot(N, H));
float LoH = saturate(dot(L, H));

vec3 specular = specular(F0, roughness, H, NdotV, NoL, NoH, LoH);
vec3 specular = specular(F0, roughness, H, NdotV, NoL, NoH, LoH, specularIntensity);

// Diffuse.
// Comes after specular since its NoL is used in the lighting equation.
L = normalize(light_to_frag);
H = normalize(L + V);
NoL = saturate(dot(N, L));
NoH = saturate(dot(N, H));
LoH = saturate(dot(L, H));

vec3 diffuse = diffuseColor * Fd_Burley(roughness, NdotV, NoL, LoH);

// Lout = f(v,l) Φ / { 4 π d^2 }⟨n⋅l⟩
Expand Down