gizmo_renderer/components/
material.rs1use std::sync::Arc;
2
3#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
4pub enum MaterialType {
5 Pbr,
6 Unlit,
7 Skybox,
8 Water,
9 Grid,
10}
11
12#[derive(Clone)]
13pub struct Material {
14 pub bind_group: Arc<wgpu::BindGroup>,
15 pub albedo: gizmo_math::Vec4,
16 pub roughness: f32,
17 pub metallic: f32,
18 pub anisotropy: f32,
19 pub clear_coat: f32,
20 pub subsurface: f32,
21 pub texture_source: Option<String>,
22 pub material_type: MaterialType,
23 pub is_transparent: bool,
24 pub is_double_sided: bool,
25}
26
27impl Material {
28 pub fn new(bind_group: Arc<wgpu::BindGroup>) -> Self {
29 Self {
30 bind_group,
31 albedo: gizmo_math::Vec4::new(1.0, 1.0, 1.0, 1.0),
32 roughness: 0.5,
33 metallic: 0.0,
34 anisotropy: 0.0,
35 clear_coat: 0.0,
36 subsurface: 0.0,
37 texture_source: None,
38 material_type: MaterialType::Pbr,
39 is_transparent: false,
40 is_double_sided: false,
41 }
42 }
43
44 pub fn with_pbr(mut self, albedo: gizmo_math::Vec4, roughness: f32, metallic: f32) -> Self {
48 self.albedo = albedo;
49 self.roughness = roughness.clamp(0.0, 1.0);
50 self.metallic = metallic.clamp(0.0, 1.0);
51 self.material_type = MaterialType::Pbr;
52 if albedo.w < 1.0 {
53 self.is_transparent = true;
54 }
55 self
56 }
57
58 pub fn with_anisotropy(mut self, anisotropy: f32) -> Self {
59 self.anisotropy = anisotropy.clamp(0.0, 1.0);
60 self
61 }
62
63 pub fn with_clear_coat(mut self, clear_coat: f32) -> Self {
64 self.clear_coat = clear_coat.clamp(0.0, 1.0);
65 self
66 }
67
68 pub fn with_subsurface(mut self, subsurface: f32) -> Self {
69 self.subsurface = subsurface.clamp(0.0, 1.0);
70 self
71 }
72
73 pub fn with_transparent(mut self, transparent: bool) -> Self {
77 self.is_transparent = transparent;
78 self
79 }
80
81 pub fn with_double_sided(mut self, double_sided: bool) -> Self {
82 self.is_double_sided = double_sided;
83 self
84 }
85
86 pub fn with_unlit(mut self, albedo: gizmo_math::Vec4) -> Self {
89 self.albedo = albedo;
90 self.material_type = MaterialType::Unlit;
91 if albedo.w < 1.0 {
92 self.is_transparent = true;
93 }
94 self
95 }
96
97 pub fn with_skybox(mut self) -> Self {
98 self.material_type = MaterialType::Skybox;
99 self
100 }
101
102 pub fn with_water(mut self, base_albedo: gizmo_math::Vec4) -> Self {
106 self.albedo = base_albedo;
107 self.roughness = 0.05;
108 self.metallic = 0.0;
109 self.material_type = MaterialType::Water;
110 if base_albedo.w < 1.0 {
111 self.is_transparent = true;
112 }
113 self
114 }
115
116 pub fn with_texture_source(mut self, path: String) -> Self {
117 self.texture_source = Some(path);
118 self
119 }
120}