Skip to content

armgit / raea

Public Imported from GitHub
0

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.

raea/character: onJoint, where something carried on a joint goes as the body is posed (a slung rifle on RightShoulder)

shinyobjectz744301d1a107
diff --git a/sdk/character/src/character.tsx b/sdk/character/src/character.tsx
index 30462e40..62a6e175 100644
--- a/sdk/character/src/character.tsx
+++ b/sdk/character/src/character.tsx
@@ -13,7 +13,7 @@
// Every joint's rest frame is the world's (the T-pose the skin is bound in, no bone roll), so a joint's turn is the same
// kind of thing a BVH file gives: a rotation from rest, relative to its parent's. Frame-pure: a pose is a function of
// its inputs, so a render and a live play agree.
-export { I, handRest, B, SIDES, BODY_JOINTS, FINGER_JOINTS, sideOf, mirrorName, roleOf, isFinger, somaName, oldName, sameJoint, skinJoints, add, sub, scale3, dot, cross, len, norm, qmul, qinv, qnorm, qrot, qaxis, qfromTo, slerp, qbasis, eulerXYZ, qyaw, joint, bodyScale, captured, rest, pose, withFingers, setWorld, held, aim, twoBone, softElbows, flatHand, cupHand, ballInHand, palmAt, handAlong, palmFacing, FINGERS, fingerJoints, HAND_LIMITS, RELAXED_HAND, RELAXED_CAPTURE, relaxHands, handPose, capturedHand, HAND_POSES, capturedLocal, alongPath, pathHand, pinchPoint, SKIN_GIVE, closeOn, fingerSegments, surface, handSurface, reach, plant, lookAt, sit, blend, leanToward, solve, toBody, toScene, goalsToBody, registerBody, Character, FACE_UNITS, FACE_PRESETS, faceWeights, HAIR_LOOK, HAIR_WESL, SKIN_LOOK, SKIN_SURFACE, SKIN_WESL } from "./character/all";
+export { I, handRest, B, SIDES, BODY_JOINTS, FINGER_JOINTS, sideOf, mirrorName, roleOf, isFinger, somaName, oldName, sameJoint, skinJoints, add, sub, scale3, dot, cross, len, norm, qmul, qinv, qnorm, qrot, qaxis, qfromTo, slerp, qbasis, eulerXYZ, qyaw, joint, bodyScale, captured, rest, pose, withFingers, setWorld, held, aim, twoBone, softElbows, flatHand, cupHand, ballInHand, palmAt, handAlong, palmFacing, FINGERS, fingerJoints, HAND_LIMITS, RELAXED_HAND, RELAXED_CAPTURE, relaxHands, handPose, capturedHand, HAND_POSES, capturedLocal, alongPath, pathHand, pinchPoint, SKIN_GIVE, closeOn, fingerSegments, surface, handSurface, reach, plant, lookAt, sit, blend, leanToward, solve, toBody, toScene, onJoint, goalsToBody, registerBody, Character, FACE_UNITS, FACE_PRESETS, faceWeights, HAIR_LOOK, HAIR_WESL, SKIN_LOOK, SKIN_SURFACE, SKIN_WESL } from "./character/all";
export type { Quat, Joint, Skeleton, Pose, Posed, Side, Finger, HandShape, Closing, FingerSegment, SurfacePoint, HandGoal, FootGoal, SeatGoal, Goals, Placement, CharacterProps, CharacterGhost, FaceUnit, FacePreset, Face, HairLook } from "./character/all";
diff --git a/sdk/character/src/character/drawing.tsx b/sdk/character/src/character/drawing.tsx
index e0bbe624..71fb7944 100644
--- a/sdk/character/src/character/drawing.tsx
+++ b/sdk/character/src/character/drawing.tsx
@@ -6,7 +6,7 @@ import { SKIN_LOOK, SKIN_SURFACE, SKIN_WESL, type SkinLook, type SkinSurface } f
import { ExtendedMaterial, LinearRgba, ShaderExtension, Srgba, StandardMaterial } from "@raea/render/bevy3d";
import type { Glitch } from "@raea/physics/physics";
import { staticFile } from "@raea/video/media";
-import { Goals, Pose, Side, Skeleton, add, eulerXYZ, eyesTo, faceWeights, lidsFor, pose, qmul, qrot, qyaw, rest, scale3, skinJoints, solve, sub, type Face } from "./all";
+import { Goals, Pose, Side, Skeleton, add, eulerXYZ, eyesTo, faceWeights, lidsFor, pose, qmul, qrot, qyaw, rest, scale3, skinJoints, solve, sub, type Face, type Quat } from "./all";
// ---------------------------------------------------------------- placement
@@ -28,6 +28,19 @@ export function toScene(at: Placement, p: Vec3): Vec3 {
return add(at.position ?? [0, 0, 0], qrot(qyaw(at.yaw ?? 0), scale3(p, at.scale ?? 1)));
}
+/**
+ * Where something carried on a joint goes as the body is posed (a slung rifle on RightShoulder, a lantern in a hand):
+ * `at` and `turn` place the thing against the joint as the body stands at rest (metres in the body's frame, which faces
+ * +z with its right at -x, and the thing's turn), and the joint's turn from rest carries both, as a cap rides Head. The
+ * result is a <Model>'s `position` and `rotation` in the scene, and the quaternion.
+ */
+export function onJoint(skel: Skeleton, p: Pose, joint: string, at: Vec3, turn: Quat = [0, 0, 0, 1], placement: Placement = {}): { position: Vec3; rotation: Vec3; quat: Quat } {
+ const j = skinJoints(skel, pose(skel, p)).find((q) => q.name === joint);
+ if (!j) throw new Error(`onJoint: ${skel.name} has no joint ${joint}`);
+ const q = qmul(qyaw(placement.yaw ?? 0), qmul(j.rot, turn));
+ return { position: toScene(placement, add(j.pos, qrot(j.rot, at))), rotation: eulerXYZ(q), quat: q };
+}
+
/** Goals in the scene's coordinates brought into the body's frame (directions turned, points moved and turned). */
export function goalsToBody(at: Placement, g: Goals): Goals {
const pt = (v: Vec3) => toBody(at, v);
diff --git a/sdk/character/test/carry.test.ts b/sdk/character/test/carry.test.ts
new file mode 100644
index 00000000..96c6904b
--- /dev/null
+++ b/sdk/character/test/carry.test.ts
@@ -0,0 +1,38 @@
+// Something carried on a joint (src/character/drawing.tsx onJoint): placed against the joint at rest, carried by the
+// joint's turn from rest, and put in the scene by the body's placement, as a cap rides Head.
+import { expect, test } from "bun:test";
+import { onJoint, pose, qaxis, qmul, qrot, type Pose, type Quat, type Skeleton } from "../src/character/all";
+
+const J = (name: string, parent: number, head: [number, number, number]) => ({ name, parent, head, tail: [head[0], head[1] + 0.02, head[2]] as [number, number, number] });
+const skel = { name: "t", height: 1.7, joints: [J("Hips", -1, [0, 1, 0]), J("Chest", 0, [0, 1.3, 0]), J("RightShoulder", 1, [-0.05, 1.45, 0])] } as unknown as Skeleton;
+const still: Pose = { root: [0, 1, 0], turns: skel.joints.map(() => [0, 0, 0, 1] as Quat) };
+const near = (a: number[], b: number[], eps = 1e-6) => a.forEach((v, i) => expect(Math.abs(v - b[i])).toBeLessThan(eps));
+const at: [number, number, number] = [-0.15, -0.3, -0.15];
+
+test("at rest the thing is the joint's place plus its offset, unturned", () => {
+ const c = onJoint(skel, still, "RightShoulder", at);
+ near(c.position, [-0.2, 1.15, -0.15]);
+ near(c.quat, [0, 0, 0, 1]);
+});
+
+test("the joint's turn carries the thing round the joint and turns it with it", () => {
+ const chest = qaxis([0, 1, 0], Math.PI / 2), own = qaxis([1, 0, 0], 0.3);
+ const turned: Pose = { ...still, turns: [[0, 0, 0, 1], chest, [0, 0, 0, 1]] };
+ const c = onJoint(skel, turned, "RightShoulder", at, own);
+ const w = pose(skel, turned), sh = w.pos[2];
+ // the offset turned as the joint turned, from the joint's new place
+ near(c.position, sh.map((v, i) => v + qrot(chest, at)[i]));
+ near(c.quat, qmul(chest, own));
+ expect(Math.hypot(c.position[0] - sh[0], c.position[1] - sh[1], c.position[2] - sh[2])).toBeCloseTo(Math.hypot(...at), 6);
+});
+
+test("the body's placement moves and turns the carried thing with the body", () => {
+ const c = onJoint(skel, still, "RightShoulder", at, [0, 0, 0, 1], { position: [2, 0, 1], yaw: Math.PI / 2 });
+ // the body turned a quarter about up: its -x (its right) now points along +z
+ near(c.position, [2 + -0.15, 1.15, 1 + 0.2]);
+ near(c.quat, qaxis([0, 1, 0], Math.PI / 2));
+});
+
+test("a joint the body hasn't is an error that names it", () => {
+ expect(() => onJoint(skel, still, "LeftHand", at)).toThrow(/no joint LeftHand/);
+});