{{ message }}
Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
408 lines (313 loc) · 9.99 KB
/
Copy pathmain.js
File metadata and controls
408 lines (313 loc) · 9.99 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { VRMLoaderPlugin, VRMUtils } from '@pixiv/three-vrm';
import { loadMixamoAnimation } from './loadMixamoAnimation.js';
import GUI from 'three/addons/libs/lil-gui.module.min.js';
let audioContext;
window.addEventListener('load', init, false);
function init() {
try {
audioContext = new AudioContext();
}
catch (e) {
alert('Web Audio API is not supported in this browser');
}
}
const animations = {
"Angry": { url: "Angry.fbx" },
"Neutral": { url: "Neutral_Idle.fbx" },
"Joy": { url: "Happy.fbx" },
"Sorrow": { url: "Sad_Idle.fbx" },
"Fun": { url: "Laughing.fbx" },
}
const DEFAULT_ANIMATION = "Neutral";
const synth = window.speechSynthesis;
let voices;
const params = {
timeScale: 1.0,
blinkLeft: 0,
blinkRight: 0,
aa: 0,
ee: 0,
ih: 0,
oh: 0,
ou: 0,
};
function populateVoices() {
voices = synth.getVoices().sort(function (a, b) {
const aname = a.name.toUpperCase();
const bname = b.name.toUpperCase();
if (aname < bname) {
return -1;
} else if (aname == bname) {
return 0;
} else {
return +1;
}
});
}
populateVoices();
const utterances = [];
const saySomething = (sentence = "안녕") => {
if (synth.speaking) return;
console.log("aimouto says ", sentence);
utteranceClock = new THREE.Clock();
const utterance = new SpeechSynthesisUtterance(sentence);
utterance.voice = synth.getVoices().findLast(o=>o.lang==='ko-KR');
utterance.pitch = 1.21;
utterances.push(utterance);
utterance.onend = function () {
console.log("SpeechSynthesisUtterance.onend");
currentVrm.expressionManager.setValue('oh', 0);
const animation = animations[DEFAULT_ANIMATION];
currentMixer = animation.mixer;
currentMixer.clipAction(animation.clip).play();
// res();
};
utterance.onerror = function () {
// rej("SpeechSynthesisUtterance.onerror");
};
utterance.addEventListener('boundary', function (event) {
console.log(event.name + ' boundary reached after ' + event.elapsedTime + ' milliseconds.');
});
synth.speak(utterance);
};
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
/* speechRecognition */
const recognition = new SpeechRecognition();
recognition.continuous = true;
recognition.lang = "ko-KR";
recognition.interimResults = false;
recognition.maxAlternatives = 1;
// renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild(renderer.domElement);
// camera
const camera = new THREE.PerspectiveCamera(30.0, window.innerWidth / window.innerHeight, 0.1, 20.0);
camera.position.set(0.0, 1.0, 5.0);
// camera controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.screenSpacePanning = true;
controls.target.set(0.0, 1.0, 0.0);
controls.update();
// scene
const scene = new THREE.Scene();
// light
const light = new THREE.DirectionalLight(0xffffff);
light.position.set(1.0, 1.0, 1.0).normalize();
scene.add(light);
// lookat target
const lookAtTarget = new THREE.Object3D();
camera.add(lookAtTarget);
const defaultModelUrl = './assets/models/aimouto.vrm';
// gltf and vrm
let currentVrm = undefined;
let currentAnimationUrl = undefined;
let currentMixer = undefined;
const helperRoot = new THREE.Group();
helperRoot.renderOrder = 10000;
scene.add(helperRoot);
function parseString(input) {
const regex = /\[face:([\d.]+):(\w+)]\s*([^[\]]+)/g;
const output = [];
let match;
while ((match = regex.exec(input)) !== null) {
output.push({
message: match[3].trim(),
face: {
type: match[2],
strength: parseFloat(match[1]),
},
});
}
return output;
}
function loadVRM(modelUrl) {
const loader = new GLTFLoader();
loader.crossOrigin = 'anonymous';
helperRoot.clear();
loader.register((parser) => {
return new VRMLoaderPlugin(parser);
});
loader.load(
// URL of the VRM you want to load
modelUrl,
// called when the resource is loaded
async (gltf) => {
const vrm = gltf.userData.vrm;
// calling these functions greatly improves the performance
VRMUtils.removeUnnecessaryVertices(gltf.scene);
VRMUtils.removeUnnecessaryJoints(gltf.scene);
if (currentVrm) {
scene.remove(currentVrm.scene);
VRMUtils.deepDispose(currentVrm.scene);
}
currentVrm = vrm;
vrm.lookAt.target = lookAtTarget;
// Disable frustum culling
vrm.scene.traverse((obj) => {
obj.frustumCulled = false;
});
await loadAnimations(animations);
const initialAnimation = animations[DEFAULT_ANIMATION];
currentMixer = initialAnimation.mixer;
currentMixer.clipAction(initialAnimation.clip).play();
currentMixer.timeScale = params.timeScale;
// rotate if the VRM is VRM0.0
VRMUtils.rotateVRM0(vrm);
console.log(vrm);
// put the model to the scene
scene.add(vrm.scene);
document.getElementById('send').addEventListener('click', async () => {
const sentence = document.getElementById('sentence');
const response = await (await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
sentence: sentence.value
})
})).json();
sentence.value = "";
let sentences = [];
if (response?.content) {
sentences = parseString(response?.content);
if (sentences.length > 0) {
const animation = animations[sentences[0].face.type];
currentMixer = animation.mixer;
currentMixer.clipAction(animation.clip).play();
saySomething(sentences[0].message);
} else {
/* 감정을 못가져올 때 예외처리 */
saySomething(response.content);
}
}
});
},
// called while loading is progressing
(progress) => console.log('Loading model...', 100.0 * (progress.loaded / progress.total), '%'),
// called when loading has errors
(error) => console.error(error),
);
}
loadVRM(defaultModelUrl);
// mixamo animation
async function loadFBX(animationUrl) {
currentAnimationUrl = animationUrl;
// create AnimationMixer for VRM
const mixer = new THREE.AnimationMixer(currentVrm.scene);
// Load animation
const clip = await loadMixamoAnimation(animationUrl, currentVrm);
return [mixer, clip];
}
// helpers
const gridHelper = new THREE.GridHelper(10, 10);
scene.add(gridHelper);
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// animate
const clock = new THREE.Clock();
let utteranceClock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const deltaTime = clock.getDelta();
// if animation is loaded
if (currentMixer) {
if (synth.speaking) {
const et = utteranceClock.getElapsedTime();
currentVrm.expressionManager.setValue('oh', Math.sin(et * 18) * 0.3 + 0.3);
}
// update the animation
currentMixer.update(deltaTime);
}
if (currentVrm) {
currentVrm.update(deltaTime);
}
renderer.render(scene, camera);
}
animate();
// gui
const gui = new GUI();
const listen = function () {
recognition.start();
}
params.saySomething = saySomething;
params.listen = listen;
recognition.onresult = async (event) => {
const sentence = event.results[0][0].transcript;
console.log(sentence);
try {
const response = await fetch('http://localhost:8080/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
sentence
})
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const responseData = await response.text();
console.log('Response Data:', responseData);
} catch (err) {
console.error('Error:', err);
}
};
gui.add(params, 'saySomething');
gui.add(params, 'listen');
gui.add(params, 'timeScale', 0.0, 2.0, 0.001).onChange((value) => {
currentMixer.timeScale = value;
});
gui.add(params, 'blinkLeft', 0.0, 1.0, 0.1).onChange((value) => {
currentVrm.expressionManager.setValue('blinkLeft', value);
});
gui.add(params, 'blinkRight', 0.0, 1.0, 0.1).onChange((value) => {
currentVrm.expressionManager.setValue('blinkRight', value);
});
gui.add(params, 'aa', 0.0, 1.0, 0.1).onChange((value) => {
currentVrm.expressionManager.setValue('aa', value);
});
gui.add(params, 'ee', 0.0, 1.0, 0.1).onChange((value) => {
currentVrm.expressionManager.setValue('ee', value);
});
gui.add(params, 'ih', 0.0, 1.0, 0.1).onChange((value) => {
currentVrm.expressionManager.setValue('ih', value);
});
gui.add(params, 'oh', 0.0, 1.0, 0.1).onChange((value) => {
currentVrm.expressionManager.setValue('oh', value);
});
gui.add(params, 'ou', 0.0, 1.0, 0.1).onChange((value) => {
currentVrm.expressionManager.setValue('ou', value);
});
// mouse listener
window.addEventListener('mousemove', (event) => {
lookAtTarget.position.x = 10.0 * ((event.clientX - 0.5 * window.innerWidth) / window.innerHeight);
lookAtTarget.position.y = - 10.0 * ((event.clientY - 0.5 * window.innerHeight) / window.innerHeight);
});
async function* animationsGenerator(animations) {
for (const animationName in animations) {
const result = await loadFBX(`./assets/anims/${animations[animationName].url}`);
const [mixer, clip] = result;
animations[animationName].mixer = mixer;
animations[animationName].clip = clip;
params[animationName] = () => {
currentMixer = mixer;
currentMixer.clipAction(clip).play();
};
yield animationName;
}
return;
}
async function loadAnimations(animations) {
for await (const key of animationsGenerator(animations)) {
console.log(key);
}
for (const animationName in animations) gui.add(params, animationName);
}
You can’t perform that action at this time.
