-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3dRoom.html
More file actions
513 lines (338 loc) · 13.3 KB
/
3dRoom.html
File metadata and controls
513 lines (338 loc) · 13.3 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <meta name="viewport" content= "width=device-width, user-scalable=no">--> <!--does not work -->
<title>3DRoom</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
/* 17756649/disable-the-horizontal-scroll */
max-width: 100%; overflow-x: hidden;
max-height: 100%; overflow-y: hidden;
/* Prevent document pinch-zoom & touch-hold-to-highlight */
touch-action: none;
-webkit-touch-callout: none;
/* iOS Safari */
-webkit-user-select: none;
/* Safari */
-khtml-user-select: none;
/* Konqueror HTML */
-moz-user-select: none;
/* Old versions of Firefox */
-ms-user-select: none;
/* Internet Explorer/Edge */
user-select: none;
/* Non-prefixed version, currently supported by Chrome, Edge, Opera and Firefox */
}
</style>
<script src="./lib/3D/three.bundle.min.js" type="text/javascript"></script>
<script>
console.log("THREE", THREE);
</script>
<script src="./lib/3D/three-laser-pointer.min.js" type="text/javascript"></script>
</head>
<body>
<style>
#myCanvas {
position: absolute;
left: 0;
top: 0;
border: 0px solid green;
width: 100%;
height: 100%;
display: none;
}
</style>
<canvas id="myCanvas"></canvas>
<style>
#rendererCanvas {
position: absolute;
left: 0;
top: 0;
border: 0px solid green;
width: 100%;
height: 100%;
}
</style>
<canvas id="rendererCanvas"></canvas>
<script>
let camera, scene, renderer, controls;
const arr_object = [];
let raycaster;
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
let canJump = false;
let prevTime = performance.now();
const velocity = new THREE.Vector3();
const direction = new THREE.Vector3();
const vertex = new THREE.Vector3();
const color = new THREE.Color();
let mesh__wall;
let laser;
let isLaserPointingIC;
let isCanCapture;
init();
animate();
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xffffff );
scene.fog = new THREE.Fog( 0xffffff, 0, 750 );
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.y = 10;
const light = new THREE.HemisphereLight( 0xeeeeff, 0x777788, 2.5 );
light.position.set( 0.5, 1, 0.75 );
scene.add( light );
//controls
controls = new THREE.PointerLockControls( camera, document.body );
window.addEventListener( 'click', function () {
controls.lock();
} );
// const blocker = document.getElementById( 'blocker' );
// const instructions = document.getElementById( 'instructions' );
// instructions.addEventListener( 'click', function () {
// controls.lock();
// } );
// controls.addEventListener( 'lock', function () {
// instructions.style.display = 'none';
// blocker.style.display = 'none';
// } );
// controls.addEventListener( 'unlock', function () {
// blocker.style.display = 'block';
// instructions.style.display = '';
// } );
scene.add( controls.getObject() );
const onKeyDown = function ( event ) {
switch ( event.code ) {
case 'ArrowUp':
case 'KeyW':
moveForward = true;
break;
case 'ArrowLeft':
case 'KeyA':
moveLeft = true;
break;
case 'ArrowDown':
case 'KeyS':
moveBackward = true;
break;
case 'ArrowRight':
case 'KeyD':
moveRight = true;
break;
case 'Space':
if ( canJump === true ) velocity.y += 350;
canJump = false;
break;
}
};
const onKeyUp = function ( event ) {
switch ( event.code ) {
case 'ArrowUp':
case 'KeyW':
moveForward = false;
break;
case 'ArrowLeft':
case 'KeyA':
moveLeft = false;
break;
case 'ArrowDown':
case 'KeyS':
moveBackward = false;
break;
case 'ArrowRight':
case 'KeyD':
moveRight = false;
break;
}
};
document.addEventListener( 'keydown', onKeyDown );
document.addEventListener( 'keyup', onKeyUp );
const onMouseUp = function ( event ) {
if(isCanCapture) {
console.log("captureNow");
}
};
document.addEventListener( 'mouseup', onMouseUp );
//laser
laser = new Laser({color: 0xff0000});
scene.add(laser);
raycaster = new THREE.Raycaster( new THREE.Vector3(), new THREE.Vector3( 0, - 1, 0 ), 0, 10 );
scene.add( new THREE.AmbientLight( 0x444444 ) );
//geometry
const size__room = { //size's x, y, z are width, height, depth.
x: 35,
y: 35,
z: 55,
};
const geom__walls = new THREE.BoxGeometry(size__room.x,size__room.y,size__room.z);
const mat__walls = new THREE.MeshNormalMaterial({
side: THREE.DoubleSide, // fix #3 - can do this to make room walls visible from both sides.
// side: THREE.BackSide,
transparent: true,
opacity: 0.7,
});
const room = new THREE.Mesh(geom__walls, mat__walls);
console.log("room:", room);
// room.rotation.set(-Math.PI/3, 0.3, -0.3 );
// const boundingBox__room = new THREE.Box3().setFromObject(room);
// const size__room = boundingBox__room.getSize(); //size's x, y, z are width, height, depth.
room.position.y = room.position.y + size__room.y / 2.0;
scene.add(room);
// {
// var geom__edges = new THREE.EdgesGeometry( geom__walls );
// var geom__line = new LineSegmentsGeometry().fromEdgesGeometry( geom__edges );
// const mat__line = new LineMaterial( {
// color: 0x4080ff,
// linewidth: 5,
// } );
// wireframe = new Wireframe( geom__line, mat__line );
// wireframe.computeLineDistances();
// wireframe.scale.set( 1, 1, 1 );
// scene.add( wireframe );
// }
// floor
let geom__floor = new THREE.PlaneGeometry( 2000, 3000, 100, 100 );
geom__floor.rotateX( - Math.PI / 2 );
// const mat__floor = new THREE.MeshBasicMaterial( { vertexColors: true } );
const mat__floor = new THREE.MeshLambertMaterial({
side: THREE.DoubleSide // fix #3 - can do this to make room walls visible from both sides.
// side: THREE.FrontSide,
});
const floor = new THREE.Mesh( geom__floor, mat__floor );
scene.add( floor );
// objects
//box in room
const geom__box = new THREE.BoxGeometry( 6, 6, 6 );
const mat__box = new THREE.MeshNormalMaterial({
side: THREE.FrontSide,
});
const box = new THREE.Mesh( geom__box, mat__box );
console.log("box:", box);
box.position.y = box.position.y + 3;
scene.add( box );
arr_object.push( box );
//canvas on wall
const canvas__wall = document.getElementById("myCanvas");
const texture__wall = new THREE.CanvasTexture(canvas__wall);
// Setting magFilter and minFilter to THREE.NearestFilter
// console.log(texture__wall.magFilter); // 1006 ( THREE.LinearFilter - r146 )
// console.log(texture__wall.minFilter); // 1008 ( THREE.LinearMipmapLinearFilter - r146 )
// console.log(THREE.NearestFilter); // 1003 ( r146 )
texture__wall.magFilter = THREE.NearestFilter;
texture__wall.minFilter = THREE.NearestFilter;
const geom__wall = new THREE.PlaneGeometry( size__room.z, size__room.x );
const mat__wall = new THREE.MeshBasicMaterial({ map: texture__wall });
mesh__wall = new THREE.Mesh( geom__wall, mat__wall);
mesh__wall.position.y = mesh__wall.position.y + size__room.y / 2.0;
mesh__wall.position.x = mesh__wall.position.x - size__room.x / 2.0 + 0.1;
// mesh__wall.rotation.set(-Math.PI/2, 0, -0.1 );
mesh__wall.rotateY(Math.PI/2);
scene.add(mesh__wall);
console.log("mesh__wall:", mesh__wall);
const canvas__renderer = document.getElementById("rendererCanvas");
renderer = new THREE.WebGLRenderer( {
canvas: canvas__renderer,
antialias: true,
});
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
// checkCanCapture();
requestAnimationFrame( animate );
const time = performance.now();
if ( controls.isLocked === true ) {
raycaster.ray.origin.copy( controls.getObject().position );
raycaster.ray.origin.y -= 10;
const intersections = raycaster.intersectObjects( arr_object, false );
const onObject = intersections.length > 0;
const delta = ( time - prevTime ) / 1000;
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
direction.z = Number( moveForward ) - Number( moveBackward );
direction.x = Number( moveRight ) - Number( moveLeft );
direction.normalize(); // this ensures consistent movements in all directions
if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * delta;
if ( moveLeft || moveRight ) velocity.x -= direction.x * 400.0 * delta;
if ( onObject === true ) {
velocity.y = Math.max( 0, velocity.y );
canJump = true;
}
controls.moveRight( - velocity.x * delta );
controls.moveForward( - velocity.z * delta );
controls.getObject().position.y += ( velocity.y * delta ); // new behavior
if ( controls.getObject().position.y < 10 ) {
velocity.y = 0;
controls.getObject().position.y = 10;
canJump = true;
}
}
{
// laser.point(pLocal);
isCanCapture = checkCanCapture_withLaser();
if(isCanCapture) {
const rgb__green = 0x00ff00;
laser.setColor(rgb__green);
} else {
const rgb__red = 0xff0000;
laser.setColor(rgb__red);
}
}
prevTime = time;
renderer.render( scene, camera );
}
// function checkCanCapture() {
// const arr_intersected = raycaster.intersectObjects( scene.children, true );
// console.log("arr_intersected:", arr_intersected[0]);
// // const arr_3dObj = arr_intersected.map(intersected => intersected.object);
// const distance = arr_intersected.filter(x => (x.object == mesh__wall))?.[0]?.distance;
// if(distance != null) {
// console.log("distance:", distance);
// }
// }
function checkCanCapture_withLaser() {
var outBool = false;
laser.setSource(new THREE.Vector3(3, -4, -2), camera);
var pLocal = new THREE.Vector3( 0, 0, -1e4 );
var pWorld = pLocal.applyMatrix4( camera.matrixWorld );
const arr_mesh = [mesh__wall];
laser.pointWithRaytrace(pLocal, arr_mesh, null, 1);
const arr_hit = laser.getMeshesHit();
// console.log("arr_hit:", arr_hit);
isLaserPointingIC = arr_hit.includes(mesh__wall);
// console.log("isLaserPointingIC:", isLaserPointingIC);
if(isLaserPointingIC) {
const arr_point = laser.getPoints();
// console.log("arr_point:", arr_point);
if(arr_point.length >= 2) {
const vect3__origin = arr_point[0];
// console.log("vect3__origin:", vect3__origin);
const vect3__direction = (new THREE.Vector3()).subVectors(arr_point[1], arr_point[0]).normalize();
// console.log("vect3__direction:", vect3__direction);
const distance_max = 30;
const raycaster = new THREE.Raycaster(vect3__origin, vect3__direction, 0, distance_max);
const arr_intersected = raycaster.intersectObject(mesh__wall, true);
const isCanCapture = (arr_intersected.length > 0);
outBool = isCanCapture;
}
}
return outBool;
}
</script>
</body>