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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use glam::{Mat4, Vec3, Vec4};
use crate::graphics::{Canvas3d, Color};
use super::{LinearColor, ZIndex};
/// A 3d version of `DrawParam` used for transformation of 3d meshes
#[derive(Clone, Copy, Debug)]
pub struct DrawParam3d {
/// The transform of the mesh to draw see `Transform3d`
pub transform: Transform3d,
/// The alpha component is used for intensity of blending instead of actual alpha
pub color: Color,
/// This is used for the order of rendering
pub z: ZIndex,
}
impl DrawParam3d {
/// Change the scale of the `Transform3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn scale<V>(mut self, scale_: V) -> Self
where
V: Into<mint::Vector3<f32>>,
{
let p: mint::Vector3<f32> = scale_.into();
self.transform = self.transform.scale(p);
self
}
/// Change the position of the `Transform3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn position<P>(mut self, position_: P) -> Self
where
P: Into<mint::Point3<f32>>,
{
let p: mint::Point3<f32> = position_.into();
self.transform = self.transform.position(p);
self
}
/// Change the rotation of the `Transform3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn rotation<R>(mut self, rotation_: R) -> Self
where
R: Into<mint::Quaternion<f32>>,
{
let p: mint::Quaternion<f32> = rotation_.into();
self.transform = self.transform.rotation(p);
self
}
/// Move the position by given amount
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn translate<T>(mut self, translate_: T) -> Self
where
T: Into<mint::Vector3<f32>>,
{
let t: mint::Vector3<f32> = translate_.into();
self.transform = self.transform.translate(t);
self
}
/// Change the pivot of the `DrawParam3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn pivot<P>(mut self, pivot_: P) -> Self
where
P: Into<mint::Point3<f32>>,
{
let p: mint::Point3<f32> = pivot_.into();
self.transform = self.transform.pivot(p);
self
}
/// Change the offset of the `DrawParam3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn offset<O>(mut self, offset_: O) -> Self
where
O: Into<mint::Point3<f32>>,
{
let o: mint::Point3<f32> = offset_.into();
self.transform = self.transform.offset(o);
self
}
/// Change the color of the `DrawParam3d`
#[must_use]
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
/// Change the transform of the `DrawParam3d`
#[must_use]
pub fn transform(mut self, transform: Transform3d) -> Self {
self.transform = transform;
self
}
}
impl Default for DrawParam3d {
fn default() -> Self {
Self {
transform: Transform3d::default(),
color: Color::new(1.0, 1.0, 1.0, 0.0),
z: 0,
}
}
}
/// Represents transformations for 3d objects
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Transform3d {
/// Transform made of individual values
Values {
/// The position to draw the graphic expressed as a `Point3`.
pos: mint::Point3<f32>,
/// The orientation of the graphic as a Quaternion.
rotation: mint::Quaternion<f32>,
/// The x/y scale factors expressed as a `Vector3`.
scale: mint::Vector3<f32>,
/// An offset, which is applied before scaling and rotation happen. By default this will be the distance to the center of the given mesh or model
offset: Option<mint::Point3<f32>>,
/// The pivot point or origin of the transform3d. By default this will be the center of the mesh/model in world space.
pivot: Option<mint::Point3<f32>>,
},
/// Transform made of an arbitrary matrix.
///
/// It should represent the final model matrix of the given drawable. This is useful for
/// situations where, for example, you build your own hierarchy system, where you calculate
/// matrices of each hierarchy item and store a calculated world-space model matrix of an item.
/// This lets you implement transform stacks, skeletal animations, etc.
Matrix(mint::ColumnMatrix4<f32>),
}
fn transform_to_matrix(
pos: mint::Point3<f32>,
rotation: mint::Quaternion<f32>,
scale: mint::Vector3<f32>,
offset: Option<mint::Point3<f32>>,
pivot: Option<mint::Point3<f32>>,
) -> Mat4 {
let offset = if let Some(offset) = offset {
offset
} else {
Vec3::ZERO.into()
};
let pivot = if let Some(piv) = pivot {
Vec3::from(piv) + Vec3::from(offset)
} else {
Vec3::from(pos) + Vec3::from(offset)
};
Mat4::from_translation(pivot)
* Mat4::from_scale(scale.into())
* Mat4::from_quat(rotation.into())
* Mat4::from_translation(-(pivot))
* Mat4::from_translation(Vec3::from(pos))
}
impl Default for Transform3d {
fn default() -> Self {
Transform3d::Values {
pos: mint::Point3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
rotation: glam::Quat::IDENTITY.into(),
scale: mint::Vector3 {
x: 1.0,
y: 1.0,
z: 1.0,
},
offset: None,
pivot: None,
}
}
}
impl Transform3d {
/// Change the scale of the `Transform3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn scale<V>(mut self, scale_: V) -> Self
where
V: Into<mint::Vector3<f32>>,
{
let p: mint::Vector3<f32> = scale_.into();
if let Self::Values { ref mut scale, .. } = self {
*scale = p;
} else {
panic!("Setting values of a Transform3d when it is a Matrix doesn't work!");
}
self
}
/// Change the position of the `Transform3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn position<P>(mut self, position_: P) -> Self
where
P: Into<mint::Point3<f32>>,
{
let p: mint::Point3<f32> = position_.into();
if let Self::Values { ref mut pos, .. } = self {
*pos = p;
} else {
panic!("Setting values of a Transform3d when it is a Matrix doesn't work!");
}
self
}
/// Change the rotation of the `Transform3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn rotation<R>(mut self, rotation_: R) -> Self
where
R: Into<mint::Quaternion<f32>>,
{
let p: mint::Quaternion<f32> = rotation_.into();
if let Self::Values {
ref mut rotation, ..
} = self
{
*rotation = p;
} else {
panic!("Setting values of a Transform3d when it is a Matrix doesn't work!");
}
self
}
/// Move the position by given amount
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn translate<T>(mut self, translate_: T) -> Self
where
T: Into<mint::Vector3<f32>>,
{
let t: mint::Vector3<f32> = translate_.into();
if let Self::Values { ref mut pos, .. } = self {
*pos = (glam::Vec3::from(*pos) + glam::Vec3::from(t)).into();
} else {
panic!("Setting values of a Transform3d when it is a Matrix doesn't work!");
}
self
}
/// Sets the pivot point basically the origin of the mesh
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn pivot<P>(mut self, pivot_: P) -> Self
where
P: Into<mint::Point3<f32>>,
{
let p: mint::Point3<f32> = pivot_.into();
if let Self::Values { ref mut pivot, .. } = self {
*pivot = Some(p)
} else {
panic!("Setting values of a Transform3d when it is a Matrix doesn't work!");
}
self
}
/// Change the offset of the `DrawParam3d`
///
/// # Panics
///
/// Panics if `Transform3d` is of the `Matrix` variant.
#[must_use]
pub fn offset<O>(mut self, offset_: O) -> Self
where
O: Into<mint::Point3<f32>>,
{
let o: mint::Point3<f32> = offset_.into();
if let Self::Values { ref mut offset, .. } = self {
*offset = Some(o);
} else {
panic!("Setting values of a Transform3d when it is a Matrix doesn't work!");
}
self
}
/// Crunches the transform down to a single matrix, if it's not one already.
#[must_use]
pub fn to_matrix(self) -> Self {
Transform3d::Matrix(self.to_bare_matrix())
}
/// Same as `to_matrix()` but just returns a bare `mint` matrix.
#[must_use]
pub fn to_bare_matrix(self) -> mint::ColumnMatrix4<f32> {
match self {
Transform3d::Matrix(m) => m,
Transform3d::Values {
pos,
rotation,
scale,
offset,
pivot,
} => transform_to_matrix(pos, rotation, scale, offset, pivot).into(),
}
}
/// Same as `to_matrix()` but just returns a bare `glam` matrix.
#[must_use]
pub(crate) fn to_bare_matrix_glam(self) -> Mat4 {
match self {
Transform3d::Matrix(m) => m.into(),
Transform3d::Values {
pos,
rotation,
scale,
offset,
pivot,
} => transform_to_matrix(pos, rotation, scale, offset, pivot),
}
}
}
/// All types that can be drawn onto a canvas3d implement the `Drawable3d` trait.
pub trait Drawable3d {
/// Draws the drawable3d onto the canvas3d.
fn draw(&self, canvas: &mut Canvas3d, param: impl Into<DrawParam3d>);
}
#[derive(Debug, Copy, Clone, crevice::std140::AsStd140, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(C)]
pub(crate) struct DrawUniforms3d {
pub color: Vec4,
pub model_transform: Mat4,
pub camera_transform: Mat4,
}
impl DrawUniforms3d {
pub fn from_param(param: &DrawParam3d) -> Self {
let param = match param.transform {
Transform3d::Values { .. } => *param,
Transform3d::Matrix(m) => param.transform(Transform3d::Matrix(m)),
};
let color = LinearColor::from(param.color);
DrawUniforms3d {
color: Vec4::from_array(color.into()),
model_transform: param.transform.to_bare_matrix_glam(),
camera_transform: Mat4::IDENTITY,
}
}
pub fn projection(mut self, projection: impl Into<mint::ColumnMatrix4<f32>>) -> Self {
self.camera_transform = projection.into().into();
self
}
}