-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainlib.js
374 lines (334 loc) · 12.9 KB
/
mainlib.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
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
(function() {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const labelRenderer = new THREE.CSS2DRenderer();
labelRenderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.domElement.style.position = 'absolute';
labelRenderer.domElement.style.top = '0px';
labelRenderer.domElement.style.pointerEvents = 'none';
document.body.appendChild(labelRenderer.domElement);
camera.position.z = 5;
const direction = new THREE.Vector3();
const euler = new THREE.Euler(0, 0, 0, 'YXZ');
const uiElements = new Map();
let elementCounter = 0;
function createCube() {
const cubeGeometry = new THREE.BoxGeometry();
const cubeMaterial = new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff });
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
const cameraDirection = new THREE.Vector3();
camera.getWorldDirection(cameraDirection);
cube.position.set(
camera.position.x + cameraDirection.x * 10,
camera.position.y + cameraDirection.y * 10,
camera.position.z + cameraDirection.z * 10
);
scene.add(cube);
return cube;
}
const key = {};
var baseSpeed = 0.02;
var freeCameraMode = true;
document.addEventListener('keydown', (event) => {
key[event.code] = true;
if (event.code === 'KeyH') {
freeCameraMode = !freeCameraMode;
}
});
document.addEventListener('keyup', (event) => {
key[event.code] = false;
});
function moveCamera(deltaTime) {
const forwardDirection = new THREE.Vector3();
const rightDirection = new THREE.Vector3();
const upDirection = new THREE.Vector3(0, 1, 0);
camera.getWorldDirection(forwardDirection);
forwardDirection.normalize();
rightDirection.setFromMatrixColumn(camera.matrixWorld, 0);
rightDirection.normalize();
let moveDirection = new THREE.Vector3(0, 0, 0);
if (!freeCameraMode) {
forwardDirection.y = 0;
forwardDirection.normalize();
rightDirection.y = 0;
rightDirection.normalize();
}
let currentSpeed = baseSpeed * deltaTime;
if (key['ShiftLeft'] || key['ShiftRight']) {
currentSpeed *= 2;
}
if (key['KeyW']) moveDirection.add(forwardDirection);
if (key['KeyS']) moveDirection.sub(forwardDirection);
if (key['KeyA']) moveDirection.sub(rightDirection);
if (key['KeyD']) moveDirection.add(rightDirection);
if (key['Space']) moveDirection.add(upDirection);
if (key['ControlLeft'] || key['ControlRight']) moveDirection.sub(upDirection);
if (moveDirection.lengthSq() > 0) {
moveDirection.normalize();
}
camera.position.addScaledVector(moveDirection, currentSpeed);
}
let isPointerLocked = false;
document.addEventListener('click', (event) => {
if (!isPointerLocked && event.target === renderer.domElement) {
renderer.domElement.requestPointerLock();
}
});
document.addEventListener('pointerlockchange', () => {
isPointerLocked = document.pointerLockElement === renderer.domElement;
document.body.classList.toggle('hidden-cursor', isPointerLocked);
});
document.addEventListener('mousemove', (event) => {
if (isPointerLocked) {
euler.setFromQuaternion(camera.quaternion);
euler.y -= event.movementX * 0.002;
euler.x -= event.movementY * 0.002;
euler.x = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, euler.x));
camera.quaternion.setFromEuler(euler);
}
});
function newUI() {
const id = elementCounter++;
uiElements.set(id, null);
return id;
}
function setTxtField(id, text = undefined, x = 0, y = 0, color = undefined) {
let element = uiElements.get(id);
if (!element) {
element = document.createElement('input');
element.type = 'text';
element.style.fontSize = '20px';
element.style.pointerEvents = 'auto';
element.style.position = 'absolute';
document.body.appendChild(element);
uiElements.set(id, element);
}
if (text !== undefined) element.value = text;
if (color !== undefined) element.style.color = color;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function setSquare(id, x, y, width, height, color, follow = undefined) {
let element = uiElements.get(id);
if (!element) {
element = document.createElement('div');
element.className = 'square';
element.style.position = 'absolute';
document.body.appendChild(element);
uiElements.set(id, element);
}
element.style.width = `${width}px`;
element.style.height = `${height}px`;
element.style.backgroundColor = color;
if (follow !== undefined) {
const squareLabel = new THREE.CSS2DObject(element);
squareLabel.position.set(x, y, 0);
follow.add(squareLabel);
uiElements.set(id, squareLabel);
} else {
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
}
function setCheckbox(id, checked, x = 0, y = 0) {
let element = uiElements.get(id);
if (!element) {
element = document.createElement('input');
element.type = 'checkbox';
element.style.pointerEvents = 'auto';
element.style.position = 'absolute';
document.body.appendChild(element);
uiElements.set(id, element);
}
element.checked = checked;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function setButton(id, text, x = 0, y = 0, color = undefined, onClickFunction = null) {
let element = uiElements.get(id);
if (!element) {
element = document.createElement('button');
element.style.pointerEvents = 'auto';
element.style.position = 'absolute';
document.body.appendChild(element);
uiElements.set(id, element);
}
element.textContent = text;
if (color !== undefined) element.style.color = color;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
if (onClickFunction) {
element.onclick = onClickFunction;
}
}
function setSlider(id, value, x = 0, y = 0) {
let element = uiElements.get(id);
if (!element) {
element = document.createElement('input');
element.type = 'range';
element.style.pointerEvents = 'auto';
element.style.position = 'absolute';
document.body.appendChild(element);
uiElements.set(id, element);
}
element.value = value;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function setText(id, text, x = 0, y = 0, z = 0, color = undefined, follow = undefined) {
let element = uiElements.get(id);
if (!element) {
element = document.createElement('div');
element.style.color = 'rgba(255, 255, 255, 1)';
element.style.fontSize = '20px';
element.style.pointerEvents = 'none';
element.classList.add('overlay-text');
document.body.appendChild(element);
uiElements.set(id, element);
}
element.textContent = text;
if (color !== undefined) element.style.color = color;
if (follow !== undefined) {
const textLabel = new THREE.CSS2DObject(element);
textLabel.position.set(x, y, z);
follow.add(textLabel);
uiElements.set(id, textLabel);
} else {
element.style.position = 'absolute';
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
}
function removeElement(id, time = 0) {
const element = uiElements.get(id);
if (element) {
if (time > 0) {
setTimeout(() => {
if (element instanceof THREE.CSS2DObject) {
element.parent.remove(element);
} else {
document.body.removeChild(element);
}
uiElements.delete(id);
}, time);
} else {
if (element instanceof THREE.CSS2DObject) {
element.parent.remove(element);
} else {
document.body.removeChild(element);
}
uiElements.delete(id);
}
}
}
function makeElementDraggable(element) {
let offsetX, offsetY;
function onMouseDown(e) {
offsetX = e.clientX - element.getBoundingClientRect().left;
offsetY = e.clientY - element.getBoundingClientRect().top;
element.style.cursor = 'grabbing';
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
}
function onMouseMove(e) {
element.style.left = `${e.clientX - offsetX}px`;
element.style.top = `${e.clientY - offsetY}px`;
}
function onMouseUp() {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
element.style.cursor = 'grab';
}
element.addEventListener('mousedown', onMouseDown);
return {
enable() {
element.style.cursor = 'grab';
},
disable() {
element.style.cursor = 'default';
}
};
}
function syncSliderAndTextField(sliderId, textFieldId) {
const slider = uiElements.get(sliderId);
const textField = uiElements.get(textFieldId);
if (slider && textField) {
slider.addEventListener('input', () => {
textField.value = slider.value;
});
textField.addEventListener('input', () => {
slider.value = textField.value;
});
}
}
let lastTime = performance.now();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
window.onbeforeunload = function (e) {
e.preventDefault();
e.returnValue = 'Really want to quit the game?';
};
document.onkeydown = function (e) {
e = e || window.event; // Get event
if (e.keyCode === 123) { // F12 devtools
e.preventDefault();
e.stopPropagation();
}
if (e.keyCode === 9 && e.ctrlKey) { // Ctrl+Tab
e.preventDefault();
e.stopPropagation();
}
if (e.which || e.keyCode === 116) { // F5
e.preventDefault();
e.stopPropagation();
}
if (!(e.ctrlKey || e.metaKey)) return;
var code = e.which || e.keyCode;
switch (code) {
case 83: // Ctrl+S
case 87: // Ctrl+W
case 82: // Ctrl+R
case 84: // Ctrl+T
case 78: // Ctrl+N
e.preventDefault();
e.stopPropagation();
break;
}
};
function animate(time) {
requestAnimationFrame(animate);
const deltaTime = (time - lastTime);
moveCamera(deltaTime);
renderer.render(scene, camera);
labelRenderer.render(scene, camera);
lastTime = time;
}
requestAnimationFrame(animate);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize);
// Expose functions to the global scope
window.MyLibrary = {
camera,
createCube,
newUI,
setTxtField,
setSquare,
setCheckbox,
setButton,
setSlider,
setText,
removeElement,
makeElementDraggable,
syncSliderAndTextField
};
})();