-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmod.rs
More file actions
173 lines (155 loc) · 5.31 KB
/
mod.rs
File metadata and controls
173 lines (155 loc) · 5.31 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
pub mod custom;
pub mod pbr;
use crate::render::material::UntypedMaterial;
use bevy::material::descriptor::RenderPipelineDescriptor;
use bevy::material::specialize::SpecializedMeshPipelineError;
use bevy::mesh::MeshVertexBufferLayoutRef;
use bevy::pbr::{
ExtendedMaterial, MaterialExtension, MaterialExtensionKey, MaterialExtensionPipeline,
};
use bevy::prelude::*;
use bevy::render::render_resource::{AsBindGroup, BlendState};
use bevy::shader::ShaderRef;
use processing_core::error::{self, ProcessingError};
pub struct ProcessingMaterialPlugin;
impl Plugin for ProcessingMaterialPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(bevy::pbr::MaterialPlugin::<
ExtendedMaterial<StandardMaterial, ProcessingMaterial>,
>::default());
let world = app.world_mut();
let handle = world
.resource_mut::<Assets<StandardMaterial>>()
.add(StandardMaterial {
unlit: true,
cull_mode: None,
base_color: Color::WHITE,
..default()
});
let entity = world.spawn(UntypedMaterial(handle.untyped())).id();
world.insert_resource(DefaultMaterial(entity));
}
}
#[derive(Resource)]
pub struct DefaultMaterial(pub Entity);
#[derive(Debug, Clone)]
pub enum MaterialValue {
Float(f32),
Float2([f32; 2]),
Float3([f32; 3]),
Float4([f32; 4]),
Int(i32),
Int2([i32; 2]),
Int3([i32; 3]),
Int4([i32; 4]),
UInt(u32),
Mat4([f32; 16]),
Texture(Entity),
}
pub fn create_pbr(
mut commands: Commands,
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ProcessingMaterial>>>,
) -> Entity {
let handle = materials.add(ExtendedMaterial {
base: StandardMaterial {
unlit: false,
cull_mode: None,
..default()
},
extension: ProcessingMaterial { blend_state: None },
});
commands.spawn(UntypedMaterial(handle.untyped())).id()
}
pub fn set_property(
In((entity, name, value)): In<(Entity, String, MaterialValue)>,
material_handles: Query<&UntypedMaterial>,
mut extended_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ProcessingMaterial>>>,
mut custom_materials: ResMut<Assets<custom::CustomMaterial>>,
) -> error::Result<()> {
let untyped = material_handles
.get(entity)
.map_err(|_| ProcessingError::MaterialNotFound)?;
if let Ok(handle) = untyped
.0
.clone()
.try_typed::<ExtendedMaterial<StandardMaterial, ProcessingMaterial>>()
{
let mut extended = extended_materials
.get_mut(&handle)
.ok_or(ProcessingError::MaterialNotFound)?;
return pbr::set_property(&mut extended.base, &name, &value);
}
if let Ok(handle) = untyped.0.clone().try_typed::<custom::CustomMaterial>() {
let mut mat = custom_materials
.get_mut(&handle)
.ok_or(ProcessingError::MaterialNotFound)?;
return custom::set_property(&mut mat, &name, &value);
}
Err(ProcessingError::MaterialNotFound)
}
pub fn destroy(
In(entity): In<Entity>,
mut commands: Commands,
material_handles: Query<&UntypedMaterial>,
mut extended_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ProcessingMaterial>>>,
mut custom_materials: ResMut<Assets<custom::CustomMaterial>>,
) -> error::Result<()> {
let untyped = material_handles
.get(entity)
.map_err(|_| ProcessingError::MaterialNotFound)?;
if let Ok(handle) = untyped
.0
.clone()
.try_typed::<ExtendedMaterial<StandardMaterial, ProcessingMaterial>>()
{
extended_materials.remove(&handle);
}
if let Ok(handle) = untyped.0.clone().try_typed::<custom::CustomMaterial>() {
custom_materials.remove(&handle);
}
commands.entity(entity).despawn();
Ok(())
}
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone, Default)]
#[bind_group_data(ProcessingMaterialKey)]
pub struct ProcessingMaterial {
pub blend_state: Option<BlendState>,
}
#[repr(C)]
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct ProcessingMaterialKey {
blend_state: Option<BlendState>,
}
impl From<&ProcessingMaterial> for ProcessingMaterialKey {
fn from(mat: &ProcessingMaterial) -> Self {
ProcessingMaterialKey {
blend_state: mat.blend_state,
}
}
}
impl MaterialExtension for ProcessingMaterial {
fn vertex_shader() -> ShaderRef {
<StandardMaterial as Material>::vertex_shader()
}
fn fragment_shader() -> ShaderRef {
<StandardMaterial as Material>::fragment_shader()
}
fn specialize(
_pipeline: &MaterialExtensionPipeline,
descriptor: &mut RenderPipelineDescriptor,
_layout: &MeshVertexBufferLayoutRef,
key: MaterialExtensionKey<Self>,
) -> std::result::Result<(), SpecializedMeshPipelineError> {
if let Some(blend_state) = key.bind_group_data.blend_state {
// this should never be null but we have to check it anyway
if let Some(fragment_state) = &mut descriptor.fragment {
fragment_state.targets.iter_mut().for_each(|target| {
if let Some(target) = target {
target.blend = Some(blend_state);
}
});
}
}
Ok(())
}
}