forked from processing/libprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
90 lines (81 loc) · 2.4 KB
/
mod.rs
File metadata and controls
90 lines (81 loc) · 2.4 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
pub mod pbr;
use bevy::prelude::*;
use crate::error::{ProcessingError, Result};
use crate::render::material::UntypedMaterial;
pub struct MaterialPlugin;
impl Plugin for MaterialPlugin {
fn build(&self, app: &mut App) {
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<StandardMaterial>>,
) -> Entity {
let handle = materials.add(StandardMaterial {
unlit: false,
cull_mode: None,
..default()
});
commands.spawn(UntypedMaterial(handle.untyped())).id()
}
pub fn set_property(
In((entity, name, value)): In<(Entity, String, MaterialValue)>,
material_handles: Query<&UntypedMaterial>,
mut standard_materials: ResMut<Assets<StandardMaterial>>,
) -> Result<()> {
let untyped = material_handles
.get(entity)
.map_err(|_| ProcessingError::MaterialNotFound)?;
let handle = untyped
.0
.clone()
.try_typed::<StandardMaterial>()
.map_err(|_| ProcessingError::MaterialNotFound)?;
let mut standard = standard_materials
.get_mut(&handle)
.ok_or(ProcessingError::MaterialNotFound)?;
pbr::set_property(&mut standard, &name, &value)?;
Ok(())
}
pub fn destroy(
In(entity): In<Entity>,
mut commands: Commands,
material_handles: Query<&UntypedMaterial>,
mut standard_materials: ResMut<Assets<StandardMaterial>>,
) -> Result<()> {
let untyped = material_handles
.get(entity)
.map_err(|_| ProcessingError::MaterialNotFound)?;
if let Ok(handle) = untyped.0.clone().try_typed::<StandardMaterial>() {
standard_materials.remove(&handle);
}
commands.entity(entity).despawn();
Ok(())
}