Skip to content

Commit da15273

Browse files
committed
feat: 调整动作相关方法, 新增获取动作文件列表方法
1 parent 4ecef5c commit da15273

File tree

6 files changed

+131
-22
lines changed

6 files changed

+131
-22
lines changed

docs/.vitepress/theme/components/DemoModal.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ watch(showModal, () => {
5959
if (showModal.value) {
6060
import('../../../../dist').then(({ init }) => {
6161
demo(init, l2dCanvas).then(model => {
62-
if ((model as any).onClose) {
62+
if ((model as any)?.onClose) {
6363
closeCallback.value = (model as any).onClose;
6464
}
65-
if (model.on) {
65+
if (model?.on) {
6666
model.on('settingsJSONLoaded', () => {
6767
settingsJsonLoaded.value = true;
6868
});

docs/demos/index.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
/* eslint-disable max-lines */
23
/* eslint-disable antfu/no-import-dist */
34

@@ -311,3 +312,87 @@ export async function demo9(init, l2dCanvas) {
311312
}
312313
};
313314
}
315+
316+
export async function demo10(init, l2dCanvas) {
317+
const loadModel = async () => {
318+
const l2d: L2D = init(l2dCanvas.value! as HTMLCanvasElement);
319+
// #region demo10
320+
const model = await l2d.create({
321+
path: 'https://model.hacxy.cn/live2d_002_101/object_live2d_002_101.asset.model3.json',
322+
scale: 0.1
323+
});
324+
const motionGroups = model.getMotionGroupNames();
325+
console.log(motionGroups);
326+
// #endregion demo10
327+
328+
message.info(`动作组: ${JSON.stringify(motionGroups)}`, { showIcon: false });
329+
};
330+
loadModel();
331+
}
332+
333+
export async function demo11(init, l2dCanvas) {
334+
const loadModel = async () => {
335+
const l2d: L2D = init(l2dCanvas.value! as HTMLCanvasElement);
336+
// #region demo11
337+
const model = await l2d.create({
338+
path: 'https://model.hacxy.cn/live2d_002_101/object_live2d_002_101.asset.model3.json',
339+
scale: 0.1,
340+
motionPreload: MotionPreload.ALL
341+
});
342+
const motionGroups = model.getMotionGroupNames();
343+
console.log(motionGroups);
344+
const motionNames = model.getMotionListByGroupName('Tap');
345+
console.log(motionNames);
346+
// #endregion demo11
347+
348+
message.info(`动作组: ${JSON.stringify(motionGroups)}`, { showIcon: false });
349+
350+
message.info(`动作: ${JSON.stringify(motionNames)}`, { showIcon: false });
351+
};
352+
loadModel();
353+
}
354+
355+
export async function demo12(init, l2dCanvas) {
356+
const loadModel = async () => {
357+
const l2d: L2D = init(l2dCanvas.value! as HTMLCanvasElement);
358+
// #region demo12
359+
const model = await l2d.create({
360+
path: 'https://model.hacxy.cn/live2d_002_101/object_live2d_002_101.asset.model3.json',
361+
scale: 0.1,
362+
motionPreload: MotionPreload.ALL
363+
});
364+
const motionGroups = model.getMotionGroupNames();
365+
366+
model.showHitAreaFrames();
367+
368+
setTimeout(() => {
369+
model.playMotion(motionGroups[1]);
370+
}, 1000);
371+
372+
// #endregion demo12
373+
};
374+
loadModel();
375+
}
376+
377+
export async function demo13(init, l2dCanvas) {
378+
const loadModel = async () => {
379+
const l2d: L2D = init(l2dCanvas.value! as HTMLCanvasElement);
380+
// #region demo13
381+
const model = await l2d.create({
382+
path: 'https://model.hacxy.cn/live2d_002_101/object_live2d_002_101.asset.model3.json',
383+
scale: 0.1,
384+
motionPreload: MotionPreload.ALL
385+
});
386+
const motionGroups = model.getMotionGroupNames();
387+
388+
model.showHitAreaFrames();
389+
390+
setTimeout(() => {
391+
model.playMotion(motionGroups[1], 1);
392+
}, 1000);
393+
394+
// #endregion demo13
395+
};
396+
loadModel();
397+
}
398+

docs/guide/motion/list.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/guide/motion/motion-sync.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
sidebar:
33
icon: line-md:emoji-neutral
44
sort: 5
5-
65
---
76

87
# 口型动作同步

docs/guide/motion/play.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
---
22
sidebar:
33
icon: line-md:cog-filled
4+
sort: 4
45
---
56

67
# 动作播放
78

8-
待完善
9+
## 获取动作组列表
10+
11+
<<< ../../demos/index.ts#demo10
12+
13+
<DemoModal :demo="demo10"/>
14+
15+
## 根据动作组名称获取动作列表
16+
17+
<<< ../../demos/index.ts#demo11
18+
19+
<DemoModal :demo="demo11"/>
20+
21+
## 播放动作
22+
23+
### 随机播放
24+
25+
接下来我们可以使用上面的方法在1秒钟后从`Tap`动作组中随机播放一个动作:
26+
27+
<<< ../../demos/index.ts#demo12
28+
29+
<DemoModal :demo="demo12"/>
30+
31+
### 指定播放
32+
33+
`playMotion`方法支持在第二个参数传入一个索引值, 来准确指定播放某个动作组的哪一个动作:
34+
35+
<<< ../../demos/index.ts#demo13
36+
37+
<DemoModal :demo="demo13"/>
38+
39+
<script setup>
40+
import { demo10, demo11, demo12, demo13 } from '../../demos/index.ts'
41+
</script>

src/model.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class Model {
2323
) {
2424
this.hitAreaFrames = new HitAreaFrames();
2525

26-
live2dModel.once('load', () => {
27-
this.live2dModel.internalModel.on('afterMotionUpdate', () => this.emittery.emit('afterMotionUpdate'));
28-
});
26+
// live2dModel.once('load', () => {
27+
// this.live2dModel.internalModel.on('afterMotionUpdate', () => this.emittery.emit('afterMotionUpdate'));
28+
// });
2929

3030
live2dModel.on('hit', area => {
3131
this.emittery.emit('hit', area);
@@ -74,25 +74,25 @@ export class Model {
7474
/**
7575
* 获取当前模型所有动作组名称
7676
*/
77-
getMotionGroups() {
78-
return Object.keys(this.live2dModel.internalModel.motionManager.motionGroups);
77+
getMotionGroupNames() {
78+
return Object.keys(this.live2dModel.internalModel.motionManager.definitions);
7979
}
8080

8181
/**
82-
* 根据名称获取动作组
82+
* 根据动作组名称获取动作文件列表
8383
* @param name
8484
*/
85-
getMotion(name: string) {
86-
return this.live2dModel.internalModel.motionManager.motionGroups[name];
85+
getMotionListByGroupName(name: string) {
86+
return this.live2dModel.internalModel.motionManager.definitions[name];
8787
}
8888

8989
/**
9090
* 根据动作组名称播放动作
9191
* @param group
9292
* @param index
9393
*/
94-
playMotion(group: string, index?: number) {
95-
this.live2dModel.motion(group, index);
94+
async playMotion(group: string, index?: number) {
95+
return this.live2dModel.motion(group, index);
9696
}
9797

9898
/** @ignore */

0 commit comments

Comments
 (0)