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
use super::Descriptor;
use super::ShaderMaterial;
use super::Uniforms;
use crate::buffer::Buffer;
use crate::buffer::BufferUsage;
use crate::color::Rgbf;
use crate::device::Device;
use crate::error::Result;
use crate::image::Canvas;
use crate::image::Texture;
use crate::math::Vec4;
use crate::resources::Handle;
pub struct Material {
pub a: Vec4,
pub b: Vec4,
pub c: Vec4,
pub d: Vec4,
pub e: Vec4,
pub f: Vec4,
pub g: Vec4,
pub h: Vec4,
pub textures: Vec<Handle<Texture>>,
descriptor: Descriptor,
buffer: Buffer<ShaderMaterial>,
}
impl Material {
pub(crate) fn new(device: &Device, uniforms: &mut Uniforms) -> Result<Self> {
let buffer = Buffer::dynamic(device, BufferUsage::Uniform, 1);
let descriptor = uniforms.material_set(device, &buffer)?;
Ok(Self {
a: Vec4::default(),
b: Vec4::default(),
c: Vec4::default(),
d: Vec4::default(),
e: Vec4::default(),
f: Vec4::default(),
g: Vec4::default(),
h: Vec4::default(),
textures: vec![],
buffer,
descriptor,
})
}
pub fn albedo_color(&mut self, color: impl Into<Rgbf>) {
let temp = self.a[3];
self.a = color.into().into();
self.a[3] = temp;
}
pub fn albedo_texture(&mut self, texture: Handle<Texture>) {
self.a[3] = texture.read().shader_index() as f32;
self.textures.push(texture);
}
pub fn albedo_canvas(&mut self, f: &Handle<Canvas>) {
self.a[3] = f.read().shader_index() as f32;
}
pub fn metalness(&mut self, value: f32) {
self.b[0] = value;
}
pub fn roughness(&mut self, value: f32) {
self.b[1] = value;
}
pub fn emissive(&mut self, color: impl Into<Rgbf>) {
let temp = self.d[3];
self.d = color.into().into();
self.d[3] = temp;
}
pub fn metalness_roughness_texture(&mut self, texture: Handle<Texture>) {
self.b[2] = texture.read().shader_index() as f32;
self.textures.push(texture);
}
pub fn ambient_occlusion_texture(&mut self, texture: Handle<Texture>) {
self.b[3] = texture.read().shader_index() as f32;
self.textures.push(texture);
}
pub fn normal_texture(&mut self, texture: Handle<Texture>) {
self.c[0] = texture.read().shader_index() as f32;
self.textures.push(texture);
}
pub fn emissive_texture(&mut self, texture: Handle<Texture>) {
self.c[1] = texture.read().shader_index() as f32;
self.textures.push(texture);
}
pub(crate) fn update(&mut self) {
self.buffer.copy_from_data(&[ShaderMaterial {
a: self.a,
b: self.b,
c: self.c,
d: self.d,
e: self.e,
f: self.f,
g: self.g,
h: self.h,
}]);
}
pub(crate) const fn descriptor(&self) -> Descriptor {
self.descriptor
}
pub(crate) fn destroy(&self, device: &Device) {
self.buffer.destroy(device);
}
}
impl PartialEq for Material {
fn eq(&self, other: &Self) -> bool {
self.buffer == other.buffer
}
}