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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use rin_math::{Rotation2, vec2, vec3, Angle};
use rin_material::{
PbrMaterial as MutinyPbrMaterial, StandardMaterial, LambertMaterial, AnisotropicMaterial,
ClothMaterial, ClothSubsurfaceMaterial, SubsurfaceMaterial, AlphaType, ClearcoatMaterial
};
use rin_gl as gl;
use std::mem;
use super::{std140_data::Data, PropertyChanged, MaterialTransparency, ToGlinShaderPrecision};
pub trait PbrMaterial: super::Material + MutinyPbrMaterial{
fn pbr_data(&self) -> Data;
}
impl PbrMaterial for StandardMaterial{
fn pbr_data(&self) -> Data{
let mut data = Data::with_capacity(12 * mem::size_of::<f32>());
let mut pbr_data = data.start_struct();
pbr_data.push(*self.base_color);
pbr_data.push(*self.emissive_color);
pbr_data.push(*self.normal_scale);
pbr_data.push(*self.roughness);
pbr_data.push(*self.metallic);
pbr_data.push(*self.reflectance);
pbr_data.push(*self.reflectance_tint);
pbr_data.finish();
data
}
}
impl PbrMaterial for LambertMaterial{
fn pbr_data(&self) -> Data{
let mut data = Data::with_capacity(8 * mem::size_of::<f32>());
let mut pbr_data = data.start_struct();
pbr_data.push(*self.base_color);
pbr_data.push(*self.emissive_color);
pbr_data.push(*self.normal_scale);
pbr_data.finish();
data
}
}
impl PbrMaterial for AnisotropicMaterial{
fn pbr_data(&self) -> Data{
let mut data = Data::with_capacity(22 * mem::size_of::<f32>());
let ani_direction = Rotation2::new(self.anisotropic_rotation.value()) * vec2(1.0, 0.0);
let ani_direction = vec3!(ani_direction, 0.0);
let mut pbr_data = data.start_struct();
pbr_data.push(*self.base_color);
pbr_data.push(*self.emissive_color);
pbr_data.push(*self.normal_scale);
pbr_data.push(*self.roughness);
pbr_data.push(*self.metallic);
pbr_data.push(*self.reflectance);
pbr_data.push(*self.reflectance_tint);
pbr_data.push(ani_direction);
pbr_data.push(*self.anisotropy);
pbr_data.finish();
data
}
}
impl PbrMaterial for ClothMaterial{
fn pbr_data(&self) -> Data{
let mut data = Data::with_capacity(16 * mem::size_of::<f32>());
let mut pbr_data = data.start_struct();
pbr_data.push(*self.base_color);
pbr_data.push(*self.emissive_color);
pbr_data.push(*self.normal_scale);
pbr_data.push(*self.sheen_color);
pbr_data.push(*self.roughness);
pbr_data.finish();
data
}
}
impl PbrMaterial for ClothSubsurfaceMaterial{
fn pbr_data(&self) -> Data{
let mut data = Data::with_capacity(20 * mem::size_of::<f32>());
let mut pbr_data = data.start_struct();
pbr_data.push(*self.base_color);
pbr_data.push(*self.emissive_color);
pbr_data.push(*self.normal_scale);
pbr_data.push(*self.sheen_color);
pbr_data.push(*self.subsurface_color);
pbr_data.push(*self.roughness);
pbr_data.finish();
data
}
}
impl PbrMaterial for SubsurfaceMaterial{
fn pbr_data(&self) -> Data{
let mut data = Data::with_capacity(20 * mem::size_of::<f32>());
let mut pbr_data = data.start_struct();
pbr_data.push(*self.base_color);
pbr_data.push(*self.emissive_color);
pbr_data.push(*self.normal_scale);
pbr_data.push(*self.roughness);
pbr_data.push(*self.metallic);
pbr_data.push(*self.reflectance);
pbr_data.push(*self.reflectance_tint);
pbr_data.push(*self.subsurface_color);
pbr_data.push(*self.subsurface_power);
pbr_data.push(*self.thickness);
pbr_data.finish();
data
}
}
impl PbrMaterial for ClearcoatMaterial{
fn pbr_data(&self) -> Data{
let mut data = Data::with_capacity(16 * mem::size_of::<f32>());
let mut pbr_data = data.start_struct();
pbr_data.push(*self.base_color);
pbr_data.push(*self.emissive_color);
pbr_data.push(*self.normal_scale);
pbr_data.push(*self.roughness);
pbr_data.push(*self.metallic);
pbr_data.push(*self.reflectance);
pbr_data.push(*self.reflectance_tint);
pbr_data.push(*self.clearcoat);
pbr_data.push(*self.clearcoat_roughness);
pbr_data.finish();
data
}
}
impl<M: PbrMaterial> super::Material for M{
fn data(&self) -> PropertyChanged<Option<Data>>{
if self.any_data_changed() {
PropertyChanged::New(Some(self.pbr_data()))
}else{
PropertyChanged::Old
}
}
fn properties(&self) -> PropertyChanged<Vec<glin::Property>> {
if self.is_double_sided().has_changed() || self.alpha_ty().has_changed(){
let props = if **self.is_double_sided() {
vec![glin::Property::CullFace(None)]
}else{
vec![gl::Property::CullFace(Some(gl::BACK))]
};
PropertyChanged::New(props)
}else{
PropertyChanged::Old
}
}
fn transparency(&self) -> PropertyChanged<MaterialTransparency>{
if self.alpha_ty().has_changed()
|| self.base_color().has_changed()
|| self.override_translucent().has_changed()
{
let base_color_alpha = self.base_color_alpha();
PropertyChanged::New(MaterialTransparency{
alpha_type: **self.alpha_ty(),
is_translucent: if let Some(is_translucent) = **self.override_translucent(){
is_translucent
}else{
**self.alpha_ty() == AlphaType::Blending
&& base_color_alpha > 0.
&& base_color_alpha < 1.
},
is_visible: **self.alpha_ty() == AlphaType::None || base_color_alpha > 0.,
priority: None,
})
}else{
PropertyChanged::Old
}
}
fn shaders(&self) -> PropertyChanged<Vec<super::Shader>>{
PropertyChanged::Always(vec![
gl::vertex_shader!("shaders/pbr.vs.glsl"),
gl::fragment_shader!("shaders/pbr.fs.glsl"),
])
}
fn program_settings(&self) -> PropertyChanged<super::ProgramSettings>{
if self.any_texture_option_changed()
|| self.is_double_sided().has_changed()
|| self.alpha_ty().has_changed()
|| self.shader_precision().has_changed()
{
let has_metallic_roughness_map = (self.metallic_roughness_map().is_some() as u8).to_string();
let has_metallic_map = (self.metallic_map().is_some() as u8).to_string();
let has_roughness_map = (self.roughness_map().is_some() as u8).to_string();
let has_base_color_tex = (self.base_color_map().is_some() as u8).to_string();
let has_normal_map = (self.normal_map().is_some() as u8).to_string();
let has_emissive_map = (self.emissive_map().is_some() as u8).to_string();
let has_occlusion_map = (self.occlusion_map().is_some() as u8).to_string();
let has_anisotropy_map = (self.anisotropy_map().is_some() as u8).to_string();
let ty = self.pbr_material_type().to_string();
let alpha_type = self.alpha_ty().to_string();
let ibl_brdf = if self.pbr_material_type().is_cloth() {
"NEEDS_IBL_CLOTH_BRDF".to_string()
}else{
"NEEDS_IBL_BRDF".to_string()
};
let is_double_sided = (**self.is_double_sided() as u8).to_string();
let has_separate_alpha = "0".to_string();
#[cfg(image_ty="freeimage")]
let inverted_ibl_lut = "1".to_string();
#[cfg(image_ty="image")]
let inverted_ibl_lut = "1".to_string();
#[cfg(not(image_enabled))]
{
log::error!("no support for image loading, can't load brdf lut for pbr materials. Add one of \"image\" or \"freeimage\" features to your Cargo.toml");
panic!();
}
let debug_texcoords = (*self.debug_texcoords().get() as u8).to_string();
let debug_normals = (*self.debug_normals().get() as u8).to_string();
if *self.debug_texcoords().get() && *self.debug_normals().get() {
panic!("Debug normals and debug texcoords can't be true simulataneously")
}
let settings = super::ProgramSettings{
defines: vec![
("HAS_TEXTURE".to_owned(), has_base_color_tex),
("HAS_NORMALMAP".to_owned(), has_normal_map),
("HAS_METALROUGHNESSMAP".to_owned(), has_metallic_roughness_map),
("HAS_METALLICMAP".to_owned(), has_metallic_map),
("HAS_ROUGHNESSMAP".to_owned(), has_roughness_map),
("HAS_EMISSIVEMAP".to_owned(), has_emissive_map),
("HAS_ANISOTROPYMAP".to_owned(), has_anisotropy_map),
("HAS_OCCLUSIONMAP".to_owned(), has_occlusion_map),
("HAS_SEPARATE_ALPHA".to_owned(), has_separate_alpha),
("DOUBLE_SIDED".to_owned(), is_double_sided),
("MANUAL_SRGB".to_owned(), "1".to_owned()),
("SRGB_FAST_APPROXIMATION".to_owned(), "1".to_owned()),
("MATERIAL_TYPE".to_owned(), ty),
("ALPHA_TYPE".to_owned(), alpha_type),
("TARGET_MOBILE".to_owned(), "0".to_owned()),
#[cfg(image_enabled)]
("INVERTED_IBL_BRDF_LUT".to_owned(), inverted_ibl_lut),
#[cfg(image_enabled)]
(ibl_brdf, "1".to_owned()),
("DEBUG_TEXCOORDS".to_owned(), debug_texcoords),
("DEBUG_NORMALS".to_owned(), debug_normals),
],
includes_replace: vec![
("material_uniforms.glsl".to_string(), include_str!("shaders/material_uniforms.glsl").to_owned()),
],
precision: self.shader_precision().to_glin(),
.. Default::default()
};
PropertyChanged::New(settings)
}else{
PropertyChanged::Old
}
}
}