Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
HG14: the grip's code (the hand's lie at its grab, the swivel, an arm past its ranges) is its own file; take.rs is back under 500 lines
shinyobjectzffb56a8f0963
diff --git a/docs/hands.feature b/docs/hands.featureindex cf3b6905..662f39a3 100644--- a/docs/hands.feature+++ b/docs/hands.feature@@ -61,7 +61,8 @@ Feature: Procedural arms and hands: a character reaches for, takes, holds, carri| raea-sys-grab | src/hand.rs | 200 | holds, the wrist that lays a palm on one, the fingers' curl || raea-sys-grab | src/trunk.rs | 130 | stoop, look, clearances, where to stand || raea-sys-grab | src/interact.rs | 40 | which thing is taken |- | raea-sys-grab | src/take.rs | 280 | the take's timeline |+ | raea-sys-grab | src/take.rs | 460 | the take's timeline, each hand's part, the frame's solve |+ | raea-sys-grab | src/grip.rs | 110 | the hand's lie at its grab, the swivel, an arm past its ranges || raea-sys-grab | src/json.rs | 190 | character.take, character.fit, character.hand || raea-sys-grab | tests/grab.rs | 470 | the hands' measures, on the stand-in body || raea-sys-grab | tests/body.rs | 150 | limits, the body out of solids, the hand prior |diff --git a/systems/grab/src/grip.rs b/systems/grab/src/grip.rsnew file mode 100644index 00000000..53afdbd5--- /dev/null+++ b/systems/grab/src/grip.rs@@ -0,0 +1,77 @@+//! The grip: how a hand lies on its hold, decided once at the grab and kept; how a thing in one hand turns with the+//! forearm; and how far an arm is past its joints' ranges.++use glam::{Quat, Vec3};+use raea_sys_body::arm::{ArmOptions, reach};+use raea_sys_body::limits::clamp_turn;+use raea_sys_body::rig::{Pose, Skeleton, hand_rest};+use raea_sys_body::shape::Thing;++use crate::hand::{facing, wrist_for};+use crate::take::{Part, Release, Take, smooth, span, yaw};+use crate::trunk::{clear, stoop};++/// how far past their ranges an arm's shoulder and elbow are in a pose, degrees+pub(crate) fn past_range(skel: &Skeleton, arm: [usize; 3], pose: &Pose) -> f32 {+ arm[..2].iter().map(|&joint| clamp_turn(skel, joint, pose.turns[joint]).1).sum()+}++/// How a hand lies on its hold at its grab, in the thing's own frame (the wrist's place and the hand's turn): the+/// fingers along the forearm as it arrives (found by solving the arm to the hold alone, from the body as it is+/// then), so the wrist is asked for the least bend; where that would tip the fingers into a solid (a cup on a table,+/// the forearm coming down to it) they are brought level, by as much as they would be pushed. A hold that is a way+/// of holding says where the fingers point.+pub(crate) fn lie_at_grab(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, part: &Part) -> Option<(Vec3, Quat)> {+ let hold = part.hold;+ let thing = if part.grab <= take.grab + 1e-4 { take.thing } else { take.thing_at(skel, base, part.grab).0 };+ let own = base(part.grab);+ let w = skel.fk(&own);+ let (face, arm, rest) = (yaw(&w) * Vec3::Z, skel.arm(hold.side)?, hand_rest(skel, hold.side)?);+ let from = thing.world(hold.at) - w.pos[arm[0]];+ let found = Vec3::new(from.x, 0.0, from.z).normalize_or(face).lerp(facing(&thing, w.pos[0]), 0.4).normalize_or(face);+ let level = wrist_for(skel, &thing, &hold, hold.along.map_or(found, |a| thing.turn * a))?;+ let (at, turn) = if hold.along.is_some() {+ level+ } else {+ let pose = stoop(skel, &own, &w, face, level.0.y, 1.0, 0.0, &take.avoid);+ let probe = reach(skel, &pose, hold.side, thing.world(hold.at) + thing.turn * hold.normal * 0.04, ArmOptions::default());+ let forearm = probe.map_or(face, |r| {+ let drawn = skel.fk(&r.pose);+ (drawn.pos[arm[2]] - drawn.pos[arm[1]]).normalize_or(face)+ });+ let lie = |keep: f32| wrist_for(skel, &thing, &hold, Vec3::new(forearm.x, forearm.y * keep, forearm.z).normalize_or(face));+ match lie(1.0) {+ Some(along) => lie(1.0 - smooth((clear(along.0, along.1, &rest, &take.avoid) - along.0).length() / 0.03)).unwrap_or(level),+ None => level,+ }+ };+ Some((thing.local(at), (thing.turn.inverse() * turn).normalize()))+}++/// How far a thing carried in one hand is turned about the vertical at `t`, radians: so the hand that holds it lies+/// along the forearm as the arm comes to it, up to a quarter turn. A grip doesn't change once taken, so without this a cup raised to the mouth would be+/// held by a wrist bent past what a wrist does; with it the cup turns in the hand's favour, as a person's does.+/// None while it stands, all once carried, none again by the time it is set down.+pub(crate) fn swivel(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, part: &Part, t: f32, thing: &Thing) -> f32 {+ let down = match take.release {+ Some(Release::Put { at, down, .. }) => 1.0 - smooth(span(t, at, down)),+ _ => 1.0,+ };+ let weight = smooth(span(t, take.grab, take.carried)) * down;+ let (Some(arm), Some(rest), Some((_, lie))) = (skel.arm(part.hold.side), hand_rest(skel, part.hold.side), lie_at_grab(skel, base, take, part)) else { return 0.0 };+ if weight <= 0.0 {+ return 0.0;+ }+ let own = base(t);+ let level = |v: Vec3| Vec3::new(v.x, 0.0, v.z);+ // the arm solved to the thing's middle (not the hold, which the turn itself moves): the way the forearm comes+ let Some(probe) = reach(skel, &own, part.hold.side, thing.at, ArmOptions::default()) else { return 0.0 };+ let drawn = skel.fk(&probe.pose);+ let (fingers, forearm) = (level(thing.turn * lie * rest.along), level((drawn.pos[arm[2]] - drawn.pos[arm[1]]).normalize_or_zero()));+ // a hand or a forearm that points nearly straight up or down says little about which way round: less turn+ let sure = smooth(fingers.length().min(forearm.length()) / 0.3);+ // by the sine of the angle between them: up to a quarter turn, none where they are in line or dead against each+ // other, so it never flips+ let turned = (fingers.cross(forearm).y / (fingers.length() * forearm.length()).max(1e-6)).clamp(-1.0, 1.0).asin() * sure;+ turned * weight+}diff --git a/systems/grab/src/lib.rs b/systems/grab/src/lib.rsindex ce7d0acb..18845868 100644--- a/systems/grab/src/lib.rs+++ b/systems/grab/src/lib.rs@@ -4,6 +4,7 @@//! and get the same frame.//!//! - `hand`: hold points found on the thing, the wrist that lays a palm on one, fingers curled onto the surface.+//! - `grip`: how a hand lies on its hold, decided at the grab and kept; a thing in one hand turning with the forearm.//! - `trunk`: the body bent and squatted for a low thing with its feet planted, the head turned to the thing, hands//! and head kept out of the scene's solids, and where to stand to take a thing.//! - `interact`: which thing a body takes: the nearest in reach that its eyes see.@@ -11,6 +12,7 @@//! - `json`: takes, fitted poses and hand shapes asked for as JSON: the engine's native calls character.take,//! character.fit and character.hand (raea/character's take, fitPose and handShape).+pub mod grip;pub mod hand;pub mod interact;pub mod json;diff --git a/systems/grab/src/take.rs b/systems/grab/src/take.rsindex 98c8ecca..651c469f 100644--- a/systems/grab/src/take.rs+++ b/systems/grab/src/take.rs@@ -9,10 +9,11 @@use glam::{Quat, Vec3};use raea_sys_body::arm::{ArmOptions, forearm_twist, reach};+use crate::grip::{lie_at_grab, past_range, swivel};use crate::trunk::{clear, look, own_solids, roomy, stoop, torso};use raea_sys_body::limits::clamp_turn;use raea_sys_body::prior::shape;-use crate::hand::{Grip, Hold, Touch, curl, facing, wrist_for};+use crate::hand::{Grip, Hold, Touch, curl};use raea_sys_body::rig::hand_rest;use raea_sys_body::rig::{Pose, Posed, Skeleton};use raea_sys_body::shape::Thing;@@ -55,11 +56,11 @@ pub struct Take {/// One hand's part in a take: its hold, when it leaves the body's own motion, when it is on, and when it lets go.#[derive(Clone, Copy)]-struct Part {- hold: Hold,- start: f32,- grab: f32,- gone: Option<f32>,+pub(crate) struct Part {+ pub(crate) hold: Hold,+ pub(crate) start: f32,+ pub(crate) grab: f32,+ pub(crate) gone: Option<f32>,}/// Where a carried thing is at a time: off the chest as `carry` is, and tilted from how it is carried (the tilt in@@ -105,17 +106,17 @@ const WIND: f32 = 0.4;/// how open the hand comes in, in the hand prior's first component (its standard deviations)const OPEN: f32 = 1.5;-fn smooth(u: f32) -> f32 {+pub(crate) fn smooth(u: f32) -> f32 {let u = u.clamp(0.0, 1.0);u * u * u * (u * (u * 6.0 - 15.0) + 10.0)}-fn span(t: f32, a: f32, b: f32) -> f32 {+pub(crate) fn span(t: f32, a: f32, b: f32) -> f32 {if b <= a { (t >= b) as u8 as f32 } else { ((t - a) / (b - a)).clamp(0.0, 1.0) }}/// the turn about the vertical that the body faces by-fn yaw(w: &Posed) -> Quat {+pub(crate) fn yaw(w: &Posed) -> Quat {let f = w.rot[0] * Vec3::Z;Quat::from_rotation_y(f.x.atan2(f.z))}@@ -190,7 +191,7 @@ impl Take {}/// where the thing is at `t`, and whether the hands are on it- fn thing_at(&self, skel: &Skeleton, base: &dyn Fn(f32) -> Pose, t: f32) -> (Thing, Phase) {+ pub(crate) fn thing_at(&self, skel: &Skeleton, base: &dyn Fn(f32) -> Pose, t: f32) -> (Thing, Phase) {if t < self.start {return (self.thing, Phase::Before);}@@ -441,68 +442,3 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)frame.pose = pose;frame}--/// how far past their ranges an arm's shoulder and elbow are in a pose, degrees-fn past_range(skel: &Skeleton, arm: [usize; 3], pose: &Pose) -> f32 {- arm[..2].iter().map(|&joint| clamp_turn(skel, joint, pose.turns[joint]).1).sum()-}--/// How a hand lies on its hold at its grab, in the thing's own frame (the wrist's place and the hand's turn): the-/// fingers along the forearm as it arrives (found by solving the arm to the hold alone, from the body as it is-/// then), so the wrist is asked for the least bend; where that would tip the fingers into a solid (a cup on a table,-/// the forearm coming down to it) they are brought level, by as much as they would be pushed. A hold that is a way-/// of holding says where the fingers point.-fn lie_at_grab(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, part: &Part) -> Option<(Vec3, Quat)> {- let hold = part.hold;- let thing = if part.grab <= take.grab + 1e-4 { take.thing } else { take.thing_at(skel, base, part.grab).0 };- let own = base(part.grab);- let w = skel.fk(&own);- let (face, arm, rest) = (yaw(&w) * Vec3::Z, skel.arm(hold.side)?, hand_rest(skel, hold.side)?);- let from = thing.world(hold.at) - w.pos[arm[0]];- let found = Vec3::new(from.x, 0.0, from.z).normalize_or(face).lerp(facing(&thing, w.pos[0]), 0.4).normalize_or(face);- let level = wrist_for(skel, &thing, &hold, hold.along.map_or(found, |a| thing.turn * a))?;- let (at, turn) = if hold.along.is_some() {- level- } else {- let pose = stoop(skel, &own, &w, face, level.0.y, 1.0, 0.0, &take.avoid);- let probe = reach(skel, &pose, hold.side, thing.world(hold.at) + thing.turn * hold.normal * 0.04, ArmOptions::default());- let forearm = probe.map_or(face, |r| {- let drawn = skel.fk(&r.pose);- (drawn.pos[arm[2]] - drawn.pos[arm[1]]).normalize_or(face)- });- let lie = |keep: f32| wrist_for(skel, &thing, &hold, Vec3::new(forearm.x, forearm.y * keep, forearm.z).normalize_or(face));- match lie(1.0) {- Some(along) => lie(1.0 - smooth((clear(along.0, along.1, &rest, &take.avoid) - along.0).length() / 0.03)).unwrap_or(level),- None => level,- }- };- Some((thing.local(at), (thing.turn.inverse() * turn).normalize()))-}--/// How far a thing carried in one hand is turned about the vertical at `t`, radians: so the hand that holds it lies-/// along the forearm as the arm comes to it, up to a quarter turn. A grip doesn't change once taken, so without this a cup raised to the mouth would be-/// held by a wrist bent past what a wrist does; with it the cup turns in the hand's favour, as a person's does.-/// None while it stands, all once carried, none again by the time it is set down.-fn swivel(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, part: &Part, t: f32, thing: &Thing) -> f32 {- let down = match take.release {- Some(Release::Put { at, down, .. }) => 1.0 - smooth(span(t, at, down)),- _ => 1.0,- };- let weight = smooth(span(t, take.grab, take.carried)) * down;- let (Some(arm), Some(rest), Some((_, lie))) = (skel.arm(part.hold.side), hand_rest(skel, part.hold.side), lie_at_grab(skel, base, take, part)) else { return 0.0 };- if weight <= 0.0 {- return 0.0;- }- let own = base(t);- let level = |v: Vec3| Vec3::new(v.x, 0.0, v.z);- // the arm solved to the thing's middle (not the hold, which the turn itself moves): the way the forearm comes- let Some(probe) = reach(skel, &own, part.hold.side, thing.at, ArmOptions::default()) else { return 0.0 };- let drawn = skel.fk(&probe.pose);- let (fingers, forearm) = (level(thing.turn * lie * rest.along), level((drawn.pos[arm[2]] - drawn.pos[arm[1]]).normalize_or_zero()));- // a hand or a forearm that points nearly straight up or down says little about which way round: less turn- let sure = smooth(fingers.length().min(forearm.length()) / 0.3);- // by the sine of the angle between them: up to a quarter turn, none where they are in line or dead against each- // other, so it never flips- let turned = (fingers.cross(forearm).y / (fingers.length() * forearm.length()).max(1e-6)).clamp(-1.0, 1.0).asin() * sure;- turned * weight-}