Spaces:
Sleeping
Sleeping
File size: 2,214 Bytes
af60542 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import * as THREE from "three";
import {
VRM,
VRMExpressionManager,
VRMExpressionPresetName,
} from "@pixiv/three-vrm";
import { AutoLookAt } from "./autoLookAt";
import { AutoBlink } from "./autoBlink";
/**
* Expressionを管理するクラス
*
* 主に前の表情を保持しておいて次の表情を適用する際に0に戻す作業や、
* 前の表情が終わるまで待ってから表情適用する役割を持っている。
*/
export class ExpressionController {
private _autoLookAt: AutoLookAt;
private _autoBlink?: AutoBlink;
private _expressionManager?: VRMExpressionManager;
private _currentEmotion: VRMExpressionPresetName;
private _currentLipSync: {
preset: VRMExpressionPresetName;
value: number;
} | null;
constructor(vrm: VRM, camera: THREE.Object3D) {
this._autoLookAt = new AutoLookAt(vrm, camera);
this._currentEmotion = "neutral";
this._currentLipSync = null;
if (vrm.expressionManager) {
this._expressionManager = vrm.expressionManager;
this._autoBlink = new AutoBlink(vrm.expressionManager);
}
}
public playEmotion(preset: VRMExpressionPresetName) {
if (this._currentEmotion != "neutral") {
this._expressionManager?.setValue(this._currentEmotion, 0);
}
if (preset == "neutral") {
this._autoBlink?.setEnable(true);
this._currentEmotion = preset;
return;
}
const t = this._autoBlink?.setEnable(false) || 0;
this._currentEmotion = preset;
setTimeout(() => {
this._expressionManager?.setValue(preset, 1);
}, t * 1000);
}
public lipSync(preset: VRMExpressionPresetName, value: number) {
if (this._currentLipSync) {
this._expressionManager?.setValue(this._currentLipSync.preset, 0);
}
this._currentLipSync = {
preset,
value,
};
}
public update(delta: number) {
if (this._autoBlink) {
this._autoBlink.update(delta);
}
if (this._currentLipSync) {
const weight =
this._currentEmotion === "neutral"
? this._currentLipSync.value * 0.5
: this._currentLipSync.value * 0.25;
this._expressionManager?.setValue(this._currentLipSync.preset, weight);
}
}
}
|