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.

CH20c: the eyes take a look target. <Character gaze={point}> turns each eye joint to a point in the scene on top of the pose, in its head's frame, within an eye's range (35 degrees to a side, 25 down, 17 up), so the eyes cover what the head has not turned; a part that has the eye joints has its upper lids ride the eyes down (blink morphs raised to 0.45 at the lowest). character/eyes.ts: eyesTo, lidsFor, EYE_RANGE.

shinyobjectz523fd309297e
diff --git a/docs/character.feature b/docs/character.feature
index 22ff19ac..13c5aa5e 100644
--- a/docs/character.feature
+++ b/docs/character.feature
@@ -422,6 +422,19 @@ Feature: The character SDK as one system: SOMA bodies, Kimodo motion, generated
Then the body dressed with that head named has its skin in the head's neck colour, graded to the head's
own skin at the collar, and no step of colour shows across the join
+ @done
+ Scenario: CH20c. The eyes take a look target
+ # 11-15. `<Character gaze={[x, y, z]}>` (sdk/character/src/character/eyes.ts): each eye joint is turned to
+ # the point on top of the pose, in its head's frame, so the eyes cover what the head has not turned; no
+ # further than an eye goes (35 degrees to a side, 25 down, 17 up: the head's upper lid has no morph that
+ # lifts it, and at 25 up the iris went under it). A part that has the eye joints has its upper lids ride
+ # the eyes down (its blink morphs raised to 0.45 at the lowest): without that the white showed over the
+ # iris. Seen on the first head, straight, to each side, up and down (sheet3d compositions/gaze_check.tsx).
+ # Not yet: lids that lift, and a stage act's `look` sending the eyes as well as the head.
+ Given a body whose head has eye joints, and a point in the scene
+ Then the eyes turn to it within their range, whatever the neck and head are doing
+ And the upper lids come down with them
+
Scenario: CH21. Hair
# Agreed with the face session 11-15. sdk/body/src/cloth.ts already moves hair and clothes that follow
# skin. Hair cards are the face session's working choice, pending its tests; strand hair stays a reference
diff --git a/sdk/character/src/character/all.ts b/sdk/character/src/character/all.ts
index 7b79152f..5ea2d869 100644
--- a/sdk/character/src/character/all.ts
+++ b/sdk/character/src/character/all.ts
@@ -8,6 +8,7 @@ export * from "./fingers";
export * from "./captured";
export * from "./solve";
export * from "./face";
+export * from "./eyes";
export * from "./hair";
export * from "./skin";
export * from "./drawing";
diff --git a/sdk/character/src/character/drawing.tsx b/sdk/character/src/character/drawing.tsx
index 41144d27..c78c0599 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, faceWeights, 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 } from "./all";
// ---------------------------------------------------------------- placement
@@ -81,6 +81,11 @@ export interface CharacterProps extends Placement {
* "mouth-open", "eyebrows-left-up" ...) and presets mixed from them (FACE_PRESETS: blink, smile, frown, surprise,
* sad, and the lips' open, pucker and wide), 0 to 1; they add */
face?: Face;
+ /** a point in the scene the eyes turn to (character/eyes.ts): each eye joint turned to it on top of the pose, no
+ * further than an eye goes (35 degrees to a side, 17 up, 25 down), so the eyes cover what the head has not turned.
+ * For a body whose head has eye joints (SOMA's LeftEye and RightEye, which a generated head's eyes follow); a part
+ * that has those joints has its upper lids ride the eyes down (its blink_left and blink_right morphs raised) */
+ gaze?: Vec3;
/** the hair drawn as hair (character/hair.ts: strand highlights on the body's "hair" material, after lighting): on by
* default, false for the plain surface, or settings over HAIR_LOOK; `coverage: true` softens the cards' cut edges
* (alphaMode "coverage", for a scene with <AntiAliasing mode="msaa">: without MSAA the cards draw solid); `color`
@@ -175,6 +180,7 @@ export function Character(props: CharacterProps) {
const k = at.scale ?? 1;
let p = props.pose ?? rest(skel);
if (props.goals) p = solve(skel, p, goalsToBody(at, props.goals));
+ if (props.gaze) p = eyesTo(skel, p, toBody(at, props.gaze));
const w = pose(skel, p);
const turn = qyaw(at.yaw ?? 0);
// the skin stands at the origin scaled by k, so its joints go where they are in the scene over k
@@ -199,8 +205,11 @@ export function Character(props: CharacterProps) {
</Model>
{(props.parts ?? []).map((part) => {
const id = part.src.split("/").pop()!.replace(/\.glb$/, "");
+ // (a head's lids ride its eyes down: character/eyes.ts)
+ const lids = props.gaze && part.joints?.some((j) => j === "LeftEye" || j === "eye_l") ? lidsFor(skel, p) : 0;
+ const pm = lids > 0 ? { ...part.morphs, blink_left: Math.min(1, (part.morphs?.blink_left ?? 0) + lids), blink_right: Math.min(1, (part.morphs?.blink_right ?? 0) + lids) } : part.morphs;
return (
- <Model key={id} name={`${name}-${id}`} src={part.src} bones={part.joints ? Object.fromEntries(part.joints.filter((j) => j in bones).map((j) => [j, bones[j]])) : bones} scale={k} {...(glitch ? { glitch } : {})} {...pass} {...(part.morphs && Object.keys(part.morphs).length ? { morphs: part.morphs } : {})} {...(part.cloth ? { cloth: part.cloth } : {})}>
+ <Model key={id} name={`${name}-${id}`} src={part.src} bones={part.joints ? Object.fromEntries(part.joints.filter((j) => j in bones).map((j) => [j, bones[j]])) : bones} scale={k} {...(glitch ? { glitch } : {})} {...pass} {...(pm && Object.keys(pm).length ? { morphs: pm } : {})} {...(part.cloth ? { cloth: part.cloth } : {})}>
{part.children}
{id.includes("hair") && props.hair !== false && hairMaterial(props.hair)}
</Model>
diff --git a/sdk/character/src/character/eyes.ts b/sdk/character/src/character/eyes.ts
new file mode 100644
index 00000000..5567e05e
--- /dev/null
+++ b/sdk/character/src/character/eyes.ts
@@ -0,0 +1,39 @@
+// The eyes' look target (docs/character.feature CH20c): each eye joint turned to a point, within what an eye does,
+// on top of whatever the body's motion has done with the neck and head. A head part's eyes are skinned to SOMA's
+// LeftEye and RightEye joints (a creature's eye_l and eye_r), which no clip turns: this is what turns them.
+import { I, norm, qaxis, qinv, qmul, qrot, sub, pose, type Pose, type Quat, type Skeleton, type Vec3 } from "./all";
+
+/** how far an eye turns from straight ahead in its head, radians: to a side, up and down. (Up is the least: a head's
+ * upper lid has no morph that lifts it, and at 25 degrees up the iris went under it.) */
+export const EYE_RANGE = { side: 0.61, up: 0.3, down: 0.44 };
+const EYES = [["LeftEye", "RightEye"], ["eye_l", "eye_r"]];
+
+/** A pose with its eyes turned to `target` (a point in the body's frame), each no further than `range` from straight
+ * ahead in the head: past that the eye stops at its limit, and the rest is the head's to turn. A body with no eye
+ * joints comes back as it was. */
+export function eyesTo(skel: Skeleton, p: Pose, target: Vec3, range = EYE_RANGE): Pose {
+ const idx = (n: string) => skel.joints.findIndex((j) => j.name === n);
+ const eyes = (EYES.find((pair) => pair.every((n) => idx(n) >= 0)) ?? []).map(idx);
+ if (!eyes.length) return p;
+ const turns: Quat[] = skel.joints.map((_, j) => p.turns[j] ?? I);
+ const w = pose(skel, { root: p.root, turns });
+ const clamp = (v: number, lim: number) => Math.max(-lim, Math.min(lim, v));
+ for (const i of eyes) {
+ // the way to the target in the head's own frame (the eye's parent): at rest an eye looks along +z
+ const d = qrot(qinv(w.rot[skel.joints[i].parent]), norm(sub(target, w.pos[i])));
+ const side = clamp(Math.atan2(d[0], d[2]), range.side), up = Math.max(-range.down, Math.min(range.up, Math.atan2(d[1], Math.hypot(d[0], d[2]))));
+ turns[i] = qmul(qaxis([0, 1, 0], side), qaxis([1, 0, 0], -up));
+ }
+ return { root: p.root, turns };
+}
+
+/** How far the upper lids come down with the eyes in a pose (0 to 1 of a blink): a lid rides its eye, so eyes
+ * turned all the way down lower it to `most` of shut, and eyes level or raised leave it open. For the head's
+ * blink_left and blink_right morphs, added to whatever blink it has. */
+export function lidsFor(skel: Skeleton, p: Pose, most = 0.45, range = EYE_RANGE): number {
+ const i = skel.joints.findIndex((j) => j.name === "LeftEye" || j.name === "eye_l"), q = i >= 0 ? p.turns[i] : undefined;
+ if (!q) return 0;
+ // how far below level the eye's own +z points, in its head's frame
+ const down = Math.asin(Math.max(-1, Math.min(1, 2 * (q[3] * q[0] - q[1] * q[2]))));
+ return Math.max(0, Math.min(1, down / range.down)) * most;
+}
diff --git a/sdk/character/test/eyes.test.ts b/sdk/character/test/eyes.test.ts
new file mode 100644
index 00000000..39bab6b7
--- /dev/null
+++ b/sdk/character/test/eyes.test.ts
@@ -0,0 +1,40 @@
+// The eyes' look target (src/character/eyes.ts, docs/character.feature CH20c): the eye joints turned to a point within
+// an eye's range, on top of the head's turn, and the lids riding the eyes down.
+import { expect, test } from "bun:test";
+import { EYE_RANGE, eyesTo, lidsFor, pose, qaxis, qrot, type Pose, 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("Head", 0, [0, 1.6, 0]), J("LeftEye", 1, [0.03, 1.65, 0.06]), J("RightEye", 1, [-0.03, 1.65, 0.06])] } as unknown as Skeleton;
+const rest: Pose = { root: [0, 1, 0], turns: skel.joints.map(() => [0, 0, 0, 1]) };
+const ahead = (p: Pose, i: number) => qrot(pose(skel, p).rot[i], [0, 0, 1]);
+const angle = (a: number[], b: number[]) => Math.acos(Math.min(1, a[0] * b[0] + a[1] * b[1] + a[2] * b[2]));
+
+test("each eye points at a target within its range", () => {
+ const t: [number, number, number] = [0.4, 1.75, 1.5], p = eyesTo(skel, rest, t);
+ for (const i of [2, 3]) {
+ const e = skel.joints[i].head, d = [t[0] - e[0], t[1] - e[1], t[2] - e[2]], l = Math.hypot(...d);
+ expect(angle(ahead(p, i), d.map((v) => v / l))).toBeLessThan(1e-3);
+ }
+});
+
+test("past its range an eye stops: 35 degrees to a side, 17 up, 25 down", () => {
+ const side = ahead(eyesTo(skel, rest, [9, 1.65, 0.06]), 2), up = ahead(eyesTo(skel, rest, [0.03, 9, 0.5]), 2), down = ahead(eyesTo(skel, rest, [0.03, -9, 0.5]), 2);
+ expect(Math.atan2(side[0], side[2])).toBeCloseTo(EYE_RANGE.side, 4);
+ expect(Math.asin(up[1])).toBeCloseTo(EYE_RANGE.up, 4);
+ expect(Math.asin(-down[1])).toBeCloseTo(EYE_RANGE.down, 4);
+});
+
+test("the eyes cover what the head has not turned", () => {
+ // the head turned 20 degrees to its left, the target straight ahead of the body: the eyes turn back toward it
+ const turned: Pose = { root: rest.root, turns: [rest.turns[0], qaxis([0, 1, 0], 0.35), rest.turns[2], rest.turns[3]] };
+ const w = pose(skel, turned), t: [number, number, number] = [0, 1.65, 3], p = eyesTo(skel, turned, t);
+ const d = [t[0] - w.pos[2][0], t[1] - w.pos[2][1], t[2] - w.pos[2][2]], l = Math.hypot(...d);
+ expect(angle(ahead(p, 2), d.map((v) => v / l))).toBeLessThan(1e-3);
+});
+
+test("the lids come down with the eyes, and stay up otherwise", () => {
+ expect(lidsFor(skel, rest)).toBe(0);
+ expect(lidsFor(skel, eyesTo(skel, rest, [0.03, 9, 0.5]))).toBe(0);
+ expect(lidsFor(skel, eyesTo(skel, rest, [0.03, -9, 0.5]))).toBeCloseTo(0.45, 3);
+ expect(eyesTo({ ...skel, joints: skel.joints.slice(0, 2) } as Skeleton, rest, [0, 0, 1])).toBe(rest);
+});