Make a 3D visual simulation wave function of schirondinger equation that show the…
- User
Make a 3D visual simulation wave function of schirondinger equation that show the colapse of the wave function when I touch my mobile screen (peak more high and narrowed with color gradually turn from blue to yellow), after that the wave will envolving smooth, make it using tree.js or better mode aproach. Put all code in wave.html for I will run on my Brouser. After screen touch the peak will return smoothing spreaded again. And surface plane will oscillating low smoothing again.
- Assistant
Okay, this is a fascinating and complex request! Simulating a "true" Schrödinger wave function evolution is computationally very intensive. What we can do is create a visual representation that mimics the described behavior: a 3D wave that localizes (peaks and narrows) on touch, changes color, and then smoothly spreads out again, with an oscillating underlying surface.
We'll use Three.js for this. The core idea is:
- A
PlaneGeometryfor the wave function, with its vertices manipulated in thezdirection. - Vertex colors for the blue-to-yellow transition.
- Raycasting to detect touch point on the wave.
- State variables to manage collapse, spreading, and normal evolution.
- A Gaussian function to model the peak during collapse/spread.
- Another
PlaneGeometryfor the oscillating surface below.
Here's the
wave.htmlfile. This is quite a bit of code, so I've added comments to explain each part.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Wave Function Collapse Simulation</title> <style> body { margin: 0; overflow: hidden; background-color: #000; } canvas { display: block; } #info { position: absolute; top: 10px; width: 100%; text-align: center; color: white; font-family: Arial, sans-serif; font-size: 16px; } </style> </head> <body> <div id="info">Touch / Click the wave to see it collapse</div> <script type="importmap"> { "imports": { "three": "https://unpkg.com/three@0.160.0/build/three.module.js", "three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/" } } </script> <script type="module"> import * as THREE from 'three'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; let scene, camera, renderer, clock; let waveMesh, surfaceMesh; let waveGeometry, surfaceGeometry; const waveSegments = 64; // More segments = smoother wave, but more computation const waveSize = 10; // Wave state let isCollapsing = false; let isSpreading = false; let collapseProgress = 0; // 0 to 1 let spreadProgress = 0; // 0 to 1 let collapseTarget = new THREE.Vector2(); // x, y in plane's local coordinates const collapseDuration = 1.0; // seconds const spreadDuration = 2.5; // seconds const maxPeakHeight = 4.0; const minSigma = 0.2; // Narrowest width of the Gaussian peak const baseSigma = 2.0; // Initial wider spread for the Gaussian when spreading out const baseColor = new THREE.Color(0x0077ff); // Blue const peakColor = new THREE.Color(0xffff00); // Yellow let raycaster, pointer; init(); animate(); function init() { scene = new THREE.Scene(); clock = new THREE.Clock(); // Camera camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.set(5, 7, 10); camera.lookAt(scene.position); // Renderer renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(window.devicePixelRatio); document.body.appendChild(renderer.domElement); // Controls const controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true; controls.dampingFactor = 0.05; controls.minDistance = 5; controls.maxDistance = 30; // Lighting const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); scene.add(ambientLight); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight.position.set(5, 10, 7); scene.add(directionalLight); // Wave Mesh waveGeometry = new THREE.PlaneGeometry(waveSize, waveSize, waveSegments, waveSegments); waveGeometry.rotateX(-Math.PI / 2); // Lay flat on XZ plane const waveMaterial = new THREE.MeshPhongMaterial({ vertexColors: true, shininess: 80, transparent: true, opacity: 0.85, side: THREE.DoubleSide }); waveMesh = new THREE.Mesh(waveGeometry, waveMaterial); scene.add(waveMesh); // Initialize vertex colors for the wave const waveColors = []; for (let i = 0; i < waveGeometry.attributes.position.count; i++) { waveColors.push(baseColor.r, baseColor.g, baseColor.b); } waveGeometry.setAttribute('color', new THREE.Float32BufferAttribute(waveColors, 3)); // Surface Plane Mesh (oscillating) surfaceGeometry = new THREE.PlaneGeometry(waveSize * 1.2, waveSize * 1.2, waveSegments / 2, waveSegments / 2); surfaceGeometry.rotateX(-Math.PI / 2); const surfaceMaterial = new THREE.MeshPhongMaterial({ color: 0x333355, shininess: 30, transparent: true, opacity: 0.6 }); surfaceMesh = new THREE.Mesh(surfaceGeometry, surfaceMaterial); surfaceMesh.position.y = -1.5; // Position below the wave scene.add(surfaceMesh); // Raycasting for touch/click raycaster = new THREE.Raycaster(); pointer = new THREE.Vector2(); // Event Listeners window.addEventListener('resize', onWindowResize); renderer.domElement.addEventListener('pointerdown', onPointerDown); } function updateWave(elapsedTime, deltaTime) { const positions = waveGeometry.attributes.position.array; const colors = waveGeometry.attributes.color.array; const normal = new THREE.Vector3(); // For recalculating normals (optional, but good for lighting) if (isCollapsing) { collapseProgress += deltaTime / collapseDuration; collapseProgress = Math.min(collapseProgress, 1); const currentPeakHeight = THREE.MathUtils.lerp(0, maxPeakHeight, collapseProgress); const currentSigma = THREE.MathUtils.lerp(baseSigma * 2, minSigma, collapseProgress); // Start wider, then narrow for (let i = 0; i < positions.length; i += 3) { const x = positions[i]; const z_plane = positions[i + 2]; // z in plane's local coords (world y is plane z) // Gaussian peak const dx = x - collapseTarget.x; const dz = z_plane - collapseTarget.y; // Using y for the plane's z-axis const distanceSq = dx * dx + dz * dz; const newY = currentPeakHeight * Math.exp(-distanceSq / (2 * currentSigma * currentSigma)); positions[i + 1] = newY; // y is up/down in world space // Color interpolation const colorFactor = Math.min(1, Math.abs(newY) / (maxPeakHeight * 0.8)); // Normalize for color const tempColor = new THREE.Color().lerpColors(baseColor, peakColor, colorFactor); colors[i] = tempColor.r; colors[i + 1] = tempColor.g; colors[i + 2] = tempColor.b; } if (collapseProgress >= 1) { isCollapsing = false; isSpreading = true; spreadProgress = 0; } } else if (isSpreading) { spreadProgress += deltaTime / spreadDuration; spreadProgress = Math.min(spreadProgress, 1); // Peak lowers and widens, transitions back to base wave const currentPeakHeight = THREE.MathUtils.lerp(maxPeakHeight, 0, spreadProgress); const currentSigma = THREE.MathUtils.lerp(minSigma, baseSigma * 1.5, spreadProgress); // Spread out wider for (let i = 0; i < positions.length; i += 3) { const x = positions[i]; const z_plane = positions[i + 2]; // Gaussian component (fading out) const dx = x - collapseTarget.x; const dz = z_plane - collapseTarget.y; const distanceSq = dx * dx + dz * dz; const gaussianY = currentPeakHeight * Math.exp(-distanceSq / (2 * currentSigma * currentSigma)); // Base wave component (fading in) const baseWaveY = calculateBaseWaveY(x, z_plane, elapsedTime); // Interpolate between Gaussian and base wave positions[i + 1] = THREE.MathUtils.lerp(gaussianY, baseWaveY, spreadProgress); // Color interpolation back to blue const colorFactor = Math.min(1, Math.abs(positions[i+1]) / (maxPeakHeight * 0.5) ); // Adjusted for spread const tempColor = new THREE.Color().lerpColors(peakColor, baseColor, spreadProgress); // More nuanced color based on current height during spread const heightBasedColorFactor = Math.min(1, Math.abs(positions[i+1]) / (baseSigma * 0.5)); // Normalize height const spreadColor = new THREE.Color().lerpColors(baseColor, peakColor, heightBasedColorFactor * (1-spreadProgress)); colors[i] = spreadColor.r; colors[i + 1] = spreadColor.g; colors[i + 2] = spreadColor.b; } if (spreadProgress >= 1) { isSpreading = false; } } else { // Normal wave evolution for (let i = 0; i < positions.length; i += 3) { const x = positions[i]; const z_plane = positions[i + 2]; const y = calculateBaseWaveY(x, z_plane, elapsedTime); positions[i + 1] = y; // Color based on height const colorFactor = THREE.MathUtils.clamp( (y + 0.5) / 1.5, 0, 1); // Normalize height for color const tempColor = new THREE.Color().lerpColors(baseColor, new THREE.Color(0x33aaff), colorFactor); // Slight variation colors[i] = tempColor.r; colors[i + 1] = tempColor.g; colors[i + 2] = tempColor.b; } } waveGeometry.attributes.position.needsUpdate = true; waveGeometry.attributes.color.needsUpdate = true; waveGeometry.computeVertexNormals(); // Recalculate normals for correct lighting } function calculateBaseWaveY(x, z_plane, time) { // A combination of sine waves for a more complex pattern const k1 = 1.5, a1 = 0.2, speed1 = 1.0; const k2 = 1.0, a2 = 0.3, speed2 = 0.7; const k3 = 2.5, a3 = 0.15, speed3 = 1.3; let y = a1 * Math.sin(k1 * x + speed1 * time); y += a2 * Math.cos(k2 * z_plane + speed2 * time * 1.2); y += a3 * Math.sin(k3 * Math.sqrt(x*x + z_plane*z_plane) + speed3 * time * 0.8); y *= 0.6; // Overall amplitude scale return y; } function updateSurfacePlane(elapsedTime) { const positions = surfaceGeometry.attributes.position.array; const planeSize = waveSize * 1.2; for (let i = 0; i < positions.length; i += 3) { const x = positions[i]; const z_plane = positions[i + 2]; const wave1 = 0.1 * Math.sin(x / 2 + elapsedTime * 0.5); const wave2 = 0.05 * Math.cos(z_plane / 1.5 + elapsedTime * 0.3); positions[i+1] = wave1 + wave2; } surfaceGeometry.attributes.position.needsUpdate = true; surfaceGeometry.computeVertexNormals(); } function onPointerDown(event) { if (isCollapsing || isSpreading) return; // Don't re-trigger if already in transition pointer.x = (event.clientX / window.innerWidth) * 2 - 1; pointer.y = -(event.clientY / window.innerHeight) * 2 + 1; raycaster.setFromCamera(pointer, camera); const intersects = raycaster.intersectObject(waveMesh); // Intersect with the wave mesh if (intersects.length > 0) { // Convert intersection point (world space) to plane's local space const intersectionPointWorld = intersects[0].point; const intersectionPointLocal = waveMesh.worldToLocal(intersectionPointWorld.clone()); collapseTarget.set(intersectionPointLocal.x, intersectionPointLocal.z); // Plane's local x and z isCollapsing = true; isSpreading = false; collapseProgress = 0; document.getElementById('info').innerText = "Wave collapsing..."; setTimeout(() => { if (isSpreading) document.getElementById('info').innerText = "Wave spreading..."; }, collapseDuration * 1000); setTimeout(() => { if (!isCollapsing && !isSpreading) document.getElementById('info').innerText = "Touch / Click the wave to see it collapse"; }, (collapseDuration + spreadDuration) * 1000); } } function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); } function animate() { requestAnimationFrame(animate); const elapsedTime = clock.getElapsedTime(); const deltaTime = clock.getDelta(); updateWave(elapsedTime, deltaTime); updateSurfacePlane(elapsedTime); renderer.render(scene, camera); } </script> </body> </html>How to Run:
- Save the code above as
wave.html. - Open it in a modern web browser (like Chrome, Firefox, Edge, Safari).
Explanation and Key Parts:
- Three.js Setup: Standard scene, camera, renderer, lights, and OrbitControls.
waveMeshandsurfaceMesh:- Both are
PlaneGeometryrotated to lie flat on the XZ plane. waveMeshhasvertexColors: trueto allow per-vertex coloring. Its opacity is slightly lower to see through it.surfaceMeshis a simpler, darker plane underneath, with its own oscillation.
- Both are
- State Variables (
isCollapsing,isSpreading, etc.):- These control the current phase of the animation.
collapseTarget: Stores the (x, z) coordinates in the plane's local space where the touch occurred. This is crucial for centering the Gaussian peak correctly.
updateWave(elapsedTime, deltaTime): This is the core logic.isCollapsing:collapseProgressincreases from 0 to 1.- Peak height (
currentPeakHeight) increases. - Gaussian width (
currentSigma) decreases (narrows). - Vertex Y positions are set according to the Gaussian function centered at
collapseTarget. - Vertex colors are interpolated from
baseColor(blue) topeakColor(yellow) based on the calculated height orcollapseProgress.
isSpreading:spreadProgressincreases from 0 to 1.- Peak height decreases.
- Gaussian width increases (spreads out).
- Vertex Y positions are interpolated from the collapsed Gaussian state back to the
calculateBaseWaveYstate. This ensures it returns to the smooth, evolving wave. - Vertex colors are interpolated back from
peakColortobaseColor.
- Normal Evolution (neither collapsing nor spreading):
- Vertex Y positions are set by
calculateBaseWaveY. - Colors are set based on height, generally blueish.
- Vertex Y positions are set by
*.needsUpdate = true: Tells Three.js to update the GPU buffers with the new vertex positions and colors.computeVertexNormals(): Recalculates normals after deforming the geometry, which is important for correct lighting.
calculateBaseWaveY(x, z_plane, time):- Defines the "normal" smooth wave evolution using a sum of sine/cosine functions. This creates a somewhat complex, dynamic surface.
updateSurfacePlane(elapsedTime):- A simpler oscillation for the plane beneath the main wave.
onPointerDown(event):- Uses
Raycasterto find where the click/touch intersects thewaveMesh. waveMesh.worldToLocal(): Converts the world-space intersection point to the local coordinate system of thewaveMesh. This is essential because thePlaneGeometryvertices are defined in local space (centered at 0,0 before transformations).- Sets the state to start collapsing.
- Uses
- Coloring:
- A
Float32BufferAttributeis added towaveGeometryfor colors. THREE.Color.lerpColors()is used for smooth transitions.
- A
- Timings:
collapseDurationandspreadDurationcontrol how long each phase takes.
This simulation provides a good visual analogy of wave function collapse and subsequent spreading, using common visual computing techniques. It's not a direct solution of the Schrödinger equation, but it captures the requested visual behavior effectively.
- A
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.