-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
131 lines (115 loc) · 3.9 KB
/
script.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Setup Cubism Model and Pixi live2d
//const cubismModel = "models/Hi/hiyori_free_t08.model3.json"
const urlParams = new URLSearchParams(window.location.search);
const model_path = urlParams.get('model');
var color = urlParams.get('bg');
var cubismModel;
if (model_path != null) {
cubismModel = "models/" + model_path
} else {
cubismModel = "models/Arch/arch chan model0.model3.json";
}
if (color == null) {
color = "#000000";
}
function convertHexColor(hexColor) {
return '0x' + parseInt(hexColor.replace(/^#/, ''), 16).toString(16);
}
const live2d = PIXI.live2d;
var updateFn;
var model_proxy;
const xs = window.matchMedia('screen and (max-width: 768px)');
xs.addEventListener('change', (e) => {
if (e.matches) model.scale.set(0.1);
});
(async function main() {
const app = new PIXI.Application({
view: document.getElementById("canvas"),
autoStart: true,
resizeTo: window,
backgroundColor: convertHexColor(color) });
const models = await Promise.all([live2d.Live2DModel.from(cubismModel)]);
const model = models[0]
model_proxy = model;
updateFn = model.internalModel.motionManager.update;
app.stage.addChild(model);
// Scale the model
const scaleX = innerWidth * 0.7 / (model.width * 0.5);
const scaleY = innerHeight * 0.7 / (model.height * 0.5);
// fit the window
model.scale.set(Math.min(scaleY, scaleX));
model.y = innerHeight * 0.5 - (model.height * 0.5);
model.x = (innerWidth * 0.5) - (model.width * 0.5);
draggable(model);
//addFrame(model);
// handle tapping
model.on("hit", hitAreas => {
if (hitAreas.includes("Body")) {
model.motion("tap");
}
if (hitAreas.includes("Head")) {
model.expression();
}
});
})();
function draggable(model) {
model.buttonMode = true;
model.on("pointerdown", e => {
model.dragging = true;
model._pointerX = e.data.global.x - model.x;
model._pointerY = e.data.global.y - model.y;
});
model.on("pointermove", e => {
if (model.dragging) {
model.position.x = e.data.global.x - model._pointerX;
model.position.y = e.data.global.y - model._pointerY;
}
});
model.on("pointerupoutside", () => model.dragging = false);
model.on("pointerup", () => model.dragging = false);
}
function addFrame(model) {
const foreground = PIXI.Sprite.from(PIXI.Texture.WHITE);
foreground.width = model.internalModel.width;
foreground.height = model.internalModel.height;
foreground.alpha = 0.2;
model.addChild(foreground);
//checkbox("Model Frames", checked => foreground.visible = checked);
}
// To be run in Console
function playAudio(audio_link, volume=1, expression=0) {
model_proxy.speak(audio_link, volume, expression);
}
function set_mouth_y(value) {
model_proxy.internalModel.coreModel.setParameterValueById('ParamMouthOpenY', value)
// Cubism 4
if (model_proxy.internalModel instanceof live2d.Cubism4InternalModel) {
model = model_proxy
model.internalModel.motionManager.update = () => {
updateFn.call(model.internalModel.motionManager, model.internalModel.coreModel, Date.now()/1000);
if (model_proxy.internalModel.motionManager.lipSyncIds.length > 0) {
for (id in model_proxy.internalModel.motionManager.lipSyncIds) {
model.internalModel.coreModel.setParameterValueById(model_proxy.internalModel.motionManager.lipSyncIds[id], value);
}
}
model.internalModel.coreModel.setParameterValueById("PARAM_MOUTH_OPEN_Y", value);
}
}
}
function get_expressions() {
if (model_proxy.internalModel.motionManager.expressionManager == null) {
return []
}
result = []
def = model_proxy.internalModel.motionManager.expressionManager.definitions;
for (expression in def) {
result[expression] = def[expression].Name
}
return result
}
function get_expressions_json() {
return JSON.stringify(get_expressions(), null, 2)
}
function set_expression(expression) {
model_proxy.expression(expression)
}