BlueGit

golden-age paperdoll-renderer

Files

paperdoll-renderer / tests / fbx_integration.rs

fbx_integration.rs

fbx_integration.rs 15.3 KiB Download Raw
1//! FBX bodies in the live/production path and rigid FBX equipment import.
2use paperdoll_renderer::equipment::{self, Preset};
3use paperdoll_renderer::preview::{self, Mat4, PreviewCharacter, PreviewControls};
4use paperdoll_renderer::production::{self, RenderJob, SourceFingerprint};
5use paperdoll_renderer::project::CameraFraming;
6use std::path::PathBuf;
7
8fn repo(path: &str) -> PathBuf {
9 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(path)
10}
11fn fbx_body() -> PreviewCharacter {
12 preview::load_body_file(&repo("tests/fixtures/bodies/test-body-male.fbx")).unwrap()
13}
14fn glb_body() -> PreviewCharacter {
15 preview::load_body_file(&repo("tests/fixtures/bodies/test-body-male.glb")).unwrap()
16}
17fn scratch(tag: &str) -> PathBuf {
18 let dir = std::env::temp_dir().join(format!(
19 "paperdoll-fbx-{tag}-{}",
20 std::time::SystemTime::now()
21 .duration_since(std::time::UNIX_EPOCH)
22 .unwrap()
23 .as_nanos()
24 ));
25 std::fs::create_dir_all(&dir).unwrap();
26 dir
27}
28fn job(character: PreviewCharacter, preset: Option<Preset>) -> RenderJob {
29 RenderJob {
30 depth_format: paperdoll_renderer::camera::DepthFormat::GRAY16,
31 character,
32 sex: "male".into(),
33 preset,
34 target_width: 32,
35 target_height: 48,
36 fps: 4,
37 framing: CameraFraming {
38 height_milli: 2400,
39 center_y_milli: 1000,
40 pivot_y_milli: None,
41 depth_range_milli: None,
42 },
43 output: std::env::temp_dir(),
44 project_id: "fbx-test".into(),
45 project_name: "FBX".into(),
46 body_model: SourceFingerprint {
47 path: "assets/body/test-body-male.fbx".into(),
48 sha256: "0".repeat(64),
49 },
50 preset_sha256: None,
51 }
52}
53fn bounds(scene: &production::Scene) -> ([f32; 3], [f32; 3]) {
54 let mut lo = [f32::MAX; 3];
55 let mut hi = [f32::MIN; 3];
56 for draw in &scene.draws {
57 for &i in &draw.indices {
58 let p = draw.positions[i as usize];
59 for k in 0..3 {
60 lo[k] = lo[k].min(p[k]);
61 hi[k] = hi[k].max(p[k]);
62 }
63 }
64 }
65 (lo, hi)
66}
67fn clip_named(body: &PreviewCharacter, prefix: &str) -> String {
68 body.clips
69 .iter()
70 .find(|c| c.name.contains(prefix))
71 .unwrap_or_else(|| panic!("clip {prefix} ontbreekt"))
72 .name
73 .clone()
74}
75fn apply(m: &Mat4, p: [f32; 3]) -> [f32; 3] {
76 [
77 m[0] * p[0] + m[4] * p[1] + m[8] * p[2] + m[12],
78 m[1] * p[0] + m[5] * p[1] + m[9] * p[2] + m[13],
79 m[2] * p[0] + m[6] * p[1] + m[10] * p[2] + m[14],
80 ]
81}
82
83#[test]
84fn body_loader_dispatches_by_extension_and_rejects_other_formats() {
85 assert!(preview::is_drawable_body(std::path::Path::new("a/B.FBX")));
86 assert!(preview::is_drawable_body(std::path::Path::new("b.glb")));
87 // OBJ is drawable as a static body (no skeleton); STL/PLY are not bodies.
88 assert!(preview::is_drawable_body(std::path::Path::new("c.obj")));
89 assert!(!preview::is_drawable_body(std::path::Path::new("d.stl")));
90 let fbx = fbx_body();
91 let direct = paperdoll_renderer::fbx_scene::load_fbx_drawable(&repo(
92 "tests/fixtures/bodies/test-body-male.fbx",
93 ))
94 .unwrap();
95 assert_eq!(fbx.positions, direct.character.positions);
96 assert_eq!(fbx.clips.len(), 2);
97 let obj = preview::load_body_file(&repo("tests/fixtures/equipment/sword.obj")).unwrap();
98 assert!(obj.is_static() && obj.clips.is_empty());
99 let err = preview::load_body_file(&repo("tests/fixtures/equipment/sword.ply")).unwrap_err();
100 assert!(err.contains("geen tekenbaar lichaam"), "{err}");
101 // A file named .glb that actually holds FBX bytes is not silently accepted.
102 let dir = scratch("renamed");
103 let fake = dir.join("body.glb");
104 std::fs::copy(repo("tests/fixtures/bodies/test-body-male.fbx"), &fake).unwrap();
105 assert!(preview::load_body_file(&fake).is_err());
106 let _ = std::fs::remove_dir_all(dir);
107}
108
109#[test]
110fn fbx_body_production_scene_matches_glb_silhouette_without_double_normalization() {
111 let fbx = fbx_body();
112 let glb = glb_body();
113 let fbx_plan = production::plan(&fbx, 12).unwrap();
114 let glb_plan = production::plan(&glb, 12).unwrap();
115 assert_eq!(fbx_plan.images, glb_plan.images);
116 assert_eq!(fbx_plan.files, glb_plan.files);
117 let fbx_job = job(fbx, None);
118 let glb_job = job(glb, None);
119 let mut cache = equipment::TextureCache::default();
120 // Only `walk` is compared as a whole silhouette: Blender's FBX exporter
121 // baked the legs of the fixture's `attack` stack in a leftover walk pose
122 // (all 60 curves present in the file), so the attack clip is compared per
123 // animated arm joint below instead.
124 for (prefix, time) in [("walk", 0.0), ("walk", 0.25), ("walk", 0.6)] {
125 let f = production::scene(
126 &fbx_job,
127 &clip_named(&fbx_job.character, prefix),
128 time,
129 30.,
130 90.,
131 &mut cache,
132 )
133 .unwrap();
134 let g = production::scene(&glb_job, prefix, time, 30., 90., &mut cache).unwrap();
135 f.validate().unwrap();
136 let (flo, fhi) = bounds(&f);
137 let (glo, ghi) = bounds(&g);
138 for k in 0..3 {
139 assert!(
140 (flo[k] - glo[k]).abs() < 0.03 && (fhi[k] - ghi[k]).abs() < 0.03,
141 "{prefix}@{time} axis {k}: fbx {flo:?}..{fhi:?} vs glb {glo:?}..{ghi:?}"
142 );
143 }
144 // Body height in metres (no centimetre or axis double-conversion).
145 assert!((fhi[1] - flo[1]) > 1.4 && (fhi[1] - flo[1]) < 2.2);
146 // Same fixed per-body framing: identical orthographic projection and
147 // a rest-pose centre pivot from the FBX close to the GLB one (the
148 // pivot offset is the only projection term that may differ slightly).
149 for (a, b) in f.projection.iter().zip(g.projection) {
150 assert!(
151 (a - b).abs() < 0.01,
152 "{:?} vs {:?}",
153 f.projection,
154 g.projection
155 );
156 }
157 assert_eq!((f.width, f.height), (g.width, g.height));
158 }
159 let attack = clip_named(&fbx_job.character, "attack");
160 for name in [
161 "RightUpperArm",
162 "RightLowerArm",
163 "RightHand",
164 "Chest",
165 "Spine",
166 ] {
167 for time in [0.0, 0.2, 0.4, 0.7] {
168 let f =
169 preview::joint_world_translation(&fbx_job.character, &attack, time, name).unwrap();
170 let g =
171 preview::joint_world_translation(&glb_job.character, "attack", time, name).unwrap();
172 let d = ((f[0] - g[0]).powi(2) + (f[1] - g[1]).powi(2) + (f[2] - g[2]).powi(2)).sqrt();
173 assert!(d < 0.02, "attack {name}@{time}: {f:?} vs {g:?}");
174 }
175 }
176 let fp = paperdoll_renderer::camera::resolve(&fbx_job.character, fbx_job.framing);
177 let gp = paperdoll_renderer::camera::resolve(&glb_job.character, glb_job.framing);
178 assert!(matches!(
179 fp.source,
180 paperdoll_renderer::camera::PivotSource::BoundsCenter { .. }
181 ));
182 let (fy, gy) = (
183 fp.framing.pivot_y_milli.unwrap(),
184 gp.framing.pivot_y_milli.unwrap(),
185 );
186 assert!((fy - gy).abs() <= 30, "centre pivot fbx {fy} vs glb {gy}");
187}
188
189#[test]
190fn rigid_fbx_import_normalizes_axes_units_and_embeds_png() {
191 let mut preset = Preset::new("fbxsword".into());
192 let part = equipment::import_fbx(&mut preset, &repo("tests/fixtures/equipment/sword-fbx.fbx"))
193 .unwrap();
194 let item = &preset.attachments[part];
195 assert_eq!(item.label, "sword-fbx.fbx");
196 assert_eq!(item.meshes.len(), 2);
197 let mut lo = [f32::MAX; 3];
198 let mut hi = [f32::MIN; 3];
199 for p in item.meshes.iter().flat_map(|m| &m.positions) {
200 for k in 0..3 {
201 lo[k] = lo[k].min(p[k]);
202 hi[k] = hi[k].max(p[k]);
203 }
204 }
205 // Exported Z-up from Blender: blade length must end up along +Y in metres.
206 assert!(
207 lo[1].abs() < 1e-3 && (hi[1] - 0.58).abs() < 1e-3,
208 "{lo:?} {hi:?}"
209 );
210 assert!((hi[0] - 0.09).abs() < 1e-3 && (lo[0] + 0.09).abs() < 1e-3);
211 assert!((hi[2] - 0.015).abs() < 1e-3 && (lo[2] + 0.015).abs() < 1e-3);
212 for mesh in &item.meshes {
213 assert_eq!(mesh.positions.len(), mesh.normals.len());
214 assert_eq!(mesh.positions.len(), mesh.uvs.len());
215 for n in &mesh.normals {
216 let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt();
217 assert!((len - 1.).abs() < 1e-3);
218 }
219 }
220 let blade = item.meshes.iter().find(|m| m.albedo.is_some()).unwrap();
221 let guard = item.meshes.iter().find(|m| m.albedo.is_none()).unwrap();
222 // A connected texture replaces the FBX diffuse colour; the guard keeps its colour.
223 assert_eq!(blade.color, [1., 1., 1., 1.]);
224 for (got, want) in guard.color.iter().zip([0.75, 0.55, 0.2, 1.]) {
225 assert!((got - want).abs() < 0.01, "{:?}", guard.color);
226 }
227 // UVs keep the OBJ/FBX convention (V up): on the broad blade faces the
228 // tip has V=1 and the base V=0 (the fixture maps each face to the unit square).
229 let broad = |want_y: f32| {
230 blade
231 .positions
232 .iter()
233 .zip(&blade.normals)
234 .zip(&blade.uvs)
235 .filter(|((p, n), _)| n[2].abs() > 0.9 && (p[1] - want_y).abs() < 1e-4)
236 .map(|(_, uv)| uv[1])
237 .collect::<Vec<_>>()
238 };
239 let (tip, base) = (broad(0.58), broad(0.03));
240 assert!(
241 !tip.is_empty() && tip.iter().all(|v| (*v - 1.).abs() < 1e-4),
242 "{tip:?}"
243 );
244 assert!(
245 !base.is_empty() && base.iter().all(|v| v.abs() < 1e-4),
246 "{base:?}"
247 );
248 let albedo = blade.albedo.unwrap();
249 assert!(preset.assets[albedo].path.contains("/embedded/"));
250 let embedded = preset.texture(albedo).unwrap();
251 let mut reference = Preset::new("ref".into());
252 reference.assets.push(equipment::EmbeddedAsset {
253 path: "checker.png".into(),
254 bytes: std::fs::read(repo("tests/fixtures/equipment/checker.png")).unwrap(),
255 });
256 assert_eq!(embedded.rgba, reference.texture(0).unwrap().rgba);
257 // Original FBX payload is kept, and the binary preset round-trips.
258 assert!(item.source_assets.iter().any(|i| preset.assets[*i].bytes
259 == std::fs::read(repo("tests/fixtures/equipment/sword-fbx.fbx")).unwrap()));
260 let decoded = Preset::decode(&preset.encode().unwrap()).unwrap();
261 assert_eq!(decoded, preset);
262}
263
264#[test]
265fn rigid_fbx_external_png_is_embedded_and_missing_texture_fails() {
266 let mut preset = Preset::new("fbxsword".into());
267 let part = equipment::import_model(
268 &mut preset,
269 &repo("tests/fixtures/equipment/sword-fbx-external.fbx"),
270 )
271 .unwrap();
272 let blade = preset.attachments[part]
273 .meshes
274 .iter()
275 .find(|m| m.albedo.is_some())
276 .unwrap();
277 let asset = &preset.assets[blade.albedo.unwrap()];
278 assert!(asset.path.ends_with("/checker.png"), "{}", asset.path);
279 assert_eq!(
280 asset.bytes,
281 std::fs::read(repo("tests/fixtures/equipment/checker.png")).unwrap()
282 );
283 let dir = scratch("missing-tex");
284 std::fs::copy(
285 repo("tests/fixtures/equipment/sword-fbx-external.fbx"),
286 dir.join("lonely.fbx"),
287 )
288 .unwrap();
289 let mut other = Preset::new("lonely".into());
290 let err = equipment::import_fbx(&mut other, &dir.join("lonely.fbx")).unwrap_err();
291 assert!(err.contains("FBX-texture"), "{err}");
292 assert!(other.attachments.is_empty() && other.assets.is_empty());
293 let _ = std::fs::remove_dir_all(dir);
294}
295
296#[test]
297fn skinned_garbage_or_unsupported_equipment_is_rejected() {
298 let mut preset = Preset::new("bad".into());
299 let err = equipment::import_fbx(
300 &mut preset,
301 &repo("tests/fixtures/bodies/test-body-male.fbx"),
302 )
303 .unwrap_err();
304 assert!(err.contains("geskinned"), "{err}");
305 let dir = scratch("garbage");
306 std::fs::write(dir.join("junk.fbx"), b"not an fbx at all").unwrap();
307 assert!(equipment::import_fbx(&mut preset, &dir.join("junk.fbx")).is_err());
308 // Tim t1027u: a skinned model is now a skinned part with its skeleton.
309 for skinned in [
310 "tests/fixtures/bodies/test-body-male.glb",
311 "tests/fixtures/bodies/test-body-male.fbx",
312 ] {
313 let mut ok = Preset::new("suit".into());
314 let part = equipment::import_model(&mut ok, &repo(skinned)).unwrap();
315 assert!(ok.attachments[part].skeleton.is_some(), "{skinned}");
316 }
317 assert!(preset.attachments.is_empty() && preset.assets.is_empty());
318 // OBJ still goes through the same dispatcher.
319 equipment::import_model(&mut preset, &repo("tests/fixtures/equipment/sword.obj")).unwrap();
320 assert_eq!(preset.attachments.len(), 1);
321 let _ = std::fs::remove_dir_all(dir);
322}
323
324#[test]
325fn fbx_equipment_on_fbx_body_follows_the_animated_hand_in_production() {
326 let body = fbx_body();
327 let joint = body
328 .joint_names
329 .iter()
330 .position(|n| n == "RightHand")
331 .unwrap();
332 let mut preset = Preset::new("fbxsword".into());
333 equipment::import_fbx(&mut preset, &repo("tests/fixtures/equipment/sword-fbx.fbx")).unwrap();
334 preset.bind(0, &body, 0, joint).unwrap();
335 preset.attachments[0].rotation_deg = [0., 0., -15.];
336 let attack = clip_named(&body, "attack");
337 let job = job(body, Some(preset.clone()));
338 let mut cache = equipment::TextureCache::default();
339 let early = production::scene(&job, &attack, 0.0, 0., 0., &mut cache).unwrap();
340 let late = production::scene(&job, &attack, 0.4, 0., 0., &mut cache).unwrap();
341 assert_eq!(early.draws.len(), 2);
342 assert!(early.draws.iter().any(|d| d.albedo.is_some()));
343 assert_eq!(early.view, late.view);
344 assert_ne!(early.draws[0].positions, late.draws[0].positions);
345 // Exact attachment: animated joint world × local transform × source vertex.
346 let world = preview::animated_joint_world(&job.character, &attack, 0.4, joint).unwrap();
347 let model = preview::mul(world, equipment::local_matrix([0.; 3], [0., 0., -15.]));
348 let layers = equipment::layers(
349 &preset,
350 0,
351 &job.character,
352 &PreviewControls {
353 clip_name: attack.clone(),
354 time: 0.4,
355 ..Default::default()
356 },
357 )
358 .unwrap();
359 assert_eq!(layers[0].model, model);
360 let expected = apply(&model, preset.attachments[0].meshes[0].positions[0]);
361 let got = late.draws[0].positions[0];
362 for k in 0..3 {
363 assert!(
364 (expected[k] - got[k]).abs() < 1e-5,
365 "{expected:?} vs {got:?}"
366 );
367 }
368 // The hand-bound sword sits at the FBX hand, not at the origin.
369 let hand = [world[12], world[13], world[14]];
370 assert!(hand[1] > 0.5);
371 let d = ((got[0] - hand[0]).powi(2) + (got[1] - hand[1]).powi(2) + (got[2] - hand[2]).powi(2))
372 .sqrt();
373 assert!(d < 0.7, "sword vertex {got:?} far from hand {hand:?}");
374 // A preset bound to the GLB rig is refused on the FBX rig instead of guessing.
375 let mut glb_bound = Preset::new("glbsword".into());
376 equipment::import_fbx(
377 &mut glb_bound,
378 &repo("tests/fixtures/equipment/sword-fbx.fbx"),
379 )
380 .unwrap();
381 let glb = glb_body();
382 glb_bound
383 .bind(
384 0,
385 &glb,
386 0,
387 glb.joint_names
388 .iter()
389 .position(|n| n == "RightHand")
390 .unwrap(),
391 )
392 .unwrap();
393 let err =
394 equipment::layers(&glb_bound, 0, &job.character, &PreviewControls::default()).unwrap_err();
395 assert!(err.contains("rig wijkt af"), "{err}");
396}