Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Animate film actors with coherent authored joints

Nguyen Xuan Anff05328e2c32
diff --git a/src/film-rig.js b/src/film-rig.js
new file mode 100644
index 00000000..2e32f064
--- /dev/null
+++ b/src/film-rig.js
@@ -0,0 +1,37 @@
+import { mat4, quat } from 'gl-matrix';
+import { createAnimationClip } from './animation.js';
+import { createAnimationMixer } from './animation-mixer.js';
+import { createSceneGraph } from './scene-graph.js';
+
+/** Rigid actor groups keep attached clothing and hands on the same authored joints. */
+export function createFilmActorRig(parts) {
+ const groups = new Map();
+ const bindings = [];
+ for (const part of parts) {
+ if (!part.actorJoint) continue;
+ const joint = part.actorJoint;
+ if (!groups.has(joint.name)) groups.set(joint.name, joint);
+ bindings.push({ part, base: mat4.clone(part.matrix), joint: joint.name });
+ }
+ if (!groups.size) return () => {};
+ const graph = createSceneGraph(Array.from(groups.values(), joint => ({ name: joint.name, pivot: joint.pivot })));
+ const tracks = Array.from(groups.values(), joint => {
+ const poses = joint.kind === 'head'
+ ? [[0, 0], [1.7, -0.055], [2.5, -0.055], [4.8, 0.095], [7, 0.095], [10.2, 0], [12, 0]]
+ : [[0, 0], [2.2, -0.018], [4.3, -0.075], [5.7, -0.075], [8.8, -0.025], [10.5, 0], [12, 0]];
+ return {
+ target: joint.name,
+ property: 'rotation',
+ interpolation: 'SMOOTH',
+ keyframes: poses.map(([time, angle]) => ({ time, value: Array.from(quat.setAxisAngle(quat.create(), joint.axis, angle)) })),
+ };
+ });
+ const mixer = createAnimationMixer(graph.animationTargets);
+ mixer.clipAction(createAnimationClip({ duration: 12, loop: true, tracks })).play({ at: 0 });
+ for (const binding of bindings) binding.worldMatrix = graph.getNode(binding.joint).worldMatrix;
+ return time => {
+ mixer.apply(time);
+ graph.update();
+ for (const binding of bindings) mat4.multiply(binding.part.matrix, binding.worldMatrix, binding.base);
+ };
+}
diff --git a/src/film.js b/src/film.js
index 804a152c..091163fa 100644
--- a/src/film.js
+++ b/src/film.js
@@ -1,5 +1,6 @@
import { mat4, quat, vec3 } from 'gl-matrix';
import { createCameraPath } from './camera-path.js';
+import { createFilmActorRig } from './film-rig.js';
import { box, cylinder, sphere, torus } from './geometry.js';
export const FILM_DURATION = 187;
@@ -284,6 +285,8 @@ function createIgnition(meshes) {
function addScientist(builder, position = [0, 0, 0], hat = true, rotation = 0) {
const { add } = builder;
+ const actorName = `Scientist ${builder.parts.length}`;
+ const joints = new Map();
const transform = point => [position[0] + point[0] * Math.cos(rotation) + point[2] * Math.sin(rotation), position[1] + point[1], position[2] - point[0] * Math.sin(rotation) + point[2] * Math.cos(rotation)];
const silhouette = {
tag: 'unlit', color: [0.0045, 0.0055, 0.005], metallic: 0, roughness: 1, emission: 0,
@@ -295,6 +298,21 @@ function addScientist(builder, position = [0, 0, 0], hat = true, rotation = 0) {
const local = quat.fromEuler(quat.create(), ...angles.map(angle => angle * 180 / Math.PI));
const orientation = quat.multiply(quat.create(), quat.setAxisAngle(quat.create(), [0, 1, 0], rotation), local);
mat4.fromRotationTranslationScale(part.matrix, orientation, transform(point), scale);
+ const head = name === 'Continuous sculpted head' || name === 'Swept hair' || name === 'Ear silhouette' || name.startsWith('Fedora');
+ const arm = ['Jacket sleeve', 'Shirt cuff', 'Resting hand silhouette'].includes(name);
+ if (head || arm) {
+ const side = point[0] < 0 ? -1 : 1;
+ const jointName = head ? `${actorName} head` : `${actorName} arm ${side}`;
+ if (!joints.has(jointName)) {
+ joints.set(jointName, {
+ name: jointName,
+ kind: head ? 'head' : 'arm',
+ pivot: transform(head ? [0, 1.56, -0.005] : [side * 0.205, 1.425, -0.018]),
+ axis: head ? [0, 1, 0] : [Math.cos(rotation), 0, -Math.sin(rotation)],
+ });
+ }
+ part.actorJoint = joints.get(jointName);
+ }
return part;
};
for (const side of [-1, 1]) {
@@ -592,12 +610,11 @@ function createSparks(meshes) {
}
function animateSet(parts, sceneName) {
+ const animateActors = createFilmActorRig(parts);
const moving = parts.filter(part =>
sceneName === 'sparks' || sceneName === 'atom' ||
(sceneName === 'waves' && part.name !== 'Black reflecting pool') ||
- Boolean(part.motionGroup) || part.name === 'Meter pointer' || part.name === 'Signal pilot light' ||
- part.name === 'Continuous sculpted head' || part.name === 'Fedora sweeping brim' ||
- part.name === 'Fedora tapered felt crown' || part.name === 'Fedora grosgrain band' || part.name === 'Fedora crown crease');
+ Boolean(part.motionGroup) || part.name === 'Meter pointer' || part.name === 'Signal pilot light');
const bases = moving.map(part => new Float32Array(part.matrix));
const pivots = moving.map((part, index) => part.motionPivot ?? [bases[index][12] + 0.04, bases[index][13], bases[index][14] - 0.05]);
const inversePivots = pivots.map(pivot => pivot.map(value => -value));
@@ -607,6 +624,7 @@ function animateSet(parts, sceneName) {
if (part.name === 'Signal pilot light') part.material = { ...materials.amber };
}
return function animate(time) {
+ animateActors(time);
moving.forEach((part, index) => {
const base = bases[index];
part.matrix.set(base);
diff --git a/tests/film.test.js b/tests/film.test.js
index c9a6c2e9..c9d60e0c 100644
--- a/tests/film.test.js
+++ b/tests/film.test.js
@@ -3,6 +3,7 @@ import test from 'node:test';
import { createFilmScenes, FILM_DURATION, FILM_SHOTS, sampleFilm } from '../src/film.js';
import { resolveEffects } from '../src/effects.js';
import { planRenderSize } from '../src/budget.js';
+import { mat4 } from 'gl-matrix';
const distance = (first, second) => Math.hypot(...first.map((value, axis) => value - second[axis]));
@@ -158,3 +159,29 @@ test('tailored figure surfaces face outward and keep a closed shoulder silhouett
assert.ok(coat.normals[farthest] > 0.5);
assert.ok(scene.parts.some(part => part.name === 'Tailored shoulder line'));
});
+
+test('authored actor joints retain attached headwear and hands through holds and arbitrary seeks', () => {
+ const scene = createFilmScenes().portrait;
+ const head = scene.parts.find(part => part.name === 'Continuous sculpted head');
+ const hat = scene.parts.find(part => part.name === 'Fedora tapered felt crown');
+ const hand = scene.parts.find(part => part.name === 'Resting hand silhouette');
+ const cuff = scene.parts.find(part => part.name === 'Shirt cuff' && part.actorJoint.name === hand.actorJoint.name);
+ const relative = (parent, child) => Array.from(mat4.multiply(mat4.create(), mat4.invert(mat4.create(), parent.matrix), child.matrix));
+ scene.animate(0);
+ const headAttachment = relative(head, hat);
+ const armAttachment = relative(cuff, hand);
+ const assembled = Array.from(head.matrix);
+ scene.animate(5);
+ const held = Array.from(head.matrix);
+ assert.notDeepEqual(held, assembled);
+ scene.animate(6.5);
+ assert.deepEqual(Array.from(head.matrix), held);
+ for (const time of [10.3, 2.7, 23.9, 0, 5]) {
+ scene.animate(time);
+ relative(head, hat).forEach((value, index) => assert.ok(Math.abs(value - headAttachment[index]) < 1e-5));
+ relative(cuff, hand).forEach((value, index) => assert.ok(Math.abs(value - armAttachment[index]) < 1e-5));
+ }
+ assert.deepEqual(Array.from(head.matrix), held);
+ scene.animate(12);
+ assert.deepEqual(Array.from(head.matrix), assembled);
+});