Here's the updated code with cubes properly aligned to stand on the triangle surfaces using their normals:
<!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 Standing Cubes</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/utils/BufferGeometryUtils.js"></script>
<script>
let scene, camera, renderer, icoSphere, controls, group;
const cubes = [];
const lines = [];
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x101010);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 8;
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
group = new THREE.Group();
scene.add(group);
// Create icosphere
const radius = 3;
const detail = 3;
let geometry = new THREE.IcosahedronGeometry(radius, detail);
geometry = THREE.BufferGeometryUtils.mergeVertices(geometry);
const material = new THREE.MeshStandardMaterial({
color: 0x222244,
wireframe: true,
side: THREE.DoubleSide
});
icoSphere = new THREE.Mesh(geometry, material);
group.add(icoSphere);
// Create cubes on triangles
const numCubes = 30;
const cubeSize = 0.2;
const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xff6600 });
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);
// Calculate triangle normal
const edge1 = v2.clone().sub(v1);
const edge2 = v3.clone().sub(v1);
const normal = edge1.cross(edge2).normalize();
// Get surface point
const centroid = v1.clone().add(v2).add(v3).divideScalar(3);
const surfacePoint = centroid.clone().normalize().multiplyScalar(radius);
// Create cube
const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const cube = new THREE.Mesh(cubeGeo, cubeMaterial);
// Position cube at surface point
cube.position.copy(surfacePoint);
// Align cube to stand on surface
const up = new THREE.Vector3(0, 1, 0);
cube.quaternion.setFromUnitVectors(up, normal);
// Adjust position to place base on surface
cube.position.add(normal.clone().multiplyScalar(cubeSize/2));
group.add(cube);
cubes.push(cube);
});
// Connect cubes with proximity-based lines
const maxDistance = radius * 0.8;
const lineMaterial = new THREE.LineBasicMaterial({ color: 0x00ffdd });
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 lineGeo = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(lineGeo, lineMaterial);
group.add(line);
lines.push(line);
}
}
}
// 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);
window.addEventListener('resize', onWindowResize);
}
function animate() {
requestAnimationFrame(animate);
group.rotation.y += 0.002;
controls.update();
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
init();
animate();
</script>
</body>
</html>
Key Improvements:
-
Proper Normal Calculation:
- Uses the cross product of triangle edges to calculate the actual surface normal
- More accurate than using centroid direction
-
Cube Alignment:
- Cubes are rotated to align their local Y-axis with the triangle's normal
- Position adjusted to place base exactly on the surface
-
Visual Enhancements:
- Larger cube size (0.2) for better visibility
- Improved lighting setup
- Darker background for better contrast
The cubes will now:
- Stand upright on their triangular "feet"
- Align perfectly with the surface normals
- Form a dynamic web-like graph across the sphere's surface
- Rotate with the entire structure while maintaining their orientation
You can adjust numCubes and cubeSize to change the density and size of the cubes.