-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (44 loc) · 1.62 KB
/
Copy pathscript.js
File metadata and controls
52 lines (44 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// --- Three.js 3D Background ---
let scene, camera, renderer, stars;
function init3D() {
const canvas = document.getElementById('bg-canvas');
if (!canvas) return;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 1;
renderer = new THREE.WebGLRenderer({ canvas: canvas, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
const starGeo = new THREE.BufferGeometry();
const starVertices = [];
for(let i=0; i<5000; i++) {
const x = (Math.random() - 0.5) * 2000;
const y = (Math.random() - 0.5) * 2000;
const z = (Math.random() - 0.5) * 2000;
starVertices.push(x, y, z);
}
starGeo.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));
const starMaterial = new THREE.PointsMaterial({ color: 0xaaaaaa, size: 0.7, transparent: true });
stars = new THREE.Points(starGeo, starMaterial);
scene.add(stars);
animate3D();
}
function animate3D() {
if (stars) {
stars.rotation.y += 0.0001;
}
if (renderer) {
renderer.render(scene, camera);
}
requestAnimationFrame(animate3D);
}
function onWindowResize() {
if (camera && renderer) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
}
window.addEventListener('DOMContentLoaded', () => {
init3D();
window.addEventListener('resize', onWindowResize);
});