Struct RenderObject

Source
pub struct RenderObject { /* private fields */ }

Implementations§

Source§

impl RenderObject

Source

pub fn new(model: impl Model<TextureVertex>, texture_path: String) -> Self

Examples found in repository?
examples/run/main.rs (line 57)
27    fn build(&self, scene: &mut matrix_engine::engine::scene::Scene<CustomEvents>) {
28        let mut latest = Instant::now();
29        let mut v = Vec::<f32>::new();
30        let mut latest_second = Instant::now();
31        scene.add_send_system(
32            move |(events, write_events, id): &mut (
33                ReadE<CustomEvents>,
34                WriteE<CustomEvents>,
35                ReadSystemID,
36            )| {
37                let now = Instant::now();
38
39                v.push(1.0 / (now - latest).as_secs_f32());
40
41                if (now - latest_second).as_secs() > 0 {
42                    let fps = v.iter().sum::<f32>() / v.len() as f32;
43                    println!("fps: {:10.5}, {:10.5}", fps, 1.0 / fps);
44                    latest_second = now;
45                }
46                latest = now;
47            },
48        );
49        scene.add_send_startup_system(
50            |render_objs: &mut WriteC<RenderObject>,
51             transforms: &mut WriteC<Transform>,
52             camera: &mut WriteR<Camera, CustomEvents>| {
53                for i in 0..100 {
54                    for y in 0..100 {
55                        for z in 0..10 {
56                            let e = Entity::new();
57                            render_objs.insert(e, RenderObject::new(Cube, "./img.jpg".to_string()));
58                            transforms.insert(
59                                e,
60                                Transform::new_position(Vector3::new(
61                                    5. * i as f32,
62                                    5. * y as f32,
63                                    5. * z as f32,
64                                )),
65                            );
66                        }
67                    }
68                }
69
70                camera.insert_and_notify(Camera {
71                    eye: Vector3::new(0.0, 0.0, -1.),
72                    dir: Vector3::new(1., 0., 0.),
73                    up: Vector3::new(0., 1., 0.),
74                    aspect: 1.,
75                    fovy: PI / 4.,
76                    znear: 0.1,
77                    zfar: 1000.,
78                });
79            },
80        );
81
82        let mut yaw: f32 = 0.0; // Horizontal rotation around the y-axis
83        let mut pitch: f32 = 0.0; // Vertical rotation
84        scene.add_send_system(
85            move |camera: &mut WriteR<Camera, CustomEvents>, events: &mut ReadE<CustomEvents>| {
86                if let Some(camera) = camera.get_mut() {
87                    let dt = events.dt();
88                    let move_speed = dt * 10.;
89                    let rotation_speed = 4. * dt * camera.fovy / PI;
90
91                    // Get forward (z-axis), right (x-axis), and up (y-axis) direction vectors
92                    let forward = camera.dir.normalized();
93                    let right = forward.cross(&Vector3::unit_y()).normalized();
94                    let up = right.cross(&forward);
95
96                    if events.keyboard().is_pressed(KeyCode::KeyW) {
97                        camera.eye += &forward * move_speed;
98                    }
99                    if events.keyboard().is_pressed(KeyCode::KeyS) {
100                        camera.eye -= &forward * move_speed;
101                    }
102                    if events.keyboard().is_pressed(KeyCode::KeyA) {
103                        camera.eye -= &right * move_speed;
104                    }
105                    if events.keyboard().is_pressed(KeyCode::KeyD) {
106                        camera.eye += &right * move_speed;
107                    }
108                    if events.keyboard().is_pressed(KeyCode::Space) {
109                        camera.eye += &up * move_speed;
110                    }
111                    if events.keyboard().is_pressed(KeyCode::ControlLeft) {
112                        camera.eye -= &up * move_speed;
113                    }
114
115                    match events.mouse_wheel_delta() {
116                        dx if dx != 0. => {
117                            camera.fovy *= if dx.is_positive() { 0.5 } else { 2. };
118                        }
119                        _ => (),
120                    }
121
122                    let (x, y) = events.mouse_dx();
123                    yaw += x * rotation_speed;
124                    pitch -= y * rotation_speed;
125
126                    // Update the camera's direction (yaw and pitch)
127                    let (sin_yaw, cos_yaw) = yaw.sin_cos();
128                    let (sin_pitch, cos_pitch) = pitch.sin_cos();
129
130                    // Calculate the new forward direction after applying yaw and pitch
131                    let new_forward =
132                        Vector3::new(cos_pitch * cos_yaw, sin_pitch, cos_pitch * sin_yaw);
133
134                    camera.dir = new_forward.normalized();
135                }
136            },
137        );
138        let mut is_on = false;
139        scene.add_send_system(
140            move |transforms: &mut WriteC<Transform>,
141                  obj: &mut ReadC<RenderObject>,
142                  events: &mut ReadE<CustomEvents>| {
143                if events.keyboard().is_just_pressed(KeyCode::KeyG) {
144                    is_on = !is_on;
145                    println!("started {}", is_on);
146                }
147                if is_on {
148                    let dt = events.dt();
149                    (transforms.iter_mut(), obj.iter())
150                        .into_wrapper()
151                        .for_each(|(_, (t, _))| {
152                            *t.rotation.x_mut() += dt * 5.;
153                            t.update_raw();
154                        });
155                }
156            },
157        );
158    }

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeIDable for T
where T: 'static,

Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Component for T
where T: Send + Sync + Any,

Source§

impl<T> Resource for T
where T: Send + 'static + ?Sized,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,