-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree.html
536 lines (409 loc) · 17.7 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
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>run</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
div {
width: 100%;
height: 100%;
position: absolute;
top: 0;
/* left: 0; */
}
.buttons {
position: absolute;
top: 0;
right: 0;
width: 20%;
height: 10%;
/* background-color: darkgrey; */
z-index: 999;
}
input {
width: 100%;
cursor: pointer;
}
</style>
</head>
<body>
<div class="buttons">
<input id='start' type="button" value="START">
<input id="stop" type="button" value="STOP">
<input id='end' type="button" value="END">
<input id='toggle' type="button" value="切换">
<input id='top' type="button" value="上">
<input id='bottom' type="button" value="下">
</div>
<script type="module">
import * as THREE from './three/build/three.module.js';
import { OrbitControls } from "./three/examples/jsm/controls/experimental/CameraControls.js";
import coord from "./coord.js";
let scene = new THREE.Scene();
scene.background = new THREE.Color('gray');
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.set(250, 250, 250);
let renderer = new THREE.WebGLRenderer({ alpha: false });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
let controls = new OrbitControls(camera, renderer.domElement);
{
let directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
let light = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light);
let axesHelper = new THREE.AxesHelper(5000);
scene.add(axesHelper);
}
/************场景布置结束**************/
//添加星空背景
{
let vertices = [];
for (let i = 0; i < 10000; i++) {
let x = THREE.MathUtils.randFloatSpread(2000);
let y = THREE.MathUtils.randFloatSpread(2000);
let z = THREE.MathUtils.randFloatSpread(2000);
vertices.push(x, y, z);
}
let geometry6 = new THREE.BufferGeometry();
geometry6.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
let material6 = new THREE.PointsMaterial({ color: 0xffffff, size: 0.5 });
let starPoints = new THREE.Points(geometry6, material6);
scene.add(starPoints);
}
// let material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
// let geometry = new THREE.CylinderBufferGeometry(0, 10, 50, 12);
// geometry.rotateX(Math.PI / 2);
// let cone = new THREE.Mesh(geometry, material);
// scene.add(cone);
let angle = Math.PI / 3;
let cylinderGroup = new THREE.Group();
let cylinderData = [
{
position: [0, 0, 0],
translateY: -17,
rotateZ: 0
},
{
position: [15, 0, 0],
translateY: -8.5,
rotateZ: angle
},
{
position: [15, 0, 0],
translateY: 8.5,
rotateZ: 2 * angle
},
{
position: [0, 0, 0],
translateY: 17,
rotateZ: 3 * angle
},
{
position: [-15, 0, 0],
translateY: 8.5,
rotateZ: -2 * angle
},
{
position: [-15, 0, 0],
translateY: -8.5,
rotateZ: -1 * angle
}
];
var cylinderGeo = new THREE.CylinderGeometry(4, 6, 12, 100);
var cylinderMat = new THREE.MeshPhongMaterial({
// 创建材料
color: "yellow",
});
for (let i = 0; i < 6; i++) {
var cylinderMesh = new THREE.Mesh(cylinderGeo, cylinderMat);
cylinderMesh.geometry.center();
// cylinderMesh.rotateX(-Math.PI / 2);
cylinderMesh.position.set(...cylinderData[i].position);
cylinderMesh.translateY(cylinderData[i].translateY);
cylinderMesh.rotateZ(cylinderData[i].rotateZ);
cylinderGroup.add(cylinderMesh);
}
cylinderGroup.name = "cylinderGroup";
cylinderGroup.scale.set(0.5, 0.5, 0.5);
let cone = cylinderGroup;
scene.add(cone);
//自定义路径类
class myPath {
constructor(array) {
//将传进来的数组转换为Vec3集合
let pointsArr = [];
if (array.length % 3 !== 0) {
console.error('错误,数据的个数非3的整数倍!', array);
return null;
}
// for (let index = 0; index < array.length; index += 3) {
// pointsArr.push(new THREE.Vector3(array[index], array[index + 1], array[index + 2]));
// }
for (let index = 0; index < array.length; index += 1) {
pointsArr.push(new THREE.Vector3(array[index][0], array[index][1], array[index][2]));
}
//顶点位置三维向量数组
this.pointsArr = pointsArr;
//折线几何体
this.line = null;
{
let lineMaterial = new THREE.LineBasicMaterial({
color: 0xff00ff
});
let lineGeometry = new THREE.BufferGeometry().setFromPoints(pointsArr);
this.line = new THREE.Line(lineGeometry, lineMaterial);
}
//锚点几何体
this.points = null;
{
// let pointsBufferGeometry = new THREE.BufferGeometry();
// pointsBufferGeometry.setAttribute('position', new THREE.Float32BufferAttribute(array, 3));
// let pointsMaterial = new THREE.PointsMaterial({ color: 0xffff00, size: 10 });
// this.points = new THREE.Points(pointsBufferGeometry, pointsMaterial);
// let angle = Math.PI / 3;
// let cylinderGroup = new THREE.Group();
// let cylinderData = [
// {
// position: [0, 0, 0],
// translateY: -17,
// rotateZ: 0
// },
// {
// position: [15, 0, 0],
// translateY: -8.5,
// rotateZ: angle
// },
// {
// position: [15, 0, 0],
// translateY: 8.5,
// rotateZ: 2 * angle
// },
// {
// position: [0, 0, 0],
// translateY: 17,
// rotateZ: 3 * angle
// },
// {
// position: [-15, 0, 0],
// translateY: 8.5,
// rotateZ: -2 * angle
// },
// {
// position: [-15, 0, 0],
// translateY: -8.5,
// rotateZ: -1 * angle
// }
// ];
// var cylinderGeo = new THREE.CylinderGeometry(4, 6, 12, 100);
// var cylinderMat = new THREE.MeshPhongMaterial({
// // 创建材料
// color: "#FFFF00",
// });
// for (let i = 0; i < 6; i++) {
// var cylinderMesh = new THREE.Mesh(cylinderGeo, cylinderMat);
// cylinderMesh.geometry.center();
// // cylinderMesh.rotateX(-Math.PI / 2);
// cylinderMesh.position.set(...cylinderData[i].position);
// cylinderMesh.translateY(cylinderData[i].translateY);
// cylinderMesh.rotateZ(cylinderData[i].rotateZ);
// cylinderGroup.add(cylinderMesh);
// }
// cylinderGroup.name = "cylinderGroup";
// cylinderGroup.scale.set(0.5, 0.5, 0.5);
// this.points = cylinderGroup;
}
//计算每个锚点在整条折线上所占的百分比
this.pointPercentArr = [];
{
let distanceArr = []; //每段距离
let sumDistance = 0; //总距离
for (let index = 0; index < pointsArr.length - 1; index++) {
distanceArr.push(pointsArr[index].distanceTo(pointsArr[index + 1]));
}
sumDistance = distanceArr.reduce(function (tmp, item) {
return tmp + item;
})
let disPerSumArr = [0];
disPerSumArr.push(distanceArr[0]);
distanceArr.reduce(function (tmp, item) {
disPerSumArr.push(tmp + item);
return tmp + item;
})
disPerSumArr.forEach((value, index) => {
disPerSumArr[index] = value / sumDistance;
})
this.pointPercentArr = disPerSumArr;
}
// console.log(this.pointPercentArr);
//上一次的朝向
this.preUp = new THREE.Vector3(0, 0, 0);
//run函数需要的数据
this.perce = 0; //控制当前位置占整条线百分比
this.speed = 0.0005; //控制是否运动
this.turnFactor = 0; //暂停时间因子
this.turnSpeedFactor = 0.001; //转向速度因子
this.obj = null;
this.preTime = new Date().getTime();
this.firstTurn = false;
}
//获取点,是否转弯,朝向等
getPoint(percent) {
let indexP = 0;
let indexN = 0;
let turn = false;
for (let i = 0; i < this.pointPercentArr.length; i++) {
if (percent >= this.pointPercentArr[i] && percent < this.pointPercentArr[i + 1]) {
indexN = i + 1;
indexP = i;
if (percent === this.pointPercentArr[i]) {
turn = true;
}
}
}
let factor = (percent - this.pointPercentArr[indexP]) / (this.pointPercentArr[indexN] - this.pointPercentArr[indexP]);
let position = new THREE.Vector3();
position.lerpVectors(this.pointsArr[indexP], this.pointsArr[indexN], factor); //position的计算完全正确
//计算朝向
let up = new THREE.Vector3().subVectors(this.pointsArr[indexN], this.pointsArr[indexP]);
let preUp = this.preUp;
if (this.preUp.x != up.x || this.preUp.y != up.y || this.preUp.z != up.z) {
console.info('当前朝向与上次朝向不等,将turn置为true!');
turn = true;
}
this.preUp = up;
return {
position,
direction: up,
turn, //是否需要转向
preUp, //当需要转向时的上次的方向
};
}
//参数:是否运动,运动的对象,是否运动到结尾
run(animata, camera, end) {
if (end) {
this.perce = 0.99999;
this.obj = this.getPoint(this.perce);
//修改位置
let posi = this.obj.position;
// cone.position.set(posi.x, posi.y, posi.z);
camera.position.set(posi.x, posi.y, posi.z); //相机漫游2
}
else if (animata) {
//转弯时
if (this.obj && this.obj.turn) {
if (this.turnFactor == 0) {
this.preTime = new Date().getTime();
this.turnFactor += 0.000000001;
}
else {
let nowTime = new Date().getTime();
let timePass = nowTime - this.preTime;
this.preTime = nowTime;
this.turnFactor += this.turnSpeedFactor * timePass;
}
// console.log('--->>> 当前需要turn , turnFactor值为 :', this.turnFactor);
if (this.turnFactor > 1) {
this.turnFactor = 0;
this.perce += this.speed;
this.obj = this.getPoint(this.perce);
}
else {
//修改朝向 (向量线性插值方式)
let interDirec = new THREE.Vector3();
interDirec.lerpVectors(this.obj.preUp, this.obj.direction, this.turnFactor);
let look = new THREE.Vector3();
look = look.add(this.obj.position);
look = look.add(interDirec);
// cone.lookAt(look);
camera.lookAt(look); //相机漫游1
}
}
//非转弯时
else {
this.obj = this.getPoint(this.perce);
//修改位置
let posi = this.obj.position;
// cone.position.set(posi.x, posi.y, posi.z);
camera.position.set(posi.x, posi.y, posi.z); //相机漫游2
//当不需要转向时进行
if (!this.obj.turn) {
let look = posi.add(this.obj.direction);
// cone.lookAt(look);
camera.lookAt(look); //相机漫游3
}
this.perce += this.speed;
}
}
}
}
// let a = new myPath([
// 0, 0, 0,
// 500, 0, 0,
// 500, 0, 500,
// 0, 0, 500,
// 0, 500, 500,
// 500, 750, 0,
// ]);
let a = new myPath(coord);
scene.add(a.points);
scene.add(a.line);
let startFlag = true;
let endFlag = false;
let toggleFlag = true;
let runMesh = cone;
document.getElementById('start').onclick = function timeStart() {
console.log('点击了start');
startFlag = true;
endFlag = false;
};
document.getElementById('stop').onclick = function timeStart() {
console.log('点击了stop');
startFlag = false;
};
document.getElementById('end').onclick = function timeStart() {
console.log('点击了end');
endFlag = true;
};
document.getElementById('top').onclick = function timeStart() {
this.perce -= this.speed;
a.run(startFlag, runMesh, endFlag);
};
document.getElementById('bottom').onclick = function timeStart() {
this.perce += this.speed;
a.run(startFlag, runMesh, endFlag);
};
document.getElementById('toggle').onclick = function timeStart() {
toggleFlag = !toggleFlag;
console.log('点击了toggle',toggleFlag);
if (toggleFlag) {
runMesh = cone;
camera.position.set(500, 500, 500);
}
else {
runMesh = camera;
}
};
a.run(startFlag, runMesh, endFlag);
let animate = function () {
requestAnimationFrame(animate);
controls.update();
// a.run(startFlag, runMesh, endFlag);
//路程循环
if (a.perce >= 1) {
a.perce = 0;
}
renderer.render(scene, camera);
};
animate();
</script>
</body>
</html>