deepseek-v3-0324· DeepSeek LMArena Human Preference 140K
Make it so that the player can't see a voxel if another voxel is blocking its view from…
- User
Make it so that the player can't see a voxel if another voxel is blocking its view from the player, so as a performance fix.
- Assistant
To optimize performance by only rendering voxels that are visible from the player's perspective (i.e., not occluded by other voxels), we can implement frustum culling and raycasting to check visibility. However, doing this perfectly in real-time for all voxels is computationally expensive. Instead, we'll use a simpler approximation:
Approach:
- Frustum Culling: Only render voxels that are within the camera's view.
- Distance-Based Culling: Only render voxels within a certain distance from the camera.
- Surface-Only Rendering: Only render voxels on the planet's surface (skip internal ones).
Here's the updated code with these optimizations:
<!DOCTYPE html> <html lang="en"> <head> <title>Optimized Voxel Planet Generator</title> <style> body { margin: 0; overflow: hidden; } canvas { width: 100%; height: 100%; } </style> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script> <script> let scene, camera, renderer; let planet; const planetRadius = 10; const noiseScale = 10; const resolutionStep = 0.5; const maxRenderDistance = 20; // Only render voxels within this distance from the camera // Store all voxels in a grid for visibility checks const voxelGrid = {}; function init() { scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // Lighting const light = new THREE.DirectionalLight(0xffffff, 1); light.position.set(50, 50, 50); scene.add(light); scene.add(new THREE.AmbientLight(0x404040)); camera.position.z = 30; generatePlanet(); addStars(); animate(); // Controls document.addEventListener('keydown', handleKeyDown); document.addEventListener('mousemove', handleMouseMove); } function generatePlanet() { planet = new THREE.Group(); const materials = { water: new THREE.MeshPhongMaterial({ color: 0x0077be }), sand: new THREE.MeshPhongMaterial({ color: 0xc2b280 }), grass: new THREE.MeshPhongMaterial({ color: 0x567d46 }) }; const voxelSize = resolutionStep; const voxelGeometry = new THREE.BoxGeometry(voxelSize, voxelSize, voxelSize); // First pass: Generate all voxels and store in grid for (let x = -planetRadius; x <= planetRadius; x += resolutionStep) { for (let y = -planetRadius; y <= planetRadius; y += resolutionStep) { for (let z = -planetRadius; z <= planetRadius; z += resolutionStep) { const position = new THREE.Vector3(x, y, z); if (position.length() <= planetRadius) { const noise = Math.abs(Math.sin(x / noiseScale) * Math.cos(z / noiseScale)); let materialType; if (noise < 0.3) materialType = 'water'; else if (noise < 0.6) materialType = 'sand'; else materialType = 'grass'; // Store voxel data in grid const key = `${x.toFixed(1)},${y.toFixed(1)},${z.toFixed(1)}`; voxelGrid[key] = { position, materialType }; } } } } // Second pass: Only add surface voxels to scene for (const key in voxelGrid) { const { position, materialType } = voxelGrid[key]; // Simple surface check: If any adjacent position is empty, it's a surface voxel const adjacentPositions = [ new THREE.Vector3(position.x + resolutionStep, position.y, position.z), new THREE.Vector3(position.x - resolutionStep, position.y, position.z), new THREE.Vector3(position.x, position.y + resolutionStep, position.z), new THREE.Vector3(position.x, position.y - resolutionStep, position.z), new THREE.Vector3(position.x, position.y, position.z + resolutionStep), new THREE.Vector3(position.x, position.y, position.z - resolutionStep) ]; let isSurface = false; for (const adjPos of adjacentPositions) { const adjKey = `${adjPos.x.toFixed(1)},${adjPos.y.toFixed(1)},${adjPos.z.toFixed(1)}`; if (!voxelGrid[adjKey] || adjPos.length() > planetRadius) { isSurface = true; break; } } if (isSurface) { const voxel = new THREE.Mesh(voxelGeometry, materials[materialType]); voxel.position.copy(position); planet.add(voxel); } } scene.add(planet); } function updateVisibleVoxels() { // Clear previous voxels while (planet.children.length > 0) { planet.remove(planet.children[0]); } // Add only voxels that are: // 1. Surface voxels // 2. Within maxRenderDistance from camera const voxelSize = resolutionStep; const voxelGeometry = new THREE.BoxGeometry(voxelSize, voxelSize, voxelSize); const materials = { water: new THREE.MeshPhongMaterial({ color: 0x0077be }), sand: new THREE.MeshPhongMaterial({ color: 0xc2b280 }), grass: new THREE.MeshPhongMaterial({ color: 0x567d46 }) }; for (const key in voxelGrid) { const { position, materialType } = voxelGrid[key]; // Distance check if (position.distanceTo(camera.position) > maxRenderDistance) continue; // Surface check (simplified) const adjacentPositions = [ new THREE.Vector3(position.x + resolutionStep, position.y, position.z), new THREE.Vector3(position.x - resolutionStep, position.y, position.z), new THREE.Vector3(position.x, position.y + resolutionStep, position.z), new THREE.Vector3(position.x, position.y - resolutionStep, position.z), new THREE.Vector3(position.x, position.y, position.z + resolutionStep), new THREE.Vector3(position.x, position.y, position.z - resolutionStep) ]; let isSurface = false; for (const adjPos of adjacentPositions) { const adjKey = `${adjPos.x.toFixed(1)},${adjPos.y.toFixed(1)},${adjPos.z.toFixed(1)}`; if (!voxelGrid[adjKey] || adjPos.length() > planetRadius) { isSurface = true; break; } } if (isSurface) { const voxel = new THREE.Mesh(voxelGeometry, materials[materialType]); voxel.position.copy(position); planet.add(voxel); } } } function addStars() { const starGeometry = new THREE.BufferGeometry(); const starMaterial = new THREE.PointsMaterial({ color: 0xFFFFFF, size: 0.1 }); const starVertices = []; for (let i = 0; i < 10000; i++) { starVertices.push( (Math.random() - 0.5) * 2000, (Math.random() - 0.5) * 2000, (Math.random() - 0.5) * 2000 ); } starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3)); const stars = new THREE.Points(starGeometry, starMaterial); scene.add(stars); } // Camera controls const moveSpeed = 0.5; const rotateSpeed = 0.01; function handleKeyDown(event) { switch(event.key) { case 'w': camera.position.z -= moveSpeed; break; case 's': camera.position.z += moveSpeed; break; case 'a': camera.position.x -= moveSpeed; break; case 'd': camera.position.x += moveSpeed; break; case ' ': camera.position.y += moveSpeed; break; case 'Shift': camera.position.y -= moveSpeed; break; } updateVisibleVoxels(); // Update visibility when camera moves } function handleMouseMove(event) { if (event.buttons === 1) { camera.rotation.y += event.movementX * rotateSpeed; camera.rotation.x += event.movementY * rotateSpeed; updateVisibleVoxels(); // Update visibility when camera rotates } } function animate() { requestAnimationFrame(animate); planet.rotation.y += 0.001;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.