To create a non-circular graph that connects the cubes based on their actual positions rather than just a loop, we’ll modify the code to connect cubes that are spatially close to each other. This will form a more organic, web-like structure across the sphere's surface.
Here’s the updated code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js Icosahedral Sphere with Web Graph</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Import Three.js from a CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- Import OrbitControls for easy camera movement -->
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<!-- Import BufferGeometryUtils to ensure geometry has indices -->
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/utils/BufferGeometryUtils.js"></script>
<script>
// --- 1. Scene Setup ---
let scene, camera, renderer, icoSphere, controls, group;
const cubes = [];
const lines = [];
function init() {
// Create the scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x101010);
// Create the camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 8;
// Create the renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add OrbitControls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// Group to hold sphere, cubes, and lines
group = new THREE.Group();
scene.add(group);
// --- 2. Create the Icosahedral Sphere ---
const radius = 3;
const detail = 3; // Higher = smoother sphere
let geometry = new THREE.IcosahedronGeometry(radius, detail);
geometry = THREE.BufferGeometryUtils.mergeVertices(geometry); // Ensures index exists
const material = new THREE.MeshStandardMaterial({
color: 0x222244, // Dark blue
wireframe: true,
side: THREE.DoubleSide
});
icoSphere = new THREE.Mesh(geometry, material);
group.add(icoSphere);
// --- 3. Place Cubes on Random Triangles ---
const numCubes = 30;
const cubeSize = 0.1;
const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xff6600 }); // Orange cubes
const positionAttr = geometry.attributes.position;
const index = geometry.index;
const faceCount = index.count / 3;
const selectedFaces = new Set();
while (selectedFaces.size < numCubes) {
selectedFaces.add(Math.floor(Math.random() * faceCount));
}
selectedFaces.forEach(faceIndex => {
const i = faceIndex * 3;
const a = index.getX(i);
const b = index.getX(i + 1);
const c = index.getX(i + 2);
const v1 = new THREE.Vector3().fromBufferAttribute(positionAttr, a);
const v2 = new THREE.Vector3().fromBufferAttribute(positionAttr, b);
const v3 = new THREE.Vector3().fromBufferAttribute(positionAttr, c);
const centroid = v1.clone().add(v2).add(v3).divideScalar(3);
centroid.normalize().multiplyScalar(radius + cubeSize / 2); // Slightly outward
const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const cube = new THREE.Mesh(cubeGeo, cubeMaterial);
cube.position.copy(centroid);
group.add(cube);
cubes.push(cube);
});
// --- 4. Connect Cubes with Lines Based on Proximity ---
const maxDistance = radius * 0.8; // Maximum distance for connection
const lineMaterial = new THREE.LineBasicMaterial({ color: 0x00ffdd }); // Cyan lines
for (let i = 0; i < cubes.length; i++) {
for (let j = i + 1; j < cubes.length; j++) {
const dist = cubes[i].position.distanceTo(cubes[j].position);
if (dist < maxDistance) {
const points = [cubes[i].position, cubes[j].position];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, lineMaterial);
group.add(line);
lines.push(line);
}
}
}
// --- 5. Lighting ---
const ambientLight = new THREE.AmbientLight(0x222222, 1);
scene.add(ambientLight);
const pointLight = new THREE.PointLight(0xffffff, 1, 100);
pointLight.position.set(10, 10, 10);
scene.add(pointLight);
// Handle window resizing
window.addEventListener('resize', onWindowResize, false);
}
// --- 6. Animation Loop ---
function animate() {
requestAnimationFrame(animate);
// Slowly rotate the entire group
group.rotation.y += 0.002;
// Update controls
controls.update();
// Render the scene
renderer.render(scene, camera);
}
// Function to handle window resizing
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Start the application
init();
animate();
</script>
</body>
</html>
Key Changes for Non-Circular Graph:
-
Proximity-Based Connections:
- Cubes are connected with lines if their distance is less than
maxDistance (set to 80% of the sphere's radius).
- This creates a web-like structure where nearby cubes are linked.
-
Organic Structure:
- The graph adapts to the random positions of the cubes, forming a natural network instead of a forced circular path.
-
Visual Improvements:
- Added a point light for better illumination.
- Changed colors for better contrast.
How It Works:
- Cubes are placed at random triangle centroids on the sphere's surface.
- Lines are drawn between cubes that are within a certain distance threshold.
- The result is a dynamic, non-circular graph that follows the natural distribution of the cubes.
Let me know if you'd like further adjustments!