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
//! Unity Camera component wrapper
use super::screen::Screen;
use crate::api::cache;
use crate::structs::collections::Il2cppArray;
use crate::structs::components::{Component, ComponentTrait};
use crate::structs::math::{Matrix4x4, Ray, Vector2, Vector3};
use std::ffi::c_void;
use std::ops::Deref;
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CameraEye {
Left = 0,
Right = 1,
Mono = 2,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Camera {
/// Base Component structure
pub component: Component,
}
impl ComponentTrait for Camera {
fn from_ptr(ptr: *mut c_void) -> Self {
Self {
component: Component::from_ptr(ptr),
}
}
}
impl Deref for Camera {
type Target = Component;
fn deref(&self) -> &Self::Target {
&self.component
}
}
impl Camera {
/// Gets the Camera class definition
///
/// # Returns
/// * `Option<Class>` - The UnityEngine.Camera class
pub fn get_class() -> Option<crate::structs::core::Class> {
cache::coremodule().class("UnityEngine.Camera")
}
/// Gets the main camera in the scene (Camera.main)
///
/// # Returns
/// * `Result<Option<Camera>, String>` - The main camera if it exists
pub fn get_main() -> Result<Option<Camera>, String> {
let class = Self::get_class().ok_or("Class 'UnityEngine.Camera' not found")?;
let method = class
.method("get_main")
.ok_or("Method 'get_main' not found")?;
unsafe {
let ptr = method.call::<*mut c_void>(&[])?;
if ptr.is_null() {
Ok(None)
} else {
Ok(Some(Camera::from_ptr(ptr)))
}
}
}
/// Gets the current camera (for rendering events)
///
/// # Returns
/// * `Result<Option<Camera>, String>` - The current camera if it exists
pub fn get_current() -> Result<Option<Camera>, String> {
let class = Self::get_class().ok_or("Class 'UnityEngine.Camera' not found")?;
let method = class
.method("get_current")
.ok_or("Method 'get_current' not found")?;
unsafe {
let ptr = method.call::<*mut c_void>(&[])?;
if ptr.is_null() {
Ok(None)
} else {
Ok(Some(Camera::from_ptr(ptr)))
}
}
}
/// Gets the count of all active cameras
///
/// # Returns
/// * `Result<i32, String>` - The number of all cameras enabled in the scene
pub fn get_all_count() -> Result<i32, String> {
let class = Self::get_class().ok_or("Class 'UnityEngine.Camera' not found")?;
let method = class
.method("get_allCamerasCount")
.ok_or("Method 'get_allCamerasCount' not found")?;
unsafe { method.call::<i32>(&[]) }
}
/// Gets a list of all active cameras
///
/// # Returns
/// * `Result<Vec<Camera>, String>` - A vector containing all active cameras
pub fn get_all_cameras() -> Result<Vec<Camera>, String> {
let class = Self::get_class().ok_or("Class 'UnityEngine.Camera' not found")?;
let method = class
.method("GetAllCameras")
.ok_or("Method 'GetAllCameras' not found")?;
let count = Self::get_all_count()?;
if count == 0 {
return Ok(Vec::new());
}
let array_ptr = Il2cppArray::<*mut c_void>::new(&class, count as usize);
if array_ptr.is_null() {
return Err("Failed to create array".to_string());
}
unsafe {
method.call::<i32>(&[array_ptr as *mut c_void])?;
let array = &*array_ptr;
let mut cameras = Vec::with_capacity(count as usize);
for i in 0..count as usize {
let ptr = array.at(i);
if !ptr.is_null() {
cameras.push(Camera::from_ptr(ptr));
}
}
Ok(cameras)
}
}
/// Gets the depth of the camera
///
/// # Returns
/// * `Result<f32, String>` - The camera's depth in the rendering order
pub fn get_depth(&self) -> Result<f32, String> {
unsafe {
self.method("get_depth")
.ok_or("Method 'get_depth' not found")?
.call::<f32>(&[])
}
}
/// Sets the depth of the camera
///
/// # Arguments
/// * `depth` - The new depth value
///
/// # Returns
/// * `Result<(), String>` - Ok if success
pub fn set_depth(&self, depth: f32) -> Result<(), String> {
unsafe {
self.method("set_depth")
.ok_or("Method 'set_depth' not found")?
.call::<()>(&[&depth as *const f32 as *mut c_void])?;
}
Ok(())
}
/// Gets the field of view of the camera
///
/// # Returns
/// * `Result<f32, String>` - The field of view in degrees
pub fn get_field_of_view(&self) -> Result<f32, String> {
unsafe {
self.method("get_fieldOfView")
.ok_or("Method 'get_fieldOfView' not found")?
.call::<f32>(&[])
}
}
/// Sets the field of view of the camera
///
/// # Arguments
/// * `fov` - The new field of view in degrees
///
/// # Returns
/// * `Result<(), String>` - Ok if success
pub fn set_field_of_view(&self, fov: f32) -> Result<(), String> {
unsafe {
self.method("set_fieldOfView")
.ok_or("Method 'set_fieldOfView' not found")?
.call::<()>(&[&fov as *const f32 as *mut c_void])?;
}
Ok(())
}
/// Converts a point from world space to screen space
///
/// # Arguments
/// * `position` - The world position to convert
/// * `eye` - The camera eye to use for conversion
///
/// # Returns
/// * `Result<Vector3, String>` - The screen point
pub fn world_to_screen_point(
&self,
position: Vector3,
eye: CameraEye,
) -> Result<Vector3, String> {
unsafe {
self.method("WorldToScreenPoint")
.ok_or("Method 'WorldToScreenPoint' not found")?
.call::<Vector3>(&[
&position as *const Vector3 as *mut c_void,
&eye as *const CameraEye as *mut c_void,
])
}
}
/// Converts a point from screen space to world space
///
/// # Arguments
/// * `position` - The screen position to convert
/// * `eye` - The camera eye to use for conversion
///
/// # Returns
/// * `Result<Vector3, String>` - The world point
pub fn screen_to_world_point(
&self,
position: Vector3,
eye: CameraEye,
) -> Result<Vector3, String> {
unsafe {
self.method("ScreenToWorldPoint")
.ok_or("Method 'ScreenToWorldPoint' not found")?
.call::<Vector3>(&[
&position as *const Vector3 as *mut c_void,
&eye as *const CameraEye as *mut c_void,
])
}
}
/// Gets the camera to world matrix
///
/// # Returns
/// * `Result<Matrix4x4, String>` - The matrix that transforms from camera space to world space
pub fn camera_to_world_matrix(&self) -> Result<Matrix4x4, String> {
unsafe {
self.method("get_cameraToWorldMatrix")
.ok_or("Method 'get_cameraToWorldMatrix' not found")?
.call::<Matrix4x4>(&[])
}
}
/// Converts world position to screen coordinates and checks if it's on screen
///
/// # Arguments
/// * `position` - The world position
/// * `eye` - The camera eye
///
/// # Returns
/// * `Result<(Vector2, bool), String>` - A tuple of (screen position, is on screen)
pub fn world_to_screen(
&self,
position: Vector3,
eye: CameraEye,
) -> Result<(Vector2, bool), String> {
let mut screen_point = self.world_to_screen_point(position, eye)?;
if screen_point.z < 0.01 {
return Ok((Vector2 { x: 0.0, y: 0.0 }, false));
}
let screen_width = Screen::get_width()? as f32;
let screen_height = Screen::get_height()? as f32;
screen_point.y = screen_height - screen_point.y;
let on_screen = screen_point.x > 0.0
&& screen_point.x < screen_width
&& screen_point.y > 0.0
&& screen_point.y < screen_height;
Ok((
Vector2 {
x: screen_point.x,
y: screen_point.y,
},
on_screen,
))
}
/// Returns a ray from the camera through a screen point
///
/// # Arguments
/// * `position` - The screen position
/// * `eye` - The camera eye
///
/// # Returns
/// * `Result<Ray, String>` - The ray passing through the screen point
pub fn screen_point_to_ray(&self, position: Vector2, eye: CameraEye) -> Result<Ray, String> {
unsafe {
self.method("ScreenPointToRay")
.ok_or("Method 'ScreenPointToRay' not found")?
.call::<Ray>(&[
&position as *const Vector2 as *mut c_void,
&eye as *const CameraEye as *mut c_void,
])
}
}
}