-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
66 lines (43 loc) · 1.82 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
const canvas= document.querySelector('canvas.webgl');
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 100, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer( { canvas});
renderer.setSize( window.innerWidth, window.innerHeight );
const geometry = new THREE.TorusGeometry( 1, 3, 16, 100 );
const particlesgeometry = new THREE.BufferGeometry();
const particlesCnt=5000;
//float32array
const posArray= new Float32Array(particlesCnt * 3);
for( let i= 0; i < particlesCnt * 3; i++) {
posArray[i]= ((Math.random() - 0.5) * 50)
}
particlesgeometry.setAttribute( 'position' , new THREE.BufferAttribute(posArray, 3));
const material = new THREE.PointsMaterial( { size: 0.05, sizeAttenuation:true } );
const sphere= new THREE.Points( geometry, material );
const particlesMesh= new THREE.Points( particlesgeometry,material);
scene.add( sphere, particlesMesh );
const pointlight= new THREE.PointLight(0xffffff, 0.1);
pointlight.position.x=2;
pointlight.position.y=3;
pointlight.position.z=4;
scene.add(pointlight);
//mouse
document.addEventListener('mousemove', animateParticles);
let mouseX= 0;
let mouseY =0;
function animateParticles(event){
mouseY= event.clientY;
mouseX= event.clientX;
}
const clock= new THREE.Clock();
camera.position.z = 10;
function animate(){
requestAnimationFrame(animate);
const elapsedTime= clock.getElapsedTime();
sphere.rotation.x+= 0.01;
sphere.rotation.y+= 0.01;
sphere.rotation.z+= 0.01;
particlesMesh.rotation.y= mouseY * (elapsedTime) * 0.00008;
renderer.render( scene, camera);
}
animate();