1use derive_cmp_ops::*;
2use glium::uniforms::AsUniformValue;
3use super::{vec3::Vec3, uvec2::*, uvec4::*, bvec3::*};
4#[derive(
5 Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, CmpAdd, CmpAddAssign, CmpDiv,
6 CmpDivAssign, CmpMul, CmpMulAssign, CmpRem, CmpRemAssign, CmpSub, CmpSubAssign
7)]
8pub struct UVec3 {
10 pub x: u32,
11 pub y: u32,
12 pub z: u32
13}
14impl UVec3 {
15 pub const ZERO: Self = uvec3(0, 0, 0);
17 pub const ONE: Self = uvec3(1, 1, 1);
19 pub const X: Self = uvec3(1, 0, 0);
21 pub const Y: Self = uvec3(0, 1, 0);
23 pub const Z: Self = uvec3(0, 0, 1);
25
26 pub const fn new(x: u32, y: u32, z: u32) -> Self { Self { x, y, z } }
27 pub const fn extend(self, w: u32) -> UVec4 { uvec4(self.x, self.y, self.z, w) }
28 pub const fn truncate(self) -> UVec2 { uvec2(self.x, self.y) }
29 pub const fn splat(value: u32) -> Self { Self::new(value, value, value) }
31
32 pub fn length_squared(self) -> u32 {
34 self.x*self.x + self.y*self.y + self.z*self.z
35 }
36 pub fn distance_squared(self, other: UVec3) -> u32 {
38 (self - other).length_squared()
39 }
40 pub fn dot(self, other: UVec3) -> u32 {
42 self.x * other.x + self.y * other.y + self.z * other.z
43 }
44 pub fn cross(self, other: UVec3) -> UVec3 {
54 uvec3(
55 self.y*other.z - self.z*other.y,
56 self.z*other.x - self.x*other.z,
57 self.x*other.y - self.y*other.x
58 )
59 }
60 pub fn scale(self, scalar: u32) -> UVec3 {
62 Self::new(self.x * scalar, self.y * scalar, self.z * scalar)
63 }
64 pub fn eq(self, rhs: Self) -> BVec3 { bvec3(self.x == rhs.x, self.y == rhs.y, self.z == rhs.z) }
66 pub fn less(self, rhs: Self) -> BVec3 { bvec3(self.x < rhs.x, self.y < rhs.y, self.z < rhs.z) }
68 pub fn more(self, rhs: Self) -> BVec3 { bvec3(self.x > rhs.x, self.y > rhs.y, self.z > rhs.z) }
70 pub fn less_or_eq(self, rhs: Self) -> BVec3 { bvec3(self.x <= rhs.x, self.y <= rhs.y, self.z <= rhs.z) }
72 pub fn more_or_eq(self, rhs: Self) -> BVec3 { bvec3(self.x >= rhs.x, self.y >= rhs.y, self.z >= rhs.z) }
74}
75impl std::ops::Mul<UVec3> for u32 {
76 fn mul(self, rhs: UVec3) -> Self::Output { rhs * self }
77 type Output = UVec3;
78}
79impl std::ops::Mul<u32> for UVec3 {
80 fn mul(self, rhs: u32) -> Self::Output { self.scale(rhs) }
81 type Output = Self;
82}
83impl std::ops::MulAssign<u32> for UVec3 { fn mul_assign(&mut self, rhs: u32) { *self = *self * rhs } }
84impl std::ops::Div<UVec3> for u32 {
85 fn div(self, rhs: UVec3) -> Self::Output { UVec3::splat(self) / rhs }
86 type Output = UVec3;
87}
88impl std::ops::Div<u32> for UVec3 {
89 fn div(self, rhs: u32) -> Self::Output { self / UVec3::splat(rhs) }
90 type Output = Self;
91}
92impl std::ops::DivAssign<u32> for UVec3 { fn div_assign(&mut self, rhs: u32) { *self = *self / rhs } }
93impl std::ops::Rem<UVec3> for u32 {
94 fn rem(self, rhs: UVec3) -> Self::Output { UVec3::splat(self) % rhs }
95 type Output = UVec3;
96}
97impl std::ops::Rem<u32> for UVec3 {
98 fn rem(self, rhs: u32) -> Self::Output { self % UVec3::splat(rhs) }
99 type Output = Self;
100}
101impl std::ops::RemAssign<u32> for UVec3 { fn rem_assign(&mut self, rhs: u32) { *self = *self % rhs } }
102
103impl AsUniformValue for UVec3 {
104 fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
105 glium::uniforms::UniformValue::UnsignedIntVec3([self.x, self.y, self.z])
106 }
107}
108impl From<Vec3> for UVec3 {
109 fn from(value: Vec3) -> Self {
110 Self { x: value.x as u32, y: value.y as u32, z: value.z as u32 }
111 }
112}
113impl From<(u32, u32, u32)> for UVec3 {
114 fn from(value: (u32, u32, u32)) -> Self {
115 Self { x: value.0, y: value.1, z: value.2 }
116 }
117}
118impl From<[u32; 3]> for UVec3 {
119 fn from(value: [u32; 3]) -> Self {
120 Self { x: value[0], y: value[1], z: value[2] }
121 }
122}
123impl From<UVec3> for [u32; 3] {
124 fn from(value: UVec3) -> Self {
125 [value.x, value.y, value.z]
126 }
127}
128impl From<UVec3> for (u32, u32, u32) {
129 fn from(value: UVec3) -> Self {
130 (value.x, value.y, value.z)
131 }
132}
133pub const fn uvec3(x: u32, y: u32, z: u32) -> UVec3 {
135 UVec3 { x, y, z }
136}