webizen_studio/render/
scene.rs1#[derive(Clone, Copy, Debug, Default, PartialEq)]
11pub struct Vec3 {
12 pub x: f64,
13 pub y: f64,
14 pub z: f64,
15}
16
17impl Vec3 {
18 pub fn new(x: f64, y: f64, z: f64) -> Self {
20 Self { x, y, z }
21 }
22
23 pub fn sub(self, other: Self) -> Self {
25 Self::new(self.x - other.x, self.y - other.y, self.z - other.z)
26 }
27
28 pub fn add(self, other: Self) -> Self {
30 Self::new(self.x + other.x, self.y + other.y, self.z + other.z)
31 }
32
33 pub fn scale(self, k: f64) -> Self {
35 Self::new(self.x * k, self.y * k, self.z * k)
36 }
37
38 pub fn dot(self, other: Self) -> f64 {
40 self.x * other.x + self.y * other.y + self.z * other.z
41 }
42
43 pub fn cross(self, other: Self) -> Self {
45 Self::new(
46 self.y * other.z - self.z * other.y,
47 self.z * other.x - self.x * other.z,
48 self.x * other.y - self.y * other.x,
49 )
50 }
51
52 pub fn length(self) -> f64 {
54 self.dot(self).sqrt()
55 }
56
57 pub fn normalize(self) -> Self {
59 let len = self.length();
60 if len <= f64::EPSILON {
61 Self::default()
62 } else {
63 Self::new(self.x / len, self.y / len, self.z / len)
64 }
65 }
66}
67
68#[derive(Clone, Copy, Debug, Default, PartialEq)]
71pub struct ScreenPoint {
72 pub x: f64,
73 pub y: f64,
74 pub depth: f64,
75}
76
77#[derive(Clone, Copy, Debug, PartialEq)]
81pub struct Camera {
82 pub eye: Vec3,
83 pub target: Vec3,
84 pub world_up: Vec3,
85 pub focal_scale: f64,
86}
87
88impl Default for Camera {
89 fn default() -> Self {
90 Self {
91 eye: Vec3::new(0.0, 8.0, 18.0),
92 target: Vec3::new(0.0, 0.5, 0.0),
93 world_up: Vec3::new(0.0, 1.0, 0.0),
94 focal_scale: 0.88,
95 }
96 }
97}
98
99impl Camera {
100 pub fn project(&self, world: Vec3, viewport: (f64, f64)) -> Option<ScreenPoint> {
105 let forward = self.target.sub(self.eye).normalize();
106 let right = forward.cross(self.world_up).normalize();
107 let up = right.cross(forward).normalize();
108 let relative = world.sub(self.eye);
109
110 let view_x = relative.dot(right);
111 let view_y = relative.dot(up);
112 let view_z = relative.dot(forward);
113
114 if view_z <= 0.2 {
115 return None;
116 }
117
118 let focal = viewport.0.min(viewport.1) * self.focal_scale;
119 Some(ScreenPoint {
120 x: viewport.0 * 0.5 + (view_x / view_z) * focal,
121 y: viewport.1 * 0.52 - (view_y / view_z) * focal,
122 depth: view_z,
123 })
124 }
125
126 pub fn orbit(&mut self, yaw: f64, pitch: f64) {
130 let dx = self.eye.x - self.target.x;
132 let dy = self.eye.y - self.target.y;
133 let dz = self.eye.z - self.target.z;
134
135 let radius = (dx * dx + dy * dy + dz * dz).sqrt();
137 let current_yaw = dz.atan2(dx);
138 let current_pitch = (dy / radius).asin();
139
140 let new_yaw = current_yaw + yaw;
142 let new_pitch = (current_pitch + pitch).clamp(
143 -std::f64::consts::FRAC_PI_2 + 0.01,
144 std::f64::consts::FRAC_PI_2 - 0.01,
145 );
146
147 let cos_pitch = new_pitch.cos();
149 let sin_pitch = new_pitch.sin();
150 let cos_yaw = new_yaw.cos();
151 let sin_yaw = new_yaw.sin();
152
153 self.eye.x = self.target.x + radius * cos_pitch * cos_yaw;
155 self.eye.y = self.target.y + radius * sin_pitch;
156 self.eye.z = self.target.z + radius * cos_pitch * sin_yaw;
157 }
158
159 pub fn zoom(&mut self, delta: f64) {
163 let dx = self.eye.x - self.target.x;
165 let dy = self.eye.y - self.target.y;
166 let dz = self.eye.z - self.target.z;
167 let current_distance = (dx * dx + dy * dy + dz * dz).sqrt();
168
169 let zoom_factor = (-delta * 0.1).exp(); let new_distance = (current_distance * zoom_factor).max(0.5);
172
173 let scale = new_distance / current_distance;
175 self.eye.x = self.target.x + dx * scale;
176 self.eye.y = self.target.y + dy * scale;
177 self.eye.z = self.target.z + dz * scale;
178 }
179
180 pub fn pan(&mut self, dx: f64, dy: f64) {
184 let forward = self.target.sub(self.eye).normalize();
186 let right = forward.cross(self.world_up).normalize();
187 let up = right.cross(forward).normalize();
188
189 let distance = self.eye.sub(self.target).length();
191 let pan_scale = distance * 0.001;
192
193 let pan = right.scale(dx * pan_scale).add(up.scale(dy * pan_scale));
195 self.eye = self.eye.add(pan);
196 self.target = self.target.add(pan);
197 }
198}