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
use super::prelude::*;
use super::{uvec4, vec4, UVec4, Vec4};

#[cfg(target_arch = "spirv")]
use num_traits::Float;

/// Extensions to [`Vec4`]
///
/// Adds additional functionality to [`Vec4`] that [`glam`] doesn't have.
pub trait Vec4Ext {
    /// For element `i` of `self`, return `v[i].trunc()`
    fn trunc(self) -> Self;

    /// For element `i` of the return value, returns 0.0 if `v[i] < edge[i]` and 1.0 otherwise.
    fn step(self, edge: Self) -> Self;

    /// Return only the fractional parts of each component.
    fn fract(self) -> Self;

    /// Return the square root of each component.
    fn sqrt(self) -> Self;

    /// Raw transmute each component to u32.
    fn to_bits(self) -> UVec4;
}

impl Vec4Ext for Vec4 {
    #[inline]
    fn trunc(self) -> Self {
        vec4(
            self.x.trunc(),
            self.y.trunc(),
            self.z.trunc(),
            self.w.trunc(),
        )
    }

    fn step(self, edge: Vec4) -> Self {
        vec4(
            self.x.step(edge.x),
            self.y.step(edge.y),
            self.z.step(edge.z),
            self.w.step(edge.z),
        )
    }

    #[inline]
    fn fract(self) -> Self {
        vec4(
            self.x.fract(),
            self.y.fract(),
            self.z.fract(),
            self.w.fract(),
        )
    }

    fn sqrt(self) -> Self {
        vec4(self.x.sqrt(), self.y.sqrt(), self.z.sqrt(), self.w.sqrt())
    }

    fn to_bits(self) -> UVec4 {
        uvec4(
            self.x.to_bits(),
            self.y.to_bits(),
            self.z.to_bits(),
            self.w.to_bits(),
        )
    }
}