Skip to main content

il2cpp_bridge_rs/structs/math/
math.rs

1//! Math structs and functions using glam
2pub use glam::*;
3
4pub type Vector2 = Vec2;
5pub type Vector3 = Vec3;
6pub type Vector4 = Vec4;
7pub type Quaternion = Quat;
8pub type Matrix4x4 = Mat4;
9pub type Matrix3x3 = Mat3;
10pub type Matrix2x2 = Mat2;
11
12#[repr(C)]
13#[derive(Debug, Clone, Copy, PartialEq)]
14pub struct Color {
15    /// Red component (0.0 - 1.0)
16    pub r: f32,
17    /// Green component (0.0 - 1.0)
18    pub g: f32,
19    /// Blue component (0.0 - 1.0)
20    pub b: f32,
21    /// Alpha component (0.0 - 1.0)
22    pub a: f32,
23}
24
25impl Color {
26    /// Creates a new color
27    pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
28        Self { r, g, b, a }
29    }
30
31    pub const WHITE: Self = Self::new(1.0, 1.0, 1.0, 1.0);
32    pub const BLACK: Self = Self::new(0.0, 0.0, 0.0, 1.0);
33    pub const RED: Self = Self::new(1.0, 0.0, 0.0, 1.0);
34    pub const GREEN: Self = Self::new(0.0, 1.0, 0.0, 1.0);
35    pub const BLUE: Self = Self::new(0.0, 0.0, 1.0, 1.0);
36    pub const YELLOW: Self = Self::new(1.0, 1.0, 0.0, 1.0);
37    pub const CYAN: Self = Self::new(0.0, 1.0, 1.0, 1.0);
38    pub const MAGENTA: Self = Self::new(1.0, 0.0, 1.0, 1.0);
39    pub const TRANSPARENT: Self = Self::new(0.0, 0.0, 0.0, 0.0);
40}
41
42impl From<Vec4> for Color {
43    fn from(v: Vec4) -> Self {
44        Self::new(v.x, v.y, v.z, v.w)
45    }
46}
47
48impl From<Color> for Vec4 {
49    fn from(c: Color) -> Self {
50        Vec4::new(c.r, c.g, c.b, c.a)
51    }
52}
53
54#[repr(C)]
55#[derive(Debug, Clone, Copy, PartialEq)]
56pub struct Bounds {
57    /// Center of the bounding box
58    pub center: Vector3,
59    /// Extents of the bounding box (half size)
60    pub extents: Vector3,
61}
62
63impl Default for Bounds {
64    fn default() -> Self {
65        Self {
66            center: Vector3::ZERO,
67            extents: Vector3::ZERO,
68        }
69    }
70}
71
72#[repr(C)]
73#[derive(Debug, Clone, Copy, PartialEq)]
74pub struct Ray {
75    /// Origin point of the ray
76    pub origin: Vector3,
77    /// Direction vector of the ray
78    pub direction: Vector3,
79}
80
81impl Default for Ray {
82    fn default() -> Self {
83        Self {
84            origin: Vector3::ZERO,
85            direction: Vector3::ZERO,
86        }
87    }
88}
89
90#[repr(C)]
91#[derive(Debug, Clone, Copy, PartialEq)]
92pub struct Rect {
93    /// X coordinate of the top-left corner
94    pub x: f32,
95    /// Y coordinate of the top-left corner
96    pub y: f32,
97    /// Width of the rectangle
98    pub width: f32,
99    /// Height of the rectangle
100    pub height: f32,
101}
102
103impl Default for Rect {
104    fn default() -> Self {
105        Self {
106            x: 0.0,
107            y: 0.0,
108            width: 0.0,
109            height: 0.0,
110        }
111    }
112}
113
114impl Rect {
115    /// Creates a new rectangle
116    ///
117    /// # Arguments
118    /// * `x` - The X coordinate of the top-left corner
119    /// * `y` - The Y coordinate of the top-left corner
120    /// * `width` - The width of the rectangle
121    /// * `height` - The height of the rectangle
122    ///
123    /// # Returns
124    /// * `Rect` - The new rectangle
125    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
126        Self {
127            x,
128            y,
129            width,
130            height,
131        }
132    }
133}
134
135#[repr(C)]
136#[derive(Debug, Clone, Copy, PartialEq)]
137pub struct Plane {
138    /// Normal vector of the plane
139    pub normal: Vector3,
140    /// Distance from the origin to the plane
141    pub distance: f32,
142}
143
144impl Default for Plane {
145    fn default() -> Self {
146        Self {
147            normal: Vector3::ZERO,
148            distance: 0.0,
149        }
150    }
151}
152
153impl Plane {
154    /// Creates a new plane
155    ///
156    /// # Arguments
157    /// * `normal` - The normal vector of the plane
158    /// * `distance` - The distance from the origin to the plane
159    ///
160    /// # Returns
161    /// * `Plane` - The new plane
162    pub fn new(normal: Vector3, distance: f32) -> Self {
163        Self { normal, distance }
164    }
165}
166
167use std::ffi::c_void;
168
169#[repr(C)]
170#[derive(Debug, Clone, Copy, PartialEq)]
171pub struct RaycastHit {
172    /// The impact point in world space where the ray hit the collider
173    pub point: Vector3,
174    /// The normal of the surface the ray hit
175    pub normal: Vector3,
176    /// The barycentric coordinate of the triangle we hit
177    pub barycentric_coordinate: Vector3,
178    /// The distance from the ray's origin to the impact point
179    pub distance: f32,
180    /// The index of the triangle that was hit
181    pub triangle_index: i32,
182    /// The uv texture coordinate at the collision location
183    pub texture_coord: Vector2,
184    /// The secondary uv texture coordinate at the impact point
185    pub texture_coord2: Vector2,
186    /// The uv lightmap coordinate at the impact point
187    pub lightmap_coord: Vector2,
188    /// The Collider that was hit
189    pub collider: *mut c_void,
190    /// The Rigidbody of the collider that was hit
191    pub rigidbody: *mut c_void,
192    /// The ArticulationBody of the collider that was hit
193    pub articulation_body: *mut c_void,
194    /// The Transform of the rigidbody or collider that was hit
195    pub transform: *mut c_void,
196    /// EntityId of the Collider that was hit
197    pub collider_entity_id: u32,
198}
199
200impl Default for RaycastHit {
201    fn default() -> Self {
202        Self {
203            point: Vector3::ZERO,
204            normal: Vector3::ZERO,
205            barycentric_coordinate: Vector3::ZERO,
206            distance: 0.0,
207            triangle_index: 0,
208            texture_coord: Vector2::ZERO,
209            texture_coord2: Vector2::ZERO,
210            lightmap_coord: Vector2::ZERO,
211            collider: std::ptr::null_mut(),
212            rigidbody: std::ptr::null_mut(),
213            articulation_body: std::ptr::null_mut(),
214            transform: std::ptr::null_mut(),
215            collider_entity_id: 0,
216        }
217    }
218}