-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (62 loc) · 2.17 KB
/
script.js
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
let camera;
let scene;
let renderer;
let material;
let mouseX = 0;
let mouseY = 0;
let windowhalfX = window.innerWidth / 2;
let windowhalfY = window.innerHeight / 2;
init();
animate();
function init(){
camera=new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 5, 2000);
camera.position.z = 500;
scene=new THREE.Scene();
scene.fog = new THREE.FogExp2(0x000ff, 0.001);
const geometry =new THREE.BufferGeometry();
const vertices = [];
const size = 2000;
for (let i = 0; i< 20000; i++) {
const x = (Math.random() * size + Math.random() * size) /2 - size / 2;
const y = (Math.random() * size + Math.random() * size) /2 - size / 2;
const z = (Math.random() * size + Math.random() * size) /2 - size / 2;
vertices.push(x, y, z);
};
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
material = new THREE.PointsMaterial({
size: 2,
color: 0xffffff,
});
const particles = new THREE.Points(geometry, material);
scene.add(particles);
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
document.body.style.touchAction = 'none';
document.body.addEventListener('pointermove', onPointerMove);
window.addEventListener('resize', onWindonwResize);
};
function onWindonwResize() {
windowhalfX = window.innerWidth / 2;
windowhalfY = window.innerHeight /2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
};
function onPointerMove(event){
mouseX = event.clientX - windowhalfX;
mouseY = event.clientY - windowhalfY;
};
function animate () {
requestAnimationFrame(animate);
render()
};
function render (){
camera.position.x += (mouseX * 2 - camera.position.x)* 0.02;
camera.position.y += (mouseY * 2 - camera.position.y)* 0.02;
camera.lookAt(scene.position);
renderer.render(scene,camera);
scene.rotation.x += 0.001;
scene.rotation.y += 0.002;
};