verdant 0.7.0

A windowing and rendering library, made to be accessible. Clean API, SDF-based rendering, multi-window support, built on wgpu and winit.
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

use bytemuck::{Pod, Zeroable};

// lmao
// it's practical okay
macro_rules! op_binop {
    ($t:ty, $opname:ident, $func:ident, $op:tt; $($var:ident),*) => {
        impl $opname for $t {
            type Output = Self;
            fn $func(self, rhs: Self) -> Self::Output {
                Self::new($(self.$var $op rhs.$var),*)
            }
        }
    };
    ($t:ty, $opname:ident, $func:ident, $op:tt, $rhs:ty; $($var:ident),*) => {
        impl $opname<$rhs> for $t {
            type Output = Self;
            fn $func(self, rhs: $rhs) -> Self::Output {
                Self::new($(self.$var $op rhs.$var),*)
            }
        }
    };
    ($t:ty, $opname:ident, $func:ident, $op:tt, $rhs:ty [scalar]; $($var:ident),*) => {
        impl $opname<$rhs> for $t {
            type Output = Self;
            fn $func(self, rhs: $rhs) -> Self::Output {
                Self::new($(self.$var $op rhs),*)
            }
        }
    };
}

macro_rules! op_assign {
    ($t:ty, $opname:ident, $func:ident, $op:tt; $($var:ident),*) => {
        impl $opname for $t {
            fn $func(&mut self, rhs: Self) {
                $(self.$var $op rhs.$var;)*
            }
        }
    };
    ($t:ty, $opname:ident, $func:ident, $op:tt, $rhs:ty; $($var:ident),*) => {
        impl $opname<$rhs> for $t {
            fn $func(&mut self, rhs: $rhs) {
                $(self.$var $op rhs.$var;)*
            }
        }
    };
    ($t:ty, $opname:ident, $func:ident, $op:tt, $rhs:ty [scalar]; $($var:ident),*) => {
        impl $opname<$rhs> for $t {
            fn $func(&mut self, rhs: $rhs) {
                $(self.$var $op rhs;)*
            }
        }
    };
}

macro_rules! vec_ops {
    ($t:ty; $($var:ident),*) => {
        op_binop!($t, Add, add, +; $($var),*);
        op_binop!($t, Sub, sub, -; $($var),*);
        op_binop!($t, Mul, mul, *; $($var),*);
        op_binop!($t, Div, div, /; $($var),*);

        op_assign!($t, AddAssign, add_assign, +=; $($var),*);
        op_assign!($t, SubAssign, sub_assign, -=; $($var),*);
        op_assign!($t, MulAssign, mul_assign, *=; $($var),*);
        op_assign!($t, DivAssign, div_assign, /=; $($var),*);
    };
    ($t:ty, $rhs:ty; $($var:ident),*) => {
        op_binop!($t, Add, add, +, $rhs; $($var),*);
        op_binop!($t, Sub, sub, -, $rhs; $($var),*);
        op_binop!($t, Mul, mul, *, $rhs; $($var),*);
        op_binop!($t, Div, div, /, $rhs; $($var),*);

        op_assign!($t, AddAssign, add_assign, +=, $rhs; $($var),*);
        op_assign!($t, SubAssign, sub_assign, -=, $rhs; $($var),*);
        op_assign!($t, MulAssign, mul_assign, *=, $rhs; $($var),*);
        op_assign!($t, DivAssign, div_assign, /=, $rhs; $($var),*);
    };
    ($t:ty, $rhs:ty [scalar]; $($var:ident),*) => {
        op_binop!($t, Add, add, +, $rhs [scalar]; $($var),*);
        op_binop!($t, Sub, sub, -, $rhs [scalar]; $($var),*);
        op_binop!($t, Mul, mul, *, $rhs [scalar]; $($var),*);
        op_binop!($t, Div, div, /, $rhs [scalar]; $($var),*);

        op_assign!($t, AddAssign, add_assign, +=, $rhs [scalar]; $($var),*);
        op_assign!($t, SubAssign, sub_assign, -=, $rhs [scalar]; $($var),*);
        op_assign!($t, MulAssign, mul_assign, *=, $rhs [scalar]; $($var),*);
        op_assign!($t, DivAssign, div_assign, /=, $rhs [scalar]; $($var),*);
    };
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Pod, Zeroable, Default, PartialEq)]
/// A struct representing a 2-dimensional vector, with `x` and `y` components.
pub struct Vec2 {
    pub x: f32,
    pub y: f32,
}

impl Vec2 {
    /// A [`Vec2`] with all values set to 0.
    pub const ZERO: Vec2 = Vec2::new(0., 0.);
    /// A [`Vec2`] with all values set to 1.
    pub const ONE:  Vec2 = Vec2::new(1., 1.);

    /// Create a new [`Vec2`] with `x` and `y`.
    pub const fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    /// Returns the length of this [`Vec2`].
    pub fn length(&self) -> f32 {
        (self.x * self.x + self.y * self.y).sqrt()
    }

    /// Returns the squared length of this [`Vec2`]
    /// Useful for comparing lengths of vectors without needing a `sqrt`.
    pub fn length_squared(&self) -> f32 {
        self.x * self.x + self.y * self.y
    }

    /// Returns a normalized copy of this [`Vec2`].
    pub fn normalize(&self) -> Vec2 {
        *self / self.length()
    }

    /// Returns the angle of this [`Vec2`] in radians.
    pub fn angle_rad(&self) -> f32 {
        self.y.atan2(self.x)
    }

    /// Returns the angle of this [`Vec2`] in degrees.
    pub fn angle_deg(&self) -> f32 {
        self.y.atan2(self.x).to_degrees()
    }

    /// Returns the distance between this [`Vec2`] and `other`.
    pub fn dist(&self, other: Self) -> f32 {
        let dx = other.x - self.x;
        let dy = other.y - self.y;
        (dx * dx + dy * dy).sqrt()
    }

    /// Returns the component-wise maximum of this [`Vec2`] and `other`.
    pub fn max(&self, other: Self) -> Vec2 {
        Self {
            x: self.x.max(other.x),
            y: self.y.max(other.y),
        }
    }

    /// Returns the longer vector between this [`Vec2`] and `other`.
    pub fn longest(&self, other: Self) -> Vec2 {
        if self.length_squared() >= other.length_squared() {
            *self
        } else {
            other
        }
    }
}

impl From<Vec2> for (f32, f32) {
    fn from(v: Vec2) -> Self {
        (v.x, v.y)
    }
}

impl From<(f32, f32)> for Vec2 {
    fn from(v: (f32, f32)) -> Self {
        Self::new(v.0, v.1)
    }
}

impl From<Vec2> for [f32; 2] {
    fn from(v: Vec2) -> Self {
        [v.x, v.y]
    }
}

impl From<[f32; 2]> for Vec2 {
    fn from(v: [f32; 2]) -> Self {
        Self::new(v[0], v[1])
    }
}

vec_ops!(Vec2; x, y);
vec_ops!(Vec2, f32 [scalar]; x, y);

#[repr(C)]
#[derive(Debug, Copy, Clone, Pod, Zeroable, Default, PartialEq)]
/// A struct representing a 3-dimensional vector, with `x`, `y`, and `z` components.
pub struct Vec3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl Vec3 {
    /// A [`Vec3`] with all values set to 0.
    pub const ZERO: Vec3 = Vec3::new(0., 0., 0.);
    /// A [`Vec3`] with all values set to 1.
    pub const ONE:  Vec3 = Vec3::new(1., 1., 1.);

    /// Create a new [`Vec3`] with `x`, `y`, and `z`.
    pub const fn new(x: f32, y: f32, z: f32) -> Self {
        Self { x, y, z }
    }

    /// Returns the length of this [`Vec3`].
    pub fn length(&self) -> f32 {
        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
    }

    /// Returns the squared length of this [`Vec3`]
    /// Useful for comparing lengths of vectors without needing a `sqrt`.
    pub fn length_squared(&self) -> f32 {
        self.x * self.x + self.y * self.y + self.z * self.z
    }

    /// Returns a normalized copy of this [`Vec3`].
    pub fn normalize(&self) -> Vec3 {
        let len = self.length();
        Vec3::new(self.x / len, self.y / len, self.z / len)
    }

    /// Returns the spherical angles of this [`Vec3`] in radians.
    pub fn angles_rad(&self) -> Vec2 {
        Vec2::new(
            Vec2::new(self.x, self.y).angle_rad(),
            (self.z / self.length()).acos(),
        )
    }

    /// Returns the spherical angles of this [`Vec3`] in degrees.
    pub fn angles_deg(&self) -> Vec2 {
        Vec2::new(
            Vec2::new(self.x, self.y).angle_deg(),
            (self.z / self.length()).acos().to_degrees(),
        )
    }

    /// Returns the distance between this [`Vec3`] and `other`.
    pub fn dist(&self, other: Self) -> f32 {
        let dx = other.x - self.x;
        let dy = other.y - self.y;
        let dz = other.z - self.z;
        (dx * dx + dy * dy + dz * dz).sqrt()
    }

    /// Returns the component-wise maximum of this [`Vec3`] and `other`.
    pub fn max(&self, other: Self) -> Vec3 {
        Self {
            x: self.x.max(other.x),
            y: self.y.max(other.y),
            z: self.z.max(other.z),
        }
    }

    /// Returns the longer vector between this [`Vec3`] and `other`.
    pub fn longest(&self, other: Self) -> Vec3 {
        if self.length_squared() >= other.length_squared() {
            *self
        } else {
            other
        }
    }
}

impl From<Vec3> for (f32, f32, f32) {
    fn from(v: Vec3) -> Self {
        (v.x, v.y, v.z)
    }
}

impl From<(f32, f32, f32)> for Vec3 {
    fn from(v: (f32, f32, f32)) -> Self {
        Self::new(v.0, v.1, v.2)
    }
}

impl From<Vec3> for [f32; 3] {
    fn from(v: Vec3) -> Self {
        [v.x, v.y, v.z]
    }
}

impl From<[f32; 3]> for Vec3 {
    fn from(v: [f32; 3]) -> Self {
        Self::new(v[0], v[1], v[2])
    }
}

vec_ops!(Vec3; x, y, z);
vec_ops!(Vec3, f32 [scalar]; x, y, z);

#[repr(C)]
#[derive(Debug, Copy, Clone, Pod, Zeroable, Default, PartialEq)]
pub struct Vec4 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}

impl Vec4 {
    /// A [`Vec4`] with all values set to 0.
    pub const ZERO: Vec4 = Vec4::new(0., 0., 0., 0.);
    /// A [`Vec4`] with all values set to 1.
    pub const ONE:  Vec4 = Vec4::new(1., 1., 1., 1.);

    /// Create a new [`Vec4`] with `x`, `y`, `z`, and `w`.
    pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
        Self { x, y, z, w }
    }

    /// Returns the length of this [`Vec4`].
    pub fn length(&self) -> f32 {
        (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
    }

    /// Returns the squared length of this [`Vec4`]
    /// Useful for comparing lengths of vectors without needing a `sqrt`.
    pub fn length_squared(&self) -> f32 {
        self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w
    }

    /// Returns a normalized copy of this [`Vec4`].
    pub fn normalize(&self) -> Vec4 {
        let len = self.length();
        Vec4::new(self.x / len, self.y / len, self.z / len, self.w / len)
    }

    /// Returns the hyperspherical angles of this [`Vec4`] in radians.
    pub fn angles_rad(&self) -> Vec3 {
        let inner_angle = Vec3::new(self.x, self.y, self.z).angles_rad();
        Vec3::new(
            (self.w / self.length()).acos(),
            inner_angle.y,
            inner_angle.x,
        )
    }

    /// Returns the hyperspherical angles of this [`Vec4`] in degrees.
    pub fn angles_deg(&self) -> Vec3 {
        let inner_angle = Vec3::new(self.x, self.y, self.z).angles_deg();
        Vec3::new(
            (self.w / self.length()).acos().to_degrees(),
            inner_angle.y,
            inner_angle.x,
        )
    }

    /// Returns the distance between this [`Vec4`] and `other`.
    pub fn dist(&self, other: impl Into<Self>) -> f32 {
        let other = other.into();
        let dx = other.x - self.x;
        let dy = other.y - self.y;
        let dz = other.z - self.z;
        let dw = other.w - self.w;
        (dx * dx + dy * dy + dz * dz + dw * dw).sqrt()
    }

    /// Returns the component-wise maximum of this [`Vec4`] and `other`.
    pub fn max(&self, other: impl Into<Self>) -> Vec4 {
        let other = other.into();
        Self {
            x: self.x.max(other.x),
            y: self.y.max(other.y),
            z: self.z.max(other.z),
            w: self.w.max(other.w),
        }
    }

    /// Returns the longer vector between this [`Vec4`] and `other`.
    pub fn longest(&self, other: impl Into<Self>) -> Vec4 {
        let other = other.into();
        if self.length_squared() >= other.length_squared() {
            *self
        } else {
            other
        }
    }
}

impl From<Vec4> for (f32, f32, f32, f32) {
    fn from(v: Vec4) -> Self {
        (v.x, v.y, v.z, v.w)
    }
}

impl From<(f32, f32, f32, f32)> for Vec4 {
    fn from(v: (f32, f32, f32, f32)) -> Self {
        Self::new(v.0, v.1, v.2, v.3)
    }
}

impl From<Vec4> for [f32; 4] {
    fn from(v: Vec4) -> Self {
        [v.x, v.y, v.z, v.w]
    }
}

impl From<[f32; 4]> for Vec4 {
    fn from(v: [f32; 4]) -> Self {
        Self::new(v[0], v[1], v[2], v[3])
    }
}

vec_ops!(Vec4; x, y, z, w);
vec_ops!(Vec4, f32 [scalar]; x, y, z, w);

#[cfg(feature = "glam")]
mod glam_compat {
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

    use super::{Vec2, Vec3, Vec4};

    impl From<glam::Vec2> for Vec2 {
        fn from(v: glam::Vec2) -> Self {
            Self::new(v.x, v.y)
        }
    }

    impl From<Vec2> for glam::Vec2 {
        fn from(v: Vec2) -> Self {
            Self::new(v.x, v.y)
        }
    }

    vec_ops!(Vec2, glam::Vec2; x, y);
    vec_ops!(glam::Vec2, Vec2; x, y);

    impl From<glam::Vec3> for Vec3 {
        fn from(v: glam::Vec3) -> Self {
            Self::new(v.x, v.y, v.z)
        }
    }

    impl From<Vec3> for glam::Vec3 {
        fn from(v: Vec3) -> Self {
            Self::new(v.x, v.y, v.z)
        }
    }

    vec_ops!(Vec3, glam::Vec3; x, y, z);
    vec_ops!(glam::Vec3, Vec3; x, y, z);

    impl From<glam::Vec4> for Vec4 {
        fn from(v: glam::Vec4) -> Self {
            Self::new(v.x, v.y, v.z, v.w)
        }
    }

    impl From<Vec4> for glam::Vec4 {
        fn from(v: Vec4) -> Self {
            Self::new(v.x, v.y, v.z, v.w)
        }
    }

    vec_ops!(Vec4, glam::Vec4; x, y, z, w);
    vec_ops!(glam::Vec4, Vec4; x, y, z, w);
}