Skip to main content

il2cpp_bridge_rs/structs/components/rendering/
renderer.rs

1//! Unity Renderer component wrapper
2use super::material::Material;
3use crate::structs::collections::Il2cppArray;
4use crate::structs::components::{Component, ComponentTrait};
5use crate::structs::math::Bounds;
6use std::ffi::c_void;
7use std::ops::Deref;
8
9#[repr(C)]
10#[derive(Debug, Clone, Copy)]
11pub struct Renderer {
12    /// Base Component structure
13    pub component: Component,
14}
15
16impl ComponentTrait for Renderer {
17    fn from_ptr(ptr: *mut c_void) -> Self {
18        Self {
19            component: Component::from_ptr(ptr),
20        }
21    }
22}
23
24impl Deref for Renderer {
25    type Target = Component;
26    fn deref(&self) -> &Self::Target {
27        &self.component
28    }
29}
30
31impl Renderer {
32    /// Gets the bounding box of the renderer
33    ///
34    /// # Returns
35    /// * `Result<Bounds, String>` - The axis-aligned bounding box of the renderer
36    pub fn get_bounds(&self) -> Result<Bounds, String> {
37        let mut bounds = Bounds::default();
38        unsafe {
39            self.method("get_bounds_Injected")
40                .ok_or("Method 'get_bounds_Injected' not found")?
41                .call::<()>(&[&mut bounds as *mut Bounds as *mut c_void])?;
42        }
43        Ok(bounds)
44    }
45
46    /// Returns the first instantiated Material assigned to the renderer.
47    ///
48    /// # Returns
49    /// * `Result<Material, String>` - The first material assigned to this renderer
50    pub fn get_material(&self) -> Result<Material, String> {
51        unsafe {
52            let ptr = self
53                .method(("get_material", 0))
54                .ok_or("Method 'get_material' not found")?
55                .call::<*mut c_void>(&[])?;
56            if ptr.is_null() {
57                return Err("Material is null".to_string());
58            }
59            Ok(Material::from_ptr(ptr))
60        }
61    }
62
63    /// Assigns a Material to the renderer.
64    ///
65    /// # Arguments
66    /// * `material` - The material to assign
67    ///
68    /// # Returns
69    /// * `Result<(), String>` - Ok if success
70    pub fn set_material(&self, material: Material) -> Result<(), String> {
71        unsafe {
72            self.method(("set_material", 1))
73                .ok_or("Method 'set_material' not found")?
74                .call::<()>(&[material.object.ptr as *mut c_void])?;
75        }
76        Ok(())
77    }
78
79    /// Returns all the instantiated materials of this object.
80    ///
81    /// # Returns
82    /// * `Result<Vec<Material>, String>` - A vector containing all materials
83    pub fn get_materials(&self) -> Result<Vec<Material>, String> {
84        unsafe {
85            let ptr = self
86                .method(("get_materials", 0))
87                .ok_or("Method 'get_materials' not found")?
88                .call::<*mut Il2cppArray<*mut c_void>>(&[])?;
89
90            if ptr.is_null() {
91                return Ok(Vec::new());
92            }
93
94            Ok((*ptr)
95                .to_vector()
96                .into_iter()
97                .filter(|&p| !p.is_null())
98                .map(Material::from_ptr)
99                .collect())
100        }
101    }
102}