-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.jsx
265 lines (219 loc) · 9.06 KB
/
main.jsx
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
// app/components/globe/main.jsx
import * as THREE from 'three';
import { useEffect, useRef, useState } from 'react';
import App from './app';
import Globe from './globe';
import Lines from './lines';
import Marker from './marker';
import Markers from './markers';
import Points from './points';
import gridData from '~/components/globe/data/grid';
import countriesData from '~/components/globe/data/countries';
import connectionsData from '~/components/globe/data/connections';
import { getCountries } from '~/components/globe/data/processing';
import { config, elements, groups, animations } from '~/components/globe/utils/config';
import "./main.module.css"
const Main = () => {
const [controls, setControls] = useState({ changed: true });
const [data, setData] = useState({ grid: [], countries: [], connections: [] });
const [isLoading, setIsLoading] = useState(true);
const appRef = useRef();
useEffect(() => {
if (!appRef.current) {
appRef.current = new App({ setup, animate, preload });
window.onresize = appRef.current.handleResize;
}
return () => {
if (appRef.current && appRef.current.guiRef) {
appRef.current.guiRef.destroy();
}
window.onload = null;
window.onresize = null;
};
}, []);
const preload = async () => {
try {
const loadedData = {
grid: gridData.grid,
countries: countriesData.countries,
connections: getCountries(connectionsData.connections, countriesData.countries),
};
setData(loadedData);
return true;
} catch (error) {
setData({ error });
return false;
}//
};
const setup = (app) => {
const controllers = [];
// Panneau de configuration décommenter pour activer le panneau de configuration.
{/*app.addControlGui(gui => {
const colorFolder = gui.addFolder('Colors');
controllers.push(colorFolder.addColor(config.colors, 'globeDotColor'));
controllers.push(colorFolder.addColor(config.colors, 'globeMarkerColor'));
controllers.push(colorFolder.addColor(config.colors, 'globeMarkerGlow'));
controllers.push(colorFolder.addColor(config.colors, 'globeLines'));
controllers.push(colorFolder.addColor(config.colors, 'globeLinesDots'));
const sizeFolder = gui.addFolder('Sizes');
controllers.push(sizeFolder.add(config.sizes, 'globeDotSize', 1, 5));
controllers.push(sizeFolder.add(config.scale, 'globeScale', 0.1, 1));
const displayFolder = gui.addFolder('Display');
controllers.push(displayFolder.add(config.display, 'map'));
controllers.push(displayFolder.add(config.display, 'points'));
controllers.push(displayFolder.add(config.display, 'markers'));
controllers.push(displayFolder.add(config.display, 'markerLabel'));
controllers.push(displayFolder.add(config.display, 'markerPoint'));
controllers.push(displayFolder.add(config.display, 'atmosphere'));
const animationsFolder = gui.addFolder('Animations');
controllers.push(animationsFolder.add(animations, 'rotateGlobe'));
sizeFolder.open();
});*/}
controllers.forEach(controller => {
controller.onChange(() => {
setControls(prevControls => ({ ...prevControls, changed: true }));
});
});
app.camera.position.z = config.sizes.globe * 2.85;
app.camera.position.y = config.sizes.globe * 0;
groups.globe = new THREE.Group();
groups.globe.name = 'Globe';
const globeInstance = new Globe();
const globeObject = globeInstance.getObject3D();
if (globeObject) {
groups.globe.add(globeObject);
} else {
console.error("globeObject is not initialized");
}
const points = new Points(gridData);
if (points) {
groups.globe.add(points);
} else {
console.error("points are not initialized");
}
const markers = new Markers(countriesData);
if (markers) {
groups.globe.add(markers);
} else {
console.error("markers are not initialized");
}
const lines = new Lines({ connections: connectionsData.connections });
if (lines) {
groups.globe.add(lines);
} else {
console.error("lines are not initialized");
}
if (elements.atmosphere) {
groups.globe.add(elements.atmosphere);
} else {
console.error("Atmosphere is not initialized.");
}
app.scene.add(groups.globe);
};
const animate = (app) => {
if (controls.changed) {
const updateMaterial = (element, property, value) => {
if (element) {
element.material[property] = value;
}
};
const updateVisibility = (elements, visible) => {
elements.forEach(element => {
if (element) element.visible = visible;
});
};
if (elements.atmosphere) {
elements.atmosphere.visible = config.display.atmosphere;
}
if (elements.globePoints) {
updateMaterial(elements.globePoints, 'size', config.sizes.globeDotSize);
elements.globePoints.material.color.set(config.colors.globeDotColor);
}
if (elements.globe) {
elements.globe.scale.set(config.scale.globeScale, config.scale.globeScale, config.scale.globeScale);
} else {
console.error("Globe is not initialized.");
}
if (elements.lines) {
elements.lines.forEach(line => {
line.material.color.set(config.colors.globeLines);
});
}
[groups.map, groups.markers, groups.points].forEach((group, index) => {
const configKeys = ['map', 'markers', 'points'];
if (group) group.visible = config.display[configKeys[index]];
});
updateVisibility(elements.markerLabel, config.display.markerLabel);
updateVisibility(elements.markerPoint, config.display.markerPoint);
setControls(prevControls => ({ ...prevControls, changed: false }));
}
if (elements.lineDots) {
elements.lineDots.forEach(dot => {
if (dot && dot.material) {
dot.material.color.set(config.colors.globeLinesDots);
dot.animate();
}
});
}
if (elements.markers && elements.markers.length) {
elements.markers.forEach(marker => {
if (marker instanceof Marker) {
if (marker.point && marker.point.material) {
marker.point.material.color.set(config.colors.globeMarkerColor);
}
if (marker.glow && marker.glow.material) {
marker.glow.material.color.set(config.colors.globeMarkerGlow);
}
if (marker.label && marker.label.material) {
marker.label.material.map.needsUpdate = true;
}
marker.animateGlow && marker.animateGlow();
}
});
}
if (!groups.globe) {
console.error("groups.globe is not initialized.");
} else {
groups.globe.add(groups.atmosphere || new THREE.Group());
groups.globe.add(groups.lines || new THREE.Group());
groups.globe.add(groups.markers || new THREE.Group());
groups.globe.add(groups.points || new THREE.Group());
groups.globe.add(groups.map || new THREE.Group());
if (!app.renderer) {
console.error("Renderer is not initialized.");
}
}
if (animations.rotateGlobe) {
groups.globe.rotation.y -= 0.0025;
}
app.renderer.render(app.scene, app.camera);
};
useEffect(() => {
const animateLoop = () => {
if (appRef.current) {
animate(appRef.current);
}
animationId = requestAnimationFrame(animateLoop);
};
let animationId = requestAnimationFrame(animateLoop);
return () => {
cancelAnimationFrame(animationId);
};
}, []);
if (data.error) {
return <div>Error loading data: {data.error.message}</div>;
}
return (
<div className="app-wrapper">
{/* {isLoading && <div>Loading main.jsx...</div>} */}
{isLoading}
<Globe
scene={appRef.current?.scene}
setIsLoading={setIsLoading}
loader={new THREE.TextureLoader()}
/>
<ul className="markers"></ul>
</div>
);
};
export default Main;