BlueGit

golden-age paperdoll-renderer

Files

paperdoll-renderer / examples / auto_pivot_probe.rs

auto_pivot_probe.rs

auto_pivot_probe.rs 9.8 KiB Download Raw
1//! Real Mesa/Xvfb proof that the automatic (rest-pose centre) pivot differs from a center-only orbit at pitched yaw.
2//! Usage: OUTDIR=... xvfb-run -a cargo run --example auto_pivot_probe --locked
3use paperdoll_renderer::{camera, preview, production, project::CameraFraming};
4use std::{
5 path::{Path, PathBuf},
6 sync::{Arc, Mutex},
7};
8
9struct Quit;
10impl eframe::App for Quit {
11 fn update(&mut self, ctx: &eframe::egui::Context, _: &mut eframe::Frame) {
12 ctx.send_viewport_cmd(eframe::egui::ViewportCommand::Close);
13 }
14}
15
16fn write_rgba(path: &Path, width: u32, height: u32, rgba: &[u8]) -> Result<(), String> {
17 let file = std::fs::File::create(path).map_err(|e| e.to_string())?;
18 let mut encoder = png::Encoder::new(file, width, height);
19 encoder.set_color(png::ColorType::Rgba);
20 encoder.set_depth(png::BitDepth::Eight);
21 encoder
22 .write_header()
23 .map_err(|e| e.to_string())?
24 .write_image_data(rgba)
25 .map_err(|e| e.to_string())
26}
27
28fn downsample(src: &[u8], sw: u32, sh: u32, dw: u32, dh: u32) -> Vec<u8> {
29 let mut out = vec![0u8; (dw * dh * 4) as usize];
30 for y in 0..dh {
31 for x in 0..dw {
32 let sx = x * sw / dw;
33 let sy = y * sh / dh;
34 let si = ((sy * sw + sx) * 4) as usize;
35 let di = ((y * dw + x) * 4) as usize;
36 out[di..di + 4].copy_from_slice(&src[si..si + 4]);
37 }
38 }
39 out
40}
41
42fn main() -> Result<(), Box<dyn std::error::Error>> {
43 let out = PathBuf::from(
44 std::env::var("OUTDIR").unwrap_or_else(|_| "/workspace/paperdoll-pivot-artifacts".into()),
45 );
46 std::fs::create_dir_all(&out)?;
47 let outcome = Arc::new(Mutex::new(None));
48 let result = outcome.clone();
49 let options = eframe::NativeOptions {
50 viewport: eframe::egui::ViewportBuilder::default().with_inner_size([64., 64.]),
51 ..paperdoll_renderer::preview_gl::native_options()
52 };
53 eframe::run_native(
54 "Paperdoll auto pivot probe",
55 options,
56 Box::new(move |creation| {
57 let test = (|| -> Result<serde_json::Value, String> {
58 let gl = creation
59 .gl
60 .as_ref()
61 .ok_or_else(|| "geen Glow-context".to_string())?;
62 let body = preview::load_gltf_file(
63 &Path::new(env!("CARGO_MANIFEST_DIR"))
64 .join("tests/fixtures/bodies/test-body-male.glb"),
65 )?;
66 let resolved = camera::resolve(&body, CameraFraming::default());
67 let pivot_y = resolved
68 .framing
69 .pivot_y_milli
70 .ok_or("automatische pivot ontbreekt")?;
71 if pivot_y == CameraFraming::default().center_y_milli {
72 return Err(
73 "automatische pivot mag niet gelijk zijn aan default centrum".into(),
74 );
75 }
76 let mut renderer = production::GpuRenderer::default();
77 let mut cache = paperdoll_renderer::equipment::TextureCache::default();
78 let pitch = 30.;
79 let yaw = 90.;
80 let mut job = production::RenderJob {
81 depth_format: paperdoll_renderer::camera::DepthFormat::GRAY16,
82 atlas_trim: false,
83 character: body,
84 sex: "male".into(),
85 preset: None,
86 target_width: 64,
87 target_height: 96,
88 fps: 12,
89 framing: CameraFraming {
90 height_milli: 2600,
91 center_y_milli: 1000,
92 pivot_y_milli: Some(1000),
93 depth_range_milli: None,
94 },
95 output: out.clone(),
96 project_id: "auto-pivot-probe".into(),
97 project_name: "Neck probe".into(),
98 body_model: production::SourceFingerprint {
99 path: "assets/body/test-body-male.glb".into(),
100 sha256: paperdoll_renderer::digest::sha256_hex(b"auto-pivot-probe-body"),
101 },
102 preset_sha256: None,
103 };
104 let old_scene = production::scene(&job, "walk", 0.0, pitch, yaw, &mut cache)?;
105 let old = unsafe { renderer.render(gl, &old_scene) }?;
106 job.framing.pivot_y_milli = None;
107 let pivot_scene = production::scene(&job, "walk", 0.0, pitch, yaw, &mut cache)?;
108 let pivoted = unsafe { renderer.render(gl, &pivot_scene) }?;
109 unsafe { renderer.destroy(gl) };
110 assert_eq!((old.width, old.height), (pivoted.width, pivoted.height));
111 let mut changed = 0usize;
112 for (a, b) in old
113 .diffuse
114 .chunks_exact(4)
115 .zip(pivoted.diffuse.chunks_exact(4))
116 {
117 if a != b {
118 changed += 1;
119 }
120 }
121 if changed < 200 {
122 return Err(format!(
123 "te weinig pixelverschil tussen centrum-pivot en automatische pivot: {changed}"
124 ));
125 }
126 // Zero-pitch image center / feet framing must stay close after raising pivot.
127 job.framing.pivot_y_milli = Some(1000);
128 let old0 = production::scene(&job, "walk", 0.0, 0., 0., &mut cache)?;
129 job.framing.pivot_y_milli = None;
130 let pivot0 = production::scene(&job, "walk", 0.0, 0., 0., &mut cache)?;
131 let old0b = unsafe {
132 let mut r = production::GpuRenderer::default();
133 let b = r.render(gl, &old0)?;
134 r.destroy(gl);
135 b
136 };
137 let pivot0b = unsafe {
138 let mut r = production::GpuRenderer::default();
139 let b = r.render(gl, &pivot0)?;
140 r.destroy(gl);
141 b
142 };
143 let mid = ((old0b.height / 2) * old0b.width + old0b.width / 2) as usize;
144 let feet = ((old0b.height * 9 / 10) * old0b.width + old0b.width / 2) as usize;
145 let mid_delta = old0b.diffuse[mid * 4..mid * 4 + 4]
146 .iter()
147 .zip(&pivot0b.diffuse[mid * 4..mid * 4 + 4])
148 .map(|(a, b)| a.abs_diff(*b) as u32)
149 .sum::<u32>();
150 let feet_delta = old0b.diffuse[feet * 4..feet * 4 + 4]
151 .iter()
152 .zip(&pivot0b.diffuse[feet * 4..feet * 4 + 4])
153 .map(|(a, b)| a.abs_diff(*b) as u32)
154 .sum::<u32>();
155 write_rgba(
156 &out.join("center-pivot-p30-y90.png"),
157 old.width,
158 old.height,
159 &old.diffuse,
160 )?;
161 write_rgba(
162 &out.join("auto-pivot-p30-y90.png"),
163 pivoted.width,
164 pivoted.height,
165 &pivoted.diffuse,
166 )?;
167 let contact_w = 256;
168 let contact_h = 192;
169 let left = downsample(
170 &old.diffuse,
171 old.width,
172 old.height,
173 contact_w / 2,
174 contact_h,
175 );
176 let right = downsample(
177 &pivoted.diffuse,
178 pivoted.width,
179 pivoted.height,
180 contact_w / 2,
181 contact_h,
182 );
183 let mut contact = vec![0u8; (contact_w * contact_h * 4) as usize];
184 for y in 0..contact_h {
185 for x in 0..contact_w / 2 {
186 let si = ((y * (contact_w / 2) + x) * 4) as usize;
187 let li = ((y * contact_w + x) * 4) as usize;
188 let ri = ((y * contact_w + contact_w / 2 + x) * 4) as usize;
189 contact[li..li + 4].copy_from_slice(&left[si..si + 4]);
190 contact[ri..ri + 4].copy_from_slice(&right[si..si + 4]);
191 }
192 }
193 write_rgba(
194 &out.join("auto-vs-center-contact.png"),
195 contact_w,
196 contact_h,
197 &contact,
198 )?;
199 let report = serde_json::json!({
200 "success": true,
201 "pitch": pitch,
202 "yaw": yaw,
203 "center_y_milli": 1000,
204 "pivot_y_milli": pivot_y,
205 "pivot_source": resolved.source,
206 "highres": [old.width, old.height],
207 "changed_pixels_pitch30": changed,
208 "zero_pitch_mid_channel_delta": mid_delta,
209 "zero_pitch_feet_channel_delta": feet_delta,
210 "artifacts": [
211 "center-pivot-p30-y90.png",
212 "auto-pivot-p30-y90.png",
213 "auto-vs-center-contact.png"
214 ]
215 });
216 std::fs::write(
217 out.join("auto-pivot-probe.json"),
218 serde_json::to_vec_pretty(&report).unwrap(),
219 )
220 .map_err(|e| e.to_string())?;
221 println!("AUTO_PIVOT_PROBE {report}");
222 Ok(report)
223 })();
224 *result.lock().unwrap() = Some(test);
225 Ok(Box::new(Quit))
226 }),
227 )?;
228 let value = outcome
229 .lock()
230 .unwrap()
231 .take()
232 .ok_or("GPU-probe is niet uitgevoerd")??;
233 if value["success"] != true {
234 return Err("probe failed".into());
235 }
236 Ok(())
237}