-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
565 lines (470 loc) · 17 KB
/
Copy pathmain.js
File metadata and controls
565 lines (470 loc) · 17 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import gsap from "gsap";
import * as THREE from "three";
import { XRButton } from "three/examples/jsm/Addons.js";
let scene, camera, renderer, controls;
let trees = [];
let data;
let currentData;
// let currentCountry;
// VR state management (independent of DOM)
let currentYear = 2000; // Default starting year
let minYear = 2000; // Minimum year
let maxYear = 2020; // Maximum year
let yearStep = 5; // Year increment/decrement step
let currentCountryIndex = 0; // Current country index
// let sceneContainer;
let controller1, controller2;
let prevButtonAState = false;
let prevButtonBState = false;
let joystickThreshold = 0.5; // Threshold for joystick movement detection
let lastJoystickTime = 0; // To prevent too frequent year changes
let joystickCooldown = 500; // Cooldown in milliseconds between joystick actions
let sceneContainer;
// Add a debug counter to track button presses
let buttonBPressCount = 0;
let lastButtonDebugTime = 0;
let buttonDebugInterval = 2000; // Log button state every 2 seconds
function setupControllers() {
controller1 = renderer.xr.getController(0);
controller2 = renderer.xr.getController(1);
scene.add(controller1);
scene.add(controller2);
// Remove the selectstart event listeners as we'll check for specific buttons in the animation loop
controller1.addEventListener("selectstart", () => {
console.log("Controller 1 button pressed");
});
controller2.addEventListener("selectstart", () => {
console.log("Controller 2 button pressed");
});
}
function changeCountry() {
console.log("=== CHANGE COUNTRY FUNCTION CALLED ===");
if (!data || !Array.isArray(data) || data.length === 0) {
console.error("Data is not available or empty!");
return;
}
// Update our internal country index
currentCountryIndex = (currentCountryIndex + 1) % data.length;
console.log("New country index:", currentCountryIndex);
// If we're not in VR mode, also update the select element
const select = document.getElementById("countrySelect");
if (select) {
select.selectedIndex = currentCountryIndex;
}
// Clear existing trees
console.log("Clearing existing trees, count:", trees.length);
while (trees.length > 0) {
const tree = trees.pop();
sceneContainer.remove(tree);
}
// Update currentData with the new country's data
currentData = data[currentCountryIndex];
if (!currentData) {
console.error(
"Could not find country data for index:",
currentCountryIndex
);
return;
}
console.log("Changed to country:", currentData.country);
console.log("Current data:", currentData);
// Reinitialize trees based on the new country's `basic` value
console.log("Initializing trees with basic value:", currentData.basic);
initializeTrees(currentData.basic);
// Update visualization for the current period
const period = getCurrentPeriod();
console.log("Updating visualization for period:", period);
updateVisualization(period);
console.log("=== COUNTRY CHANGE COMPLETED ===");
}
// Function to change year by a specific amount (positive or negative)
function changeYearBy(amount) {
console.log("=== CHANGE YEAR FUNCTION CALLED ===");
console.log("Current year:", currentYear, "Change amount:", amount);
// Calculate new year value, ensuring it stays within bounds
let newValue = currentYear + amount;
// Ensure the value stays within the valid range
if (newValue > maxYear) {
newValue = maxYear;
} else if (newValue < minYear) {
newValue = minYear;
}
// Update our internal year state
currentYear = newValue;
console.log("New year:", currentYear);
// If we're not in VR mode, also update the slider
const slider = document.getElementById("yearSlider");
if (slider) {
slider.value = currentYear;
}
// Update visualization for the new period
const period = getCurrentPeriod();
console.log("Updating visualization for period:", period);
updateVisualization(period);
console.log("=== YEAR CHANGE COMPLETED ===");
updateYearText(currentYear, currentData.country);
}
function changeYear() {
changeYearBy(yearStep);
}
// 1. Initialize Scene here
function init() {
// Scene setup
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
document.body.appendChild(
XRButton.createButton(
renderer
// sessionInit
)
);
// Lighting
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);
// Camera position
camera.position.set(0, 10, 20);
camera.lookAt(0, 0, 0);
sceneContainer = new THREE.Group();
scene.add(sceneContainer);
// Load data
loadData();
setupControllers();
createYearPlane();
}
// 2. Load Data
async function loadData() {
try {
const response = await fetch("test_data.json");
data = await response.json(); // Assign to the global data variable
// Set currentData to the first country's data
if (data && data.length > 0) {
currentData = data[0];
console.log("Data loaded successfully:", data);
console.log("Initial country data:", currentData);
// Initialize min/max years based on data if available
if (data[0] && data[0].years) {
minYear = data[0].years.min || minYear;
maxYear = data[0].years.max || maxYear;
currentYear = minYear;
}
}
setupUI(data);
// Initialize trees for the first country
if (currentData) {
initializeTrees(currentData.basic);
updateVisualization(getCurrentPeriod());
}
} catch (error) {
console.error("Error loading data:", error);
}
}
// 3. Create Tree Model
function createTree() {
const tree = new THREE.Group();
// Trunk
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.1, 1),
new THREE.MeshPhongMaterial({ color: 0x8b4513 })
);
// Leaves
const leaves = new THREE.Mesh(
new THREE.SphereGeometry(0.5),
new THREE.MeshPhongMaterial({ color: 0x228b22 })
);
leaves.position.y = 0.8;
tree.add(trunk, leaves);
return tree;
}
// 4. Initialize Trees Using `basic`
function initializeTrees(basic) {
// Scale: 1 tree = 1000 ha
const initialTrees = Math.floor(basic / 1000);
for (let i = 0; i < initialTrees; i++) {
const tree = createTree();
tree.position.set(Math.random() * 20 - 10, 0, Math.random() * 20 - 10);
sceneContainer.add(tree);
trees.push(tree);
}
}
// 5. Update Visualization
function updateVisualization(period) {
// Calculate tree counts (example: 1 tree = 1000 ha)
const gain = currentData[`${period}_umd_tree_cover_gain__ha`] / 1000;
const loss = currentData[`${period}_cover_loss`] / 1000;
// Check if in immersive XR mode
if (renderer.xr && renderer.xr.isPresenting) {
console.log(
"Immersive mode detected: applying immediate visualization update."
);
// Apply gains immediately: create new trees with full scale
for (let i = 0; i < gain; i++) {
const tree = createTree();
tree.position.set(Math.random() * 20 - 10, 0, Math.random() * 20 - 10);
tree.scale.set(1, 1, 1); // full scale immediately
sceneContainer.add(tree);
trees.push(tree);
}
// Remove losses immediately: remove the last 'loss' trees if available
for (let i = 0; i < loss && trees.length > 0; i++) {
const tree = trees.pop();
sceneContainer.remove(tree);
}
} else {
// Non-immersive mode: animate growth
for (let i = 0; i < gain; i++) {
const tree = createTree();
tree.position.set(Math.random() * 20 - 10, 0, Math.random() * 20 - 10);
tree.scale.set(0, 0, 0); // Start small
sceneContainer.add(tree);
trees.push(tree);
// Animate growth
gsap.to(tree.scale, {
x: 1,
y: 1,
z: 1,
duration: 1,
delay: i * 0.1, // Stagger animations
});
}
// Animate loss after growth
gsap.delayedCall(gain * 0.1 + 1, () => {
for (let i = 0; i < loss && trees.length > 0; i++) {
const tree = trees.pop();
gsap.to(tree.scale, {
x: 0,
y: 0,
z: 0,
duration: 1,
onComplete: () => sceneContainer.remove(tree),
});
}
});
}
}
// 6. UI Controls
function setupUI(data) {
const select = document.getElementById("countrySelect");
const slider = document.getElementById("yearSlider");
if (!select || !slider) {
console.warn("UI elements not found, possibly in VR mode");
return;
}
// Set slider min/max based on our internal state
slider.min = minYear;
slider.max = maxYear;
slider.value = currentYear;
// Populate country dropdown
data.forEach((country) => {
const option = document.createElement("option");
option.value = country.iso;
option.textContent = country.country;
select.appendChild(option);
});
// Event listener for changing country
select.addEventListener("change", () => {
currentCountryIndex = select.selectedIndex;
currentData = data[currentCountryIndex];
// Clear existing trees
while (trees.length > 0) {
const tree = trees.pop();
sceneContainer.remove(tree);
}
// Re-initialize trees and update the scene
initializeTrees(currentData.basic);
updateVisualization(getCurrentPeriod());
// Update the text on the canvas (year + country)
updateYearText(currentYear, currentData.country);
});
// Event listener for changing the year via slider
slider.addEventListener("input", () => {
currentYear = parseInt(slider.value);
updateVisualization(getCurrentPeriod());
// Update the text on the canvas (year + current country)
updateYearText(currentYear, currentData.country);
});
}
function getCurrentPeriod() {
// Use our internal year state instead of reading from DOM
return `${currentYear}-${currentYear + yearStep}`;
}
let yearPlane; // The Mesh that holds our plane
let yearTexture; // Dynamic CanvasTexture used for text
let yearMaterial; // Material for the plane
function createYearPlane() {
// Create a <canvas> for drawing text
const canvas = document.createElement("canvas");
canvas.width = 800;
canvas.height = 150;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Default text (before the slider changes)
ctx.fillStyle = "#000000";
ctx.font = "60px sans-serif"; // Adjust font size as needed
if (currentData && currentData.country) {
ctx.fillText(`Country: ${currentData.country}`, 50, 60);
}
ctx.fillText(`Year: ${currentYear}`, 50, 120);
// Create a texture from the canvas
yearTexture = new THREE.CanvasTexture(canvas);
yearMaterial = new THREE.MeshBasicMaterial({ map: yearTexture, side: THREE.DoubleSide });
// Create a plane geometry and mesh
const planeGeometry = new THREE.PlaneGeometry(2, 1); // (width, height) – adjust as needed
yearPlane = new THREE.Mesh(planeGeometry, yearMaterial);
// Position the plane in front of the camera
// Adjust position so it’s clearly visible in VR
yearPlane.position.set(0, 2, -3);
// Add to the main scene (or sceneContainer)
scene.add(yearPlane);
}
function updateYearText(newYear, countryName) {
if (!yearTexture) return; // If plane not created yet, do nothing
// Get the same <canvas> we used before
const canvas = yearTexture.image;
const ctx = canvas.getContext("2d");
// Clear previous text
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Redraw with updated year
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#000000";
ctx.font = "60px sans-serif";
ctx.fillText(`Country: ${countryName}`, 50, 70);
ctx.fillText(`Year: ${newYear}`, 50, 130);
// Mark texture for update
yearTexture.needsUpdate = true;
}
// 7. WebXR Setup
function enableXR() {
renderer.xr.enabled = true;
// Set up the animation loop to handle rendering and joystick input
renderer.setAnimationLoop(function () {
// Check for joystick movement and button presses on controllers
if (renderer.xr.isPresenting) {
const session = renderer.xr.getSession();
if (session) {
// Periodically log controller state for debugging
const currentTime = Date.now();
const shouldLogDebug =
currentTime - lastButtonDebugTime > buttonDebugInterval;
if (shouldLogDebug) {
lastButtonDebugTime = currentTime;
console.log("=== CONTROLLER DEBUG INFO ===");
console.log(
"Session active, input sources count:",
session.inputSources.length
);
}
for (const source of session.inputSources) {
if (!source) {
if (shouldLogDebug)
console.log("Input source is null or undefined");
continue;
}
if (shouldLogDebug) {
console.log("Input source handedness:", source.handedness);
console.log("Has gamepad:", !!source.gamepad);
}
if (source.gamepad) {
const gamepad = source.gamepad;
const handedness = source.handedness; // 'left' or 'right'
if (shouldLogDebug && handedness === "right") {
console.log(
"Right controller buttons:",
gamepad.buttons ? gamepad.buttons.length : 0
);
if (gamepad.buttons && gamepad.buttons.length > 1) {
console.log(
"Button B state:",
gamepad.buttons[1].pressed ? "PRESSED" : "not pressed"
);
console.log("Button B value:", gamepad.buttons[1].value);
console.log("prevButtonBState:", prevButtonBState);
}
}
// Check for button B press on Meta Quest controllers
// Button 1 is typically button B on Meta Quest controllers
if (gamepad.buttons && gamepad.buttons.length > 1) {
const buttonB = gamepad.buttons[1];
// Check if button B is pressed (and wasn't pressed in the previous frame)
if (buttonB.pressed) {
if (handedness === "right") {
if (!prevButtonBState) {
buttonBPressCount++;
console.log(
`Button B pressed on right controller (press #${buttonBPressCount})`
);
console.log("Button B value:", buttonB.value);
console.log("Button B touched:", buttonB.touched);
// Call changeCountry and track if it was successful
try {
changeCountry();
console.log("changeCountry() called successfully");
} catch (error) {
console.error("Error in changeCountry():", error);
}
prevButtonBState = true;
} else if (shouldLogDebug) {
console.log("Button B still pressed, ignoring (debounce)");
}
}
} else {
if (
handedness === "right" &&
prevButtonBState &&
shouldLogDebug
) {
console.log("Button B released on right controller");
prevButtonBState = false;
}
}
}
// Check joystick movement on right controller
if (handedness === "right") {
// Check joystick/thumbstick movement; use axis[2] if available, otherwise fallback to axis[0]
if (gamepad.axes && gamepad.axes.length >= 1) {
const horizontalAxis =
gamepad.axes.length >= 3 ? gamepad.axes[2] : gamepad.axes[0];
// Only process joystick movement if it exceeds threshold and cooldown has passed
const currentTime = Date.now();
if (
Math.abs(horizontalAxis) > joystickThreshold &&
currentTime - lastJoystickTime > joystickCooldown
) {
lastJoystickTime = currentTime;
if (horizontalAxis > joystickThreshold) {
// Move forward in time (right)
console.log("Joystick moved right");
changeYearBy(yearStep);
} else if (horizontalAxis < -joystickThreshold) {
// Move backward in time (left)
console.log("Joystick moved left");
changeYearBy(-yearStep);
}
}
}
}
}
}
if (shouldLogDebug) {
console.log("=== END CONTROLLER DEBUG INFO ===");
}
}
}
renderer.render(scene, camera);
});
console.log("WebXR session started");
}
// Initialize the app
init();
// Enable WebXR when ready (requires user gesture)
document.body.addEventListener("click", enableXR);