-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
216 lines (174 loc) · 6.1 KB
/
main.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
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { convertLatLonToVec3 } from "./src/utils";
import peopleData from "./src/peopleData.json";
import gsap from "gsap";
import "./style.css";
function init() {
const app = document.getElementById("app");
const sphereRadius = 4.5;
let isGlobRotating = true;
// create scene, camera, renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
100
);
camera.position.set(0, 0, 10);
let sizes = { width: window.innerWidth, height: window.innerHeight };
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.useLegacyLights = true;
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
app.appendChild(renderer.domElement);
/* Orbit Control */
const orbitControl = new OrbitControls(camera, renderer.domElement);
orbitControl.minDistance = 7.5;
orbitControl.maxDistance = 35;
orbitControl.enablePan = false;
orbitControl.enableZoom = false;
orbitControl.enableDamping = true;
orbitControl.dampingFactor = 0.05;
orbitControl.rotateSpeed = 0.8;
/* Textures */
const textureLoader = new THREE.TextureLoader();
const earthTexture = textureLoader.load("/images/2k_earth_nightmap.jpeg");
const earthBumpMap = textureLoader.load("/images/8081-earthbump2k.jpeg");
const earthCloudsMap = textureLoader.load("/images/2k_earth_clouds.png");
/* Light */
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 2);
directionalLight.position.set(-1, 1, 1);
scene.add(directionalLight);
/* Globe */
const earthGroup = new THREE.Group();
scene.add(earthGroup);
const earthGeoMetry = new THREE.SphereGeometry(sphereRadius, 64, 64);
const earthMaterial = new THREE.MeshPhongMaterial({
map: earthTexture,
bumpMap: earthBumpMap,
bumpScale: 0.2
});
const earthMesh = new THREE.Mesh(earthGeoMetry, earthMaterial);
earthGroup.add(earthMesh);
const earthCloudMesh = new THREE.Mesh(
new THREE.SphereGeometry(sphereRadius + 0.003, 64, 64),
new THREE.MeshPhongMaterial({
map: earthCloudsMap,
transparent: true,
opacity: 0.2
})
);
earthGroup.add(earthCloudMesh);
// Earth Glow Shader
const earthGlowGeometry = new THREE.SphereGeometry(sphereRadius + 0.65),
earthGlowMaterial = new THREE.ShaderMaterial({
uniforms: {},
vertexShader: [
"varying vec3 vNormal;",
"void main() {",
"vNormal = normalize( normalMatrix * normal );",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"varying vec3 vNormal;",
"void main() {",
"float intensity = pow( 0.99 - dot( vNormal, vec3( 0, 0, 1.0 ) ), 6.0 );",
"gl_FragColor = vec4( .28, .48, 1.0, 0.4 ) * intensity;",
"}"
].join("\n"),
side: THREE.BackSide,
blending: THREE.AdditiveBlending,
transparent: true,
depthWrite: false
});
const glowingEarth = new THREE.Mesh(earthGlowGeometry, earthGlowMaterial);
glowingEarth.renderOrder = -1;
earthGroup.add(glowingEarth);
/* People Images */
const peopleGroup = new THREE.Group();
earthGroup.add(peopleGroup);
// Add people data to the globe
peopleData.forEach((person, index) => {
const peopleMaterial = new THREE.SpriteMaterial({
map: textureLoader.load(`/images/${person.img}`),
side: THREE.DoubleSide
});
const peopleMesh = new THREE.Sprite(peopleMaterial);
peopleMesh.position.copy(
convertLatLonToVec3(person.lat, person.lon).multiplyScalar(
sphereRadius + 0.65
)
);
peopleMesh.userData.id = index; // Add ID to identify person
peopleGroup.add(peopleMesh);
});
earthGroup.rotation.y = 4;
/* Utility Function: Animate and Rotate to Focus */
function focusOnPerson(personMesh) {
// Animate scale-up using GSAP
gsap.to(personMesh.scale, {
x: 1.5,
y: 1.5,
z: 1.5,
duration: 0.5,
ease: "power2.out"
});
// Calculate rotation to bring person in front
const targetPosition = personMesh.position.clone().normalize();
const spherical = new THREE.Spherical().setFromVector3(targetPosition);
const targetRotationY = spherical.theta;
const targetRotationX = spherical.phi - Math.PI / 2;
// Animate globe rotation using GSAP
gsap.to(earthGroup.rotation, {
y: -targetRotationY,
x: -targetRotationX,
duration: 1.2,
ease: "power2.inOut"
});
}
// Raycaster for detecting clicks
const raycaster = new THREE.Raycaster();
const mousePos = new THREE.Vector2(0, 0);
renderer.domElement.addEventListener("click", (event) => {
mousePos.x = (event.clientX / window.innerWidth) * 2 - 1;
mousePos.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mousePos, camera);
const intersects = raycaster.intersectObjects(peopleGroup.children);
if (intersects.length > 0) {
isGlobRotating = false;
const selectedPerson = intersects[0].object;
// Reset scales for all people
peopleGroup.children.forEach((person) => {
gsap.to(person.scale, { x: 0.8, y: 0.8, z: 0.8, duration: 0.5 });
});
// Focus on the selected person
focusOnPerson(selectedPerson);
} else {
isGlobRotating = true;
}
});
// Handle window resize
window.addEventListener("resize", () => {
sizes = { width: window.innerWidth, height: window.innerHeight };
camera.aspect = sizes.width / sizes.height;
camera.updateProjectionMatrix();
renderer.setSize(sizes.width, sizes.height);
renderer.render(scene, camera);
});
// Animation loop
function tick() {
if (isGlobRotating) {
earthGroup.rotation.y += 0.0007; // Continuously rotate the globe
}
orbitControl.update();
renderer.render(scene, camera);
requestAnimationFrame(tick);
}
tick();
}
init();