Give me the full code
- User
Give me the full code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Give me the full code
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is the full, corrected, and working HTML code for your Three.js project. This code creates an icosahedral sphere, places cubes at the center of each triangle face, and connects neighboring cubes with lines to form a graph-like structure on the surface of the sphere.
THREE.IcosahedronGeometry to create a sphere-like shape.geometry.index might be null (e.g., when detail = 0).icosphere_graph.html.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Icosphere with Cubes and Graph Lines</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Three.js CDN -->
<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 setup
scene = new THREE.Scene();
scene.background = new THREE.Color(0x111111);
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;
// Create Icosahedron Sphere Geometry
const radius = 3;
const detail = 2; // Try 0, 1, 2, etc. (Note: detail = 0 may not work due to lack of index)
const geometry = new THREE.IcosahedronGeometry(radius, detail);
// Material for the main sphere
const material = new THREE.MeshStandardMaterial({
color: 0x4444ff, // Blue sphere
wireframe: false,
flatShading: true,
});
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
// Add cubes at face centers and connect them with lines
addCubesAndLines(geometry);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 1.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 5, 5);
scene.add(directionalLight);
window.addEventListener("resize", onWindowResize, false);
}
function addCubesAndLines(geometry) {
const positionAttr = geometry.attributes.position;
const index = geometry.index; // May be undefined if detail === 0
const faceCenters = [];
// Step 1: Compute face centers
if (index !== null && index !== undefined) {
// Indexed geometry (detail >= 1)
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);
const center = new THREE.Vector3()
.add(vA)
.add(vB)
.add(vC)
.divideScalar(3);
center.normalize().multiplyScalar(3.2); // Slightly outside the sphere
faceCenters.push(center.clone());
// Add cube
const cubeSize = 0.1;
const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const cubeMat = new THREE.MeshStandardMaterial({ color: 0xffcc00 }); // Yellow cubes
const cube = new THREE.Mesh(cubeGeo, cubeMat);
cube.position.copy(center);
// Orient cube outward
const normal = new THREE.Vector3()
.subVectors(center, new THREE.Vector3())
.normalize();
const orientation = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
normal
);
cube.quaternion.copy(orientation);
scene.add(cube);
cubes.push(cube);
}
} else {
// Non-indexed geometry (detail === 0)
console.warn("Geometry is not indexed. Cannot compute face centers. Try increasing 'detail' to 1 or higher.");
return;
}
// Step 2: Build adjacency map to find neighbors
const vertexToFaceMap = {};
const faceEdges = [];
if (index !== null && index !== undefined) {
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 edges = [
[Math.min(a, b), Math.max(a, b)],
[Math.min(b, c), Math.max(b, c)],
[Math.min(c, a), Math.max(c, a)],
];
edges.forEach(edge => {
const key = edge.join(",");
if (!vertexToFaceMap[key]) vertexToFaceMap[key] = [];
vertexToFaceMap[key].push(Math.floor(i / 3));
});
faceEdges.push(edges.map(e => e.join(",")));
}
}
// Step 3: Draw lines between adjacent face centers
const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffff00 }); // Yellow lines
for (const faceIndex in vertexToFaceMap) {
const connectedFaces = vertexToFaceMap[faceIndex];
for (let i = 0; i < connectedFaces.length - 1; i++) {
for (let j = i + 1; j < connectedFaces.length; j++) {
const f1 = connectedFaces[i];
const f2 = connectedFaces[j];
// Avoid duplicate lines
const key = [f1, f2].sort().join(",");
if (!lines.includes(key)) {
const from = faceCenters[f1];
const to = faceCenters[f2];
const points = [from, to];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, lineMaterial);
scene.add(line);
lines.push(key);
}
}
}
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
cubes.forEach((cube, i) => {
cube.rotation.y += 0.01;
cube.rotation.x += 0.005;
});
controls.update();
renderer.render(scene, camera);
}
</script>
</body>
</html>
Here are some ways you can modify the code to suit your needs:
Change the Sphere's Detail Level:
detail variable in const detail = 2; to increase or decrease the number of triangles on the sphere. Higher values (e.g., 3, 4) make the sphere smoother but increase the number of cubes and lines.Change Colors:
color: 0x4444ff in the sphere's material.color: 0xffcc00 in the cube material.color: 0xffff00 in the line material.Change Cube Size:
cubeSize variable in const cubeSize = 0.1; to make the cubes larger or smaller.Remove Cube Rotation:
animate function:
cubes.forEach((cube, i) => {
cube.rotation.y += 0.01;
cube.rotation.x += 0.005;
});
Add Interactivity:
Use a Different Geometry:
IcosahedronGeometry with another geometry, like SphereGeometry, if you want a different base shape. Note that the face-center logic will need adjustment for non-triangular faces.detail = 0, the geometry will not have an index attribute, and the code will log a warning. To avoid this, use detail >= 1.r128) for compatibility. If you want to use a newer version, update the CDN links and check for any API changes.The code includes error handling to prevent crashes when geometry.index is null or undefined. If you encounter any issues, check the browser console for warnings or errors, and let me know so I can help debug further.
Let me know if you need additional features, modifications, or help with debugging!