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
use crate::core::*;
use crate::renderer::*;
use std::rc::Rc;
#[derive(Clone)]
pub struct DeferredPhysicalMaterial {
pub name: String,
pub albedo: Color,
pub albedo_texture: Option<Rc<Texture2D<u8>>>,
pub metallic: f32,
pub roughness: f32,
pub metallic_roughness_texture: Option<Rc<Texture2D<u8>>>,
pub occlusion_strength: f32,
pub occlusion_texture: Option<Rc<Texture2D<u8>>>,
pub normal_scale: f32,
pub normal_texture: Option<Rc<Texture2D<u8>>>,
pub render_states: RenderStates,
pub alpha_cutout: Option<f32>,
}
impl DeferredPhysicalMaterial {
pub fn new(context: &Context, cpu_material: &CPUMaterial) -> ThreeDResult<Self> {
let albedo_texture = if let Some(ref cpu_texture) = cpu_material.albedo_texture {
Some(Rc::new(Texture2D::new(&context, cpu_texture)?))
} else {
None
};
let metallic_roughness_texture =
if let Some(ref cpu_texture) = cpu_material.occlusion_metallic_roughness_texture {
Some(Rc::new(Texture2D::new(&context, cpu_texture)?))
} else {
if let Some(ref cpu_texture) = cpu_material.metallic_roughness_texture {
Some(Rc::new(Texture2D::new(&context, cpu_texture)?))
} else {
None
}
};
let occlusion_texture = if cpu_material.occlusion_metallic_roughness_texture.is_some() {
metallic_roughness_texture.clone()
} else {
if let Some(ref cpu_texture) = cpu_material.occlusion_texture {
Some(Rc::new(Texture2D::new(&context, cpu_texture)?))
} else {
None
}
};
let normal_texture = if let Some(ref cpu_texture) = cpu_material.normal_texture {
Some(Rc::new(Texture2D::new(&context, cpu_texture)?))
} else {
None
};
Ok(Self {
name: cpu_material.name.clone(),
albedo: cpu_material.albedo,
albedo_texture,
metallic: cpu_material.metallic,
roughness: cpu_material.roughness,
metallic_roughness_texture,
normal_texture,
normal_scale: cpu_material.normal_scale,
occlusion_texture,
occlusion_strength: cpu_material.occlusion_strength,
render_states: RenderStates::default(),
alpha_cutout: cpu_material.alpha_cutout,
})
}
pub fn from_physical_material(physical_material: &PhysicalMaterial) -> Self {
Self {
name: physical_material.name.clone(),
albedo: physical_material.albedo,
albedo_texture: physical_material.albedo_texture.clone(),
metallic: physical_material.metallic,
roughness: physical_material.roughness,
metallic_roughness_texture: physical_material.metallic_roughness_texture.clone(),
normal_texture: physical_material.normal_texture.clone(),
normal_scale: physical_material.normal_scale,
occlusion_texture: physical_material.occlusion_texture.clone(),
occlusion_strength: physical_material.occlusion_strength,
render_states: physical_material.opaque_render_states,
alpha_cutout: None,
}
}
}
impl Material for DeferredPhysicalMaterial {
fn fragment_shader_source(&self, use_vertex_colors: bool, lights: &Lights) -> String {
let mut output = lights.fragment_shader_source();
if self.albedo_texture.is_some()
|| self.metallic_roughness_texture.is_some()
|| self.normal_texture.is_some()
|| self.occlusion_texture.is_some()
|| self.alpha_cutout.is_some()
{
output.push_str("in vec2 uvs;\n");
if self.albedo_texture.is_some() {
output.push_str("#define USE_ALBEDO_TEXTURE;\n");
}
if self.metallic_roughness_texture.is_some() {
output.push_str("#define USE_METALLIC_ROUGHNESS_TEXTURE;\n");
}
if self.occlusion_texture.is_some() {
output.push_str("#define USE_OCCLUSION_TEXTURE;\n");
}
if self.normal_texture.is_some() {
output.push_str("#define USE_NORMAL_TEXTURE;\nin vec3 tang;\nin vec3 bitang;\n");
}
if self.alpha_cutout.is_some() {
output.push_str(
format!(
"#define ALPHACUT;\nfloat acut = {};",
self.alpha_cutout.unwrap()
)
.as_str(),
);
}
}
if use_vertex_colors {
output.push_str("#define USE_VERTEX_COLORS\nin vec4 col;\n");
}
output.push_str(include_str!("shaders/deferred_physical_material.frag"));
output
}
fn use_uniforms(
&self,
program: &Program,
_camera: &Camera,
_lights: &Lights,
) -> ThreeDResult<()> {
program.use_uniform_float("metallic", &self.metallic)?;
program.use_uniform_float("roughness", &self.roughness)?;
program.use_uniform_vec4("albedo", &self.albedo.to_vec4())?;
if let Some(ref texture) = self.albedo_texture {
program.use_texture("albedoTexture", texture.as_ref())?;
}
if let Some(ref texture) = self.metallic_roughness_texture {
program.use_texture("metallicRoughnessTexture", texture.as_ref())?;
}
if let Some(ref texture) = self.occlusion_texture {
program.use_uniform_float("occlusionStrength", &self.occlusion_strength)?;
program.use_texture("occlusionTexture", texture.as_ref())?;
}
if let Some(ref texture) = self.normal_texture {
program.use_uniform_float("normalScale", &self.normal_scale)?;
program.use_texture("normalTexture", texture.as_ref())?;
}
Ok(())
}
fn render_states(&self) -> RenderStates {
self.render_states
}
fn is_transparent(&self) -> bool {
false
}
}
impl Default for DeferredPhysicalMaterial {
fn default() -> Self {
Self {
name: "default".to_string(),
albedo: Color::WHITE,
albedo_texture: None,
metallic: 0.0,
roughness: 1.0,
metallic_roughness_texture: None,
normal_texture: None,
normal_scale: 1.0,
occlusion_texture: None,
occlusion_strength: 1.0,
render_states: RenderStates::default(),
alpha_cutout: None,
}
}
}