normal factor

This commit is contained in:
Silas Bartha 2024-12-01 22:08:20 -05:00
parent 09d3766f74
commit f6e4b5d252
2 changed files with 10 additions and 3 deletions

View File

@ -11,12 +11,13 @@ struct BlackLight {
@group(2) @binding(0) var<storage> lights: array<BlackLight>; @group(2) @binding(0) var<storage> lights: array<BlackLight>;
@group(2) @binding(1) var base_texture: texture_2d<f32>; @group(2) @binding(1) var base_texture: texture_2d<f32>;
@group(2) @binding(2) var base_sampler: sampler; @group(2) @binding(2) var base_sampler: sampler;
@group(2) @binding(3) var<uniform> base_color: vec4<f32>;
@fragment @fragment
fn fragment( fn fragment(
in: VertexOutput, in: VertexOutput,
) -> @location(0) vec4<f32> { ) -> @location(0) vec4<f32> {
let base_color = textureSample(base_texture, base_sampler, in.uv); let base_texture_color = textureSample(base_texture, base_sampler, in.uv);
var final_color = vec4f(0.0, 0.0, 0.0, 0.0); var final_color = vec4f(0.0, 0.0, 0.0, 0.0);
for (var i = u32(0); i < arrayLength(&lights); i = i+1) { for (var i = u32(0); i < arrayLength(&lights); i = i+1) {
let light = lights[i]; let light = lights[i];
@ -26,12 +27,14 @@ fn fragment(
let angle_inner_factor = light.inner_angle / light.outer_angle; 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 angle_factor = linear_falloff_radius(light_to_fragment_angle / light.outer_angle, angle_inner_factor);
let normal_factor = linear_falloff_radius(1.0 - saturate(dot(in.world_normal, -light.direction)), 0.5);
let light_distance_squared = distance_squared(in.world_position.xyz, light.position); 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); 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); final_color = saturate(final_color + base_texture_color * angle_factor * distance_factor * normal_factor);
} }
return final_color; return base_color * final_color;
} }
fn distance_squared(a: vec3f, b: vec3f) -> f32 { fn distance_squared(a: vec3f, b: vec3f) -> f32 {

View File

@ -54,6 +54,9 @@ pub struct BlacklightMaterial {
#[texture(1)] #[texture(1)]
#[sampler(2)] #[sampler(2)]
pub base_texture: Option<Handle<Image>>, pub base_texture: Option<Handle<Image>>,
/// Base color of material, multiplies with texture.
#[uniform(3)]
pub base_color: LinearRgba,
/// Alpha mode for this material. /// Alpha mode for this material.
pub alpha_mode: AlphaMode, pub alpha_mode: AlphaMode,
} }
@ -63,6 +66,7 @@ impl Default for BlacklightMaterial {
Self { Self {
lights: vec![], lights: vec![],
base_texture: None, base_texture: None,
base_color: LinearRgba::WHITE,
alpha_mode: AlphaMode::Blend, alpha_mode: AlphaMode::Blend,
} }
} }