BlueGit

golden-age paperdoll-renderer

Files

paperdoll-renderer / tests / fbx_integration.rs

fbx_integration.rs

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