-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaba6.js
95 lines (84 loc) · 2.72 KB
/
laba6.js
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
let markerVisible = { A: false, B: false, C: false, D: false, F: false };
AFRAME.registerComponent('registerevents', {
init: function () {
var marker = this.el;
marker.addEventListener('markerFound', function () {
console.log('markerFound', marker.id);
markerVisible[marker.id] = true;
});
marker.addEventListener('markerLost', function () {
console.log('markerLost', marker.id);
markerVisible[marker.id] = false;
});
}
});
AFRAME.registerComponent('run', {
init: function () {
this.markers = {
A: document.querySelector("#A"),
B: document.querySelector("#B"),
C: document.querySelector("#C"),
D: document.querySelector("#D"),
F: document.querySelector("#F")
};
this.lines = {
AB: {
cylinder: this.createCylinder('#lineAB'),
markerStart: 'A',
markerEnd: 'B'
},
BC: {
cylinder: this.createCylinder('#lineBC'),
markerStart: 'B',
markerEnd: 'C'
},
CD: {
cylinder: this.createCylinder('#lineCD'),
markerStart: 'C',
markerEnd: 'D'
},
DF: {
cylinder: this.createCylinder('#lineDF'),
markerStart: 'D',
markerEnd: 'F'
},
FA: {
cylinder: this.createCylinder('#lineFA'),
markerStart: 'F',
markerEnd: 'A'
}
};
},
createCylinder: function (selector) {
let material = new THREE.MeshLambertMaterial({ color: 0xFF0000 });
let geometry = new THREE.CylinderGeometry(0.05, 0.05, 1, 12);
geometry.applyMatrix4(new THREE.Matrix4().makeTranslation(0, 0.5, 0));
geometry.applyMatrix4(new THREE.Matrix4().makeRotationX(THREE.MathUtils.degToRad(90)));
let cylinder = new THREE.Mesh(geometry, material);
let line = document.querySelector(selector).object3D;
line.add(cylinder);
cylinder.visible = false;
return cylinder;
},
updateCylinder: function (line, markerStart, markerEnd) {
let start = this.markers[markerStart].object3D.getWorldPosition(new THREE.Vector3());
let end = this.markers[markerEnd].object3D.getWorldPosition(new THREE.Vector3());
let distance = start.distanceTo(end);
line.lookAt(end);
line.scale.set(1, 1, distance);
line.visible = true;
},
hideCylinder: function (line) {
line.visible = false;
},
tick: function () {
for (let line of Object.values(this.lines)) {
let { markerStart, markerEnd, cylinder } = line;
if (markerVisible[markerStart] && markerVisible[markerEnd]) {
this.updateCylinder(cylinder, markerStart, markerEnd);
} else {
this.hideCylinder(cylinder);
}
}
}
});