-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ros.html
516 lines (399 loc) · 16.4 KB
/
Ros.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<html lang="en">
<head>
<title>robot</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body{
overflow: hidden;
background-color: #000000;
margin: 0px;
background-size: cover;
background-repeat: no-repeat;
}
a {
color: #2fa1d6;
}
p {
max-width: 600px;
margin-left: auto;
margin-right: auto;
padding: 0 2em;
}
</style>
</head>
<body>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "./build/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from './jsm/libs/stats.module.js';
import { GUI } from './jsm/libs/lil-gui.module.min.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
import { CSS3DRenderer, CSS3DObject } from './jsm/renderers/CSS3DRenderer.js';
import { FontLoader } from './jsm/loaders/FontLoader.js';
import { TextGeometry } from './jsm/geometries/TextGeometry.js';
import { ShadowMapViewer } from './jsm/utils/ShadowMapViewer.js';
let container, stats, clock, gui, mixer, actions, activeAction, previousAction;
let camera, scene, renderer, model, face,renderer2,scene2;
let video, videoImage, videoImageContext, videoTexture;
let video2, videoImage2, videoImageContext2, videoTexture2;
let video3, videoImage3, videoImageContext3, videoTexture3;
let video4, videoImage4, videoImageContext4, videoTexture4;
const api = { state: 'Walking' };
let current_state = 'Idle';
var xSpeed = 0.1;
var ySpeed = 0.25;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 200 );
camera.position.set( 45, 20, 40 );
var img = new Image();
scene = new THREE.Scene();
img.onload = function () {
scene.background = new THREE.TextureLoader().load(img.src);
setBackground(scene, img.width, img.height);
};
img.src = "./videos/imag.jpg";
clock = new THREE.Clock();
const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 50,250 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
mesh.rotation.x = - Math.PI / 2;
scene.add( mesh );
// lights
const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
hemiLight.position.set( 0, 20, 0 );
scene.add( hemiLight );
const dirLight = new THREE.DirectionalLight( 0xffffff );
dirLight.position.set( 0, 20, 10 );
scene.add( dirLight );
const loader = new GLTFLoader();
loader.load( './board/collision.glb', function ( gltf ) {
scene.add( gltf.scene );
gltf.scene.position.set(0,0,10);
gltf.scene.scale.set(5,5,5);
gltf.scene.rotateX(-3.14/2);
gltf.scene.rotateZ(3.14/4);
} );
const loader2 = new GLTFLoader();
loader2.load( './board/collision.glb', function ( gltf ) {
scene.add( gltf.scene );
gltf.scene.position.set(0,0,50);
gltf.scene.scale.set(5,5,5);
gltf.scene.rotateX(-3.14/2);
gltf.scene.rotateZ(3.14/4);
} );
const loader3 = new GLTFLoader();
loader3.load( './board/collision.glb', function ( gltf ) {
scene.add( gltf.scene );
gltf.scene.position.set(0,0,90);
gltf.scene.scale.set(5,5,5);
gltf.scene.rotateX(-3.14/2);
gltf.scene.rotateZ(3.14/4);
} );
const loader5 = new GLTFLoader();
loader5.load( 'models/gltf/RobotExpressive/RobotExpressive.glb', function ( gltf ) {
model = gltf.scene;
model.scale.set(1.5,1.5,1.5);
model.position.set(20,0,0);
scene.add( model );
createGUI( model, gltf.animations );
}, undefined, function ( e ) {
console.error( e );
} );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.domElement.style.top = 0;
renderer.domElement.style.zIndex = '1';
container.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize );
// stats
stats = new Stats();
container.appendChild( stats.dom );
//video playback
video = document.createElement( 'video' );
video.src = "./videos/arm.mp4";
video.type="video/mp4;"
video.codecs="avc1.42E01E, mp4a.40.2";
video.load(); // must call after setting/changing source
video.play();
videoImage = document.createElement( 'canvas' );
videoImage.width = 1920;
videoImage.height = 1080;
videoImageContext = videoImage.getContext( '2d' );
// background color if no video present
videoImageContext.fillStyle = '#000000';
videoImageContext.fillRect( 0, 0, videoImage.width, videoImage.height );
videoTexture = new THREE.Texture( videoImage );
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
var movieMaterial = new THREE.MeshBasicMaterial( { map: videoTexture, overdraw: true, side:THREE.DoubleSide } );
// the geometry on which the movie will be displayed;
// movie image will be scaled to fit these dimensions.
var movieGeometry = new THREE.PlaneGeometry( 24, 16 , 4, 4 );
var movieScreen = new THREE.Mesh( movieGeometry, movieMaterial );
movieScreen.position.set(1.2,9,10.5);
movieScreen.rotateY(3.14/2.575);
scene.add(movieScreen);
//video2
video2 = document.createElement( 'video' );
video2.src = "./videos/arm.mp4";
video2.type="video/mp4;"
video2.codecs="avc1.42E01E, mp4a.40.2";
video2.load(); // must call after setting/changing source
video2.play();
videoImage2 = document.createElement( 'canvas' );
videoImage2.width = 1920;
videoImage2.height = 1080;
videoImageContext2 = videoImage2.getContext( '2d' );
videoImageContext2.fillStyle = '#000000';
videoImageContext2.fillRect( 0, 0, videoImage2.width, videoImage2.height );
videoTexture2 = new THREE.Texture( videoImage2 );
videoTexture2.minFilter = THREE.LinearFilter;
videoTexture2.magFilter = THREE.LinearFilter;
var movieMaterial2 = new THREE.MeshBasicMaterial( { map: videoTexture2, overdraw: true, side:THREE.DoubleSide } );
var movieGeometry2 = new THREE.PlaneGeometry( 24, 16 , 4, 4 );
var movieScreen2 = new THREE.Mesh( movieGeometry2, movieMaterial2 );
movieScreen2.position.set(1.2,9,50.5);
movieScreen2.rotateY(3.14/2.575);
scene.add(movieScreen2);
//video3
video3 = document.createElement( 'video' );
video3.src = "./videos/arm.mp4";
video3.type="video/mp4;"
video3.codecs="avc1.42E01E, mp4a.40.2";
video3.load(); // must call after setting/changing source
video3.play();
videoImage3 = document.createElement( 'canvas' );
videoImage3.width = 1920;
videoImage3.height = 1080;
videoImageContext3 = videoImage3.getContext( '2d' );
videoImageContext3.fillStyle = '#000000';
videoImageContext3.fillRect( 0, 0, videoImage3.width, videoImage3.height );
videoTexture3 = new THREE.Texture( videoImage3 );
videoTexture3.minFilter = THREE.LinearFilter;
videoTexture3.magFilter = THREE.LinearFilter;
let movieMaterial3 = new THREE.MeshBasicMaterial( { map: videoTexture3, overdraw: true, side:THREE.DoubleSide } );
let movieGeometry3 = new THREE.PlaneGeometry( 24, 16 , 4, 4 );
let movieScreen3 = new THREE.Mesh( movieGeometry3, movieMaterial3 );
movieScreen3.position.set(1.2,9,90.5);
movieScreen3.rotateY(3.14/2.575);
scene.add(movieScreen3);
//video4
//font on screen 1
const loader_font = new FontLoader();
loader_font.load('fonts/helvetiker_bold.typeface.json', function ( font ) {
const textGeo = new TextGeometry( 'AUTONOMOUS NAVIGATION', {
font: font,
size: 1.5,
height: 2,
curveSegments: 12,
bevelThickness: .1,
bevelSize: .1,
bevelEnabled: true
} );
const textGeo2 = new TextGeometry( 'ARM MANIPULATION', {
font: font,
size: 1.5,
height: 2,
curveSegments: 12,
bevelThickness: .1,
bevelSize: .1,
bevelEnabled: true
} );
const textGeo3 = new TextGeometry( 'OBJECT DETECTION', {
font: font,
size: 1.5,
height: 2,
curveSegments: 12,
bevelThickness: .1,
bevelSize: .1,
bevelEnabled: true
} );
const textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
const mesh1 = new THREE.Mesh( textGeo, textMaterial );
mesh1.position.x = -6;
mesh1.position.y = 18.45;
mesh1.position.z = 23.5;
mesh1.rotation.y = 3.14/2.575;
mesh1.castShadow = true;
mesh1.receiveShadow = true;
scene.add( mesh1 )
const mesh2 = new THREE.Mesh( textGeo2, textMaterial );
mesh2.position.x = -5;
mesh2.position.y = 18.45;
mesh2.position.z = 59;
mesh2.rotation.y = 3.14/2.575;
mesh2.castShadow = true;
mesh2.receiveShadow = true;
scene.add( mesh2 )
const mesh3 = new THREE.Mesh( textGeo3, textMaterial );
mesh3.position.x = -5;
mesh3.position.y = 18.45;
mesh3.position.z = 99;
mesh3.rotation.y = 3.14/2.575;
mesh3.castShadow = true;
mesh3.receiveShadow = true;
scene.add( mesh3 )
});
const controls = new OrbitControls( camera, renderer.domElement );
controls.enablePan = true;
controls.minDistance = 5;
controls.maxDistance = 100;
document.addEventListener("keydown", onDocumentKeyDown, false);
}
function onDocumentKeyDown(event) {
var keyCode = event.which;
if((model.position.z > 10 && model.position.z< 10.5) || (model.position.z > 50 && model.position.z< 50.5) ||(model.position.z > 90 && model.position.z< 90.5) || (model.position.z > 130 && model.position.z< 130.5))
{
current_state='ThumbsUp';
fadeToAction('Jump',0.5);
fadeToAction('Walking',0.5);
}
if (model.position.z >230)
{model.rotation.y =3.14; }
else if (model.position.z < -100)
{model.rotation.y =0; }
if (keyCode == 38 && model.rotation.y == 0) {
model.position.z += ySpeed;
camera.position.set( camera.position.x, camera.position.y, camera.position.z+ ySpeed );
}
else if (keyCode == 38 && model.rotation.y == 3.14) {
model.position.z -= ySpeed;
camera.position.set( camera.position.x, camera.position.y, camera.position.z- ySpeed );
}
else if (keyCode == 40 ) {
if(model.rotation.y ==3.14)
{model.rotation.y = 0;}
else
{model.rotation.y = 3.14;}
}
if (keyCode == 66) {
window.open('robot.html','_parent');}
render();
};
function createGUI( model, animations ) {
const states = [ 'Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing' ];
const emotes = [ 'Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp' ];
gui = new GUI();
mixer = new THREE.AnimationMixer( model );
actions = {};
for ( let i = 0; i < animations.length; i ++ ) {
const clip = animations[ i ];
const action = mixer.clipAction( clip );
actions[ clip.name ] = action;
if ( emotes.indexOf( clip.name ) >= 0 || states.indexOf( clip.name ) >= 4 ) {
action.clampWhenFinished = true;
action.loop = THREE.LoopOnce;
}
}
// states
const statesFolder = gui.addFolder( 'States' );
const clipCtrl = statesFolder.add( api, 'state' ).options( current_state );
clipCtrl.onChange( function () {
fadeToAction( api.state, 0.5 );
} );
statesFolder.open();
// emotes
function restoreState() {
mixer.removeEventListener( 'finished', restoreState );
fadeToAction( api.state, 0.2 );
}
activeAction = actions[ 'Walking' ];
activeAction.play();
}
function fadeToAction( name, duration ) {
previousAction = activeAction;
activeAction = actions[ name ];
if ( previousAction !== activeAction ) {
previousAction.fadeOut( duration );
}
activeAction
.reset()
.setEffectiveTimeScale( 1 )
.setEffectiveWeight( 1 )
.fadeIn( duration )
.play();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
if ( video.readyState === video.HAVE_ENOUGH_DATA )
{
videoImageContext.drawImage( video, 1, 1 );
if ( videoTexture )
videoTexture.needsUpdate = true;
}
if ( video2.readyState === video2.HAVE_ENOUGH_DATA )
{
videoImageContext2.drawImage( video2, 1, 1 );
if ( videoTexture2 )
videoTexture2.needsUpdate = true;
}
if ( video3.readyState === video2.HAVE_ENOUGH_DATA )
{
videoImageContext3.drawImage( video3, 1, 1 );
if ( videoTexture3 )
videoTexture3.needsUpdate = true;
}
renderer.render( scene, camera );
}
//
function setBackground(scene, backgroundImageWidth, backgroundImageHeight) {
var windowSize = function(withScrollBar) {
var wid = 0;
var hei = 0;
if (typeof window.innerWidth != "undefined") {
wid = window.innerWidth;
hei = window.innerHeight;
}
else {
if (document.documentElement.clientWidth == 0) {
wid = document.body.clientWidth;
hei = document.body.clientHeight;
}
else {
wid = document.documentElement.clientWidth;
hei = document.documentElement.clientHeight;
}
}
return { width: wid - (withScrollBar ? (wid - document.body.offsetWidth + 1) : 0), height: hei };
};
if (scene.background) {
var size = windowSize(true);
var factor = (backgroundImageWidth / backgroundImageHeight) / (size.width / size.height);
scene.background.offset.x = factor > 1 ? (1 - 1 / factor) / 2 : 0;
scene.background.offset.y = factor > 1 ? 0 : (1 - factor) / 2;
scene.background.repeat.x = factor > 1 ? 1 / factor : 1;
scene.background.repeat.y = factor > 1 ? 1 : factor;
}
}
function animate() {
const dt = clock.getDelta();
if ( mixer ) mixer.update( dt );
requestAnimationFrame( animate );
render();
video.play();
video2.play();
video3.play();
}
</script>
</body>
</html>