diff options
author | Silas Bartha <[email protected]> | 2024-11-24 18:20:03 -0500 |
---|---|---|
committer | Silas Bartha <[email protected]> | 2024-11-24 18:20:03 -0500 |
commit | afa8cad4cf778673633f95885f5da095ba96039f (patch) | |
tree | 504bd03f521e6ff168a90822bfe7c2ffe85fba13 |
Initial Commit
-rw-r--r-- | Cargo.toml | 8 | ||||
-rw-r--r-- | assets/shaders/blacklight_material.wgsl | 45 | ||||
-rw-r--r-- | src/lib.rs | 78 |
3 files changed, 131 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..511f890 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "bevy_blacklight_material" +version = "0.1.0" +edition = "2021" + +[dependencies.bevy] +version = "0.14" +features = ["bevy_render", "bevy_asset"] diff --git a/assets/shaders/blacklight_material.wgsl b/assets/shaders/blacklight_material.wgsl new file mode 100644 index 0000000..8807404 --- /dev/null +++ b/assets/shaders/blacklight_material.wgsl @@ -0,0 +1,45 @@ +#import bevy_pbr::forward_io::VertexOutput; + +struct BlackLight { + position: vec3<f32>, + direction: vec3<f32>, + color: vec4<f32>, + range: f32, + radius: f32, +} + +@group(2) @binding(0) var<storage> lights: array<BlackLight>; +@group(2) @binding(1) var base_texture: texture_2d<f32>; +@group(2) @binding(2) var base_sampler: sampler; + +@fragment +fn fragment( + in: VertexOutput, +) -> @location(0) vec4<f32> { + let base_color = textureSample(base_texture, base_sampler, in.uv); + 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_arccosine = abs(acos(dot(normalize(light.direction), normalize(in.world_position.xyz - light.position)))) * radians(180.0); + final_color = saturate(final_color + base_color * (inverse_falloff_radius(light_distance_squared / (light.range * light.range), 0.5) * inverse_falloff_radius(light_arccosine, 0.9))); + } + return final_color; +} + +fn distance_squared(a: vec3f, b: vec3f) -> f32 { + return pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0) + pow(a.z - b.z, 2.0); +} + +fn inverse_falloff(factor: f32) -> f32 { + let squared = factor * factor; + return (1.0 - squared) / (10 * squared + 1.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 new file mode 100644 index 0000000..65dfc80 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,78 @@ +use bevy::{ + asset::embedded_asset, + prelude::*, + render::render_resource::{AsBindGroup, ShaderType}, +}; + +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::<BlacklightMaterial>::default()).add_systems(Update, update_shader_blacklight_data); + } +} + +#[derive(Component, Debug)] +pub struct Blacklight; + +#[derive(Clone, Debug, ShaderType)] +pub struct BlacklightData { + pub position: Vec3, + pub direction: Vec3, + pub color: Vec4, + pub range: f32, + pub radius: f32, +} + +#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] +pub struct BlacklightMaterial { + #[storage(0, read_only)] + pub lights: Vec<BlacklightData>, + #[texture(1)] + #[sampler(2)] + pub base_texture: Option<Handle<Image>>, + pub alpha_mode: AlphaMode, +} + +impl Default for BlacklightMaterial { + fn default() -> Self { + Self { + lights: vec![], + base_texture: None, + alpha_mode: AlphaMode::Blend, + } + } +} + +impl Material for BlacklightMaterial { + fn fragment_shader() -> bevy::render::render_resource::ShaderRef { + "embedded://bevy_blacklight_material/../assets/shaders/blacklight_material.wgsl".into() + } + + fn alpha_mode(&self) -> AlphaMode { + self.alpha_mode + } +} + +fn update_shader_blacklight_data( + blacklight_query: Query<(&ViewVisibility, &GlobalTransform, &Transform, &SpotLight), With<Blacklight>>, + blacklight_material_query: Query<&Handle<BlacklightMaterial>>, + mut blacklight_materials: ResMut<Assets<BlacklightMaterial>>, +) { + let light_data = blacklight_query + .iter() + .filter(|(visibility, _, _, _)| visibility.get()) + .map(|(_, global_transform, transform, light)| BlacklightData { + position: global_transform.translation(), + direction: *global_transform.forward(), + color: light.color.to_srgba().to_vec4(), + range: light.range, + radius: light.radius, + }) + .collect::<Vec<_>>(); + for handle in blacklight_material_query.iter() { + let material = blacklight_materials.get_mut(handle).unwrap(); + material.lights = light_data.clone(); + } +} |