-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTacticalGlobe.jsx
More file actions
156 lines (129 loc) · 4.39 KB
/
TacticalGlobe.jsx
File metadata and controls
156 lines (129 loc) · 4.39 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
import React, { useEffect, useRef } from 'react';
import * as THREE from 'three';
const TacticalGlobe = ({ className, accentColor = '#10b981' }) => {
const mountRef = useRef(null);
const materialsRef = useRef({});
const targetColorRef = useRef(new THREE.Color(accentColor));
useEffect(() => {
targetColorRef.current.set(accentColor);
}, [accentColor]);
useEffect(() => {
const mountNode = mountRef.current;
if (!mountNode) return;
// Scene Setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
75,
mountNode.clientWidth / mountNode.clientHeight,
0.1,
1000,
);
camera.position.z = 2.5;
const renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(mountNode.clientWidth, mountNode.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
mountNode.appendChild(renderer.domElement);
// --- Objects ---
// 1. Main Wireframe Globe
const geometry = new THREE.IcosahedronGeometry(1.2, 2);
const wireframeMaterial = new THREE.MeshBasicMaterial({
color: accentColor,
wireframe: true,
transparent: true,
opacity: 0.3,
});
const globe = new THREE.Mesh(geometry, wireframeMaterial);
scene.add(globe);
// 2. Inner Solid Core
const coreGeometry = new THREE.IcosahedronGeometry(1.19, 2);
const coreMaterial = new THREE.MeshBasicMaterial({
color: 0x050505,
});
const core = new THREE.Mesh(coreGeometry, coreMaterial);
scene.add(core);
// 3. Outer Rotating Ring
const ringGeometry = new THREE.TorusGeometry(1.6, 0.02, 16, 100);
const ringMaterial = new THREE.MeshBasicMaterial({
color: accentColor,
transparent: true,
opacity: 0.2,
});
const ring = new THREE.Mesh(ringGeometry, ringMaterial);
ring.rotation.x = Math.PI / 2;
scene.add(ring);
// 4. Particle Field
const particlesGeometry = new THREE.BufferGeometry();
const particlesCount = 500;
const posArray = new Float32Array(particlesCount * 3);
for (let i = 0; i < particlesCount * 3; i++) {
posArray[i] = (Math.random() - 0.5) * 10;
}
particlesGeometry.setAttribute(
'position',
new THREE.BufferAttribute(posArray, 3),
);
const particlesMaterial = new THREE.PointsMaterial({
size: 0.02,
color: accentColor,
transparent: true,
opacity: 0.4,
});
const particlesMesh = new THREE.Points(
particlesGeometry,
particlesMaterial,
);
scene.add(particlesMesh);
// Store refs for animation
materialsRef.current = {
wireframe: wireframeMaterial,
ring: ringMaterial,
particles: particlesMaterial,
};
// Animation Loop
const animate = () => {
requestAnimationFrame(animate);
// Color Interpolation
const lerpSpeed = 0.05;
wireframeMaterial.color.lerp(targetColorRef.current, lerpSpeed);
ringMaterial.color.lerp(targetColorRef.current, lerpSpeed);
particlesMaterial.color.lerp(targetColorRef.current, lerpSpeed);
// Rotate Globe
globe.rotation.y += 0.002;
// Rotate Ring
ring.rotation.z -= 0.005;
ring.rotation.x = Math.PI / 2 + Math.sin(Date.now() * 0.001) * 0.1;
// Rotate Particles
particlesMesh.rotation.y += 0.0005;
renderer.render(scene, camera);
};
animate();
// Handle Resize
const handleResize = () => {
if (!mountNode) return;
const width = mountNode.clientWidth;
const height = mountNode.clientHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
};
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
if (mountNode && mountNode.contains(renderer.domElement)) {
mountNode.removeChild(renderer.domElement);
}
geometry.dispose();
wireframeMaterial.dispose();
coreGeometry.dispose();
coreMaterial.dispose();
ringGeometry.dispose();
ringMaterial.dispose();
particlesGeometry.dispose();
particlesMaterial.dispose();
renderer.dispose();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []); // Re-run if mount changes (unlikely), but NOT on accentColor change.
return <div ref={mountRef} className={className} />;
};
export default TacticalGlobe;