forked from joshmarinacci/webxr-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.html
86 lines (72 loc) · 2.49 KB
/
test1.html
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
<!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 particles
const MAX = 100
function setupScene() {
particles = new THREE.Group()
const geo = new THREE.SphereBufferGeometry(0.1)
const mat = new THREE.MeshLambertMaterial({color: 'red'})
for(let i=0; i<MAX; i++) {
const particle = new THREE.Mesh(geo,mat)
particle.velocity = new THREE.Vector3(rand(-0.01, 0.01), 0.06, rand(-0.01, 0.01))
particle.acceleration = new THREE.Vector3(0,-0.001,0)
particle.position.x = rand(-1,1)
particle.position.z = rand(-1,1)
particle.position.y = rand(-1,-3)
particles.add(particle)
}
particles.position.z = -4
scene.add(particles)
}
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.children.forEach(p => {
p.velocity.add(p.acceleration)
p.position.add(p.velocity)
})
renderer.render( scene, camera );
}
init();
renderer.setAnimationLoop( render );
</script>
</body>
</html>