Generate an interactive particle simulator that renders different materials in 3d space. Make it customizable and have realistic physics. three.js and HTML, and leave it in a single HTML file I can copy and paste
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive Particle Simulator</title>
<style>
body { margin: 0; font-family: Arial, sans-serif; }
canvas { display: block; }
#controls {
position: absolute;
top: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.8);
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="controls">
<label for="gravity">Gravity Strength: </label>
<input type="range" id="gravity" min="0" max="10" value="1" step="0.1"><br>
<label for="numParticles">Number of Particles: </label>
<input type="number" id="numParticles" min="100" max="10000" value="1000"><br>
<label for="materialType">Material Type: </label>
<select id="materialType">
<option value="basic">Basic (Red)</option>
<option value="phong">Phong (Green, Shiny)</option>
<option value="standard">Standard (Blue, Metallic)</option>
</select><br>
<button id="regenerate">Regenerate Particles</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
// Set up the scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Smooth controls
controls.dampingFactor = 0.05;
let particlesArray = []; // Array to store Points objects
// Function to create particles with a given material and number
function createParticles(num, material) {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(num * 3); // x, y, z
const velocities = new Float32Array(num * 3); // vx, vy, vz
for(let i = 0; i < num; i++) {
positions[i * 3] = Math.random() * 100 - 50; // Random x
positions[i * 3 + 1] = Math.random() * 100 - 50; // Random y
positions[i * 3 + 2] = Math.random() * 100 - 50; // Random z
velocities[i * 3] = Math.random() * 2 - 1; // Random vx
velocities[i * 3 + 1] = Math.random() * 2 - 1; // Random vy
velocities[i * 3 + 2] = Math.random() * 2 - 1; // Random vz
}
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const points = new THREE.Points(geometry, material);
points.userData.velocities = velocities; // Store velocities for physics updates
scene.add(points);
particlesArray.push(points);
}
// Function to generate materials based on type
function getMaterial(type) {
switch(type) {
case 'basic':
return new THREE.PointsMaterial({ color: 0xff0000, size: 1 }); // Red, basic
case 'phong':
return new THREE.MeshPhongMaterial({ color: 0x00ff00, shininess: 100, size: 1.5 }); // Green, shiny
case 'standard':
return new THREE.MeshStandardMaterial({ color: 0x0000ff, metalness: 0.8, roughness: 0.2, size: 2 }); // Blue, metallic
default:
return new THREE.PointsMaterial({ color: 0xffffff, size: 1 }); // Default white
}
}
// Initial particle generation
function initializeParticles() {
const num = parseInt(document.getElementById('numParticles').value);
const materialType = document.getElementById('materialType').value;
const material = getMaterial(materialType);
// Clear existing particles
particlesArray.forEach(points => scene.remove(points));
particlesArray = [];
// Create two groups: one with the selected material, and one with a default for variety
createParticles(num / 2, material); // First half with selected material
createParticles(num / 2, getMaterial('basic')); // Second half with basic material for contrast
}
initializeParticles(); // Run on load
// Add event listener for regenerate button
document.getElementById('regenerate').addEventListener('click', initializeParticles);
// Animation loop with physics
function animate() {
requestAnimationFrame(animate);
const gravityValue = parseFloat(document.getElementById('gravity').value); // Get gravity from slider
particlesArray.forEach(points => {
const positions = points.geometry.attributes.position.array;
const velocities = points.userData.velocities;
for(let i = 0; i < positions.length; i += 3) {
// Apply gravity (only to y-velocity for downward force)
velocities[i + 1] -= gravityValue * 0.01; // Simple acceleration
// Update positions based on velocities
positions[i] += velocities[i]; // x += vx
positions[i + 1] += velocities[i + 1]; // y += vy
positions[i + 2] += velocities[i + 2]; // z += vz
// Simple boundary collisions (cube boundaries)
if(positions[i + 1] < -50) { // Bottom boundary
positions[i + 1] = -50;
velocities[i + 1] *= -0.8; // Bounce with damping
} else if(positions[i + 1] > 50) { // Top boundary
positions[i + 1] = 50;
velocities[i + 1] *= -0.8;
}
if(positions[i] < -50) { // Left boundary
positions[i] = -50;
velocities[i] *= -0.8;
} else if(positions[i] > 50) { // Right boundary
positions[i] = 50;
velocities[i] *= -0.8;
}
if(positions[i + 2] < -50) { // Far boundary
positions[i + 2] = -50;
velocities[i + 2] *= -0.8;
} else if(positions[i + 2] > 50) { // Near boundary
positions[i + 2] = 50;
velocities[i + 2] *= -0.8;
}
}
points.geometry.attributes.position.needsUpdate = true; // Mark for update
});
controls.update(); // Update controls
renderer.render(scene, camera); // Render the scene
}
animate(); // Start animation
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
This HTML file contains a complete interactive particle simulator using Three.js. Here's a quick overview:
Rendering Different Materials: Particles are rendered using different materials (e.g., Basic, Phong, Standard). You can select a material type via the dropdown, and it will generate particles with that material for one group, while the other group uses a basic material for variety.
Customizable: Use the sliders and inputs to adjust gravity strength and the number of particles. The "Regenerate" button recreates the particles with the new settings.
Realistic Physics: Basic Newtonian physics is implemented, including velocity updates, gravity, and elastic collisions with boundaries (with some damping for realism).
Interactivity: OrbitControls allow you to rotate, zoom, and pan the 3D view.
Single File: Everything is included in this one HTML file, with Three.js loaded from a CDN.
You can copy and paste this directly into an HTML file and open it in a browser. Note that performance may vary based on the number of particles, so keep it under 10,000 for smooth operation on most devices.