-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
681 lines (493 loc) · 18.6 KB
/
index.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - geometry - terrain</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="assets/css/main.css">
<style>
body {
background-color: #bfd1e5;
color: #61443e;
}
a {
color: #a06851;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="assets/js/tween.js"></script>
<script src="assets/js/UTMLatLng.js"></script>
<script type="module">
import * as THREE from '../build/three.module.js';
import Stats from './assets/jsm/libs/stats.module.js';
import { GUI } from './assets/jsm/libs/dat.gui.module.js';
import { OrbitControls } from './assets/jsm/controls/OrbitControls.js';
import { OBJLoader } from './assets/jsm/loaders/OBJLoader.js';
import { MTLLoader } from './assets/jsm/loaders/MTLLoader.js';
// import { TWEEN } from './assets/jsm/libs/tween.module.min.js';
let container, stats;
let camera, controls, scene, renderer;
let mesh, texture;
// const worldWidth = 1024, worldDepth = 512,
// worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
var worldWidth, worldDepth, worldHalfWidth, worldHalfDepth;
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
let json;
var maxE = 0, minE = 1000000, maxX = 0, minX = 1000000000, maxY = 0, minY = 1000000000;
let mousedown = false, drag = 0, startPoint = new THREE.Vector3(), endPoint = new THREE.Vector3(), rectangleFixed = true, drawMode = "line";
const material = new THREE.LineBasicMaterial({
color: 0x0000ff
});
let path, person, vehicle, motionready = true;
init();
addCar();
addControls();
readText();
animate();
function init() {
container = document.getElementById( 'container' );
container.innerHTML = "";
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild( renderer.domElement );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xbfd1e5 );
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 20000 );
controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 10;
controls.maxDistance = 10000;
controls.maxPolarAngle = Math.PI / 2;
container.addEventListener( 'pointerdown', onMouseDown, false );
container.addEventListener( 'pointermove', onMouseMove, false );
container.addEventListener( 'pointerup', onMouseUp, false );
var light = new THREE.HemisphereLight(0xffffbb, 0x080820, 0.2 );
scene.add(light)
// const plight = new THREE.PointLight( 0xffffff);
const plight = new THREE.SpotLight(0xffffff);
plight.intensity = 0.8;
plight.position.set( -1000, 1500, -1000 );
plight.castShadow = true;
plight.shadow.mapSize.width = 512;
plight.shadow.mapSize.height = 512;
plight.shadow.camera.near = 0.5;
plight.shadow.camera.far = 5000;
scene.add( plight );
// const axesHelper = new THREE.AxesHelper(1000);
// scene.add(axesHelper)
}
function addCar() {
var objloader = new OBJLoader();
var mtlloader = new MTLLoader();
mtlloader.load('assets/model/car.mtl', function(materials) {
materials.preload();
objloader.setMaterials(materials);
objloader.load('assets/model/car.obj', function(obj) {
person = obj;
console.log(person)
// obj.scale.set(0.03, 0.03, 0.03);
obj.scale.set(3, 3, 3);
scene.add(obj)
});
})
}
function generateTerrain() {
//
const data = generateHeight( worldWidth, worldDepth );
controls.target.y = data[ worldHalfWidth + worldHalfDepth * worldWidth ];
camera.position.y = controls.target.y + 300;
camera.position.x = 500;
controls.update();
var geometry = new THREE.PlaneBufferGeometry( worldWidth, worldDepth, worldWidth - 1, worldDepth - 1 );
geometry.rotateX( - Math.PI / 2 );
const vertices = geometry.attributes.position.array;
for ( let i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
vertices[ j + 1 ] = data[ i ];
}
geometry.computeFaceNormals(); // needed for helper
texture = new THREE.CanvasTexture( generateTexture( data, worldWidth, worldDepth ) );
texture.wrapS = THREE.ClampToEdgeWrapping;
texture.wrapT = THREE.ClampToEdgeWrapping;
mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
// mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('assets/image/texture.jpg') } ) );
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add( mesh );
stats = new Stats();
container.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize, false );
readPath();
}
function readText() {
// As with JSON, use the Fetch API & ES6
fetch('json/jsonData4.txt')
.then(response => response.text())
.then(data => {
// Do something with your data
json = JSON.parse(data);
analyzeJson()
});
}
function readPath() {
// As with JSON, use the Fetch API & ES6
var utmLatLng = new UTMLatLng();
console.log(utmLatLng)
fetch('json/Path_LatLng.txt')
.then(response => response.text())
.then(data => {
// Do something with your data
path = JSON.parse(data);
for(let i = 0; i < path.length; i++){
let result = utmLatLng.convertLatLngToUtm(path[i].position[0], path[i].position[1],10);
path[i].position[0] = result.Easting - minX - worldHalfWidth;
path[i].position[1] = result.Northing - minY - worldHalfDepth;
}
console.log(path[0].position[0]+minX, path[0].position[1]+minY)
});
}
function analyzeJson() {
for(var i = 0; i < json[1].layers.length; i++) {
var item = json[1].layers[i];
if(item.elevation > maxE) maxE = item.elevation;
if(item.elevation < minE) minE = item.elevation;
for(var j = 0; j < item.coordinates.length; j++) {
if(item.coordinates[j][0] > maxX) maxX = item.coordinates[j][0];
if(item.coordinates[j][0] < minX) minX = item.coordinates[j][0];
if(item.coordinates[j][1] > maxY) maxY = item.coordinates[j][1];
if(item.coordinates[j][1] < minY) minY = item.coordinates[j][1];
}
}
// console.log(maxE, minE, maxX, minX, maxY, minY)
// console.log(maxE-minE)
worldWidth = parseInt((maxX-minX)/2), worldDepth = parseInt((maxY-minY)/2),
worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
// console.log(worldWidth, worldDepth)
generateTerrain();
// console.log(maxElevation, minElevation, startX, startY, terrainW, terrainH)
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function generateHeight( width, height ) {
const size = width * height, data = new Uint16Array( size ), interpolatedData = new Uint16Array( size );
for ( let i = 0; i < size; i ++ ) {
data[ i ] = 0;
interpolatedData[ i ] = 0;
}
for(var i = 0; i < json[1].layers.length; i++) {
var item = json[1].layers[i];
for(var j = 0; j < item.coordinates.length; j++) {
var coord = item.coordinates[j];
var xInd = parseInt((coord[0] - minX) / 2);
var yInd = parseInt((coord[1] - minY) / 2);
data[ yInd * worldWidth + xInd ] = parseInt(item.elevation - minE);
}
}
for ( let i = 0; i < size; i ++ ) {
if(data[ i ] == 0) {
let count = 0;
let temp = 0;
let interpolation = 0;
for(let j = -10; j < 10; j++){
for(let k = -10; k < 10; k++){
if(data[i+j*worldWidth+k] != 0){
temp += data[i+j*worldWidth+k];
count ++;
}
}
}
if(count > 5){
interpolatedData[i] = parseInt(temp/count);
}
}
}
for ( let i = 0; i < size; i ++ ) {
if(data[i] == 0)
data[i] = interpolatedData[i];
}
for ( let i = 0; i < size; i ++ ) {
if(data[i] == 0){
const interpolation = data[i-1];
var dir = 1;
var index = i+dir;
while(1){
var breakFlag = false;
if(data[index] == 0 || data[index+worldWidth] == 0 || data[index+worldWidth*2] == 0 ){
data[index] = interpolation;
index += dir;
} else {
for(let t = dir*10; t != -dir*11; t -= dir){
if(data[index+worldWidth+t] == 0){
index += worldWidth + t;
dir *= -1;
break;
}
if(t == -dir*10) breakFlag = true;
}
}
if(breakFlag) break;
}
}
}
for(var i = 0; i < size; i++){
if(detectboundary(i, worldWidth, worldDepth))
data[i] = 0;
}
return data;
}
function detectboundary(i, w, h){
if(i < w*10 || i > w*(h-10) || (i+1)%w == 0 || i%w == 0)
return true
return false;
}
function generateTexture( data, width, height ) {
// bake lighting into texture
let context, image, imageData, shade;
const vector3 = new THREE.Vector3( 0, 0, 0 );
const sun = new THREE.Vector3( 0.3, 0.3, 0.3 );
// sun.normalize();
const canvas = document.createElement( 'canvas' );
canvas.width = width;
canvas.height = height;
context = canvas.getContext( '2d' );
context.fillStyle = '#000';
context.fillRect( 0, 0, width, height );
image = context.getImageData( 0, 0, canvas.width, canvas.height );
imageData = image.data;
for ( let i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) {
vector3.x = data[ j - 2 ] - data[ j + 2 ];
vector3.y = 1;
vector3.z = data[ j - width * 2 ] - data[ j + width * 2 ];
vector3.normalize();
shade = vector3.dot( sun );
imageData[ i ] = ( 96 + shade * 128 ) * ( 0.8 + data[ j ] * 0.002 );
imageData[ i + 1 ] = ( 32 + shade * 96 ) * ( 0.8 + data[ j ] * 0.002 );
imageData[ i + 2 ] = ( shade * 96 ) * ( 0.8 + data[ j ] * 0.002 );
}
context.putImageData( image, 0, 0 );
// Scaled 4x
const canvasScaled = document.createElement( 'canvas' );
canvasScaled.width = width * 4;
canvasScaled.height = height * 4;
context = canvasScaled.getContext( '2d' );
context.scale( 4, 4 );
context.drawImage( canvas, 0, 0 );
image = context.getImageData( 0, 0, canvasScaled.width, canvasScaled.height );
imageData = image.data;
for ( let i = 0, l = imageData.length; i < l; i += 4 ) {
const v = ~ ~ ( Math.random() * 5 );
imageData[ i ] += v;
imageData[ i + 1 ] += v;
imageData[ i + 2 ] += v;
}
context.putImageData( image, 0, 0 );
return canvasScaled;
}
//
function animate() {
requestAnimationFrame( animate );
render();
if(stats)
stats.update();
}
function render() {
renderer.render( scene, camera );
TWEEN.update();
}
function getIntersection(event) {
mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
// See if the ray from the camera into the world hits one of our meshes
const intersects = raycaster.intersectObject( mesh );
return intersects;
}
function getIntersection_car(event) {
mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
// See if the ray from the camera into the world hits one of our meshes
const intersects = raycaster.intersectObjects( person.children, true);
return intersects;
}
function onMouseDown( event ) {
mousedown = true;
// if(intersects.length == 0) return;
}
function onMouseMove( event ) {
if(mousedown) drag ++;
// if(rectangleFixed) return;
// if(intersects.length == 0) return;
// let intersect = intersects[0];
// endPoint.copy(intersect);
// drawRectangle();
}
function onMouseUp( event ) {
event.preventDefault()
var intersectCar = getIntersection_car(event);
if(intersectCar.length > 0){
alert("car is clicked");
}
var intersects = getIntersection(event)
if(intersects.length == 0) return;
let intersect = intersects[0].point;
if(drag > 4) {
drag = 0;
mousedown = false;
return;
}
if(rectangleFixed) {
startPoint.copy(intersect);
rectangleFixed = false;
} else {
endPoint.copy(intersect);
rectangleFixed = true;
if(drawMode == "rectangle")
drawRectangle();
else
drawLine();
}
mousedown = false;
drag = 0;
}
function drawLine() {
if(scene.getObjectByName("line")){
var line = scene.getObjectByName("line");
line.geometry.dispose();
line.material.dispose();
scene.remove(line);
}
let startIndex = startPoint.x + worldHalfWidth + parseInt(startPoint.z + worldHalfDepth);
let endIndex = endPoint.x + worldHalfWidth + parseInt(endPoint.z + worldHalfDepth);
let deltaX = endPoint.x - startPoint.x;
let deltaZ = endPoint.z - startPoint.z;
let distance = parseInt(startPoint.distanceTo(endPoint));
const vertices = mesh.geometry.attributes.position.array;
const points = [];
for(let x = startPoint.x, z = startPoint.z, step = 0; step < distance; x += deltaX / distance, z+= deltaZ/distance, step++){
let index = VertexFromPosition(new THREE.Vector3(x, 0, z));
points.push(new THREE.Vector3(x, vertices[index * 3 + 1]+2, z));
// console.log(new THREE.Vector3(x, vertices[index * 3 + 1], z))
}
const geometry = new THREE.BufferGeometry().setFromPoints( points );
const linesegment = new THREE.Line( geometry, material );
linesegment.name = "line";
scene.add( linesegment );
}
function drawRectangle() {
if(scene.getObjectByName("rectangle")){
var rectangle = scene.getObjectByName("rectangle");
rectangle.geometry.dispose();
rectangle.material.dispose();
scene.remove(rectangle);
}
let startIndex = startPoint.x + worldHalfWidth + parseInt(startPoint.z + worldHalfDepth);
let endIndex = endPoint.x + worldHalfWidth + parseInt(endPoint.z + worldHalfDepth);
const vertices = mesh.geometry.attributes.position.array;
const points = [];
let smallX = startPoint.x < endPoint.x ? startPoint : endPoint;
let bigX = startPoint.x > endPoint.x ? startPoint : endPoint;
let smallZ = startPoint.z < endPoint.z ? startPoint : endPoint;
let bigZ = startPoint.z > endPoint.z ? startPoint : endPoint;
let index1 = parseInt(smallX.x) + worldHalfWidth + parseInt(smallZ.z + worldHalfDepth) * worldWidth;
let index2 = parseInt(bigX.x) + worldHalfWidth + parseInt(smallZ.z + worldHalfDepth) * worldWidth;
let index3 = parseInt(bigX.x) + worldHalfWidth + parseInt(bigZ.z + worldHalfDepth) * worldWidth;
let index4 = parseInt(smallX.x) + worldHalfWidth + parseInt(bigZ.z + worldHalfDepth) * worldWidth;
for(var i = index1, delta = 0; i < index2; i++, delta ++){
points.push(new THREE.Vector3(smallX.x + delta, vertices[i*3+1]+1, smallZ.z));
}
for(var i = index2, delta = 0; i < index3; i += worldWidth, delta++){
points.push(new THREE.Vector3(bigX.x, vertices[i*3+1]+1, smallZ.z + delta));
}
for(var i = index3, delta = 0; i > index4; i--, delta --){
points.push(new THREE.Vector3(bigX.x + delta, vertices[i*3+1]+1, bigZ.z));
}
for(var i = index4, delta = 0; i > index1; i -= worldWidth, delta ++){
points.push(new THREE.Vector3(smallX.x, vertices[i*3+1]+1, bigZ.z-delta));
}
const geometry = new THREE.BufferGeometry().setFromPoints( points );
const linesegment = new THREE.Line( geometry, material );
linesegment.name = "rectangle";
scene.add( linesegment );
}
function VertexFromPosition(position) {
return parseInt(position.x + worldHalfWidth + (parseInt(position.z) + worldHalfDepth) * worldWidth);
}
function toggleCarView() {
console.log("toggle")
person.visible = !person.visible;
}
function motion() {
if(!motionready) return;
motionHelper();
}
function motionHelper(pathIndex = 0) {
motionready = false;
const vertices = mesh.geometry.attributes.position.array;
if(pathIndex === path.length) { motionready = true; return;}
const speed = 0.02;
var destination = new THREE.Vector3(path[pathIndex].position[0] - worldHalfWidth, 0, path[pathIndex].position[1]-worldHalfDepth);
person.position.y = 0;
let distance = person.position.distanceTo(destination);
let destIndex = VertexFromPosition(destination);
// destination.y = vertices[destIndex * 3 + 1];
person.lookAt(destination)
let time = distance/speed;
new TWEEN.Tween({x: person.position.x, y:person.position.y, z:person.position.z, rot: 0})
.to({x: destination.x, y: destination.y, z: destination.z, rot: 0}, time)
.onUpdate(function () {
person.position.set(this.x, this.y, this.z);
let index = VertexFromPosition(person.position);
person.position.y = vertices[index * 3+1] + 2;
})
.yoyo(true)
.start()
.onComplete(function() {
console.log("complete")
// pathIndex ++;
motionHelper(pathIndex+1);
});
}
function drawLineMode() {
console.log("test1")
drawMode = "line";
}
function drawRectangleMode() {
console.log("test2")
drawMode = "rectangle";
}
function addControls() {
const params = {
motion: motion,
drawLineMode: drawLineMode,
drawRectangleMode: drawRectangleMode,
toggleCarView: toggleCarView,
};
var gui = new GUI();
gui.add( params, 'motion' ).name( 'Motion' );
gui.add( params, 'drawLineMode' ).name( 'Draw line' );
gui.add( params, 'drawRectangleMode' ).name( 'Draw rectangle' );
gui.add( params, 'toggleCarView').name('Toggle Car View');
}
function calcPosFromLatLonRad(radius, lat, lon) {
var spherical = new THREE.Spherical(
radius,
THREE.Math.degToRad(90 - lon),
THREE.Math.degToRad(lat)
);
var vector = new THREE.Vector3();
vector.setFromSpherical(spherical);
console.log(vector.x, vector.y, vector.z);
return vector;
}
</script>
</body>
</html>