From 8b5a8ce1efeedb4333e8f10364f6b916bd10c5d9 Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Tue, 26 Nov 2024 16:32:19 -0500 Subject: Tue Nov 26 04:32:19 PM EST 2024 --- src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 65dfc80..282c9b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,9 +20,9 @@ pub struct Blacklight; pub struct BlacklightData { pub position: Vec3, pub direction: Vec3, - pub color: Vec4, pub range: f32, - pub radius: f32, + pub inner_angle: f32, + pub outer_angle: f32, } #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] @@ -56,19 +56,19 @@ impl Material for BlacklightMaterial { } fn update_shader_blacklight_data( - blacklight_query: Query<(&ViewVisibility, &GlobalTransform, &Transform, &SpotLight), With>, + blacklight_query: Query<(&ViewVisibility, &GlobalTransform, &SpotLight), With>, blacklight_material_query: Query<&Handle>, mut blacklight_materials: ResMut>, ) { let light_data = blacklight_query .iter() - .filter(|(visibility, _, _, _)| visibility.get()) - .map(|(_, global_transform, transform, light)| BlacklightData { + .filter(|(visibility, _, _)| visibility.get()) + .map(|(_, global_transform, light)| BlacklightData { position: global_transform.translation(), direction: *global_transform.forward(), - color: light.color.to_srgba().to_vec4(), range: light.range, - radius: light.radius, + inner_angle: light.inner_angle, + outer_angle: light.outer_angle, }) .collect::>(); for handle in blacklight_material_query.iter() { -- cgit v1.2.3 From 4348c2a9c5beb83ed8610e7fd2a7393a7ae1eec0 Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Sat, 30 Nov 2024 03:01:27 -0500 Subject: Sat Nov 30 03:01:27 AM EST 2024 --- assets/shaders/blacklight_material.wgsl | 27 +++++++++++++++++++++------ src/lib.rs | 25 ++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 7 deletions(-) (limited to 'src/lib.rs') 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)); + } +} diff --git a/src/lib.rs b/src/lib.rs index 282c9b0..2ff9974 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,37 +1,60 @@ +#![warn(missing_docs)] + +//! A plugin for the bevy engine providing a simple [`BlacklightMaterial`], which reveals a base +//! color based on light data from spot lights tagged with a [`Blacklight`] component. +//! +//! Possible future features: +//! - [`StandardMaterial`] extension for a "blacklight mapped" material. +//! - Point light support. + use bevy::{ asset::embedded_asset, prelude::*, render::render_resource::{AsBindGroup, ShaderType}, }; +/// Plugin which enables and updates blacklight shaders. pub struct BlacklightPlugin; impl Plugin for BlacklightPlugin { fn build(&self, app: &mut App) { embedded_asset!(app, "../assets/shaders/blacklight_material.wgsl"); - app.add_plugins(MaterialPlugin::::default()).add_systems(Update, update_shader_blacklight_data); + app.add_plugins(MaterialPlugin::::default()) + .add_systems(Update, update_shader_blacklight_data); } } +/// Marker component for spot lights to use them as blacklights for [`BlacklightMaterial`]. #[derive(Component, Debug)] pub struct Blacklight; +/// Shader data representing a single blacklight point light. #[derive(Clone, Debug, ShaderType)] pub struct BlacklightData { + /// World-space position of this light. pub position: Vec3, + /// World-space direction of this light (must be normalized). pub direction: Vec3, + /// Range of this light (see [`SpotLight`]). pub range: f32, + /// Inner angle of this light (see [`SpotLight`]). pub inner_angle: f32, + /// Outer angle of this light (see [`SpotLight`]). pub outer_angle: f32, } +/// Material which is invisible until exposed to light from a spot light tagged with the +/// [`Blacklight`] component. #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] pub struct BlacklightMaterial { + /// List of light data processed by this shader #[storage(0, read_only)] pub lights: Vec, + /// Base color texture which is revealed by blacklight exposure. #[texture(1)] #[sampler(2)] pub base_texture: Option>, + /// Alpha mode for this material. pub alpha_mode: AlphaMode, } -- cgit v1.2.3