BlueGit

golden-age paperdoll-renderer

Files

paperdoll-renderer / examples / neck_pivot_probe.rs

neck_pivot_probe.rs

neck_pivot_probe.rs 9.4 KiB Download Raw
1//! Real Mesa/Xvfb proof that neck pivot differs from center-only orbit at pitched yaw.
2//! Usage: OUTDIR=... xvfb-run -a cargo run --example neck_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-neck-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 neck 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 neck_y = resolved.framing.pivot_y_milli.ok_or("nekpivot ontbreekt")?;
68 if neck_y == CameraFraming::default().center_y_milli {
69 return Err("nekpivot mag niet gelijk zijn aan default centrum".into());
70 }
71 let mut renderer = production::GpuRenderer::default();
72 let mut cache = paperdoll_renderer::equipment::TextureCache::default();
73 let pitch = 30.;
74 let yaw = 90.;
75 let mut job = production::RenderJob {
76 character: body,
77 sex: "male".into(),
78 preset: None,
79 target_width: 64,
80 target_height: 96,
81 fps: 12,
82 framing: CameraFraming {
83 height_milli: 2600,
84 center_y_milli: 1000,
85 pivot_y_milli: Some(1000),
86 },
87 output: out.clone(),
88 project_id: "neck-probe".into(),
89 project_name: "Neck probe".into(),
90 body_model: production::SourceFingerprint {
91 path: "assets/body/test-body-male.glb".into(),
92 sha256: paperdoll_renderer::digest::sha256_hex(b"neck-probe-body"),
93 },
94 preset_sha256: None,
95 };
96 let old_scene = production::scene(&job, "walk", 0.0, pitch, yaw, &mut cache)?;
97 let old = unsafe { renderer.render(gl, &old_scene) }?;
98 job.framing.pivot_y_milli = None;
99 let neck_scene = production::scene(&job, "walk", 0.0, pitch, yaw, &mut cache)?;
100 let neck = unsafe { renderer.render(gl, &neck_scene) }?;
101 unsafe { renderer.destroy(gl) };
102 assert_eq!((old.width, old.height), (neck.width, neck.height));
103 let mut changed = 0usize;
104 for (a, b) in old
105 .diffuse
106 .chunks_exact(4)
107 .zip(neck.diffuse.chunks_exact(4))
108 {
109 if a != b {
110 changed += 1;
111 }
112 }
113 if changed < 200 {
114 return Err(format!(
115 "te weinig pixelverschil tussen centrum-pivot en nekpivot: {changed}"
116 ));
117 }
118 // Zero-pitch image center / feet framing must stay close after raising pivot.
119 job.framing.pivot_y_milli = Some(1000);
120 let old0 = production::scene(&job, "walk", 0.0, 0., 0., &mut cache)?;
121 job.framing.pivot_y_milli = None;
122 let neck0 = production::scene(&job, "walk", 0.0, 0., 0., &mut cache)?;
123 let old0b = unsafe {
124 let mut r = production::GpuRenderer::default();
125 let b = r.render(gl, &old0)?;
126 r.destroy(gl);
127 b
128 };
129 let neck0b = unsafe {
130 let mut r = production::GpuRenderer::default();
131 let b = r.render(gl, &neck0)?;
132 r.destroy(gl);
133 b
134 };
135 let mid = ((old0b.height / 2) * old0b.width + old0b.width / 2) as usize;
136 let feet = ((old0b.height * 9 / 10) * old0b.width + old0b.width / 2) as usize;
137 let mid_delta = old0b.diffuse[mid * 4..mid * 4 + 4]
138 .iter()
139 .zip(&neck0b.diffuse[mid * 4..mid * 4 + 4])
140 .map(|(a, b)| a.abs_diff(*b) as u32)
141 .sum::<u32>();
142 let feet_delta = old0b.diffuse[feet * 4..feet * 4 + 4]
143 .iter()
144 .zip(&neck0b.diffuse[feet * 4..feet * 4 + 4])
145 .map(|(a, b)| a.abs_diff(*b) as u32)
146 .sum::<u32>();
147 write_rgba(
148 &out.join("center-pivot-p30-y90.png"),
149 old.width,
150 old.height,
151 &old.diffuse,
152 )?;
153 write_rgba(
154 &out.join("neck-pivot-p30-y90.png"),
155 neck.width,
156 neck.height,
157 &neck.diffuse,
158 )?;
159 let contact_w = 256;
160 let contact_h = 192;
161 let left = downsample(
162 &old.diffuse,
163 old.width,
164 old.height,
165 contact_w / 2,
166 contact_h,
167 );
168 let right = downsample(
169 &neck.diffuse,
170 neck.width,
171 neck.height,
172 contact_w / 2,
173 contact_h,
174 );
175 let mut contact = vec![0u8; (contact_w * contact_h * 4) as usize];
176 for y in 0..contact_h {
177 for x in 0..contact_w / 2 {
178 let si = ((y * (contact_w / 2) + x) * 4) as usize;
179 let li = ((y * contact_w + x) * 4) as usize;
180 let ri = ((y * contact_w + contact_w / 2 + x) * 4) as usize;
181 contact[li..li + 4].copy_from_slice(&left[si..si + 4]);
182 contact[ri..ri + 4].copy_from_slice(&right[si..si + 4]);
183 }
184 }
185 write_rgba(
186 &out.join("neck-vs-center-contact.png"),
187 contact_w,
188 contact_h,
189 &contact,
190 )?;
191 let report = serde_json::json!({
192 "success": true,
193 "pitch": pitch,
194 "yaw": yaw,
195 "center_y_milli": 1000,
196 "neck_y_milli": neck_y,
197 "pivot_source": resolved.source,
198 "highres": [old.width, old.height],
199 "changed_pixels_pitch30": changed,
200 "zero_pitch_mid_channel_delta": mid_delta,
201 "zero_pitch_feet_channel_delta": feet_delta,
202 "artifacts": [
203 "center-pivot-p30-y90.png",
204 "neck-pivot-p30-y90.png",
205 "neck-vs-center-contact.png"
206 ]
207 });
208 std::fs::write(
209 out.join("neck-pivot-probe.json"),
210 serde_json::to_vec_pretty(&report).unwrap(),
211 )
212 .map_err(|e| e.to_string())?;
213 println!("NECK_PIVOT_PROBE {report}");
214 Ok(report)
215 })();
216 *result.lock().unwrap() = Some(test);
217 Ok(Box::new(Quit))
218 }),
219 )?;
220 let value = outcome
221 .lock()
222 .unwrap()
223 .take()
224 .ok_or("GPU-probe is niet uitgevoerd")??;
225 if value["success"] != true {
226 return Err("probe failed".into());
227 }
228 Ok(())
229}