diff --git a/crates/capri-ui/src/body.rs b/crates/capri-ui/src/body.rs new file mode 100644 index 0000000..9df18f3 --- /dev/null +++ b/crates/capri-ui/src/body.rs @@ -0,0 +1,159 @@ +//! Body-part hit testing (ported from Capri's `avatar-body-regions.mjs`) and the authored face +//! landmarks used for mouth plates, headpat and HUD placement. +//! All coordinates are on the shared 1254 x 1254 sprite canvas. + +const EXTENT: f32 = 1254.0; + +/// Tone of a tap reaction. +pub fn tone(part: &str) -> &'static str { + match part { + "head" => "affectionate", + "chest" | "pelvis" => "boundary", + p if p.ends_with("leg") || p.ends_with("foot") => "curious", + _ => "neutral", + } +} + +/// Standing classifier; `head_offset` is set for the legacy seat. +fn standing(u: f32, v: f32, head_offset: f32) -> &'static str { + let crown = v - head_offset; + if (0.0..=0.366).contains(&crown) && (0.31..=0.72).contains(&u) && (crown < 0.29 || (0.445..=0.58).contains(&u)) { + return "head"; + } + if crown < 0.366 || (crown < 0.52 && !(0.41..=0.64).contains(&u)) { + return "hair"; + } + let side = |left: bool, a: &'static str, b: &'static str| if left { a } else { b }; + if v < 0.68 && (u < 0.439 || u > if v < 0.55 { 0.555 } else { 0.576 }) { + return match (u < 0.5, v < 0.6) { + (true, true) => "right-arm", + (true, false) => "right-hand", + (false, true) => "left-arm", + (false, false) => "left-hand", + }; + } + if (0.455..=0.555).contains(&u) && (0.46..0.522).contains(&v) { + return "chest"; + } + if (0.482..=0.555).contains(&u) && (0.57..0.626).contains(&v) { + return "pelvis"; + } + if v < 0.553 { + return "torso"; + } + let right = u < if v < 0.67 { 0.5 } else { 0.525 }; + if v < 0.86 { side(right, "right-leg", "left-leg") } else { side(right, "right-foot", "left-foot") } +} + +/// Rectangles `[l,t,r,b]` or capsules `[ax,ay,bx,by,radius]`, in table order. +type Region = (&'static str, &'static [f32]); + +fn reviewed_regions(pose: &str) -> Option<&'static [Region]> { + Some(match pose { + "music-window" => &[("head", &[560., 278., 726., 448.]), ("right-hand", &[510., 383., 567., 486.]), ("left-hand", &[748., 720., 872., 798.]), ("right-foot", &[284., 1072., 443., 1166.]), ("left-foot", &[483., 1030., 549., 1168.]), ("chest", &[584., 478., 678., 559.]), ("pelvis", &[555., 662., 716., 790.]), ("right-arm", &[463., 555., 532., 453., 38.]), ("left-arm", &[716., 542., 772., 742., 32.]), ("right-leg", &[549., 802., 372., 1088., 56.]), ("left-leg", &[569., 813., 518., 1045., 40.]), ("torso", &[527., 456., 741., 712.])], + "music-floor" => &[("head", &[556., 515., 735., 701.]), ("right-hand", &[267., 863., 386., 971.]), ("left-hand", &[801., 896., 923., 1001.]), ("right-foot", &[450., 1085., 595., 1168.]), ("left-foot", &[745., 1068., 889., 1155.]), ("chest", &[600., 749., 705., 845.]), ("pelvis", &[562., 1012., 717., 1124.]), ("right-arm", &[516., 810., 346., 910., 40.]), ("left-arm", &[756., 816., 859., 923., 38.]), ("right-leg", &[344., 958., 773., 1094., 67.]), ("left-leg", &[866., 993., 602., 1130., 68.]), ("torso", &[551., 707., 751., 1052.])], + "pose-ceiling" => &[("head", &[555., 250., 704., 424.]), ("right-hand", &[440., 76., 535., 137.]), ("left-hand", &[718., 76., 812., 137.]), ("right-foot", &[482., 1035., 554., 1157.]), ("left-foot", &[632., 1035., 709., 1178.]), ("chest", &[593., 450., 662., 539.]), ("pelvis", &[566., 589., 675., 674.]), ("right-arm", &[491., 118., 571., 452., 31.]), ("left-arm", &[756., 124., 674., 460., 31.]), ("right-leg", &[565., 659., 520., 1039., 70.]), ("left-leg", &[642., 660., 668., 1060., 66.]), ("torso", &[523., 428., 716., 639.])], + "pose-ceiling-upside-down" => &[("head", &[541., 649., 704., 823.]), ("right-hand", &[392., 596., 474., 704.]), ("left-hand", &[769., 596., 841., 707.]), ("right-foot", &[558., 79., 619., 164.]), ("left-foot", &[638., 79., 699., 167.]), ("chest", &[574., 559., 668., 632.]), ("pelvis", &[579., 411., 678., 498.]), ("right-arm", &[440., 529., 525., 563., 38.]), ("left-arm", &[793., 529., 714., 563., 38.]), ("right-leg", &[550., 410., 515., 259., 91.]), ("left-leg", &[704., 404., 728., 259., 92.]), ("torso", &[516., 476., 722., 665.])], + "pose-dock-left" => &[("head", &[516., 254., 662., 448.]), ("right-hand", &[195., 414., 250., 494.]), ("left-hand", &[195., 631., 250., 708.]), ("right-foot", &[327., 1045., 403., 1179.]), ("left-foot", &[449., 815., 518., 947.]), ("chest", &[518., 462., 604., 541.]), ("pelvis", &[415., 600., 555., 696.]), ("right-arm", &[243., 481., 500., 498., 27.]), ("left-arm", &[239., 674., 506., 570., 29.]), ("right-leg", &[429., 671., 365., 1041., 54.]), ("left-leg", &[611., 653., 472., 835., 56.]), ("torso", &[419., 427., 649., 615.])], + "pose-dock-right" => &[("head", &[494., 214., 651., 422.]), ("left-hand", &[994., 370., 1057., 453.]), ("right-hand", &[990., 587., 1057., 661.]), ("right-foot", &[629., 816., 710., 925.]), ("left-foot", &[773., 1045., 844., 1179.]), ("chest", &[578., 430., 672., 522.]), ("pelvis", &[612., 555., 752., 663.]), ("left-arm", &[703., 477., 994., 435., 30.]), ("right-arm", &[692., 583., 994., 638., 29.]), ("right-leg", &[526., 626., 675., 841., 58.]), ("left-leg", &[765., 635., 805., 1051., 61.]), ("torso", &[558., 412., 796., 615.])], + "pose-sit-edge" => &[("head", &[548., 263., 719., 462.]), ("right-hand", &[303., 727., 415., 793.]), ("left-hand", &[748., 729., 863., 800.]), ("right-foot", &[384., 1044., 489., 1179.]), ("left-foot", &[486., 1029., 571., 1179.]), ("chest", &[586., 461., 667., 555.]), ("pelvis", &[543., 651., 707., 759.]), ("right-arm", &[556., 551., 410., 738., 37.]), ("left-arm", &[698., 553., 750., 738., 34.]), ("right-leg", &[511., 732., 450., 1035., 74.]), ("left-leg", &[553., 771., 520., 1030., 53.]), ("torso", &[536., 446., 706., 695.])], + "pose-sit-floor" => &[("head", &[529., 427., 703., 616.]), ("right-hand", &[254., 1110., 373., 1170.]), ("left-hand", &[731., 717., 866., 796.]), ("right-foot", &[735., 1090., 858., 1168.]), ("left-foot", &[882., 1097., 1031., 1168.]), ("chest", &[539., 628., 638., 768.]), ("pelvis", &[524., 941., 659., 1080.]), ("right-arm", &[482., 719., 370., 1115., 43.]), ("left-arm", &[635., 695., 788., 740., 35.]), ("right-leg", &[469., 1107., 735., 1134., 55.]), ("left-leg", &[808., 788., 902., 1101., 71.]), ("torso", &[476., 603., 677., 999.])], + "pose-crouch" => &[("head", &[529., 502., 687., 698.]), ("right-hand", &[447., 865., 525., 953.]), ("left-hand", &[577., 1094., 700., 1170.]), ("right-foot", &[344., 1097., 487., 1170.]), ("left-foot", &[698., 1060., 841., 1148.]), ("chest", &[553., 686., 653., 786.]), ("pelvis", &[555., 863., 654., 967.]), ("right-arm", &[392., 765., 485., 892., 36.]), ("left-arm", &[689., 828., 642., 1107., 46.]), ("right-leg", &[397., 804., 463., 1097., 49.]), ("left-leg", &[819., 932., 743., 1090., 61.]), ("torso", &[514., 680., 716., 951.])], + _ => return None, + }) +} + +/// Part under the normalized canvas point, or `None` on a transparent pixel. +// TODO: rotate the regions once limb swing on reviewed poses is animated; they are tested unrotated. +pub fn part_at(u: f32, v: f32, opaque: impl Fn(f32, f32) -> bool, frame: &str) -> Option<&'static str> { + if !opaque(u, v) { + return None; + } + let Some(regions) = reviewed_regions(frame) else { return Some(standing(u, v, 0.0)) }; + let (x, y) = (u * EXTENT, v * EXTENT); + for &(part, r) in regions { + if let [l, t, rr, b] = *r { + if x >= l && x <= rr && y >= t && y <= b { + return Some(part); + } + } else if let [ax, ay, bx, by, radius] = *r { + let (dx, dy) = (bx - ax, by - ay); + let t = (((x - ax) * dx + (y - ay) * dy) / (dx * dx + dy * dy)).clamp(0.0, 1.0); + if (x - ax - dx * t).hypot(y - ay - dy * t) <= radius { + return Some(part); + } + } + } + Some("hair") +} + +/// Face landmarks of a reviewed pose: mouth `[x, y, angle, width]`, crown y, inverted. +pub struct Landmarks { + pub mouth: [f32; 4], + pub crown: f32, + pub inverted: bool, +} + +pub fn landmarks(frame: &str) -> Option { + let l = |mouth, crown| Some(Landmarks { mouth, crown, inverted: false }); + match frame { + "music-window" => l([655., 403., -0.46, 36.], 76.), + "music-floor" => l([661., 642., -0.47, 39.], 296.), + "pose-ceiling" => l([634., 396., -0.36, 32.], 76.), + "pose-ceiling-upside-down" => Some(Landmarks { mouth: [621., 676., std::f32::consts::PI, 33.], crown: 84., inverted: true }), + "pose-dock-left" => l([593., 416., -0.32, 38.], 76.), + "pose-dock-right" => l([572., 388., -0.36, 33.], 76.), + "pose-sit-edge" => l([638., 430., -0.4, 33.], 111.), + "pose-sit-floor" => l([620., 588., -0.41, 34.], 247.), + "pose-crouch" => l([617., 679., -0.42, 36.], 339.), + _ => None, + } +} + +/// Mouth plate anchor on standing portrait frames, none on kissing/heart frames. +pub fn portrait_mouth(frame: &str) -> Option<(f32, f32)> { + if frame.contains("heart_hands") || frame.contains("kiss") { + return None; + } + Some(match frame { + f if f.starts_with("desktop-perch") => (655., 524.), + f if f.starts_with("idle-wave") => (658., 443.), + "idle-excited" => (649., 421.), + "idle-laughing" | "happy" => (648., 426.), + "skeptical" | "sad" => (644., 435.), + _ => (646., 430.), + }) +} + +/// Face outline of `assets/default/capri-headpat.png` on standing frames. +pub const HEADPAT_FACE: [(f32, f32); 11] = [(570., 390.), (607., 351.), (632., 386.), (654., 344.), (712., 326.), (715., 401.), (703., 407.), (677., 439.), (655., 462.), (605., 453.), (582., 444.)]; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn standing_parts() { + let all = |_: f32, _: f32| true; + assert_eq!(part_at(0.5, 0.2, all, "neutral"), Some("head")); + assert_eq!(part_at(0.35, 0.33, all, "neutral"), Some("hair")); + assert_eq!(part_at(0.5, 0.49, all, "neutral"), Some("chest")); + assert_eq!(part_at(0.38, 0.55, all, "neutral"), Some("right-arm")); + assert_eq!(part_at(0.6, 0.95, all, "neutral"), Some("left-foot")); + assert_eq!(part_at(0.5, 0.5, |_, _| false, "neutral"), None); + assert_eq!(tone("chest"), "boundary"); + assert_eq!(tone("left-leg"), "curious"); + assert_eq!(tone("head"), "affectionate"); + } + + #[test] + fn reviewed_parts() { + let all = |_: f32, _: f32| true; + // Inside the sit-edge head rectangle, then on the right-arm capsule axis. + assert_eq!(part_at(600. / EXTENT, 300. / EXTENT, all, "pose-sit-edge"), Some("head")); + assert_eq!(part_at(483. / EXTENT, 644. / EXTENT, all, "pose-sit-edge"), Some("right-arm")); + assert_eq!(part_at(0.05, 0.05, all, "pose-sit-edge"), Some("hair")); + assert!(landmarks("pose-ceiling-upside-down").unwrap().inverted); + assert_eq!(portrait_mouth("idle-kiss_touch"), None); + } +}