-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlib.rs
More file actions
528 lines (480 loc) · 18.3 KB
/
lib.rs
File metadata and controls
528 lines (480 loc) · 18.3 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Runs through all the gltf sample models to test and show-off renderling's
//! gltf capabilities.
use std::{
collections::{HashMap, HashSet},
sync::{Arc, Mutex},
};
use glam::{Mat4, UVec2, Vec2, Vec3, Vec4};
use renderling::{
atlas::AtlasImage,
bvol::{Aabb, BoundingSphere},
camera::Camera,
context::Context,
geometry::Vertex,
glam,
gltf::{Animator, GltfDocument},
light::{AnalyticalLight, Lux},
primitive::Primitive,
skybox::Skybox,
stage::Stage,
ui::{FontArc, Section, Text, Ui, UiPath, UiText},
};
pub mod camera;
use camera::{
CameraControl, CameraController, TurntableCameraController, WasdMouseCameraController,
};
pub mod time;
use time::FPSCounter;
pub mod utils;
const FONT_BYTES: &[u8] =
include_bytes!("../../../fonts/Recursive Mn Lnr St Med Nerd Font Complete.ttf");
const DARK_BLUE_BG_COLOR: Vec4 = Vec4::new(
0x30 as f32 / 255.0,
0x35 as f32 / 255.0,
0x42 as f32 / 255.0,
1.0,
);
pub enum SupportedFileType {
Gltf,
Hdr,
}
pub fn is_file_supported(file: impl AsRef<std::path::Path>) -> Option<SupportedFileType> {
let ext = file.as_ref().extension()?;
Some(match ext.to_str()? {
"hdr" => SupportedFileType::Hdr,
_ => SupportedFileType::Gltf,
})
}
#[cfg(not(target_arch = "wasm32"))]
lazy_static::lazy_static! {
static ref START: std::time::Instant = std::time::Instant::now();
}
fn now() -> f64 {
#[cfg(target_arch = "wasm32")]
{
let doc = web_sys::window().unwrap();
let perf = doc.performance().unwrap();
perf.now()
}
#[cfg(not(target_arch = "wasm32"))]
{
let now = std::time::Instant::now();
let duration = now.duration_since(*START);
duration.as_secs_f64()
}
}
struct AppUi {
ui: Ui,
fps_text: UiText,
fps_counter: FPSCounter,
fps_background: UiPath,
last_fps_display: f64,
}
impl AppUi {
fn make_fps_widget(fps_counter: &FPSCounter, ui: &Ui) -> (UiText, UiPath) {
let translation = Vec2::new(2.0, 2.0);
let text = format!("{}fps", fps_counter.current_fps_string());
let fps_text = ui
.text_builder()
.with_color(Vec3::ZERO.extend(1.0))
.with_section(Section::new().add_text(Text::new(&text).with_scale(32.0)))
.build();
fps_text.transform().set_translation(translation);
let background = ui
.path_builder()
.with_fill_color(Vec4::ONE)
.with_rectangle(fps_text.bounds().0, fps_text.bounds().1)
.fill();
background.transform.set_translation(translation);
background.transform.set_z(-0.9);
(fps_text, background)
}
fn tick(&mut self) {
self.fps_counter.next_frame();
let now = now();
if now - self.last_fps_display >= 1.0 {
let (fps_text, background) = Self::make_fps_widget(&self.fps_counter, &self.ui);
self.fps_text = fps_text;
self.fps_background = background;
self.last_fps_display = now;
}
}
}
pub enum Model {
Gltf(Box<GltfDocument>),
Default(Primitive),
None,
}
pub struct App {
last_frame_instant: f64,
skybox_image_bytes: Option<Vec<u8>>,
loads: Arc<Mutex<HashMap<std::path::PathBuf, Vec<u8>>>>,
pub stage: Stage,
camera: Camera,
_lighting: AnalyticalLight,
model: Model,
animators: Option<Vec<Animator>>,
animations_conflict: bool,
pub camera_controller: Box<dyn CameraController + 'static>,
ui: AppUi,
}
impl App {
pub fn new(ctx: &Context, camera_control: CameraControl) -> Self {
// Use library defaults for atlas size and MSAA -- the Context
// auto-detects appropriate settings based on GPU capabilities.
let stage = ctx
.new_stage()
.with_background_color(DARK_BLUE_BG_COLOR)
.with_bloom_mix_strength(0.5)
.with_bloom_filter_radius(4.0);
let size = ctx.get_size();
let (proj, view) =
renderling::camera::default_perspective(size.x as f32, size.y as f32);
let camera = stage.new_camera().with_projection_and_view(proj, view);
let sunlight = stage
.new_directional_light()
.with_direction(Vec3::NEG_Y)
.with_color(renderling::math::hex_to_vec4(0xFDFBD3FF))
.with_intensity(Lux::OUTDOOR_SUNSET);
let ui = Ui::new(ctx).with_background_color(Vec4::ZERO);
let _ = ui.add_font(FontArc::try_from_slice(FONT_BYTES).unwrap());
let fps_counter = FPSCounter::default();
let (fps_text, fps_background) = AppUi::make_fps_widget(&fps_counter, &ui);
Self {
ui: AppUi {
ui,
fps_text,
fps_counter,
fps_background,
last_fps_display: now(),
},
stage,
camera,
_lighting: sunlight.into_generic(),
model: Model::None,
animators: None,
animations_conflict: false,
skybox_image_bytes: None,
loads: Arc::new(Mutex::new(HashMap::default())),
last_frame_instant: now(),
camera_controller: match camera_control {
CameraControl::Turntable => Box::new(TurntableCameraController::default()),
CameraControl::WasdMouse => Box::new(WasdMouseCameraController::default()),
},
}
}
pub fn tick(&mut self) {
self.camera_controller.tick();
self.tick_loads();
self.update_view();
self.animate();
self.ui.tick();
}
pub fn render(&self, ctx: &Context) {
log::info!("render");
let frame = ctx.get_next_frame().unwrap();
self.stage.render(&frame.view());
self.ui.ui.render(&frame.view());
frame.present();
log::info!("render done");
}
pub fn update_view(&mut self) {
self.camera_controller
.update_camera(self.stage.get_size(), &self.camera);
}
pub fn load_hdr_skybox(&mut self, bytes: Vec<u8>) {
log::info!("loading skybox");
let img = AtlasImage::from_hdr_bytes(&bytes).unwrap();
let skybox = Skybox::new(self.stage.runtime(), img);
self.skybox_image_bytes = Some(bytes);
self.stage.use_skybox(&skybox);
let ibl = self.stage.new_ibl(&skybox);
self.stage.use_ibl(&ibl);
}
pub fn load_default_model(&mut self) {
log::info!("loading default model");
let mut min = Vec3::splat(f32::INFINITY);
let mut max = Vec3::splat(f32::NEG_INFINITY);
self.last_frame_instant = now();
let vertices = self
.stage
.new_vertices(renderling::math::unit_cube().into_iter().map(|(p, n)| {
let p = p * 2.0;
min = min.min(p);
max = max.max(p);
Vertex::default()
.with_position(p)
.with_normal(n)
.with_color(Vec4::new(1.0, 0.0, 0.0, 1.0))
}));
let primitive = self
.stage
.new_primitive()
.with_vertices(vertices)
.with_bounds({
log::info!("default model bounds: {min} {max}");
BoundingSphere::from((min, max))
});
self.model = Model::Default(primitive);
self.camera_controller.reset(Aabb::new(min, max));
self.camera_controller
.update_camera(self.stage.get_size(), &self.camera);
}
fn load_gltf_model(&mut self, path: impl AsRef<std::path::Path>, bytes: &[u8]) {
log::info!("loading gltf");
self.camera_controller
.reset(Aabb::new(Vec3::NEG_ONE, Vec3::ONE));
self.stage.clear_images().unwrap();
self.model = Model::None;
let doc = match self.stage.load_gltf_document_from_bytes(bytes) {
Err(e) => {
log::error!("gltf loading error: {e}");
if cfg!(not(target_arch = "wasm32")) {
log::info!("attempting to load by filesystem");
match self.stage.load_gltf_document_from_path(path) {
Ok(doc) => doc,
Err(e) => {
log::error!("gltf loading error: {e}");
return;
}
}
} else {
return;
}
}
Ok(doc) => doc,
};
// find the bounding box of the model so we can display it correctly
let mut min = Vec3::splat(f32::INFINITY);
let mut max = Vec3::splat(f32::NEG_INFINITY);
let scene = doc.default_scene.unwrap_or(0);
log::info!("Displaying scene {scene}");
fn get_children(doc: &GltfDocument, n: usize) -> Vec<usize> {
let mut children = vec![];
if let Some(parent) = doc.nodes.get(n) {
children.extend(parent.children.iter().copied());
let descendants = parent
.children
.iter()
.copied()
.flat_map(|n| get_children(doc, n));
children.extend(descendants);
}
children
}
let nodes = doc.nodes_in_scene(scene).flat_map(|n| {
let mut all_nodes = vec![n];
for child_index in get_children(&doc, n.index) {
if let Some(child_node) = doc.nodes.get(child_index) {
all_nodes.push(child_node);
}
}
all_nodes
});
log::trace!(" nodes:");
for node in nodes {
let tfrm = Mat4::from(node.global_transform());
if let Some(mesh_index) = node.mesh {
// UNWRAP: safe because we know the node exists
for primitive in doc.meshes.get(mesh_index).unwrap().primitives.iter() {
let bbmin = tfrm.transform_point3(primitive.bounding_box.0);
let bbmax = tfrm.transform_point3(primitive.bounding_box.1);
min = min.min(bbmin);
max = max.max(bbmax);
}
}
}
log::trace!("Bounding box: {min} {max}");
let bounding_box = Aabb::new(min, max);
self.camera_controller.reset(bounding_box);
self.camera_controller
.update_camera(self.stage.get_size(), &self.camera);
self.last_frame_instant = now();
if doc.animations.is_empty() {
log::trace!(" animations: none");
} else {
log::trace!(" animations:");
}
let mut animated_nodes = HashSet::default();
let mut has_conflicting_animations = false;
self.animators = Some(
doc.animations
.iter()
.enumerate()
.map(|(i, a)| {
let target_nodes = a.target_node_indices().collect::<HashSet<_>>();
has_conflicting_animations =
has_conflicting_animations || !animated_nodes.is_disjoint(&target_nodes);
animated_nodes.extend(target_nodes);
log::trace!(" {i} {:?} {}s", a.name, a.length_in_seconds());
// for (t, tween) in a.tweens.iter().enumerate() {
// log::trace!(
// " tween {t} targets node {} {}",
// tween.target_node_index,
// tween.properties.description()
// );
// }
Animator::new(doc.nodes.iter(), a.clone())
})
.collect(),
);
if has_conflicting_animations {
log::trace!(" and some animations conflict");
}
self.animations_conflict = has_conflicting_animations;
// // Update lights and shadows
// for light in doc.lights.iter() {
// if let Some(dir) = light.details.as_directional() {
// log::info!("found a directional light to use for shadows");
// {
// let (p, j) = dir.get().shadow_mapping_projection_and_view(
// &light.node_transform.get_global_transform().into(),
// &self.camera.get(),
// );
// let mut guard = self.lighting.shadow_map.descriptor_lock();
// guard.light_space_transform = p * j;
// }
// self.lighting
// .shadow_map
// .update(&self.lighting.lighting, doc.primitives.values().flatten());
// self.lighting.light = light.light.clone();
// self.lighting.light_details = dir.clone();
// }
// }
self.model = Model::Gltf(Box::new(doc));
}
pub fn tick_loads(&mut self) {
let loaded = std::mem::take(&mut *self.loads.lock().unwrap());
for (path, bytes) in loaded.into_iter() {
log::info!("loaded {}bytes from {}", bytes.len(), path.display());
match is_file_supported(&path) {
Some(SupportedFileType::Gltf) => self.load_gltf_model(path, &bytes),
Some(SupportedFileType::Hdr) => self.load_hdr_skybox(bytes),
None => {}
}
}
}
/// Queues a load operation.
pub fn load(&mut self, path: &str) {
let path = std::path::PathBuf::from(path);
let loads = self.loads.clone();
#[cfg(target_arch = "wasm32")]
{
wasm_bindgen_futures::spawn_local(async move {
let path_str = format!("{}", path.display());
let bytes = loading_bytes::load(&path_str).await.unwrap();
let mut loads = loads.lock().unwrap();
loads.insert(path, bytes);
log::debug!("loaded {path_str}");
});
}
#[cfg(not(target_arch = "wasm32"))]
{
let _ = std::thread::spawn(move || {
let bytes = std::fs::read(&path)
.unwrap_or_else(|e| panic!("could not load '{}': {e}", path.display()));
let mut loads = loads.lock().unwrap();
loads.insert(path, bytes);
});
}
}
pub fn set_size(&mut self, size: UVec2) {
self.stage.set_size(size);
}
pub fn animate(&mut self) {
let now = now();
let dt_seconds = now - self.last_frame_instant;
self.last_frame_instant = now;
self.camera_controller.tick();
if let Some(animators) = self.animators.as_mut() {
if self.animations_conflict {
if let Some(animator) = animators.first_mut() {
animator.progress(dt_seconds as f32).unwrap();
}
} else {
for animator in animators.iter_mut() {
animator.progress(dt_seconds as f32).unwrap();
}
}
}
}
}
// /// Sets up the demo for a given model
// pub async fn demo(
// ctx: &Context,
// model: Option<impl AsRef<str>>,
// skybox: Option<impl AsRef<str>>,
// ) -> impl FnMut(&mut Context, Option<&winit::event::WindowEvent>) {
// let mut app = App::new(ctx).await;
// if let Some(file) = model {
// app.load(file.as_ref());
// }
// if let Some(file) = skybox {
// app.load(file.as_ref());
// }
// let mut event_state = renderling_gpui::EventState::default();
// move |r, ev: Option<&winit::event::WindowEvent>| {
// if let Some(ev) = ev {
// match ev {
// winit::event::WindowEvent::MouseWheel { delta, .. } => {
// let delta = match delta {
// winit::event::MouseScrollDelta::LineDelta(_, up) =>
// *up, winit::event::MouseScrollDelta::PixelDelta(pos)
// => pos.y as f32, };
// app.zoom(r, delta);
// }
// winit::event::WindowEvent::CursorMoved { position, .. } => {
// app.pan(r, *position);
// }
// winit::event::WindowEvent::MouseInput { state, button, .. }
// => { app.mouse_button(*state, *button);
// }
// winit::event::WindowEvent::KeyboardInput { input, .. } => {
// app.key_input(r, *input);
// }
// winit::event::WindowEvent::Resized(size) => {
// log::trace!("resizing to {size:?}");
// app.resize(r, size.width, size.height);
// let _ = app
// .gpui
// .0
// .graph
// .get_resource::<ScreenSize>()
// .unwrap()
// .unwrap();
// }
// winit::event::WindowEvent::DroppedFile(path) => {
// log::trace!("got dropped file event: {}",
// path.display()); let path = format!("{}",
// path.display()); app.load(&path);
// }
// _ => {}
// }
// if let Some(ev) = event_state.event_from_winit(ev) {
// let scene =
// r.graph.get_resource_mut::<Scene>().unwrap().unwrap(); let
// channel = scene.get_debug_channel(); let mut
// set_debug_channel = |mode| { log::debug!("setting debug
// mode to {mode:?}"); if channel != mode {
// scene.set_debug_channel(mode);
// } else {
// scene.set_debug_channel(DebugChannel::None);
// }
// };
// match app.ui.event(ev) {
// None => {}
// Some(ev) => match ev {
// UiEvent::ToggleDebugChannel(channel) =>
// set_debug_channel(channel), },
// }
// }
// } else {
// app.tick_loads(r);
// app.update_camera_view(r);
// app.animate(r);
// app.gpui.layout(&mut app.ui);
// app.gpui.render(&mut app.ui);
// r.render().unwrap();
// }
// }
// }