1 | var particleCount;
|
---|
2 | var particleSystem;
|
---|
3 |
|
---|
4 | function rain(){
|
---|
5 | // create the particle variables
|
---|
6 | particleCount = 3600,
|
---|
7 | particles = new THREE.Geometry(),
|
---|
8 | pMaterial = new THREE.ParticleBasicMaterial({
|
---|
9 | color: 0xFFFFFF,
|
---|
10 | size: 10,
|
---|
11 | map: THREE.ImageUtils.loadTexture(
|
---|
12 | "./images/raindrop.png"
|
---|
13 | ),
|
---|
14 | blending: THREE.AdditiveBlending,
|
---|
15 | transparent: true,
|
---|
16 | opacity: 0.5
|
---|
17 | });
|
---|
18 |
|
---|
19 | // now create the individual particles
|
---|
20 | for(var p = 0; p < particleCount; p++) {
|
---|
21 |
|
---|
22 | // create a particle with random
|
---|
23 | // position values, -250 -> 250
|
---|
24 | var pX = Math.random() * window.innerWidth*2 - window.innerWidth,
|
---|
25 | pY = Math.random() * window.innerHeight*3 - window.innerHeight,
|
---|
26 | pZ = Math.random() * window.innerWidth*2 - window.innerWidth,
|
---|
27 | particle = new THREE.Vector3(pX, pY, pZ)
|
---|
28 |
|
---|
29 | // create a velocity vector
|
---|
30 | particle.velocity = new THREE.Vector3(
|
---|
31 | 0, // x
|
---|
32 | -Math.random(), // y
|
---|
33 | 0); // z
|
---|
34 |
|
---|
35 | // add it to the geometry
|
---|
36 | particles.vertices.push(particle);
|
---|
37 | }
|
---|
38 |
|
---|
39 | // create the particle system
|
---|
40 | particleSystem = new THREE.ParticleSystem(
|
---|
41 | particles,
|
---|
42 | pMaterial);
|
---|
43 |
|
---|
44 | particleSystem.sortParticles = true;
|
---|
45 |
|
---|
46 | return particleSystem;
|
---|
47 | }
|
---|
48 | // animation loop
|
---|
49 | function updateRain() {
|
---|
50 |
|
---|
51 | // add some rotation to the system
|
---|
52 | particleSystem.rotation.y += 0.001;
|
---|
53 |
|
---|
54 | var pCount = particleCount;
|
---|
55 | while(pCount--) {
|
---|
56 | // get the particle
|
---|
57 | var particle = particles.vertices[pCount];
|
---|
58 |
|
---|
59 | // check if we need to reset
|
---|
60 | if(particle.y < -200) {
|
---|
61 | particle.y = 200;
|
---|
62 | particle.velocity.y = 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | // update the velocity
|
---|
66 | particle.velocity.y -= Math.random() * .1;
|
---|
67 |
|
---|
68 | // and the position
|
---|
69 | particle.addVectors(
|
---|
70 | particle,
|
---|
71 | particle.velocity);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // flag to the particle system that we've
|
---|
75 | // changed its vertices. This is the
|
---|
76 | // dirty little secret.
|
---|
77 | particleSystem.geometry.__dirtyVertices = true;
|
---|
78 |
|
---|
79 | } |
---|