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
use crate::core::*;
use crate::renderer::*;
use std::sync::Arc;
#[derive(Clone)]
pub struct NormalMaterial {
pub normal_scale: f32,
pub normal_texture: Option<Arc<Texture2D>>,
pub render_states: RenderStates,
}
impl NormalMaterial {
pub fn new(context: &Context, cpu_material: &CpuMaterial) -> Self {
let normal_texture = if let Some(ref cpu_texture) = cpu_material.normal_texture {
Some(Arc::new(Texture2D::new(&context, cpu_texture)))
} else {
None
};
Self {
normal_scale: cpu_material.normal_scale,
normal_texture: normal_texture,
render_states: RenderStates::default(),
}
}
pub fn from_physical_material(physical_material: &PhysicalMaterial) -> Self {
Self {
normal_scale: physical_material.normal_scale,
normal_texture: physical_material.normal_texture.clone(),
render_states: RenderStates {
write_mask: WriteMask::default(),
blend: Blend::Disabled,
..physical_material.render_states
},
}
}
}
impl FromCpuMaterial for NormalMaterial {
fn from_cpu_material(context: &Context, cpu_material: &CpuMaterial) -> Self {
Self::new(context, cpu_material)
}
}
impl Material for NormalMaterial {
fn fragment_shader_source(&self, _use_vertex_colors: bool, _lights: &[&dyn Light]) -> String {
let mut shader = String::new();
if self.normal_texture.is_some() {
shader.push_str("#define USE_TEXTURE\nin vec2 uvs;\nin vec3 tang;\nin vec3 bitang;\n");
}
shader.push_str(include_str!("shaders/normal_material.frag"));
shader
}
fn use_uniforms(&self, program: &Program, _camera: &Camera, _lights: &[&dyn Light]) {
if let Some(ref tex) = self.normal_texture {
program.use_uniform("normalScale", &self.normal_scale);
program.use_texture("normalTexture", tex);
}
}
fn render_states(&self) -> RenderStates {
self.render_states
}
fn material_type(&self) -> MaterialType {
MaterialType::Opaque
}
}
impl Default for NormalMaterial {
fn default() -> Self {
Self {
normal_texture: None,
normal_scale: 1.0,
render_states: RenderStates::default(),
}
}
}