diff options
author | Silas Bartha <[email protected]> | 2024-11-30 08:06:29 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2024-11-30 08:06:29 +0000 |
commit | 09d3766f7404d5ef50fb022e9e70d700073033c2 (patch) | |
tree | ffafe3e5919e5b3e514f13ae2a102c05234ce767 /src | |
parent | afa8cad4cf778673633f95885f5da095ba96039f (diff) | |
parent | 4348c2a9c5beb83ed8610e7fd2a7393a7ae1eec0 (diff) |
Merge pull request #1 from exvacuum/wip
Wip
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 39 |
1 files changed, 31 insertions, 8 deletions
@@ -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::<BlacklightMaterial>::default()).add_systems(Update, update_shader_blacklight_data); + app.add_plugins(MaterialPlugin::<BlacklightMaterial>::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, - pub color: Vec4, + /// Range of this light (see [`SpotLight`]). pub range: f32, - pub radius: 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<BlacklightData>, + /// Base color texture which is revealed by blacklight exposure. #[texture(1)] #[sampler(2)] pub base_texture: Option<Handle<Image>>, + /// Alpha mode for this material. pub alpha_mode: AlphaMode, } @@ -56,19 +79,19 @@ impl Material for BlacklightMaterial { } fn update_shader_blacklight_data( - blacklight_query: Query<(&ViewVisibility, &GlobalTransform, &Transform, &SpotLight), With<Blacklight>>, + blacklight_query: Query<(&ViewVisibility, &GlobalTransform, &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 { + .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::<Vec<_>>(); for handle in blacklight_material_query.iter() { |