-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
Live2DPhysics.ts
56 lines (45 loc) · 1.61 KB
/
Live2DPhysics.ts
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
import type { Cubism2Spec } from "../types/Cubism2Spec";
const SRC_TYPE_MAP = {
x: PhysicsHair.Src.SRC_TO_X,
y: PhysicsHair.Src.SRC_TO_Y,
angle: PhysicsHair.Src.SRC_TO_G_ANGLE,
};
const TARGET_TYPE_MAP = {
x: PhysicsHair.Src.SRC_TO_X,
y: PhysicsHair.Src.SRC_TO_Y,
angle: PhysicsHair.Src.SRC_TO_G_ANGLE,
};
export class Live2DPhysics {
physicsHairs: PhysicsHair[] = [];
constructor(
readonly coreModel: Live2DModelWebGL,
json: Cubism2Spec.PhysicsJSON,
) {
if (json.physics_hair) {
this.physicsHairs = json.physics_hair.map((definition) => {
const physicsHair = new PhysicsHair();
physicsHair.setup(
definition.setup.length,
definition.setup.regist,
definition.setup.mass,
);
definition.src.forEach(({ id, ptype, scale, weight }) => {
const type = SRC_TYPE_MAP[ptype];
if (type) {
physicsHair.addSrcParam(type, id, scale, weight);
}
});
definition.targets.forEach(({ id, ptype, scale, weight }) => {
const type = TARGET_TYPE_MAP[ptype];
if (type) {
physicsHair.addTargetParam(type, id, scale, weight);
}
});
return physicsHair;
});
}
}
update(elapsed: DOMHighResTimeStamp) {
this.physicsHairs.forEach((physicsHair) => physicsHair.update(this.coreModel, elapsed));
}
}