-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
380 lines (320 loc) · 23.4 KB
/
app.js
File metadata and controls
380 lines (320 loc) · 23.4 KB
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
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { FontLoader } from "three/addons/loaders/FontLoader.js";
import { TextGeometry } from "three/addons/geometries/TextGeometry.js";
const scene = new THREE.Scene();
// ██████╗ █████╗ ███╗ ███╗███████╗██████╗ █████╗ ███████╗███████╗████████╗██╗ ██╗██████╗
// ██╔════╝██╔══██╗████╗ ████║██╔════╝██╔══██╗██╔══██╗ ██╔════╝██╔════╝╚══██╔══╝██║ ██║██╔══██╗
// ██║ ███████║██╔████╔██║█████╗ ██████╔╝███████║ ███████╗█████╗ ██║ ██║ ██║██████╔╝
// ██║ ██╔══██║██║╚██╔╝██║██╔══╝ ██╔══██╗██╔══██║ ╚════██║██╔══╝ ██║ ██║ ██║██╔═══╝
// ╚██████╗██║ ██║██║ ╚═╝ ██║███████╗██║ ██║██║ ██║ ███████║███████╗ ██║ ╚██████╔╝██║
// ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
const screenDim = {
height: threeJScanvas.height,
width: threeJScanvas.width,
};
const cameraSettings = {
aspectRatio: screenDim.width / screenDim.height,
width: 1000,
near: 0,
far: 1,
};
cameraSettings.height = cameraSettings.width / cameraSettings.aspectRatio;
const camera = new THREE.OrthographicCamera(
-cameraSettings.width,
cameraSettings.width,
cameraSettings.height,
-cameraSettings.height,
cameraSettings.near,
cameraSettings.far
);
camera.position.set(0, 0, 1);
camera.lookAt(0, 0, 0);
// ██╗ ██╗ ██████╗ ██╗ ██╗████████╗ ███████╗███████╗████████╗██╗ ██╗██████╗
// ██║ ██║██╔════╝ ██║ ██║╚══██╔══╝ ██╔════╝██╔════╝╚══██╔══╝██║ ██║██╔══██╗
// ██║ ██║██║ ███╗███████║ ██║ ███████╗█████╗ ██║ ██║ ██║██████╔╝
// ██║ ██║██║ ██║██╔══██║ ██║ ╚════██║██╔══╝ ██║ ██║ ██║██╔═══╝
// ███████╗██║╚██████╔╝██║ ██║ ██║ ███████║███████╗ ██║ ╚██████╔╝██║
// ╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
// const ambientLight = new THREE.AmbientLight(0xffffff);
// scene.add(ambientLight);
const light = new THREE.PointLight(0xff0000, 1, 100);
light.position.set(50, 50, 50);
scene.add(light);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(0, 1, 1);
scene.add(directionalLight);
// ██╗ ██╗███████╗██████╗ ██████╗ ███████╗███╗ ██╗██████╗ ███████╗██████╗ ███████╗██████╗
// ██║ ██║██╔════╝██╔══██╗ ██╔══██╗██╔════╝████╗ ██║██╔══██╗██╔════╝██╔══██╗██╔════╝██╔══██╗
// ██║ █╗ ██║█████╗ ██████╔╝ ██████╔╝█████╗ ██╔██╗ ██║██║ ██║█████╗ ██████╔╝█████╗ ██████╔╝
// ██║███╗██║██╔══╝ ██╔══██╗ ██╔══██╗██╔══╝ ██║╚██╗██║██║ ██║██╔══╝ ██╔══██╗██╔══╝ ██╔══██╗
// ╚███╔███╔╝███████╗██████╔╝ ██║ ██║███████╗██║ ╚████║██████╔╝███████╗██║ ██║███████╗██║ ██║
// ╚══╝╚══╝ ╚══════╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝╚═════╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
const renderer = new THREE.WebGLRenderer({
canvas: document.getElementById("threeJScanvas"),
antialias: true,
});
renderer.setSize(screen.width, screen.height);
renderer.setClearColor(0x000000, 1);
renderer.render(scene, camera);
// ██████╗ ██████╗ ██╗██████╗ ███████╗███████╗████████╗██╗ ██╗██████╗
// ██╔════╝ ██╔══██╗██║██╔══██╗ ██╔════╝██╔════╝╚══██╔══╝██║ ██║██╔══██╗
// ██║ ███╗██████╔╝██║██║ ██║ ███████╗█████╗ ██║ ██║ ██║██████╔╝
// ██║ ██║██╔══██╗██║██║ ██║ ╚════██║██╔══╝ ██║ ██║ ██║██╔═══╝
// ╚██████╔╝██║ ██║██║██████╔╝ ███████║███████╗ ██║ ╚██████╔╝██║
// ╚═════╝ ╚═╝ ╚═╝╚═╝╚═════╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
const gridHelper = new THREE.GridHelper(100000, 500, 0x07eeb3, 0x07eeb3);
gridHelper.rotation.x = -Math.PI / 2;
// scene.add(gridHelper);
// ██████╗ ██████╗ ██████╗ ██╗████████╗ ███████╗███████╗████████╗██╗ ██╗██████╗
// ██╔═══██╗██╔══██╗██╔══██╗██║╚══██╔══╝ ██╔════╝██╔════╝╚══██╔══╝██║ ██║██╔══██╗
// ██║ ██║██████╔╝██████╔╝██║ ██║ ███████╗█████╗ ██║ ██║ ██║██████╔╝
// ██║ ██║██╔══██╗██╔══██╗██║ ██║ ╚════██║██╔══╝ ██║ ██║ ██║██╔═══╝
// ╚██████╔╝██║ ██║██████╔╝██║ ██║ ███████║███████╗ ██║ ╚██████╔╝██║
// ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableRotate = false;
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = true;
controls.enablePan = true;
controls.mouseButtons = {
RIGHT: THREE.MOUSE.PAN,
MIDDLE: THREE.MOUSE.PAN,
LEFT: THREE.MOUSE.PAN,
};
controls.minZoom = 0;
controls.maxZoom = 1000;
// ███████╗ ██████╗ ███╗ ██╗████████╗ ███████╗███████╗████████╗██╗ ██╗██████╗
// ██╔════╝██╔═══██╗████╗ ██║╚══██╔══╝ ██╔════╝██╔════╝╚══██╔══╝██║ ██║██╔══██╗
// █████╗ ██║ ██║██╔██╗ ██║ ██║ ███████╗█████╗ ██║ ██║ ██║██████╔╝
// ██╔══╝ ██║ ██║██║╚██╗██║ ██║ ╚════██║██╔══╝ ██║ ██║ ██║██╔═══╝
// ██║ ╚██████╔╝██║ ╚████║ ██║ ███████║███████╗ ██║ ╚██████╔╝██║
// ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
const fontLoader = new FontLoader();
function createText(text, position, width, color = 0x000000) {
fontLoader.load(
"https://threejs.org/examples/fonts/helvetiker_regular.typeface.json",
function (font) {
const textOptions = {
font: font,
size: 20,
depth: 0,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 0.03,
bevelSize: 0.02,
bevelOffset: 0,
bevelSegments: 5,
};
const textGeometry = new TextGeometry(text, textOptions);
const textBoundingBox = new THREE.Box3().setFromObject(
new THREE.Mesh(textGeometry)
); // Get the bounding box of the text
const textWidth = textBoundingBox.max.x - textBoundingBox.min.x;
textGeometry.center();
// Scale the text geometry
const textMaterial = new THREE.MeshPhongMaterial({
color: color,
});
const textMesh = new THREE.Mesh(textGeometry, textMaterial);
textMesh.position.set(position.x, position.y, position.z);
// Calculate the scale factor to adjust the width
// const margin = width / 10;
// const scaleFactor = (width - margin) / textWidth;
// Apply the scaling to the text mesh
// textMesh.scale.set(scaleFactor, scaleFactor, 1); // Only scale on x and y axes
scene.add(textMesh);
}
);
}
// Example usage:
// createText("Hello, Three.js!", { x: 0, y: 0, z: 0 }, 200);
// ████████╗ ██████╗ ██████╗ ██╗ ████████╗██╗██████╗
// ╚══██╔══╝██╔═══██╗██╔═══██╗██║ ╚══██╔══╝██║██╔══██╗
// ██║ ██║ ██║██║ ██║██║ ██║ ██║██████╔╝
// ██║ ██║ ██║██║ ██║██║ ██║ ██║██╔═══╝
// ██║ ╚██████╔╝╚██████╔╝███████╗██║ ██║██║
// ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝
// Create a raycaster and mouse vector
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
// Modified addRectangle function
var geometriesData = [];
const tooltip = document.getElementById("tooltip"); // The tooltip element
function onMouseMove(event) {
// Normalize mouse position to (-1, 1) range for both axes
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse.clone(), camera);
var objects = raycaster.intersectObjects(scene.children);
if (objects.length) {
console.log(objects.length);
const objectUuid = objects[0].object.uuid;
// Check if the uuid is in geometriesData
const found = geometriesData.find((rect) => rect.uuid === objectUuid);
if (found) {
tooltip.style.display = "block";
tooltip.textContent = `Rectangle at (${found.position.x}, ${found.position.y}, ${found.position.z}), Width: ${found.width}, Height: ${found.height}`;
tooltip.style.left = `${event.clientX + 10}px`; // 10px offset from the mouse
tooltip.style.top = `${event.clientY + 10}px`; // 10px offset from the mouse
}
} else {
// Hide the tooltip if no object is intersected
tooltip.style.display = "none";
}
}
// ██████╗ ██╗ █████╗ ███╗ ██╗
// ██╔══██╗██║ ██╔══██╗████╗ ██║
// ██████╔╝██║ ███████║██╔██╗ ██║
// ██╔═══╝ ██║ ██╔══██║██║╚██╗██║
// ██║ ███████╗██║ ██║██║ ╚████║
// ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═══╝
function addRectangle(center, width, height, color = 0x00ff00, text) {
if (text.length) createText(text, center, 100);
let margin = 0.5;
const geometry = new THREE.PlaneGeometry(width - margin, height - margin);
const material = new THREE.MeshBasicMaterial({
color: color,
side: THREE.DoubleSide,
});
const rectangle = new THREE.Mesh(geometry, material);
// Add custom userData to store additional information (optional)
rectangle.userData = { type: "rectangle", info: "This is a rectangle" };
rectangle.position.set(center.x, center.y, center.z);
scene.add(rectangle);
// Create a rectangle data object and add it to the array
const rectangleData = {
uuid: rectangle.uuid,
position: { x: center.x, y: center.y, z: center.z },
width: width,
height: height,
color: color,
};
geometriesData.push(rectangleData);
return rectangle;
}
// ███████╗██╗███╗ ██╗██████╗ ██████╗ ██████╗ ██████╗ ███╗ ███╗ ██████╗ ███╗ ██╗ ███╗ ███╗ █████╗ ██████╗
// ██╔════╝██║████╗ ██║██╔══██╗ ██╔══██╗██╔═══██╗██╔═══██╗████╗ ████║ ██╔═══██╗████╗ ██║ ████╗ ████║██╔══██╗██╔══██╗
// █████╗ ██║██╔██╗ ██║██║ ██║ ██████╔╝██║ ██║██║ ██║██╔████╔██║ ██║ ██║██╔██╗ ██║ ██╔████╔██║███████║██████╔╝
// ██╔══╝ ██║██║╚██╗██║██║ ██║ ██╔══██╗██║ ██║██║ ██║██║╚██╔╝██║ ██║ ██║██║╚██╗██║ ██║╚██╔╝██║██╔══██║██╔═══╝
// ██║ ██║██║ ╚████║██████╔╝ ██║ ██║╚██████╔╝╚██████╔╝██║ ╚═╝ ██║ ╚██████╔╝██║ ╚████║ ██║ ╚═╝ ██║██║ ██║██║
// ╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝
function softBlinkRect(
name,
targetColor = 0xffff00,
duration = 2000,
cycles = 3
) {
const uuid = name.uuid;
const rectData = geometriesData.find((data) => data.uuid === uuid);
if (rectData) {
const rect = scene.getObjectByProperty("uuid", uuid); // Get the mesh by uuid
if (rect) {
const originalColor = rectData.color;
const colorStart = new THREE.Color(originalColor);
const colorEnd = new THREE.Color(targetColor);
// Set material properties for smooth blending
rect.material.transparent = true;
rect.material.opacity = 1;
let elapsedTime = 0;
let cycleCount = 0;
// Function to animate the color fade
const animateColorFade = () => {
elapsedTime += 16; // approx. 60fps
let progress = Math.sin((elapsedTime / duration) * Math.PI); // smooth sinusoidal progress
// Blend the colors based on the progress
rect.material.color.lerpColors(colorStart, colorEnd, progress);
if (elapsedTime < duration) {
requestAnimationFrame(animateColorFade);
} else {
elapsedTime = 0; // Reset elapsed time for next cycle
cycleCount++;
if (cycleCount < cycles) {
animateColorFade(); // Continue the animation for the next cycle
} else {
rect.material.transparent = false; // Stop transparency after the final cycle
rect.material.color.set(originalColor); // Reset the color to original
}
}
};
// Start the soft blinking animation
animateColorFade();
}
} else {
console.log("Rectangle not found!");
}
}
window.softBlinkRect = softBlinkRect;
// ██████╗ ██████╗ █████╗ ██╗ ██╗ ██████╗ ███████╗ ██████╗ ███╗ ███╗███████╗████████╗██████╗ ██╗███████╗███████╗
// ██╔══██╗██╔══██╗██╔══██╗██║ ██║ ██╔════╝ ██╔════╝██╔═══██╗████╗ ████║██╔════╝╚══██╔══╝██╔══██╗██║██╔════╝██╔════╝
// ██║ ██║██████╔╝███████║██║ █╗ ██║ ██║ ███╗█████╗ ██║ ██║██╔████╔██║█████╗ ██║ ██████╔╝██║█████╗ ███████╗
// ██║ ██║██╔══██╗██╔══██║██║███╗██║ ██║ ██║██╔══╝ ██║ ██║██║╚██╔╝██║██╔══╝ ██║ ██╔══██╗██║██╔══╝ ╚════██║
// ██████╔╝██║ ██║██║ ██║╚███╔███╔╝ ╚██████╔╝███████╗╚██████╔╝██║ ╚═╝ ██║███████╗ ██║ ██║ ██║██║███████╗███████║
// ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝╚══════╝
const colorCode = { Road: 0xbababa, lab: 0xef476f, classRoom: 0x00b4d8 };
const AM_1_1 = addRectangle(
{ x: 500, y: 0, z: 0 },
200,
100,
colorCode.classRoom,
"AM.1.1"
);
window.AM_1_1 = AM_1_1;
const AM_0_2 = addRectangle(
{ x: 0, y: 0, z: 0 },
200,
100,
colorCode.classRoom,
"AM.0.2"
);
window.AM_0_2 = AM_0_2;
const AM_1_2 = addRectangle(
{ x: -200, y: 0, z: 0 },
200,
100,
colorCode.classRoom,
"AM.1.2"
);
window.AM_1_2 = AM_1_2;
const rect4 = addRectangle(
{ x: 100, y: 100, z: 0 },
800,
100,
colorCode.Road,
"Parking"
);
const lab = addRectangle(
{ x: 250, y: 0, z: 0 },
300,
100,
colorCode.lab,
"Lab 1"
);
// Make sure to call onMouseMove for mouse movement detection
window.addEventListener("mousemove", onMouseMove, false);
// █████╗ ███╗ ██╗██╗███╗ ███╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗ ██╗ ██████╗ ██████╗ ██████╗
// ██╔══██╗████╗ ██║██║████╗ ████║██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║ ██║ ██╔═══██╗██╔═══██╗██╔══██╗
// ███████║██╔██╗ ██║██║██╔████╔██║███████║ ██║ ██║██║ ██║██╔██╗ ██║ ██║ ██║ ██║██║ ██║██████╔╝
// ██╔══██║██║╚██╗██║██║██║╚██╔╝██║██╔══██║ ██║ ██║██║ ██║██║╚██╗██║ ██║ ██║ ██║██║ ██║██╔═══╝
// ██║ ██║██║ ╚████║██║██║ ╚═╝ ██║██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║ ███████╗╚██████╔╝╚██████╔╝██║
// ╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚═╝
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
// ██████╗ ███████╗███████╗██╗███████╗███████╗
// ██╔══██╗██╔════╝██╔════╝██║╚══███╔╝██╔════╝
// ██████╔╝█████╗ ███████╗██║ ███╔╝ █████╗
// ██╔══██╗██╔══╝ ╚════██║██║ ███╔╝ ██╔══╝
// ██║ ██║███████╗███████║██║███████╗███████╗
// ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚══════╝╚══════╝
// window.addEventListener("resize", function () {
// const newWidth = screenDim.width;
// const newHeight = screenDim.height;
// camera.aspect = newWidth / newHeight;
// camera.updateProjectionMatrix();
// renderer.setSize(newWidth, newHeight);
// });