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
use crate::core::*;
use crate::renderer::*;
use std::collections::HashMap;
pub struct Mesh {
vertex_buffers: HashMap<String, VertexBuffer>,
index_buffer: Option<ElementBuffer>,
context: Context,
aabb: AxisAlignedBoundingBox,
aabb_local: AxisAlignedBoundingBox,
transformation: Mat4,
texture_transform: Mat3,
}
impl Mesh {
pub fn new(context: &Context, cpu_mesh: &CpuMesh) -> ThreeDResult<Self> {
let aabb = cpu_mesh.compute_aabb();
Ok(Self {
context: context.clone(),
index_buffer: super::index_buffer_from_mesh(context, cpu_mesh)?,
vertex_buffers: super::vertex_buffers_from_mesh(context, cpu_mesh)?,
aabb,
aabb_local: aabb.clone(),
transformation: Mat4::identity(),
texture_transform: Mat3::identity(),
})
}
pub(in crate::renderer) fn set_transformation_2d(&mut self, transformation: Mat3) {
self.set_transformation(Mat4::new(
transformation.x.x,
transformation.x.y,
0.0,
transformation.x.z,
transformation.y.x,
transformation.y.y,
0.0,
transformation.y.z,
0.0,
0.0,
1.0,
0.0,
transformation.z.x,
transformation.z.y,
0.0,
transformation.z.z,
));
}
pub fn transformation(&self) -> Mat4 {
self.transformation
}
pub fn set_transformation(&mut self, transformation: Mat4) {
self.transformation = transformation;
let mut aabb = self.aabb_local.clone();
aabb.transform(&self.transformation);
self.aabb = aabb;
}
pub fn texture_transform(&mut self) -> &Mat3 {
&self.texture_transform
}
pub fn set_texture_transform(&mut self, texture_transform: Mat3) {
self.texture_transform = texture_transform;
}
fn vertex_shader_source(fragment_shader_source: &str) -> ThreeDResult<String> {
let use_positions = fragment_shader_source.find("in vec3 pos;").is_some();
let use_normals = fragment_shader_source.find("in vec3 nor;").is_some();
let use_tangents = fragment_shader_source.find("in vec3 tang;").is_some();
let use_uvs = fragment_shader_source.find("in vec2 uvs;").is_some();
let use_colors = fragment_shader_source.find("in vec4 col;").is_some();
Ok(format!(
"{}{}{}{}{}{}{}",
if use_positions {
"#define USE_POSITIONS\n"
} else {
""
},
if use_normals {
"#define USE_NORMALS\n"
} else {
""
},
if use_tangents {
if fragment_shader_source.find("in vec3 bitang;").is_none() {
Err(CoreError::MissingBitangent)?;
}
"#define USE_TANGENTS\n"
} else {
""
},
if use_uvs { "#define USE_UVS\n" } else { "" },
if use_colors {
"#define USE_COLORS\n#define USE_VERTEX_COLORS\n"
} else {
""
},
include_str!("../../core/shared.frag"),
include_str!("shaders/mesh.vert"),
))
}
}
impl Geometry for Mesh {
fn aabb(&self) -> AxisAlignedBoundingBox {
self.aabb
}
fn render_with_material(
&self,
material: &dyn Material,
camera: &Camera,
lights: &[&dyn Light],
) -> ThreeDResult<()> {
let fragment_shader_source =
material.fragment_shader_source(self.vertex_buffers.contains_key("color"), lights);
self.context.program(
&Self::vertex_shader_source(&fragment_shader_source)?,
&fragment_shader_source,
|program| {
material.use_uniforms(program, camera, lights)?;
program.use_uniform("viewProjection", camera.projection() * camera.view())?;
program.use_uniform("modelMatrix", &self.transformation)?;
program.use_uniform_if_required("textureTransform", &self.texture_transform)?;
program.use_uniform_if_required(
"normalMatrix",
&self.transformation.invert().unwrap().transpose(),
)?;
for attribute_name in ["position", "normal", "tangent", "color", "uv_coordinates"] {
if program.requires_attribute(attribute_name) {
program.use_vertex_attribute(
attribute_name,
self.vertex_buffers
.get(attribute_name)
.ok_or(CoreError::MissingMeshBuffer(attribute_name.to_string()))?,
)?;
}
}
if let Some(ref index_buffer) = self.index_buffer {
program.draw_elements(material.render_states(), camera.viewport(), index_buffer)
} else {
program.draw_arrays(
material.render_states(),
camera.viewport(),
self.vertex_buffers.get("position").unwrap().vertex_count() as u32,
)
}
},
)
}
}