/
index.js
219 lines (174 loc) · 5.71 KB
/
index.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
/* global THREE */
/* global ColorTester */
/*
Описание панорамы.
texture - сама панорама
ciolors - текстура с цветовой разметкой. черный = null
elements - объект, ключами которого является цвет на текстуре colors
*/
const MAP = {
texture: "./maps/map.jpg",
colors: "./maps/map_colors.jpg",
elements: {
["#ff0000"]: {
name: "Window 1",
description: "Window Description"
},
["#800000"]: {
name: "Window 2",
description: "Window 2 Description"
},
["#400000"]: {
name: "Window 3",
description: "Window 3 Description"
},
["#300000"]: {
name: "Window 4",
description: "Window 4 Description"
},
["#00ff00"]: {
name: "Дверь",
description: "Дверь как дверь"
},
["#0000ff"]: {
name: "Комод",
description: "Обычный старый комод"
}
}
};
let infoElement = document.querySelector("#description"),
infoName = document.querySelector("#name"),
infoDesc = document.querySelector("#desc");
var camera, scene, renderer, colorTester, colorMap;
var isUserInteracting = false,
onMouseDownMouseX = 0,
onMouseDownMouseY = 0,
lon = 0,
onMouseDownLon = 0,
lat = 0,
onMouseDownLat = 0,
phi = 0,
theta = 0;
init();
animate();
function init() {
var container, mesh;
container = document.getElementById("container");
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
1,
1100
);
camera.target = new THREE.Vector3(0, 0, 0);
scene = new THREE.Scene();
var geometry = new THREE.SphereGeometry(500, 60, 40);
geometry.scale(-1, 1, 1);
var material = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load(MAP.texture)
});
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
document.addEventListener("mousedown", onDocumentMouseDown, false);
document.addEventListener("mousemove", onDocumentMouseMove, false);
document.addEventListener("mouseup", onDocumentMouseUp, false);
document.addEventListener("wheel", onDocumentMouseWheel, false);
// create color tester
// see colorTester.js
//mesh - наша сфера, на которой панорама
colorTester = new ColorTester(camera, mesh);
const loader = new THREE.TextureLoader();
//загружаем карту цветов и скармливаем тестеру
loader.load(MAP.colors, t => {
colorTester.setTestTexture(t);
});
//
window.addEventListener("resize", onWindowResize, false);
}
// получаем из цвета - объект, используя неточное сравнение
// так как есть погрешность при сжатии
function mapColorToObject(color, pairs) {
for (let name in pairs) {
const c = new THREE.Color(name);
if (colorTester.colorEq(lastIntersectedColor, c, 3)) {
return pairs[name];
}
}
return undefined;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
let onPointerDownPointerX,
onPointerDownPointerY,
onPointerDownLon,
onPointerDownLat;
let lastIntersectedColor = undefined;
function onDocumentMouseDown(event) {
event.preventDefault();
isUserInteracting = true;
onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;
onPointerDownLon = lon;
onPointerDownLat = lat;
lastIntersectedColor = colorTester.raycastColor(event);
// игнорируем черный цвет
if (lastIntersectedColor.getHex() < 10) {
lastIntersectedColor = undefined;
infoElement.style.display = "none";
}
}
function onDocumentMouseMove(event) {
if (isUserInteracting === true) {
lon = (onPointerDownPointerX - event.clientX) * 0.1 + onPointerDownLon;
lat = (event.clientY - onPointerDownPointerY) * 0.1 + onPointerDownLat;
}
const isNotBlack = colorTester.raycastColor(event).getHex() > 10;
renderer.domElement.style.cursor = isNotBlack ? "pointer" : "";
}
function onDocumentMouseUp(event) {
isUserInteracting = false;
const color = colorTester.raycastColor(event);
if (
lastIntersectedColor &&
color.getHex() > 10 &&
colorTester.colorEq(lastIntersectedColor, color)
) {
const e = mapColorToObject(lastIntersectedColor, MAP.elements);
if (e) {
infoElement.style.display = "";
infoElement.style.top = event.clientY + "px";
infoElement.style.left = event.clientX + "px";
infoDesc.innerHTML = e.description;
infoName.innerHTML = e.name;
}
}
}
function onDocumentMouseWheel(event) {
camera.fov += event.deltaY * 0.05;
camera.fov = Math.max(30, Math.min(camera.fov, 120));
camera.updateProjectionMatrix();
}
function animate() {
requestAnimationFrame(animate);
update();
}
function update() {
if (isUserInteracting === false) {
//lon += 0.1;
}
lat = Math.max(-85, Math.min(85, lat));
phi = THREE.Math.degToRad(90 - lat);
theta = THREE.Math.degToRad(lon);
camera.target.x = 500 * Math.sin(phi) * Math.cos(theta);
camera.target.y = 500 * Math.cos(phi);
camera.target.z = 500 * Math.sin(phi) * Math.sin(theta);
camera.lookAt(camera.target);
renderer.render(scene, camera);
}