aboutsummaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-11-30 03:01:27 -0500
committerLibravatar Silas Bartha <[email protected]>2024-11-30 03:01:27 -0500
commit4348c2a9c5beb83ed8610e7fd2a7393a7ae1eec0 (patch)
treeffafe3e5919e5b3e514f13ae2a102c05234ce767 /assets
parent5dfd34d5b0676d329e6116f810e287b42ab641b4 (diff)
Sat Nov 30 03:01:27 AM EST 2024
Diffstat (limited to 'assets')
-rw-r--r--assets/shaders/blacklight_material.wgsl27
1 files changed, 21 insertions, 6 deletions
diff --git a/assets/shaders/blacklight_material.wgsl b/assets/shaders/blacklight_material.wgsl
index 93e6969..d024755 100644
--- a/assets/shaders/blacklight_material.wgsl
+++ b/assets/shaders/blacklight_material.wgsl
@@ -20,13 +20,16 @@ fn fragment(
var final_color = vec4f(0.0, 0.0, 0.0, 0.0);
for (var i = u32(0); i < arrayLength(&lights); i = i+1) {
let light = lights[i];
- let light_distance_squared = distance_squared(in.world_position.xyz, light.position);
+
let light_to_fragment_direction = normalize(in.world_position.xyz - light.position);
let light_to_fragment_angle = acos(dot(light.direction, light_to_fragment_direction));
- let angle_inner_factor = light.inner_angle/light.outer_angle;
- let angle_factor = falloff_radius(light_to_fragment_angle / light.outer_angle, angle_inner_factor);
- let distance_factor = falloff_radius(saturate(light_distance_squared / (light.range * light.range)), 0.5);
- final_color = saturate(final_color + base_color * (angle_factor * distance_factor));
+ let angle_inner_factor = light.inner_angle / light.outer_angle;
+ let angle_factor = linear_falloff_radius(light_to_fragment_angle / light.outer_angle, angle_inner_factor);
+
+ let light_distance_squared = distance_squared(in.world_position.xyz, light.position);
+ let distance_factor = inverse_falloff_radius(saturate(light_distance_squared / (light.range * light.range)), 0.5);
+
+ final_color = saturate(final_color + base_color * angle_factor * distance_factor);
}
return final_color;
}
@@ -36,10 +39,22 @@ fn distance_squared(a: vec3f, b: vec3f) -> f32 {
return dot(vec, vec);
}
-fn falloff_radius(factor: f32, radius: f32) -> f32 {
+fn linear_falloff_radius(factor: f32, radius: f32) -> f32 {
if factor < radius {
return 1.0;
} else {
return 1.0 - (factor - radius) / (1.0 - radius);
}
}
+
+fn inverse_falloff(factor: f32) -> f32 {
+ return pow(1.0 - factor, 2.0);
+}
+
+fn inverse_falloff_radius(factor: f32, radius: f32) -> f32 {
+ if factor < radius {
+ return 1.0;
+ } else {
+ return inverse_falloff((factor - radius) / (1.0 - radius));
+ }
+}