-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtest2.html
More file actions
93 lines (77 loc) · 2.57 KB
/
test2.html
File metadata and controls
93 lines (77 loc) · 2.57 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
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
html, body {
padding:0;
margin:0;
}
</style>
<script src="./node_modules/three/build/three.min.js"></script>
</head>
<body>
<script type="module">
const rand = (min,max) => min + Math.random()*(max-min)
let scene, camera, renderer
let mesh, particles
const MAX = 100
function setupScene() {
particles = []
const geo = new THREE.Geometry()
for(let i=0; i<MAX; i++) {
const particle = {
position: new THREE.Vector3(rand(-1, 1), rand(-1, 1), rand(-1, -3)),
velocity: new THREE.Vector3(rand(-0.01, 0.01), 0.06, rand(-0.01, 0.01)),
acceleration: new THREE.Vector3(0, -0.001, 0),
}
particles.push(particle)
geo.vertices.push(particle.position)
}
const mat = new THREE.PointsMaterial({
color:0xffffff,
size: 0.1,
})
mesh = new THREE.Points(geo,mat)
mesh.position.z = -4
scene.add(mesh)
}
function init() {
//setup canvas
const container = document.createElement( 'div' );
document.body.appendChild( container );
//setup scene and cameras
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x000000 );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 50 );
//setup light
var light = new THREE.DirectionalLight( 0xffffff, 1.0 );
light.position.set( 0, -1, 1 ).normalize();
scene.add( light );
//setup renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//setup events
window.addEventListener( 'resize', ()=>{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
},false);
setupScene()
}
function render(time) {
particles.forEach(p => {
p.velocity.add(p.acceleration)
p.position.add(p.velocity)
})
mesh.geometry.verticesNeedUpdate = true
renderer.render( scene, camera );
}
init();
renderer.setAnimationLoop( render );
</script>
</body>
</html>