Skip to content

Commit 1ba9f15

Browse files
author
Jakub Hlusička
committedDec 27, 2019
Remove hardcoded model paths; Include UI models in the source code
1 parent 37a3ab9 commit 1ba9f15

File tree

7 files changed

+64
-29
lines changed

7 files changed

+64
-29
lines changed
 

‎.gitignore

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
# user-provided resources
2+
resources/showcase
3+
4+
# generated by wasm-pack
5+
pkg
6+
7+
# cargo
18
/target
29
**/*.rs.bk
310
Cargo.lock
4-
pkg

‎Cargo.toml

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ edition = "2018"
1010
crate-type = ['cdylib']
1111

1212
[dependencies]
13-
wasm-bindgen = "0.2.55"
14-
lazy_static = "1.4.0"
1513
ammolite-math = { path = "../ammolite/ammolite-math" }
16-
serde = { version = "1.0", features = ["derive"] }
17-
json5 = "0.2.5"
1814
mlib = { path = "../mlib" }
15+
include_dir = { git = "https://github.com/erickt/include_dir", branch = "sort" }
16+
json5 = "0.2.5"
17+
lazy_static = "1.4.0"
18+
regex = "1.3.1"
19+
serde = { version = "1.0", features = ["derive"] }
20+
wasm-bindgen = "0.2.55"

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Prerequisites:
44
* [https://rustwasm.github.io/wasm-pack/](wasm-pack): A tool for compiling Rust to WASM
55

66
Building:
7+
* Add `.glb` files to `resources/showcase` and make sure to give them a name in the following format:
8+
* `NAME_(SCALE).glb`
9+
* for example: `my_frog_(0.01).glb`
10+
* Run the following command from the project root:
711
```sh
812
rm -rf pkg; env WASM_INTERFACE_TYPES=1 wasm-pack build --release
913
```

‎resources/ui/button_next.glb

38.6 KB
Binary file not shown.

‎resources/ui/button_previous.glb

83.5 KB
Binary file not shown.

‎resources/ui/sphere_1m_radius.glb

189 KB
Binary file not shown.

‎src/lib.rs

+47-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::time::Duration;
22
use std::collections::VecDeque;
3+
use include_dir::{include_dir, DirEntry};
4+
use regex::Regex;
5+
use lazy_static::lazy_static;
36
use ammolite_math::*;
47
use wasm_bindgen::prelude::*;
58
use mlib::*;
@@ -57,25 +60,45 @@ macro_rules! dbg {
5760
};
5861
}
5962

60-
// const MODEL_MAIN_BYTES: &'static [u8] = include_bytes!(env!("MODEL"));
61-
// const MODEL_MAIN_BYTES: &'static [u8] = include_bytes!("/home/limeth/Downloads/meteor-crater-arizona/source/Meteor Crater.glb");
62-
const MODEL_BUTTON_PREVIOUS_BYTES: &'static [u8] = include_bytes!("/home/limeth/Documents/School/mvr/metaview models/button_previous.glb");
63-
const MODEL_BUTTON_NEXT_BYTES: &'static [u8] = include_bytes!("/home/limeth/Documents/School/mvr/metaview models/button_next.glb");
64-
// TODO: Use a procedural macro to output include_bytes! macros for every model
65-
// in the resources directory (if a feature is used/release build; otherwise it
66-
// would slow down linting)
67-
const MODELS_MAIN_LEN: usize = 1;
68-
const MODELS_MAIN_BYTES_SCALE: [(&'static [u8], f32); MODELS_MAIN_LEN] = [
69-
// (include_bytes!("../../ammolite/resources/DamagedHelmet/glTF-Binary/DamagedHelmet.glb"), 1.0),
70-
// (include_bytes!("../../ammolite/resources/Corset/glTF-Binary/Corset.glb"), 40.0),
71-
// (include_bytes!("../../ammolite/resources/AntiqueCamera/glTF-Binary/AntiqueCamera.glb"), 0.1),
72-
(include_bytes!("../../ammolite/resources/WaterBottle/glTF-Binary/WaterBottle.glb"), 5.0),
73-
// (include_bytes!("/home/limeth/Documents/School/mvr/team07/mvr_3D_models/hlusijak/laser/zaba.glb"), 0.01),
74-
];
75-
const MODEL_MARKER_BYTES: &'static [u8] = include_bytes!("../../ammolite/resources/sphere_1m_radius.glb");
7663
const SELECTION_DELAY: f32 = 1.0;
77-
const ANIMATION_SPEED: f32 = 0.0;
78-
const ENTITY_COUNT: usize = 20;
64+
const ANIMATION_SPEED: f32 = 0.2;
65+
const ENTITY_COUNT: usize = 3;
66+
67+
const MODEL_BUTTON_PREVIOUS_BYTES: &'static [u8] = include_bytes!("../resources/ui/button_previous.glb");
68+
const MODEL_BUTTON_NEXT_BYTES: &'static [u8] = include_bytes!("../resources/ui/button_next.glb");
69+
const MODEL_MARKER_BYTES: &'static [u8] = include_bytes!("../resources/ui/sphere_1m_radius.glb");
70+
71+
lazy_static! {
72+
static ref MODELS_MAIN_BYTES_SCALE: Vec<(&'static [u8], f32)> = {
73+
let dir = include_dir!("resources/showcase");
74+
let files = dir.find("**/*_(*).glb")
75+
.expect("Could not traverse the resource directory tree.")
76+
.flat_map(|dir_entry| {
77+
match dir_entry {
78+
DirEntry::File(file) => Some(file),
79+
DirEntry::Dir(_) => None,
80+
}
81+
})
82+
.collect::<Vec<_>>();
83+
84+
if files.len() <= 0 {
85+
panic!("No `.glb` glTF models in the `resources/showcase` directory.")
86+
}
87+
88+
files.into_iter()
89+
.map(|file| {
90+
lazy_static! {
91+
static ref PATTERN: Regex = Regex::new(r"^(?P<name>.*)_\((?P<scale>.*?)\)$").unwrap();
92+
}
93+
let stem = file.path().file_stem().unwrap().to_str().unwrap();
94+
let captures = PATTERN.captures(stem).unwrap();
95+
let scale = captures.name("scale").unwrap().as_str().parse().unwrap();
96+
97+
(file.contents(), scale)
98+
})
99+
.collect::<Vec<_>>()
100+
};
101+
}
79102

80103
fn construct_model_matrix(scale: f32, translation: &Vec3, rotation: &Vec3) -> Mat4 {
81104
Mat4::translation(translation)
@@ -114,7 +137,7 @@ pub struct ExampleMapp {
114137
commands: VecDeque<Command>,
115138
view_orientations: Option<Vec<Option<Orientation>>>,
116139
root_entity: Option<Entity>,
117-
models_main: [Option<Model>; MODELS_MAIN_LEN],
140+
models_main: Vec<Option<Model>>,
118141
model_marker: Option<Model>,
119142
model_button_previous: Option<Model>,
120143
model_button_next: Option<Model>,
@@ -147,13 +170,13 @@ impl ExampleMapp {
147170
}
148171

149172
fn change_main_model_next(&mut self) {
150-
let new_index = (self.current_main_model_index + 1) % MODELS_MAIN_LEN;
173+
let new_index = (self.current_main_model_index + 1) % MODELS_MAIN_BYTES_SCALE.len();
151174
self.change_main_model_index(new_index);
152175
}
153176

154177
fn change_main_model_previous(&mut self) {
155178
let new_index = if self.current_main_model_index == 0 {
156-
MODELS_MAIN_LEN - 1
179+
MODELS_MAIN_BYTES_SCALE.len() - 1
157180
} else {
158181
self.current_main_model_index - 1
159182
};
@@ -171,7 +194,7 @@ impl Mapp for ExampleMapp {
171194
commands: VecDeque::new(),
172195
view_orientations: None,
173196
root_entity: None,
174-
models_main: [None; MODELS_MAIN_LEN],
197+
models_main: vec![None; MODELS_MAIN_BYTES_SCALE.len()],
175198
model_marker: None,
176199
model_button_previous: None,
177200
model_button_next: None,
@@ -184,7 +207,7 @@ impl Mapp for ExampleMapp {
184207
current_selection: None,
185208
};
186209
result.cmd(CommandKind::EntityRootGet);
187-
for (model_bytes, _) in &MODELS_MAIN_BYTES_SCALE {
210+
for (model_bytes, _) in &MODELS_MAIN_BYTES_SCALE[..] {
188211
result.cmd(CommandKind::ModelCreate {
189212
data: model_bytes.into(),
190213
});
@@ -225,7 +248,7 @@ impl Mapp for ExampleMapp {
225248

226249
let transform = construct_model_matrix(
227250
MODELS_MAIN_BYTES_SCALE[self.current_main_model_index].1,
228-
&[0.0, 0.0, 2.0 + 1.0 * index as f32].into(),
251+
&[0.0, 0.0, 2.0 + 4.0 * index as f32].into(),
229252
&[(secs_elapsed * ANIMATION_SPEED).sin() * 1.0, std::f32::consts::PI + (secs_elapsed * ANIMATION_SPEED).cos() * 3.0 / 2.0, 0.0].into(),
230253
);
231254

0 commit comments

Comments
 (0)
Please sign in to comment.