-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (65 loc) · 2.18 KB
/
Copy pathscript.js
File metadata and controls
85 lines (65 loc) · 2.18 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const container = document.getElementById("threejs-container");
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 10;
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
const light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(10, 10, 10);
scene.add(light);
const blocks = [];
const blockCount = 100;
for (let i = 0; i < blockCount; i++) {
const size = Math.random() * 1 + 0.3;
const geometry = new THREE.BoxGeometry(size, size, size);
const material = new THREE.MeshStandardMaterial({
color: Math.random() * 0xffffff,
});
const cube = new THREE.Mesh(geometry, material);
cube.position.x = Math.random() * 40 - 20;
cube.position.y = Math.random() * 40 - 20;
cube.position.z = Math.random() * 10 - 5;
scene.add(cube);
blocks.push({
mesh: cube,
speed: Math.random() * 0.02 + 0.01,
});
}
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
window.addEventListener('mousemove', onMouseMove, false);
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
function animate() {
requestAnimationFrame(animate);
blocks.forEach((block) => {
block.mesh.rotation.x += 0.01;
block.mesh.rotation.y += 0.01;
block.mesh.position.y -= block.speed;
if (block.mesh.position.y < -20) {
block.mesh.position.y = Math.random() * 20 + 20;
block.mesh.position.x = Math.random() * 40 - 20;
block.mesh.position.z = Math.random() * 10 - 5;
}
});
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children);
for (let i = 0; i < intersects.length; i++) {
intersects[i].object.material.color.set(0xff6347);
}
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});