Here is the fully updated and working HTML code that places cubes on the surface of an icosahedral sphere, with each cube aligned to the face normal, making them appear to "stand" upright on the surface.
✅ Features:
Icosahedral Sphere (THREE.IcosahedronGeometry)
Cubes randomly placed on triangle faces
Each cube is oriented to align with the face normal
Lines connect nearby cubes to form a dynamic graph (not circular)
Smooth lighting and interactivity with OrbitControls
📄 Full HTML Code
Save this as icosphere-cubes-with-normals.html and open in a browser.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Three.js Icosphere with Normal-Aligned Cubes and Graph</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Three.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- OrbitControls -->
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
let scene, camera, renderer, controls;
const cubes = [];
const lines = [];
init();
animate();
function init() {
// Scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x111111);
// Camera
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 8;
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// Sphere
const radius = 3;
const detail = 2;
const geometry = new THREE.IcosahedronGeometry(radius, detail);
const sphereMaterial = new THREE.MeshStandardMaterial({
color: 0x4444ff,
wireframe: false,
flatShading: true
});
const sphere = new THREE.Mesh(geometry, sphereMaterial);
scene.add(sphere);
// Add cubes and lines
addCubesAndLines(geometry, radius);
// Lighting
scene.add(new THREE.AmbientLight(0x404040, 1.5));
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 5, 5);
scene.add(directionalLight);
window.addEventListener("resize", onWindowResize, false);
}
function addCubesAndLines(geometry, radius) {
const positionAttr = geometry.attributes.position;
const index = geometry.index;
const faceCenters = [];
// Step 1: Place cubes at face centers with proper orientation
for (let i = 0; i < index.count; i += 3) {
const a = index.getX(i);
const b = index.getX(i + 1);
const c = index.getX(i + 2);
const vA = new THREE.Vector3().fromBufferAttribute(positionAttr, a);
const vB = new THREE.Vector3().fromBufferAttribute(positionAttr, b);
const vC = new THREE.Vector3().fromBufferAttribute(positionAttr, c);
// Compute face normal using cross product of two edges
const edge1 = new THREE.Vector3().subVectors(vB, vA);
const edge2 = new THREE.Vector3().subVectors(vC, vA);
const normal = new THREE.Vector3().crossVectors(edge1, edge2).normalize();
// Compute face centroid and project to sphere surface
const centroid = new THREE.Vector3().addVectors(vA, vB).add(vC).divideScalar(3);
centroid.normalize().multiplyScalar(radius);
// Final cube position (slightly outside the sphere)
const cubeOffset = 0.1;
const cubePosition = centroid.clone().add(normal.clone().multiplyScalar(cubeOffset));
// Create cube
const cubeSize = 0.1;
const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const cubeMat = new THREE.MeshStandardMaterial({ color: 0xffcc00 });
const cube = new THREE.Mesh(cubeGeo, cubeMat);
cube.position.copy(cubePosition);
// Orient cube to align with face normal (Y-axis)
const up = new THREE.Vector3(0, 1, 0);
const quat = new THREE.Quaternion().setFromUnitVectors(up, normal);
cube.quaternion.copy(quat);
scene.add(cube);
cubes.push(cube);
faceCenters.push(cubePosition);
}
// Step 2: Randomly select cubes to keep
const numCubesToKeep = 20;
const randomCubes = [];
while (randomCubes.length < numCubesToKeep) {
const index = Math.floor(Math.random() * cubes.length);
if (!randomCubes.includes(index)) randomCubes.push(index);
}
// Keep only selected cubes
const keptPositions = [];
cubes.forEach((cube, i) => {
if (!randomCubes.includes(i)) {
scene.remove(cube);
} else {
keptPositions.push(cube.position);
}
});
// Step 3: Connect nearby cubes with lines
const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffff00 });
const maxDistance = 2.5;
for (let i = 0; i < keptPositions.length; i++) {
for (let j = i + 1; j < keptPositions.length; j++) {
const dist = keptPositions[i].distanceTo(keptPositions[j]);
if (dist < maxDistance) {
const points = [keptPositions[i], keptPositions[j]];
const lineGeo = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(lineGeo, lineMaterial);
scene.add(line);
lines.push(line);
}
}
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
cubes.forEach(cube => {
cube.rotation.y += 0.01;
});
controls.update();
renderer.render(scene, camera);
}
</script>
</body>
</html>
🔍 What This Code Does
Cube Orientation: Each cube is rotated so its local Y-axis aligns with the triangle’s face normal, making it "stand" on the surface.
Normal Calculation: Uses the cross product of triangle edges to get the correct face normal.
Cube Positioning: The cube is placed slightly outside the sphere along the face normal to simulate standing on the surface.
Graph Connection: Lines connect nearby cubes based on distance, forming a non-circular, organic network.
🧪 Try It Out
Save the file as icosphere-cubes-with-normals.html
Open it in your browser
Use mouse to rotate the view and see the cubes standing on the surface
🛠️ Customization Ideas
Change numCubesToKeep to increase/decrease the number of cubes
Adjust maxDistance to control how many lines are drawn
Replace cubes with spheres or custom shapes
Add labels or highlight selected cubes
Use shaders for dynamic effects
Let me know if you'd like to animate the graph, add interactivity (like clicking cubes), or export the graph structure!