Skip to main content

3d_render_textures/
3d_render_textures.rs

1use sge::prelude::*;
2
3struct MovingCircle {
4    circle: Circle,
5    velocity: Vec2,
6}
7
8impl MovingCircle {
9    pub fn random() -> Self {
10        Self {
11            circle: Circle {
12                center: Vec2::new(rand::<f32>() * 1000.0, rand::<f32>() * 1000.0),
13                radius: Vec2::splat(rand::<f32>() * 30.0),
14                color: Color::from_oklch(0.7493, 0.1184, rand::<f32>() * 360.0),
15            },
16            velocity: Vec2::ZERO,
17        }
18    }
19}
20
21#[main("3D render textures")]
22async fn main() -> anyhow::Result<()> {
23    set_magnify_filter(MagnifySamplerFilter::Linear);
24
25    let mut camera_controller = OrbitCameraController::new(Vec3::ZERO);
26
27    let render_texture = create_empty_render_texture(1000, 1000)?;
28    let textured_material = create_textured_material(render_texture.color_texture);
29    let textured_cube = Object3D::from_obj_bytes_with_material(
30        include_bytes!("../assets/models/cube.obj"),
31        textured_material,
32    )?;
33
34    let mut circles: Vec<_> = (0..500).map(|_| MovingCircle::random()).collect();
35
36    loop {
37        clear_screen(Color::WHITE);
38
39        camera_controller.update();
40
41        start_rendering_to_texture(render_texture);
42        clear_screen(Color::NEUTRAL_100);
43        for circle in circles.iter_mut() {
44            let r = || (rand::<f32>() - 0.5) * 1.0;
45            circle.velocity += Vec2::new(r(), r());
46            circle.circle.center += circle.velocity;
47            circle.circle.center %= 1000.0;
48            draw(&circle.circle);
49        }
50        end_rendering_to_texture();
51
52        textured_cube.draw();
53
54        if should_quit() {
55            break;
56        }
57
58        next_frame().await;
59    }
60
61    Ok(())
62}