amazon-nova-experimental-chat-05-14 LMArena Human Preference 140K
Give me the full code
- User
Give me the full code
- Assistant
Certainly! Below is the full updated HTML code with the fix applied. This version handles both indexed and non-indexed geometries, ensuring it works correctly with
IcosahedronGeometry.Save this as an HTML file (e.g.,
icosphere-with-cubes-and-graph.html) and open it in your browser.<!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 Cubes and 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; function init() { // Create the scene scene = new THREE.Scene(); scene.background = new THREE.Color(0x333333); // Create the camera camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; // 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; // smooth camera movement // Group to hold sphere, cubes, and lines (for collective rotation) group = new THREE.Group(); scene.add(group); // --- 2. Create the Icosahedral Sphere --- const radius = 2; const detail = 3; // Higher = smoother sphere let geometry = new THREE.IcosahedronGeometry(radius, detail); geometry = THREE.BufferGeometryUtils.mergeVertices(geometry); // Ensures index exists // Material with flatShading and wireframe to show triangles const material = new THREE.MeshStandardMaterial({ color: 0x1E90FF, // Dodger Blue flatShading: true, wireframe: true // Comment out to hide wireframe }); icoSphere = new THREE.Mesh(geometry, material); group.add(icoSphere); // --- 3. Place Small Cubes on Random Triangles --- const numCubes = 20; // Number of random cubes const cubeSize = 0.1; const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 }); // Red cubes // Get geometry data const positionAttr = geometry.attributes.position; let vertices = []; if (geometry.index) { // Indexed geometry const index = geometry.index; for (let i = 0; i < index.count; i += 3) { const a = index.getX(i); const b = index.getY(i + 1); const c = index.getZ(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); vertices.push([v1, v2, v3]); } } else { // Non-indexed geometry for (let i = 0; i < positionAttr.count; i += 3) { const v1 = new THREE.Vector3().fromBufferAttribute(positionAttr, i); const v2 = new THREE.Vector3().fromBufferAttribute(positionAttr, i + 1); const v3 = new THREE.Vector3().fromBufferAttribute(positionAttr, i + 2); vertices.push([v1, v2, v3]); } } const cubePositions = []; // Store positions for connecting later const numFaces = vertices.length; const selectedFaces = new Set(); while (selectedFaces.size < numCubes) { selectedFaces.add(Math.floor(Math.random() * numFaces)); } selectedFaces.forEach(faceIndex => { const [v1, v2, v3] = vertices[faceIndex]; // Compute centroid const centroid = v1.clone().add(v2).add(v3).divideScalar(3); // Normalize and scale to surface const normal = centroid.clone().normalize(); const posOnSphere = normal.clone().multiplyScalar(radius); // Create cube const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize); const cube = new THREE.Mesh(cubeGeo, cubeMaterial); cube.position.copy(posOnSphere); // Orient cube to face outward cube.lookAt(posOnSphere.clone().multiplyScalar(2)); // Point away from origin // Shift slightly outward to sit on surface cube.position.add(normal.clone().multiplyScalar(cubeSize / 2)); group.add(cube); // Store adjusted position for connections cubePositions.push(cube.position.clone()); }); // --- 4. Connect Cubes with Lines (Graph around the Sphere) --- // Sort positions by spherical theta (longitude) for a loop around the sphere cubePositions.sort((a, b) => { const sphA = new THREE.Spherical().setFromVector3(a); const sphB = new THREE.Spherical().setFromVector3(b); return sphA.theta - sphB.theta; }); // Offset lines slightly outward to avoid z-fighting const lineOffset = 0.05; const offsetPositions = cubePositions.map(pos => pos.clone().normalize().multiplyScalar(radius + lineOffset)); // Create line geometry const lineGeo = new THREE.BufferGeometry().setFromPoints(offsetPositions); const lineMat = new THREE.LineBasicMaterial({ color: 0xffff00 }); // Yellow lines const graphLine = new THREE.LineLoop(lineGeo, lineMat); // Closed loop group.add(graphLine); // --- 5. 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); // Handle window resizing window.addEventListener('resize', onWindowResize, false); } // --- 6. Animation Loop --- function animate() { requestAnimationFrame(animate); // Slowly rotate the entire group group.rotation.x += 0.001; 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>Explanation of the Fix
-
Geometry Handling:
- The geometry is created using
IcosahedronGeometry. BufferGeometryUtils.mergeVertices(geometry)ensures the geometry has an index buffer.
- The geometry is created using
-
Vertex Extraction:
- Checks if the geometry has an index buffer.
- If it does, it uses the index to get face vertices.
- If not, it assumes the vertices are in groups of three in the position attribute.
-
Cube Placement:
- Calculates the centroid of each selected face.
- Places a cube at the centroid, oriented outward, and slightly offset to sit on the sphere's surface.
-
Line Connection:
- Sorts cube positions by their spherical theta (longitude) to create a loop around the sphere.
- Connects the cubes with yellow lines, offset slightly to avoid z-fighting.
Result
- A blue icosahedral sphere with wireframe triangles.
- 20 small red cubes randomly placed on the sphere's surface.
- Yellow lines connecting the cubes in a loop around the sphere.
- The entire scene is interactive and slowly rotates.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.