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
use crate::core::*;
use crate::renderer::*;
use std::sync::Arc;
#[derive(Clone, Default)]
pub struct ColorMaterial {
pub color: Color,
pub texture: Option<Arc<Texture2D>>,
pub render_states: RenderStates,
pub is_transparent: bool,
}
impl ColorMaterial {
pub fn new(context: &Context, cpu_material: &CpuMaterial) -> Self {
if super::is_transparent(cpu_material) {
Self::new_transparent(context, cpu_material)
} else {
Self::new_opaque(context, cpu_material)
}
}
pub fn new_opaque(context: &Context, cpu_material: &CpuMaterial) -> Self {
let texture = if let Some(ref cpu_texture) = cpu_material.albedo_texture {
Some(Arc::new(Texture2D::new(&context, cpu_texture)))
} else {
None
};
Self {
color: cpu_material.albedo,
texture,
is_transparent: false,
render_states: RenderStates::default(),
}
}
pub fn new_transparent(context: &Context, cpu_material: &CpuMaterial) -> Self {
let texture = if let Some(ref cpu_texture) = cpu_material.albedo_texture {
Some(Arc::new(Texture2D::new(&context, cpu_texture)))
} else {
None
};
Self {
color: cpu_material.albedo,
texture,
is_transparent: true,
render_states: RenderStates {
write_mask: WriteMask::COLOR,
blend: Blend::TRANSPARENCY,
..Default::default()
},
}
}
pub fn from_physical_material(physical_material: &PhysicalMaterial) -> Self {
Self {
color: physical_material.albedo,
texture: physical_material.albedo_texture.clone(),
render_states: physical_material.render_states,
is_transparent: physical_material.is_transparent,
}
}
}
impl FromCpuMaterial for ColorMaterial {
fn from_cpu_material(context: &Context, cpu_material: &CpuMaterial) -> Self {
Self::new(context, cpu_material)
}
}
impl Material for ColorMaterial {
fn fragment_shader_source(&self, use_vertex_colors: bool, _lights: &[&dyn Light]) -> String {
let mut shader = String::new();
if self.texture.is_some() {
shader.push_str("#define USE_TEXTURE\nin vec2 uvs;\n");
}
if use_vertex_colors {
shader.push_str("#define USE_VERTEX_COLORS\nin vec4 col;\n");
}
shader.push_str(include_str!("../../core/shared.frag"));
shader.push_str(include_str!("shaders/color_material.frag"));
shader
}
fn use_uniforms(&self, program: &Program, _camera: &Camera, _lights: &[&dyn Light]) {
program.use_uniform("surfaceColor", self.color);
if let Some(ref tex) = self.texture {
program.use_texture("tex", tex);
}
}
fn render_states(&self) -> RenderStates {
self.render_states
}
fn material_type(&self) -> MaterialType {
if self.is_transparent {
MaterialType::Transparent
} else {
MaterialType::Opaque
}
}
}