Skip to content

Commit

Permalink
feat: add default expression (optional)
Browse files Browse the repository at this point in the history
Some models need to remove watermark or set a default expression, which can be done through this setting.
  • Loading branch information
ylxmf2005 committed Jan 26, 2025
1 parent ca609b6 commit bfd896b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/renderer/src/context/live2d-config-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ export interface ModelInfo {
url: string;

/** Scale factor */
kScale: number | string;
kScale: number;

/** Initial X position shift */
initialXshift: number | string;
initialXshift: number;

/** Initial Y position shift */
initialYshift: number | string;
initialYshift: number;

/** Idle motion group name */
idleMotionGroupName?: string;

/** Default emotion */
defaultEmotion?: number | string;

/** Emotion mapping configuration */
emotionMap: EmotionMap;

Expand Down
13 changes: 10 additions & 3 deletions src/renderer/src/hooks/canvas/use-live2d-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const useLive2DModel = ({
height: 0,
};

resetModelPosition(modelRef.current, width, height, modelInfo);
resetModelPosition(modelRef.current, width, height, modelInfo?.initialXshift, modelInfo?.initialYshift);
}, [modelInfo?.initialXshift, modelInfo?.initialYshift]);

// Load Live2D model with configuration
Expand Down Expand Up @@ -297,9 +297,16 @@ export const useLive2DModel = ({
// Reset expression when AI state changes to IDLE (like finishing a conversation)
useEffect(() => {
if (aiState === AiStateEnum.IDLE) {
modelRef.current?.internalModel.motionManager.expressionManager?.resetExpression();
console.log("defaultEmotion: ", modelInfo?.defaultEmotion);
if (modelInfo?.defaultEmotion) {
modelRef.current?.internalModel.motionManager.expressionManager?.setExpression(
modelInfo.defaultEmotion,
);
} else {
modelRef.current?.internalModel.motionManager.expressionManager?.resetExpression();
}
}
}, [aiState]);
}, [modelRef.current, aiState, modelInfo?.defaultEmotion]);

// Load model when URL changes and cleanup on unmount
useEffect(() => {
Expand Down
13 changes: 6 additions & 7 deletions src/renderer/src/hooks/canvas/use-live2d-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ export const resetModelPosition = (
model: Live2DModel,
width: number,
height: number,
modelInfo: ModelInfo | undefined,
initialXshift: number | undefined,
initialYshift: number | undefined,
) => {
if (!model || !modelInfo) return;

const initXshift = Number(modelInfo?.initialXshift || 0);
const initYshift = Number(modelInfo?.initialYshift || 0);

if (!model) return;
const initXshift = Number(initialXshift || 0);
const initYshift = Number(initialYshift || 0);
const targetX = (width - model.width) / 2 + initXshift;
const targetY = (height - model.height) / 2 + initYshift;

Expand Down Expand Up @@ -119,7 +118,7 @@ export const useLive2DResize = (
// Resize renderer and reset model position
appRef.current.renderer.resize(width, height);
appRef.current.renderer.clear();
resetModelPosition(modelRef.current, width, height, modelInfo);
resetModelPosition(modelRef.current, width, height, modelInfo?.initialXshift, modelInfo?.initialYshift);
}
});

Expand Down

0 comments on commit bfd896b

Please sign in to comment.