// Variables for Three.js
let scene, camera, renderer;
let controls;
let profiles = [];
// Initialize the 3D scene
function init() {
// Scene Setup
scene = new THREE.Scene();
// Camera Setup
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 5;
// Renderer Setup
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('three-container').appendChild(renderer.domElement);
// Add Light
const ambientLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(ambientLight);
// Load Textures and Materials
const textureLoader = new THREE.TextureLoader();
const leaderTexture = textureLoader.load('leader.jpg'); // Placeholder image
const comradeTexture = textureLoader.load('comrade.jpg'); // Placeholder image
// Create Leader Sphere
const leaderGeometry = new THREE.SphereGeometry(1, 32, 32);
const leaderMaterial = new THREE.MeshStandardMaterial({
map: leaderTexture,
transparent: true,
opacity: 0.9,
});
const leaderSphere = new THREE.Mesh(leaderGeometry, leaderMaterial);
leaderSphere.position.set(0, 0, 0);
scene.add(leaderSphere);
// Store profiles
profiles.push({
mesh: leaderSphere,
name: 'Leader Name',
bio: 'Short bio of the leader.',
image: 'leader.jpg',
});
// Create Comrades Spheres
const comradeGeometry = new THREE.SphereGeometry(0.5, 32, 32);
const comradeMaterial = new THREE.MeshStandardMaterial({
map: comradeTexture,
transparent: true,
opacity: 0.8,
});
const radius = 3;
const totalComrades = 14;
for (let i = 0; i < totalComrades; i++) {
const theta = (i / totalComrades) * Math.PI * 2;
const x = radius * Math.cos(theta);
const y = radius * Math.sin(theta);
const comradeSphere = new THREE.Mesh(comradeGeometry, comradeMaterial);
comradeSphere.position.set(x, y, 0);
scene.add(comradeSphere);
profiles.push({
mesh: comradeSphere,
name: `Comrade ${i + 1}`,
bio: `Short bio of comrade ${i + 1}.`,
image: 'comrade.jpg',
});
}
// Add Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableZoom = true;
controls.enablePan = false;
// Add Event Listener for Mouse Click
window.addEventListener('click', onMouseClick, false);
animate();
}
// Animation Loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
// Mouse Click Handler
function onMouseClick(event) {
event.preventDefault();
// Calculate mouse position in normalized device coordinates
const mouse = {
x: (event.clientX / window.innerWidth) * 2 - 1,
y: - (event.clientY / window.innerHeight) * 2 + 1
};
// Raycaster for detecting intersections
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(mouse, camera);
// Objects to check (meshes from profiles)
const objects = profiles.map(profile => profile.mesh);
const intersects = raycaster.intersectObjects(objects);
if (intersects.length > 0) {
const clickedMesh = intersects[0].object;
const profile = profiles.find(p => p.mesh === clickedMesh);
showBio(profile);
}
}
// Show Bio Function
function showBio(profile) {
const bioSection = document.getElementById('bio-section');
bioSection.innerHTML = `
<div class="row align-items-center">
<div class="col-md-2">
<div class="profile-pic">
<img src="${profile.image}" alt="${profile.name}" />
</div>
</div>
<div class="col-md-10">
<h2>${profile.name}</h2>
<p>${profile.bio}</p>
</div>
</div>
`;
bioSection.style.display = 'block';
}
// Resize Handler
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Initialize the Scene
init();