1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, AddAsset, Assets, Handle, HandleUntyped};
use bevy_math::Vec4;
use bevy_reflect::{prelude::*, TypeUuid};
use bevy_render::{
color::Color, prelude::Shader, render_asset::RenderAssets, render_resource::*, texture::Image,
};
use crate::{Material2d, Material2dPlugin, MaterialMesh2dBundle};
pub const COLOR_MATERIAL_SHADER_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 3253086872234592509);
#[derive(Default)]
pub struct ColorMaterialPlugin;
impl Plugin for ColorMaterialPlugin {
fn build(&self, app: &mut App) {
load_internal_asset!(
app,
COLOR_MATERIAL_SHADER_HANDLE,
"color_material.wgsl",
Shader::from_wgsl
);
app.add_plugin(Material2dPlugin::<ColorMaterial>::default())
.register_asset_reflect::<ColorMaterial>();
app.world
.resource_mut::<Assets<ColorMaterial>>()
.set_untracked(
Handle::<ColorMaterial>::default(),
ColorMaterial {
color: Color::rgb(1.0, 0.0, 1.0),
..Default::default()
},
);
}
}
#[derive(AsBindGroup, Reflect, FromReflect, Debug, Clone, TypeUuid)]
#[reflect(Default, Debug)]
#[uuid = "e228a544-e3ca-4e1e-bb9d-4d8bc1ad8c19"]
#[uniform(0, ColorMaterialUniform)]
pub struct ColorMaterial {
pub color: Color,
#[texture(1)]
#[sampler(2)]
pub texture: Option<Handle<Image>>,
}
impl Default for ColorMaterial {
fn default() -> Self {
ColorMaterial {
color: Color::WHITE,
texture: None,
}
}
}
impl From<Color> for ColorMaterial {
fn from(color: Color) -> Self {
ColorMaterial {
color,
..Default::default()
}
}
}
impl From<Handle<Image>> for ColorMaterial {
fn from(texture: Handle<Image>) -> Self {
ColorMaterial {
texture: Some(texture),
..Default::default()
}
}
}
bitflags::bitflags! {
#[repr(transparent)]
pub struct ColorMaterialFlags: u32 {
const TEXTURE = (1 << 0);
const NONE = 0;
const UNINITIALIZED = 0xFFFF;
}
}
#[derive(Clone, Default, ShaderType)]
pub struct ColorMaterialUniform {
pub color: Vec4,
pub flags: u32,
}
impl AsBindGroupShaderType<ColorMaterialUniform> for ColorMaterial {
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> ColorMaterialUniform {
let mut flags = ColorMaterialFlags::NONE;
if self.texture.is_some() {
flags |= ColorMaterialFlags::TEXTURE;
}
ColorMaterialUniform {
color: self.color.as_linear_rgba_f32().into(),
flags: flags.bits(),
}
}
}
impl Material2d for ColorMaterial {
fn fragment_shader() -> ShaderRef {
COLOR_MATERIAL_SHADER_HANDLE.typed().into()
}
}
pub type ColorMesh2dBundle = MaterialMesh2dBundle<ColorMaterial>;