From 4618e784265589c381e51f9ea3d3c485c0f191df Mon Sep 17 00:00:00 2001 From: DoodleBear Date: Mon, 23 Nov 2026 04:44:18 +0900 Subject: [PATCH] refactor(vrm-companion): split stage character lifecycle (H-2) Issue: #4521 Agent-Task: codex/local/01a204a0-debd-7873-96f6-6afc9f2e48ad --- docs/architecture/feature-map.md | 2 +- .../src/client/stage-character.ts | 72 ++++++++++++++ .../vrm-companion/src/client/stage-types.ts | 37 +++++++ packages/vrm-companion/src/client/stage.ts | 96 +++---------------- scripts/health-baseline.json | 1 - 5 files changed, 122 insertions(+), 86 deletions(-) create mode 100644 packages/vrm-companion/src/client/stage-character.ts create mode 100644 packages/vrm-companion/src/client/stage-types.ts diff --git a/docs/architecture/feature-map.md b/docs/architecture/feature-map.md index 0398bcca49..89849d123a 100644 --- a/docs/architecture/feature-map.md +++ b/docs/architecture/feature-map.md @@ -146,7 +146,7 @@ The Bot Runtime delegates Assignment creation and requests to [intake](../../pac - **Runtime.** PersonaBot presentation and the Activity Frame consume shared Bot state. The VRM Companion is a separate Plugin with its own Client registry entry. [`packages/core/src/bots/avatar-appearance.ts`](../../packages/core/src/bots/avatar-appearance.ts), [`packages/core/src/bots/part-library.ts`](../../packages/core/src/bots/part-library.ts), [`packages/core/src/state/bot-state.ts`](../../packages/core/src/state/bot-state.ts), [`packages/vrm-companion/src/index.ts`](../../packages/vrm-companion/src/index.ts) - **Host and registration.** Both bridge files expose `botAvatarSet`, `botBannerSet`, `botAppearanceSet`, `botPrimaryColorSet`, `partLibraryList` and `partLibraryImport`. [Host feature entry](../../packages/core/src/features/avatars/plugin.ts) creates the Avatar Part Library and mounts Avatar and banner Fetch routes. The composition root connects Registry changes to roster publication. The VRM Plugin owns its own registration. [`packages/core/src/bots/avatar-http.ts`](../../packages/core/src/bots/avatar-http.ts), [`packages/core/src/bots/banner-http.ts`](../../packages/core/src/bots/banner-http.ts) - **Storage.** `persona-bot-registry` holds Avatar recipes in `persona_bots`. `avatar-part-library` owns `avatar_part_library`. The VRM Client owns its own assets and presentation preferences. [`packages/core/src/bots/registry.ts`](../../packages/core/src/bots/registry.ts), [`packages/core/src/bots/part-library.ts`](../../packages/core/src/bots/part-library.ts) -- **UI and readers.** Mobile renders shared Avatar appearance. VRM is a separate Client contribution. [`packages/client/src/client/features/avatars/avatar-appearance-editor.tsx`](../../packages/client/src/client/features/avatars/avatar-appearance-editor.tsx), [`packages/client/src/client/avatar.tsx`](../../packages/client/src/client/avatar.tsx), [`packages/vrm-companion/src/client/index.tsx`](../../packages/vrm-companion/src/client/index.tsx), [`apps/mobile/src/ui/chat/avatar-editor.tsx`](../../apps/mobile/src/ui/chat/avatar-editor.tsx), [`apps/mobile/src/ui/bot-avatar.tsx`](../../apps/mobile/src/ui/bot-avatar.tsx), [`packages/host-client/src/bot-avatar-choice.ts`](../../packages/host-client/src/bot-avatar-choice.ts), [`packages/host-client/src/bot-settings.ts`](../../packages/host-client/src/bot-settings.ts) +- **UI and readers.** Mobile renders shared Avatar appearance. The VRM Companion Client [stage](../../packages/vrm-companion/src/client/stage.ts) owns rendering and its lifecycle. [Character scene helpers](../../packages/vrm-companion/src/client/stage-character.ts) mount and release the VRM model and attach its headphones. [`packages/client/src/client/features/avatars/avatar-appearance-editor.tsx`](../../packages/client/src/client/features/avatars/avatar-appearance-editor.tsx), [`packages/client/src/client/avatar.tsx`](../../packages/client/src/client/avatar.tsx), [`packages/vrm-companion/src/client/index.tsx`](../../packages/vrm-companion/src/client/index.tsx), [`apps/mobile/src/ui/chat/avatar-editor.tsx`](../../apps/mobile/src/ui/chat/avatar-editor.tsx), [`apps/mobile/src/ui/bot-avatar.tsx`](../../apps/mobile/src/ui/bot-avatar.tsx), [`packages/host-client/src/bot-avatar-choice.ts`](../../packages/host-client/src/bot-avatar-choice.ts), [`packages/host-client/src/bot-settings.ts`](../../packages/host-client/src/bot-settings.ts) - **CLI.** Bot metadata and Avatar commands. The VRM companion has no dedicated Core CLI. [`packages/core/src/bots/cli-profile.ts`](../../packages/core/src/bots/cli-profile.ts), [`packages/core/src/bots/cli-appearance.ts`](../../packages/core/src/bots/cli-appearance.ts) ### Prompt Presets and prompt inspection diff --git a/packages/vrm-companion/src/client/stage-character.ts b/packages/vrm-companion/src/client/stage-character.ts new file mode 100644 index 0000000000..ab8a9fe97f --- /dev/null +++ b/packages/vrm-companion/src/client/stage-character.ts @@ -0,0 +1,72 @@ +import { + Box3, + Group, + Mesh, + MeshStandardMaterial, + Quaternion, + SphereGeometry, + TorusGeometry, + Vector3, + type Object3D, + type Scene, +} from 'three'; +import { VRMUtils, type VRM } from '@pixiv/three-vrm'; + +export function mountCharacter(loaded: VRM, actor: Group) { + VRMUtils.rotateVRM0(loaded); + actor.add(loaded.scene); + loaded.scene.traverse((object) => { + if (object instanceof Mesh) object.castShadow = object.receiveShadow = true; + }); + const box = new Box3().setFromObject(loaded.scene); + const size = box.getSize(new Vector3()); + return { box, size }; +} + +export function attachHeadphones( + rawHead: Object3D, + actor: Group, + scene: Scene, + modelScale: number, +) { + const headphones = new Group(); + const shell = new MeshStandardMaterial({ color: 0x8d3e4c, roughness: 0.7 }); + const cushion = new MeshStandardMaterial({ color: 0x27333e, roughness: 1 }); + const band = new Mesh(new TorusGeometry(0.122, 0.018, 8, 32, Math.PI), shell); + band.position.y = 0.075; + headphones.add(band); + for (const side of [-1, 1]) { + const cup = new Mesh(new SphereGeometry(0.052, 16, 12), shell); + cup.scale.set(0.55, 1, 0.85); + cup.position.set(side * 0.122, 0.065, 0); + headphones.add(cup); + const pad = new Mesh(new SphereGeometry(0.05, 16, 12), cushion); + pad.scale.set(0.4, 1, 0.8); + pad.position.set(side * 0.105, 0.065, 0); + headphones.add(pad); + } + actor.updateMatrixWorld(true); + headphones.position.copy(rawHead.getWorldPosition(new Vector3())); + headphones.quaternion.copy(actor.getWorldQuaternion(new Quaternion())); + headphones.scale.setScalar(modelScale); + scene.add(headphones); + rawHead.attach(headphones); + return headphones; +} + +export function releaseCharacter(value: VRM): void { + value.scene.traverse((object) => { + const mesh = object as import('three').Mesh; + const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material]; + for (const material of materials) + if (material) + for (const entry of Object.values(material)) { + const image = (entry as { isTexture?: boolean; source?: { data?: unknown } } | null) + ?.isTexture + ? (entry as import('three').Texture).source.data + : undefined; + if (typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap) image.close(); + } + }); + VRMUtils.deepDispose(value.scene); +} diff --git a/packages/vrm-companion/src/client/stage-types.ts b/packages/vrm-companion/src/client/stage-types.ts new file mode 100644 index 0000000000..95c23b7e9c --- /dev/null +++ b/packages/vrm-companion/src/client/stage-types.ts @@ -0,0 +1,37 @@ +import type { BotPresentationSnapshot } from '@botharness/ui/panels'; +import type { Loading, Problem } from './copy.js'; +import type { MotionCommand } from './motion.js'; +import type { Mood, Weather } from './room.js'; +import type { CharacterShading } from './shading.js'; + +export type CameraView = 'desk' | 'room' | 'portrait'; +export interface SceneState { + phase: string; + wandering: boolean; + ready: boolean; +} +export interface SceneChoices { + camera: CameraView; + mood: Mood; + weather: Weather; + shading: CharacterShading; + effects: boolean; + tour?: boolean; +} +export interface StageCallbacks { + loading(value: Loading): void; + error(value: Problem): void; + changed(state: SceneState): void; + poster(url: string): void; +} +export interface Stage { + update(snapshot: BotPresentationSnapshot): void; + command(command: MotionCommand): void; + camera(view: CameraView): void; + mood(value: Mood): void; + weather(value: Weather): void; + effects(value: boolean): void; + tour(value: boolean): void; + shading(value: CharacterShading): void; + dispose(): void; +} diff --git a/packages/vrm-companion/src/client/stage.ts b/packages/vrm-companion/src/client/stage.ts index 3d8a1bcfd0..0d74ad64e1 100644 --- a/packages/vrm-companion/src/client/stage.ts +++ b/packages/vrm-companion/src/client/stage.ts @@ -5,7 +5,6 @@ import { PMREMGenerator, ACESFilmicToneMapping, SRGBColorSpace, - Box3, Clock, DirectionalLight, PerspectiveCamera, @@ -18,57 +17,29 @@ import { Group, Object3D, Color, - Mesh, - MeshStandardMaterial, - TorusGeometry, - SphereGeometry, } from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; import { RoomEnvironment } from 'three/examples/jsm/environments/RoomEnvironment.js'; import { VRMLoaderPlugin, VRMUtils, type VRM, type VRMHumanBoneName } from '@pixiv/three-vrm'; import type { BotPresentationSnapshot } from '@botharness/ui/panels'; -import { RoomMotion, type MotionCommand } from './motion.js'; +import { RoomMotion } from './motion.js'; import { FACE_EXPRESSIONS, FaceBlend, dominantFace, faceTarget, type FaceWeights } from './face.js'; import { IdleScheduler, type IdleAction } from './idle.js'; -import { createRoom, type Mood, type Weather } from './room.js'; +import { createRoom } from './room.js'; import { enableLilToon } from '@mochiya/three-liltoon'; import { createCharacterShading, type CharacterShading } from './shading.js'; import { createPost } from './post.js'; -import type { Loading, Problem } from './copy.js'; import { RATIOS, createPacing, frameRate, nextPacing } from './pacing.js'; +import { + attachHeadphones, + mountCharacter, + releaseCharacter as release, +} from './stage-character.js'; +import type { CameraView, SceneChoices, Stage, StageCallbacks } from './stage-types.js'; + +export type { CameraView, SceneState, SceneChoices, StageCallbacks, Stage } from './stage-types.js'; -export type CameraView = 'desk' | 'room' | 'portrait'; -export interface SceneState { - phase: string; - wandering: boolean; - ready: boolean; -} -export interface SceneChoices { - camera: CameraView; - mood: Mood; - weather: Weather; - shading: CharacterShading; - effects: boolean; - tour?: boolean; -} -export interface StageCallbacks { - loading(value: Loading): void; - error(value: Problem): void; - changed(state: SceneState): void; - poster(url: string): void; -} -export interface Stage { - update(snapshot: BotPresentationSnapshot): void; - command(command: MotionCommand): void; - camera(view: CameraView): void; - mood(value: Mood): void; - weather(value: Weather): void; - effects(value: boolean): void; - tour(value: boolean): void; - shading(value: CharacterShading): void; - dispose(): void; -} const GRIP = new Quaternion().setFromAxisAngle(new Vector3(1, 0, 0), -Math.PI / 2); const SIDEWAYS = new Quaternion().setFromAxisAngle(new Vector3(0, 1, 0), Math.PI / 2); const FINGERS = ['Index', 'Middle', 'Ring', 'Little'] as const; @@ -275,22 +246,6 @@ export function mountStage( host.dataset.vrmSeated = motion.seated.toFixed(3); host.dataset.vrmPosition = `${motion.x.toFixed(3)},${motion.z.toFixed(3)}`; }; - const release = (value: VRM) => { - value.scene.traverse((object) => { - const mesh = object as import('three').Mesh; - const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material]; - for (const material of materials) - if (material) - for (const entry of Object.values(material)) { - const image = (entry as { isTexture?: boolean; source?: { data?: unknown } } | null) - ?.isTexture - ? (entry as import('three').Texture).source.data - : undefined; - if (typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap) image.close(); - } - }); - VRMUtils.deepDispose(value.scene); - }; const canDraw = () => !disposed && visible && @@ -911,13 +866,7 @@ export function mountStage( } legacy = loaded.meta.metaVersion === '0'; host.dataset.vrmSpec = legacy ? '0' : '1'; - VRMUtils.rotateVRM0(loaded); - actor.add(loaded.scene); - loaded.scene.traverse((object) => { - if (object instanceof Mesh) object.castShadow = object.receiveShadow = true; - }); - const box = new Box3().setFromObject(loaded.scene); - const size = box.getSize(new Vector3()); + const { box, size } = mountCharacter(loaded, actor); modelScale = 1.65 / Math.max(size.y, 0.1); actor.scale.setScalar(modelScale); floorOffset = -box.min.y * modelScale; @@ -946,28 +895,7 @@ export function mountStage( const reveal: Object3D[] = [actor, room.root]; const rawHead = loaded.humanoid.getRawBoneNode('head'); if (rawHead && wearing) { - const headphones = new Group(); - const shell = new MeshStandardMaterial({ color: 0x8d3e4c, roughness: 0.7 }); - const cushion = new MeshStandardMaterial({ color: 0x27333e, roughness: 1 }); - const band = new Mesh(new TorusGeometry(0.122, 0.018, 8, 32, Math.PI), shell); - band.position.y = 0.075; - headphones.add(band); - for (const side of [-1, 1]) { - const cup = new Mesh(new SphereGeometry(0.052, 16, 12), shell); - cup.scale.set(0.55, 1, 0.85); - cup.position.set(side * 0.122, 0.065, 0); - headphones.add(cup); - const pad = new Mesh(new SphereGeometry(0.05, 16, 12), cushion); - pad.scale.set(0.4, 1, 0.8); - pad.position.set(side * 0.105, 0.065, 0); - headphones.add(pad); - } - actor.updateMatrixWorld(true); - headphones.position.copy(rawHead.getWorldPosition(new Vector3())); - headphones.quaternion.copy(actor.getWorldQuaternion(new Quaternion())); - headphones.scale.setScalar(modelScale); - scene.add(headphones); - rawHead.attach(headphones); + const headphones = attachHeadphones(rawHead, actor, scene, modelScale); reveal.push(headphones); } if (loaded.lookAt) loaded.lookAt.target = gaze; diff --git a/scripts/health-baseline.json b/scripts/health-baseline.json index 9cf85114d5..29c0dc2c03 100644 --- a/scripts/health-baseline.json +++ b/scripts/health-baseline.json @@ -40,7 +40,6 @@ "packages/core/test/messaging-weixin-dm-mirror.test.ts", "packages/core/test/operational-database.test.ts", "packages/core/test/relationship-control.test.ts", - "packages/vrm-companion/src/client/stage.ts", "prototype/avatar-hair/app.js", "prototype/dot-matrix/dotmatrix.js", "scripts/call-keyframe-eval.mjs",