forked from aws-samples/amazon-sumerian-hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree.html
494 lines (438 loc) · 15 KB
/
three.html
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!--
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Three.js Host Test</title>
<style>
html,
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
#textToSpeech {
position: fixed;
top: 0;
left: 0;
z-index: 2;
display: none;
}
#textEntry {
min-width: 305px;
min-height: 200px;
}
.button {
width: 75px;
}
#loadScreen {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
background-image: url('assets/images/load_screen.png');
background-color: gray;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: contain;
z-index: 9999;
}
#loader {
border: 16px solid #3498db38;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
position: fixed;
}
@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
@keyframes fadeOutAnimation {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-webkit-keyframes fadeOutAnimation {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
</style>
<!--Enable drag and drop text files on text to speech input area-->
<script src="./scriptDependencies/dragDropTextArea.js"></script>
<!--Text to speech dependency-->
<script
type="text/javascript"
src="https://sdk.amazonaws.com/js/aws-sdk-2.645.0.min.js"
></script>
<!--Three.js dependencies-->
<script src="./scriptDependencies/three.min.js"></script>
<script src="./scriptDependencies/three.GLTFLoader.js"></script>
<script src="./scriptDependencies/three.OrbitControls.js"></script>
<!--Host build file-->
<script type="text/javascript" src="../dist/host.three.js"></script>
</head>
<body>
<!--Loading screen-->
<div id="loadScreen">
<div id="loader"></div>
</div>
<!--Text to speech controls-->
<div id="textToSpeech">
<div>
<textarea autofocus size="23" type="text" id="textEntry">
<speak>
<amazon:domain name="conversational">Hello, my name is Wes. I used
to only be a host inside Amazon Sumerian, but now you can use me in other
Javascript runtime environments like three<sub alias="">.</sub>js and
Babylon<sub alias="">.</sub>js. Right now I'm in three<sub alias="">.</sub>js.
</amazon:domain>
</speak>
</textarea>
</div>
<button id="play" class="button">Play</button>
<button id="pause" class="button">Pause</button>
<button id="resume" class="button">Resume</button>
<button id="stop" class="button">Stop</button>
</div>
<!--Create the Three.js scene-->
<script>
async function main() {
// Initialize AWS and create Polly service objects
window.AWS.config.region = 'us-west-2';
window.AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: '<Enter Cognito Identity Pool ID here>',
});
const polly = new AWS.Polly();
const presigner = new AWS.Polly.Presigner();
const speechInit = HOST.aws.TextToSpeechFeature.initializeService(
polly,
presigner,
window.AWS.VERSION
);
// Define the glTF assets that will represent the host
const renderFn = [];
const characterFile =
'./assets/glTF/characters/adult_male/wes/wes.gltf';
const animationPath = './assets/glTF/animations/adult_male';
const animationFiles = ['stand_idle.glb', 'lipsync.glb'];
const audioAttachJoint = 'chardef_c_neckB'; // Name of the joint to attach audio to
const voice = 'Matthew'; // Polly voice. Full list of available voices at: https://docs.aws.amazon.com/polly/latest/dg/voicelist.html
const voiceEngine = 'neural'; // Neural engine is not available for all voices in all regions: https://docs.aws.amazon.com/polly/latest/dg/NTTS-main.html
// Set up the scene and host
const {scene, camera, clock} = createScene();
const {character, clips} = await loadCharacter(
scene,
characterFile,
animationPath,
animationFiles
);
const [idleClips, lipsyncClips] = clips;
const host = createHost(
character,
audioAttachJoint,
voice,
voiceEngine,
idleClips[0],
lipsyncClips,
camera,
clock
);
// Hide the load screen and how the text input
document.getElementById('textToSpeech').style.display = 'inline-block';
document.getElementById('loadScreen').style.display = 'none';
await speechInit;
// Enable drag/drop text files on the speech text area
enableDragDrop('textEntry');
// Play, pause, resume and stop the contents of the text input as speech
// when buttons are clicked
const speechInput = document.getElementById('textEntry');
['play', 'pause', 'resume', 'stop'].forEach(id => {
const button = document.getElementById(id);
button.onclick = () => {
host.TextToSpeechFeature[id](speechInput.value);
};
});
// Set up base scene
function createScene() {
// Base scene
const scene = new THREE.Scene();
const clock = new THREE.Clock();
scene.background = new THREE.Color(0x33334d);
scene.fog = new THREE.Fog(0x33334d, 0, 10);
// Renderer
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
renderer.setClearColor(0x33334d);
renderer.domElement.id = 'renderCanvas';
document.body.appendChild(renderer.domElement);
// Env map
new THREE.TextureLoader()
.setPath('assets/')
.load('images/machine_shop.jpg', hdrEquirect => {
const hdrCubeRenderTarget = pmremGenerator.fromEquirectangular(
hdrEquirect
);
hdrEquirect.dispose();
pmremGenerator.dispose();
scene.environment = hdrCubeRenderTarget.texture;
});
const pmremGenerator = new THREE.PMREMGenerator(renderer);
pmremGenerator.compileEquirectangularShader();
// Camera
const camera = new THREE.PerspectiveCamera(
THREE.MathUtils.radToDeg(0.8),
window.innerWidth / window.innerHeight,
0.1,
1000
);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
camera.position.set(0, 1.6, 2.6);
controls.target = new THREE.Vector3(0, 1, 0);
controls.screenSpacePanning = true;
controls.update();
// Handle window resize
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize, false);
// Render loop
function render() {
requestAnimationFrame(render);
controls.update();
renderFn.forEach(fn => {
fn();
});
renderer.render(scene, camera);
}
render();
// Lights
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x000000, 0.6);
hemiLight.position.set(0, 1, 0);
hemiLight.intensity = 0.6;
scene.add(hemiLight);
const dirLight = new THREE.DirectionalLight(0xffffff);
dirLight.position.set(0, 5, 5);
dirLight.castShadow = true;
dirLight.shadow.mapSize.width = 1024;
dirLight.shadow.mapSize.height = 1024;
dirLight.shadow.camera.top = 2.5;
dirLight.shadow.camera.bottom = -2.5;
dirLight.shadow.camera.left = -2.5;
dirLight.shadow.camera.right = 2.5;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 40;
scene.add(dirLight);
const dirLightTarget = new THREE.Object3D();
dirLight.add(dirLightTarget);
dirLightTarget.position.set(0, -0.5, -1.0);
dirLight.target = dirLightTarget;
// Environment
const groundMat = new THREE.MeshStandardMaterial({
color: 0x808080,
depthWrite: false,
});
groundMat.metalness = 0;
groundMat.refractionRatio = 0;
const ground = new THREE.Mesh(
new THREE.PlaneBufferGeometry(100, 100),
groundMat
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
return {scene, camera, clock};
}
// Load character model and animations
async function loadCharacter(
scene,
characterFile,
animationPath,
animationFiles
) {
// Asset loader
const fileLoader = new THREE.FileLoader();
const gltfLoader = new THREE.GLTFLoader();
function loadAsset(loader, assetPath, onLoad) {
return new Promise(resolve => {
loader.load(assetPath, async asset => {
if (onLoad[Symbol.toStringTag] === 'AsyncFunction') {
const result = await onLoad(asset);
resolve(result);
} else {
resolve(onLoad(asset));
}
});
});
}
// Load character model
const character = await loadAsset(gltfLoader, characterFile, gltf => {
// Transform the character
const character = gltf.scene;
scene.add(character);
// Cast shadows
character.traverse(object => {
if (object.isMesh) {
object.castShadow = true;
}
});
return character;
});
// Load animations
const clips = await Promise.all(
animationFiles.map((filename, index) => {
const filePath = `${animationPath}/${filename}`;
return loadAsset(gltfLoader, filePath, async gltf => {
return gltf.animations.map((clip, i) => {
// Make the lipsync animation additive
if (index) {
THREE.AnimationUtils.makeClipAdditive(clip);
}
// Slice off the reference frame for viseme poses
if (!clip.name.startsWith('stand_')) {
clip = THREE.AnimationUtils.subclip(
clip,
clip.name,
1,
2,
30
);
}
return clip;
});
});
})
);
return {character, clips};
}
// Initialize the host
function createHost(
character,
audioAttachJoint,
voice,
engine,
idleClip,
lipsyncClips,
camera,
clock
) {
// Add the host to the render loop
const host = new HOST.HostObject({owner: character, clock});
renderFn.push(() => {
host.update();
});
// Set up text to speech
const audioListener = new THREE.AudioListener();
camera.add(audioListener);
const audioAttach = character.getObjectByName(audioAttachJoint);
host.addFeature(HOST.aws.TextToSpeechFeature, false, {
listener: audioListener,
attachTo: audioAttach,
voice,
engine,
});
// Set up animation
host.addFeature(HOST.anim.AnimationFeature);
// Base idle
host.AnimationFeature.addLayer('Base');
host.AnimationFeature.addAnimation(
'Base',
idleClip.name,
HOST.anim.AnimationTypes.single,
{clip: idleClip}
);
host.AnimationFeature.playAnimation('Base', idleClip.name);
// Talking idle
host.AnimationFeature.addLayer('Talk', {
transitionTime: 0.75,
blendMode: HOST.anim.LayerBlendModes.Additive,
});
host.AnimationFeature.setLayerWeight('Talk', 0);
const talkClip = lipsyncClips.find(c => c.name === 'stand_talk');
lipsyncClips.splice(lipsyncClips.indexOf(talkClip), 1);
host.AnimationFeature.addAnimation(
'Talk',
talkClip.name,
HOST.anim.AnimationTypes.single,
{clip: talkClip}
);
host.AnimationFeature.playAnimation('Talk', talkClip.name);
// Viseme poses
host.AnimationFeature.addLayer('Viseme', {
transitionTime: 0.12,
blendMode: HOST.anim.LayerBlendModes.Additive,
});
host.AnimationFeature.setLayerWeight('Viseme', 0);
const blendStateOptions = lipsyncClips.map(clip => {
return {
name: clip.name,
clip,
weight: 0,
};
});
host.AnimationFeature.addAnimation(
'Viseme',
'visemes',
HOST.anim.AnimationTypes.freeBlend,
{blendStateOptions}
);
host.AnimationFeature.playAnimation('Viseme', 'visemes');
// Set up Lipsync
const visemeOptions = {
layers: [{name: 'Viseme', animation: 'visemes'}],
};
const talkingOptions = {
layers: [{name: 'Talk', animation: 'stand_talk'}],
};
host.addFeature(
HOST.LipsyncFeature,
false,
visemeOptions,
talkingOptions
);
return host;
}
}
main();
</script>
</body>
</html>