I'll create a 3D quantum wave function simulation using Three.js that demonstrates wave function collapse. Here's the complete code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quantum Wave Function Collapse Simulation</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: #000;
font-family: Arial, sans-serif;
touch-action: none;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
z-index: 100;
font-size: 14px;
}
canvas {
display: block;
}
</style>
</head>
<body>
<div id="info">
Touch/Click to collapse the wave function!<br>
Quantum Wave Function Simulation
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
class QuantumWaveSimulation {
constructor() {
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
this.renderer = new THREE.WebGLRenderer({ antialias: true });
// Wave parameters
this.waveWidth = 100;
this.waveHeight = 100;
this.segments = 128;
this.time = 0;
this.collapseTime = 0;
this.isCollapsed = false;
this.collapseX = 0;
this.collapseZ = 0;
this.spreadWidth = 8.0; // Normal wave spread
this.collapsedWidth = 0.5; // Collapsed wave spread
this.currentWidth = this.spreadWidth;
this.init();
this.createWave();
this.setupLighting();
this.setupEventListeners();
this.animate();
}
init() {
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setClearColor(0x000011);
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(this.renderer.domElement);
this.camera.position.set(0, 30, 40);
this.camera.lookAt(0, 0, 0);
}
createWave() {
const geometry = new THREE.PlaneGeometry(
this.waveWidth,
this.waveHeight,
this.segments,
this.segments
);
// Create custom shader material for color mapping
const vertexShader = `
varying float vHeight;
varying vec3 vPosition;
void main() {
vPosition = position;
vHeight = position.y;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = `
varying float vHeight;
varying vec3 vPosition;
uniform float time;
void main() {
float normalizedHeight = (vHeight + 5.0) / 10.0;
normalizedHeight = clamp(normalizedHeight, 0.0, 1.0);
// Color transition: blue to cyan to yellow to red
vec3 color1 = vec3(0.1, 0.3, 1.0); // Deep blue
vec3 color2 = vec3(0.0, 1.0, 1.0); // Cyan
vec3 color3 = vec3(1.0, 1.0, 0.0); // Yellow
vec3 color4 = vec3(1.0, 0.5, 0.0); // Orange-red
vec3 finalColor;
if (normalizedHeight < 0.33) {
finalColor = mix(color1, color2, normalizedHeight * 3.0);
} else if (normalizedHeight < 0.66) {
finalColor = mix(color2, color3, (normalizedHeight - 0.33) * 3.0);
} else {
finalColor = mix(color3, color4, (normalizedHeight - 0.66) * 3.0);
}
// Add some glow effect
float glow = 1.0 + normalizedHeight * 0.5;
finalColor *= glow;
gl_FragColor = vec4(finalColor, 0.8 + normalizedHeight * 0.2);
}
`;
this.material = new THREE.ShaderMaterial({
vertexShader: vertexShader,
fragmentShader: fragmentShader,
uniforms: {
time: { value: 0 }
},
transparent: true,
side: THREE.DoubleSide
});
this.waveMesh = new THREE.Mesh(geometry, this.material);
this.waveMesh.rotation.x = -Math.PI / 2;
this.waveMesh.castShadow = true;
this.waveMesh.receiveShadow = true;
this.scene.add(this.waveMesh);
// Store original positions for animation
this.originalPositions = geometry.attributes.position.array.slice();
}
setupLighting() {
// Ambient light
const ambientLight = new THREE.AmbientLight(0x404040, 0.3);
this.scene.add(ambientLight);
// Directional light
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(50, 50, 25);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
this.scene.add(directionalLight);
// Point light for dramatic effect
const pointLight = new THREE.PointLight(0x4488ff, 1, 100);
pointLight.position.set(0, 20, 0);
this.scene.add(pointLight);
}
setupEventListeners() {
// Mouse and touch events
this.renderer.domElement.addEventListener('click', (event) => {
this.handleInteraction(event);
});
this.renderer.domElement.addEventListener('touchstart', (event) => {
event.preventDefault();
this.handleInteraction(event.touches[0]);
});
window.addEventListener('resize', () => {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
});
}
handleInteraction(event) {
// Convert screen coordinates to world coordinates
const rect = this.renderer.domElement.getBoundingClientRect();
const x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
const z = -((event.clientY - rect.top) / rect.height) * 2 + 1;
// Map to wave coordinates
this.collapseX = x * this.waveWidth / 2;
this.collapseZ = z * this.waveHeight / 2;
// Trigger collapse
this.isCollapsed = true;
this.collapseTime = 0;
}
updateWave() {
const positions = this.waveMesh.geometry.attributes.position.array;
const segmentsX = this.segments + 1;
const segmentsZ = this.segments + 1;
// Handle collapse animation
if (this.isCollapsed) {
this.collapseTime += 0.03;
if (this.collapseTime < 1.0) {
// Collapsing phase
this.currentWidth = this.spreadWidth * (1 - this.collapseTime) + this.collapsedWidth * this.collapseTime;
} else if (this.collapseTime < 3.0) {
// Collapsed state
this.currentWidth = this.collapsedWidth;
} else if (this.collapseTime < 4.0) {
// Expanding phase
const expandProgress = (this.collapseTime - 3.0);
this.currentWidth = this.collapsedWidth * (1 - expandProgress) + this.spreadWidth * expandProgress;
} else {
// Reset to normal evolution
this.isCollapsed = false;
this.currentWidth = this.spreadWidth;
}
}
// Update wave function
for (let i = 0; i < segmentsX; i++) {
for (let j = 0; j < segmentsZ; j++) {
const index = (i * segmentsZ + j) * 3;
const x = (i / (segmentsX - 1) - 0.5) * this.waveWidth;
const z = (j / (segmentsZ - 1) - 0.5) * this.waveHeight;
let y = 0;
if (this.isCollapsed && this.collapseTime < 3.0) {
// Collapsed wave function (narrow Gaussian)
const dx = x - this.collapseX;
const dz = z - this.collapseZ;
const r2 = dx * dx + dz * dz;
y = 8.0 * Math.exp(-r2 / (2 * this.currentWidth * this.currentWidth));
} else {
// Normal evolving wave function (multiple wave packets)
const centerX1 = 15 * Math.sin(this.time * 0.5);
const centerZ1 = 15 * Math.cos(this.time * 0.7);
const centerX2 = -10 * Math.cos(this.time * 0.3);
const centerZ2 = 10 * Math.sin(this.time * 0.4);
// First wave packet
const dx1 = x - centerX1;
const dz1 = z - centerZ1;
const r1_2 = dx1 * dx1 + dz1 * dz1;
y += 3.0 * Math.exp(-r1_2 / (2 * this.currentWidth * this.currentWidth)) *
Math.cos(0.3 * Math.sqrt(r1_2) - this.time * 2);
// Second wave packet
const dx2 = x - centerX2;
const dz2 = z - centerZ2;
const r2_2 = dx2 * dx2 + dz2 * dz2;
y += 2.0 * Math.exp(-r2_2 / (2 * this.currentWidth * this.currentWidth)) *
Math.cos(0.25 * Math.sqrt(r2_2) - this.time * 1.5 + Math.PI/3);
// Add some quantum interference
y += 0.5 * Math.sin(x * 0.1 + this.time) * Math.cos(z * 0.1 + this.time * 0.7);
}
positions[index + 1] = y;
}
}
this.waveMesh.geometry.attributes.position.needsUpdate = true;
this.waveMesh.geometry.computeVertexNormals();
}
animate() {
requestAnimationFrame(() => this.animate());
this.time += 0.016;
this.material.uniforms.time.value = this.time;
this.updateWave();
// Smooth camera movement
const radius = 45;
const cameraAngle = this.time * 0.2;
this.camera.position.x = radius * Math.sin(cameraAngle);
this.camera.position.z = radius * Math.cos(cameraAngle);
this.camera.position.y = 25 + 10 * Math.sin(this.time * 0.1);
this.camera.lookAt(0, 0, 0);
this.renderer.render(this.scene, this.camera);
}
}
// Start the simulation
window.addEventListener('load', () => {
new QuantumWaveSimulation();
});
</script>
</body>
</html>
This simulation creates a 3D quantum wave function visualization with the following features:
Key Features:
- 3D Wave Function: Uses a plane geometry with animated vertices to represent the quantum probability amplitude
- Wave Function Collapse: When you touch/click the screen, the wave collapses to a narrow peak at that location
- Color Mapping: Colors transition from blue (low amplitude) through cyan and yellow to orange-red (high amplitude)
- Smooth Animation: The wave smoothly evolves, collapses, and then spreads out again
- Mobile Responsive: Works on both desktop and mobile devices
- Quantum Interference: Shows realistic wave packet behavior with interference patterns
How it Works:
- Normal state shows evolving wave packets moving around
- Touch/click triggers collapse at the interaction point
- Wave becomes very narrow and peaks turn yellow/orange
- After a brief collapsed state, the wave smoothly expands back to normal evolution
- Camera smoothly orbits around the wave for better visualization
Save this as wave.html and open it in your browser. The simulation will work on both desktop (click) and mobile (touch) devices!