|
| 1 | +// Initialize Three.js scene |
| 2 | +const scene = new THREE.Scene(); |
| 3 | +const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); |
| 4 | +const renderer = new THREE.WebGLRenderer(); |
| 5 | +renderer.setSize(window.innerWidth, window.innerHeight); |
| 6 | +document.getElementById('container').appendChild(renderer.domElement); |
| 7 | + |
| 8 | +// Create floating islands |
| 9 | +const islandGeometry = new THREE.BoxGeometry(5, 0.5, 5); |
| 10 | +const islandMaterial = new THREE.MeshPhongMaterial({ color: 0x00ff00 }); |
| 11 | +const islands = []; |
| 12 | + |
| 13 | +for (let i = 0; i < 10; i++) { |
| 14 | + const island = new THREE.Mesh(islandGeometry, islandMaterial); |
| 15 | + island.position.x = (Math.random() - 0.5) * 50; |
| 16 | + island.position.y = Math.random() * 10; |
| 17 | + island.position.z = (Math.random() - 0.5) * 50; |
| 18 | + islands.push(island); |
| 19 | + scene.add(island); |
| 20 | +} |
| 21 | + |
| 22 | +// Add clouds (particles) |
| 23 | +const cloudGeometry = new THREE.BufferGeometry(); |
| 24 | +const cloudMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 0.1 }); |
| 25 | +const cloudVertices = []; |
| 26 | + |
| 27 | +for (let i = 0; i < 1000; i++) { |
| 28 | + const x = (Math.random() - 0.5) * 100; |
| 29 | + const y = Math.random() * 20; |
| 30 | + const z = (Math.random() - 0.5) * 100; |
| 31 | + cloudVertices.push(x, y, z); |
| 32 | +} |
| 33 | + |
| 34 | +cloudGeometry.setAttribute('position', new THREE.Float32BufferAttribute(cloudVertices, 3)); |
| 35 | +const clouds = new THREE.Points(cloudGeometry, cloudMaterial); |
| 36 | +scene.add(clouds); |
| 37 | + |
| 38 | +// Add directional light for better shading |
| 39 | +const light = new THREE.DirectionalLight(0xffffff, 1); |
| 40 | +light.position.set(1, 1, 1); |
| 41 | +scene.add(light); |
| 42 | + |
| 43 | +// Set initial camera position |
| 44 | +camera.position.set(0, 10, 20); |
| 45 | + |
| 46 | +// Render loop |
| 47 | +const animate = () => { |
| 48 | + requestAnimationFrame(animate); |
| 49 | + |
| 50 | + // Rotate islands |
| 51 | + islands.forEach(island => { |
| 52 | + island.rotation.x += 0.005; |
| 53 | + island.rotation.y += 0.005; |
| 54 | + }); |
| 55 | + |
| 56 | + // Move clouds |
| 57 | + cloudGeometry.attributes.position.array.forEach((_, i) => { |
| 58 | + cloudGeometry.attributes.position.array[i] += i % 3 === 0 ? 0.005 : 0; |
| 59 | + if (cloudGeometry.attributes.position.array[i] > 20) { |
| 60 | + cloudGeometry.attributes.position.array[i] = -20; |
| 61 | + } |
| 62 | + }); |
| 63 | + cloudGeometry.attributes.position.needsUpdate = true; |
| 64 | + |
| 65 | + // Day-night cycle |
| 66 | + light.intensity = Math.abs(Math.sin(Date.now() * 0.0001)); |
| 67 | + |
| 68 | + renderer.render(scene, camera); |
| 69 | +}; |
| 70 | + |
| 71 | +// Handle window resize |
| 72 | +window.addEventListener('resize', () => { |
| 73 | + const newWidth = window.innerWidth; |
| 74 | + const newHeight = window.innerHeight; |
| 75 | + |
| 76 | + camera.aspect = newWidth / newHeight; |
| 77 | + camera.updateProjectionMatrix(); |
| 78 | + |
| 79 | + renderer.setSize(newWidth, newHeight); |
| 80 | +}); |
| 81 | + |
| 82 | +animate(); |
0 commit comments