1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use std::f32::consts::PI;
use zaplib::*;
const EPSILON: f32 = 0.0001;
fn look_at(eye: Vec3, at: Vec3, up: Vec3) -> Mat4 {
let forward = (eye - at).normalize();
let left = Vec3::cross(up, forward).normalize();
let up = Vec3::cross(forward, left);
let mut matrix = Mat4::identity();
matrix.v[0] = left.x;
matrix.v[4] = left.y;
matrix.v[8] = left.z;
matrix.v[1] = up.x;
matrix.v[5] = up.y;
matrix.v[9] = up.z;
matrix.v[2] = forward.x;
matrix.v[6] = forward.y;
matrix.v[10] = forward.z;
matrix.v[12] = -left.dot(eye);
matrix.v[13] = -up.dot(eye);
matrix.v[14] = -forward.dot(eye);
matrix
}
#[derive(Clone, Copy, Debug)]
pub struct SphericalAngles {
pub phi: f32,
pub theta: f32,
pub radius: f32,
}
pub enum Coordinates {
Cartesian(Vec3),
Spherical(SphericalAngles),
}
fn cartesian_to_spherical(position: Vec3) -> SphericalAngles {
let radius = position.length();
SphericalAngles { phi: position.z.atan2(position.y), theta: (position.x / radius).asin(), radius }
}
pub struct Viewport3DProps {
pub initial_camera_position: Coordinates,
pub panning_enabled: bool,
pub camera_target: Vec3,
pub vertical_panning_enabled: bool,
}
impl Viewport3DProps {
pub const DEFAULT: Self = Self {
initial_camera_position: Coordinates::Spherical(SphericalAngles { phi: EPSILON, theta: -PI / 2., radius: 50. }),
camera_target: Vec3::all(0.),
panning_enabled: true,
vertical_panning_enabled: true,
};
}
impl Default for Viewport3DProps {
fn default() -> Self {
Self::DEFAULT
}
}
pub struct Viewport3D {
component_id: ComponentId,
area: Area,
pass: Pass,
clear_color: Vec4,
pub color_texture: Texture,
depth_texture: Texture,
view_2d: View,
view_3d: View,
pub measured_size: Vec2,
camera_position: SphericalAngles,
camera_position_start: Option<SphericalAngles>,
camera_target_offset: Vec3,
camera_target_offset_start: Option<Vec3>,
props: Viewport3DProps,
has_read_props: bool,
}
impl Default for Viewport3D {
fn default() -> Self {
Self {
component_id: Default::default(),
area: Default::default(),
camera_position: SphericalAngles { phi: EPSILON, theta: -PI / 2., radius: 50. },
measured_size: Default::default(),
camera_position_start: Default::default(),
camera_target_offset: Default::default(),
camera_target_offset_start: Default::default(),
pass: Default::default(),
clear_color: Default::default(),
color_texture: Default::default(),
depth_texture: Default::default(),
view_3d: Default::default(),
view_2d: Default::default(),
has_read_props: Default::default(),
props: Default::default(),
}
}
}
impl Viewport3D {
pub fn handle(&mut self, cx: &mut Cx, event: &mut Event) -> Option<PassMatrixMode> {
match event.hits_pointer(cx, self.component_id, self.area.get_rect_for_first_instance(cx)) {
Event::PointerHover(_pe) => {
}
Event::PointerDown(pe) => {
if self.props.panning_enabled && pe.button == MouseButton::Left {
self.camera_target_offset_start = Some(self.camera_target_offset);
} else if pe.button == MouseButton::Right {
self.camera_position_start = Some(self.camera_position);
}
}
Event::PointerUp(_pe) => {
self.camera_position_start = None;
self.camera_target_offset_start = None;
}
Event::PointerScroll(pe) => {
let min_distance = 1.0;
let max_distance = 900.;
let zoom_speed = (self.camera_position.radius * (PI / 4.) / max_distance).sin().abs() / 2.0;
self.camera_position.radius =
(self.camera_position.radius + pe.scroll.y * zoom_speed).max(min_distance).min(max_distance);
return Some(self.pass_set_matrix_mode(cx));
}
Event::PointerMove(pe) => {
if let Some(SphericalAngles { phi, theta, radius }) = self.camera_position_start {
let rotate_speed = 1. / 175.;
self.camera_position = SphericalAngles {
theta: (theta - (pe.abs.x - pe.abs_start.x) * rotate_speed) % (PI * 2.),
phi: (phi - (pe.abs.y - pe.abs_start.y) * rotate_speed).clamp(EPSILON, PI - EPSILON),
radius,
};
return Some(self.pass_set_matrix_mode(cx));
} else if let Some(camera_target_offset_start) = self.camera_target_offset_start {
let pan_speed = 0.8;
let mouse_offset = (pe.rel - pe.rel_start) / self.measured_size.y * self.camera_position.radius * pan_speed;
let vertical_offset =
if self.props.vertical_panning_enabled { self.camera_position.phi.to_degrees() } else { 0. };
self.camera_target_offset = Mat4::rotation(vertical_offset, self.camera_position.theta.to_degrees(), 0.)
.transform_vec4(vec4(-mouse_offset.x, 0., -mouse_offset.y, 1.0))
.to_vec3()
+ camera_target_offset_start;
return Some(self.pass_set_matrix_mode(cx));
}
}
_ => (),
}
None
}
fn get_matrix_projection(&self) -> PassMatrixMode {
let SphericalAngles { phi, theta, radius } = self.camera_position;
let eye = radius * vec3(phi.sin() * theta.sin(), phi.cos(), phi.sin() * theta.cos());
PassMatrixMode::Projection {
fov_y: 40.0,
near: 0.1,
far: 1000.0,
cam: look_at(
eye + self.props.camera_target + self.camera_target_offset,
self.props.camera_target + self.camera_target_offset,
vec3(0., 1., 0.),
),
}
}
fn pass_set_matrix_mode(&mut self, cx: &mut Cx) -> PassMatrixMode {
let matrix_mode = self.get_matrix_projection();
self.pass.set_matrix_mode(cx, matrix_mode.clone());
matrix_mode
}
#[must_use]
pub fn skip_draw(&mut self, cx: &mut Cx) -> bool {
if self.measured_size != vec2(cx.get_width_total(), cx.get_height_total()) {
return false;
}
self.draw_viewport_2d(cx);
true
}
pub fn begin_draw(&mut self, cx: &mut Cx, props: Viewport3DProps) {
if !self.has_read_props {
self.camera_position = match props.initial_camera_position {
Coordinates::Cartesian(cartesian) => cartesian_to_spherical(cartesian),
Coordinates::Spherical(spherical) => spherical,
};
self.has_read_props = true;
}
self.props = props;
self.draw_viewport_2d(cx);
self.pass.begin_pass_without_textures(cx);
self.pass.set_size(cx, self.measured_size);
let color_texture_handle = self.color_texture.get_color(cx);
self.pass.add_color_texture(cx, color_texture_handle, ClearColor::ClearWith(self.clear_color));
let depth_texture_handle = self.depth_texture.get_depth(cx);
self.pass.set_depth_texture(cx, depth_texture_handle, ClearDepth::ClearWith(1.0));
self.view_3d.begin_view(cx, LayoutSize::FILL);
}
pub fn end_draw(&mut self, cx: &mut Cx) -> PassMatrixMode {
let matrix_mode = self.pass_set_matrix_mode(cx);
self.view_3d.end_view(cx);
self.pass.end_pass(cx);
matrix_mode
}
fn draw_viewport_2d(&mut self, cx: &mut Cx) {
self.view_2d.begin_view(cx, LayoutSize::FILL);
self.measured_size = vec2(cx.get_width_total(), cx.get_height_total());
let color_texture_handle = self.color_texture.get_color(cx);
self.area = ImageIns::draw(cx, Rect { pos: cx.get_box_origin(), size: self.measured_size }, color_texture_handle);
self.view_2d.end_view(cx);
}
}