Skip to main content

bevy_shaders/
led.rs

1use crate::color::{bevy_color_into_vec4, MY_BLUE_COLOR};
2use bevy::prelude::*;
3use bevy::{
4    reflect::TypePath,
5    render::render_resource::{AsBindGroup, ShaderRef},
6};
7
8/// Static led material.
9///
10/// It lets you add emission to material or make it like transparent like glass.
11///
12/// # Examples:
13///
14/// For the effect from the examples:
15///
16/// ```rust
17/// LEDMaterial::default();
18/// ```
19///
20/// You can specify your own color and emission intensity of cource:
21///
22/// ```rust
23/// LEDMaterial::new(Color::WHITE).with_emission(3.0);
24/// ```
25#[derive(Asset, TypePath, AsBindGroup, Clone)]
26pub struct LEDMaterial {
27    #[uniform(0)]
28    color: Vec4,
29}
30
31impl LEDMaterial {
32    pub fn new(color: Color) -> Self {
33        Self {
34            color: bevy_color_into_vec4(color),
35        }
36    }
37
38    pub fn with_emission(mut self, intensity: f32) -> Self {
39        let rgb = self.color.xyz() * intensity;
40        self.color.x = rgb.x;
41        self.color.y = rgb.y;
42        self.color.z = rgb.z;
43        self
44    }
45}
46
47impl Default for LEDMaterial {
48    fn default() -> Self {
49        Self::new(MY_BLUE_COLOR).with_emission(3.0)
50    }
51}
52
53impl Material for LEDMaterial {
54    fn fragment_shader() -> ShaderRef {
55        "shaders/led.wgsl".into()
56    }
57
58    fn alpha_mode(&self) -> AlphaMode {
59        AlphaMode::Blend
60    }
61}