Debian Codegraph @

third_party/rust/metal/examples/raytracing/scene.rs

raw
1 use std::sync::Arc;
2
3 use glam::{Mat4, Vec3, Vec4};
4 use rand::{thread_rng, Rng};
5
6 use metal::{Buffer, Device, NSRange, NSUInteger};
7
8 use super::{camera::Camera, geometry::*};
9
10 pub struct Scene {
11 pub device: Device,
12 pub camera: Camera,
13 pub geometries: Vec<Arc<dyn Geometry>>,
14 pub geometry_instances: Vec<GeometryInstance>,
15 pub lights: Vec<AreaLight>,
16 pub lights_buffer: Buffer,
17 }
18
19 impl Scene {
20 pub fn new(device: Device) -> Self {
21 let mut geometries = Vec::<Arc<dyn Geometry>>::new();
22 let mut light_mesh = TriangleGeometry::new(device.clone(), "light".to_string());
23 let transform = Mat4::from_translation(Vec3::new(0.0, 1.0, 0.0))
24 * Mat4::from_scale(Vec3::new(0.5, 1.98, 0.5));
25 light_mesh.add_cube_with_faces(
26 FACE_MASK_POSITIVE_Y,
27 Vec3::new(1.0, 1.0, 1.0),
28 transform,
29 true,
30 );
31 light_mesh.upload_to_buffers();
32 let light_mesh = Arc::new(light_mesh);
33 geometries.push(light_mesh.clone());
34
35 let mut geometry_mesh = TriangleGeometry::new(device.clone(), "geometry".to_string());
36 let transform = Mat4::from_translation(Vec3::new(0.0, 1.0, 0.0))
37 * Mat4::from_scale(Vec3::new(2.0, 2.0, 2.0));
38 geometry_mesh.add_cube_with_faces(
39 FACE_MASK_NEGATIVE_Y | FACE_MASK_POSITIVE_Y | FACE_MASK_NEGATIVE_Z,
40 Vec3::new(0.725, 0.71, 0.68),
41 transform,
42 true,
43 );
44 geometry_mesh.add_cube_with_faces(
45 FACE_MASK_NEGATIVE_X,
46 Vec3::new(0.63, 0.065, 0.05),
47 transform,
48 true,
49 );
50 geometry_mesh.add_cube_with_faces(
51 FACE_MASK_POSITIVE_X,
52 Vec3::new(0.14, 0.45, 0.091),
53 transform,
54 true,
55 );
56 let transform = Mat4::from_translation(Vec3::new(-0.335, 0.6, -0.29))
57 * Mat4::from_rotation_y(0.3)
58 * Mat4::from_scale(Vec3::new(0.6, 1.2, 0.6));
59 geometry_mesh.add_cube_with_faces(
60 FACE_MASK_ALL,
61 Vec3::new(0.725, 0.71, 0.68),
62 transform,
63 false,
64 );
65 geometry_mesh.upload_to_buffers();
66 let geometry_mesh = Arc::new(geometry_mesh);
67 geometries.push(geometry_mesh.clone());
68
69 let mut sphere_geometry = SphereGeometry::new(device.clone());
70 sphere_geometry.add_sphere_with_origin(
71 Vec3::new(0.3275, 0.3, 0.3725),
72 0.3,
73 Vec3::new(0.725, 0.71, 0.68),
74 );
75 sphere_geometry.upload_to_buffers();
76 let sphere_geometry = Arc::new(sphere_geometry);
77 geometries.push(sphere_geometry.clone());
78
79 let mut rng = thread_rng();
80 let mut geometry_instances = Vec::new();
81 let mut lights = Vec::new();
82 for y in -1..2 {
83 for x in -1..2 {
84 let transform =
85 Mat4::from_translation(Vec3::new(x as f32 * 2.5, y as f32 * 2.5, 0.0));
86 geometry_instances.push(GeometryInstance {
87 geometry: light_mesh.clone(),
88 transform,
89 mask: GEOMETRY_MASK_LIGHT,
90 index_in_scene: 0,
91 });
92 geometry_instances.push(GeometryInstance {
93 geometry: geometry_mesh.clone(),
94 transform,
95 mask: GEOMETRY_MASK_TRIANGLE,
96 index_in_scene: 1,
97 });
98 geometry_instances.push(GeometryInstance {
99 geometry: sphere_geometry.clone(),
100 transform,
101 mask: GEOMETRY_MASK_SPHERE,
102 index_in_scene: 2,
103 });
104 lights.push(AreaLight {
105 position: Vec4::new(x as f32 * 2.5, y as f32 * 2.5 + 1.98, 0.0, 0.0),
106 forward: Vec4::new(0.0, -1.0, 0.0, 0.0),
107 right: Vec4::new(0.25, 0.0, 0.0, 0.0),
108 up: Vec4::new(0.0, 0.0, 0.25, 0.0),
109 colour: Vec4::new(
110 rng.gen_range(0f32..=1.0),
111 rng.gen_range(0f32..=1.0),
112 rng.gen_range(0f32..=1.0),
113 0.0,
114 ),
115 });
116 }
117 }
118 let lights_buffer = device.new_buffer_with_data(
119 lights.as_ptr().cast(),
120 size_of_val(lights.as_slice()) as NSUInteger,
121 get_managed_buffer_storage_mode(),
122 );
123 lights_buffer.did_modify_range(NSRange::new(0, lights_buffer.length()));
124 lights_buffer.set_label("lights buffer");
125
126 Self {
127 device,
128 camera: Camera::new(),
129 geometries,
130 geometry_instances,
131 lights,
132 lights_buffer,
133 }
134 }
135 }