-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis.js
222 lines (200 loc) · 6.22 KB
/
vis.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
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
class Viewer2D {
constructor() {
window.addEventListener(
"resize",
function () {
if (this.data) {
this.renderScene(this.data);
}
}.bind(this)
);
}
renderScene(data) {
this.data = data;
var canvas = document.getElementById("overlay");
var ctx = canvas.getContext("2d");
var fullImage = new Image();
fullImage.src = $("#image").attr("src");
ctx.clearRect(0, 0, canvas.width, canvas.height);
var xScale = $("#image").width() / fullImage.naturalWidth;
var yScale = $("#image").height() / fullImage.naturalHeight;
var xOffset = ($("#overlay").width() - $("#image").width()) / 2;
var yOffset = ($("#overlay").height() - $("#image").height()) / 2;
var canvasxScale = $("#overlay").width() / canvas.width;
var canvasyScale = $("#overlay").height() / canvas.height;
var i = 0;
data.uv_shoulders.forEach((item) => {
if (data.warned.has(i)) ctx.fillStyle = "red";
else ctx.fillStyle = "green";
ctx.fillRect(
(item[0] * xScale + xOffset) / canvasxScale,
(item[1] * yScale + yOffset) / canvasyScale,
3,
3
);
i++;
});
}
}
class Viewer3D {
constructor(player) {
var canvas = document.getElementById("mapCanvas");
var engine = new BABYLON.Engine(canvas, true, {
preserveDrawingBuffer: true,
stencil: true,
});
this.scene = new BABYLON.Scene(engine);
this.scene.clearColor = new BABYLON.Color3(0.145, 0.145, 0.145);
this.camera = new BABYLON.ArcRotateCamera(
"Camera",
0,
0,
10,
new BABYLON.Vector3(0, 0, 0),
this.scene
);
this.camera.radius = 30;
this.camera.useAutoRotationBehavior = true;
this.camera.attachControl(canvas, false);
this.camera.setPosition(new BABYLON.Vector3(0, 15, -10));
this.camera.setTarget(new BABYLON.Vector3(0, 0, 10));
var light = new BABYLON.HemisphericLight(
"light1",
new BABYLON.Vector3(0, 1, 0),
this.scene
);
var arrow = BABYLON.MeshBuilder.CreateCylinder(
"arrow",
{ diameterBottom: 1, diameterTop: 0, height: 1 },
this.scene
);
arrow.rotate(BABYLON.Axis.X, Math.PI / 2, BABYLON.Space.WORLD);
var arrowstem = BABYLON.MeshBuilder.CreateCylinder(
"arrowstem",
{ diameterBottom: 0.5, diameterTop: 0.5, height: 1 },
this.scene
);
arrowstem.rotate(BABYLON.Axis.X, Math.PI / 2, BABYLON.Space.WORLD);
arrowstem.translate(BABYLON.Axis.Z, -1, BABYLON.Space.WORLD);
arrowstem.translate(BABYLON.Axis.Y, 1, BABYLON.Space.WORLD);
arrow.position.y = 1;
var ground = BABYLON.MeshBuilder.CreateGround(
"gd",
{ width: 500, height: 500, subdivisions: 100 },
this.scene
);
var wireframe = new BABYLON.StandardMaterial("material", this.scene);
wireframe.wireframe = true;
ground.material = wireframe;
this.scene.onPointerDown = () => {
var pickResult = this.scene.pick(
this.scene.pointerX,
this.scene.pointerY
);
if (pickResult.hit) {
var name = pickResult.pickedMesh.name;
if (name.includes("person") || name.includes("bounds")) {
player.registerInfection(Number(name.substring(7)));
player.render();
}
}
};
engine.runRenderLoop(
function () {
this.scene.render();
}.bind(this)
);
window.addEventListener("resize", function () {
engine.resize();
});
this.activeItems = [];
}
renderScene(data) {
this.activeItems.forEach((item) => {
item.dispose(true, true);
});
var xCentre = (data.maxX + data.minX) / 2;
var zCentre = (data.maxZ + data.minZ) / 2;
var green = new BABYLON.StandardMaterial("green", this.scene);
green.diffuseColor = new BABYLON.Color3(0, 1, 0);
var red = new BABYLON.StandardMaterial("red", this.scene);
red.diffuseColor = new BABYLON.Color3(1, 0, 0);
var yellow = new BABYLON.StandardMaterial("red", this.scene);
yellow.diffuseColor = new BABYLON.Color3(1, 1, 0.09);
var i = 0;
data.xyz_real.forEach((item) => {
var sphere = BABYLON.MeshBuilder.CreateCylinder(
`person:${i}`,
{ diameterBottom: 0.5, diameterTop: 0.5, height: 1 },
this.scene
);
this.activeItems.push(sphere);
var boundary = BABYLON.MeshBuilder.CreateCylinder(
`bounds:${i}`,
{
diameterBottom: data.safeDistance,
diameterTop: data.safeDistance,
height: 0.2,
},
this.scene
);
this.activeItems.push(boundary);
sphere.position.y = boundary.position.y = 1;
sphere.position.x = boundary.position.x = item[0];
sphere.position.z = boundary.position.z = item[2];
sphere.material = green;
if (data.warned.has(i)) {
boundary.material = red;
} else {
boundary.material = green;
}
if (data.currentInfected.has(i)) {
sphere.material = yellow;
} else {
sphere.material = green;
}
var plane = BABYLON.MeshBuilder.CreatePlane(
"plane" + i,
{ width: 2, size: 5, tileSize: 1 },
this.scene
);
this.activeItems.push(plane);
plane.parent = sphere;
plane.position.y = 4;
var materialPlane = new BABYLON.StandardMaterial(
"texturePlane" + i,
this.scene
);
materialPlane.diffuseTexture = new BABYLON.Texture(
"data:thumb" + i,
this.scene,
true,
true,
BABYLON.Texture.BILINEAR_SAMPLINGMODE,
null,
null,
data.thumbnails[i],
true
);
this.activeItems.push(materialPlane);
materialPlane.specularColor = new BABYLON.Color3(0, 0, 0);
plane.material = materialPlane;
plane.billboardMode = BABYLON.Mesh.BILLBOARDMODE_ALL;
i++;
});
data.warnings.forEach((warning) => {
var item1 = data.xyz_real[warning[0]];
var item2 = data.xyz_real[warning[1]];
var points = [
new BABYLON.Vector3(item1[0], 1.4, item1[2]),
new BABYLON.Vector3(item2[0], 1.4, item2[2]),
];
var lines = BABYLON.MeshBuilder.CreateLines(
"lines",
{ points: points },
this.scene
);
this.activeItems.push(lines);
});
}
}