-
Notifications
You must be signed in to change notification settings - Fork 70
/
fireworks.html
353 lines (289 loc) · 12.4 KB
/
fireworks.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html>
<head>
<title>Fireworks!</title>
<meta name="description" content="A-Frame Examples">
<script src="js/aframe-master.1.0.4.min.js"></script>
<script src="js/aframe-environment-component.min-old.js"></script>
<script src="js/aframe-spe-particles-component.js"></script>
<script src="js/aframe-spritesheet-animation.js"></script>
<style>
.mainUI
{
position: fixed;
top: 0px;
width:100%; height:100%;
z-index: 1;
pointer-events: none; /* allow click-through in transparent areas */
display: flex;
align-items: center;
justify-content: center;
}
.regionUI
{
position: absolute;
display: flex;
flex-direction: row;
pointer-events: none;
}
.buttonUI
{
display: flex;
flex-direction: column;
justify-content: center;
pointer-events: auto;
filter: drop-shadow(0px 0px 20px white);
}
</style>
</head>
<body>
<!-- div used for enter button (user interaction to trigger audio) and fade-in -->
<div class="mainUI" id="uiDiv" oncontextmenu="return false;">
<div class="regionUI">
<div class="buttonUI">
<img src="images/buttons/enter.png" id="buttonEnter" />
</div>
</div>
</div>
<script>
function randomNormal(min, max)
{
let rand = 0;
for (let n = 0; n < 6; n++)
rand += Math.random();
return min + (max - min) * (rand / 6);
}
function randomUniform(min, max)
{
return min + (max - min) * Math.random();
}
AFRAME.registerComponent('spawner',
{
init: function()
{
this.sceneElement = document.querySelector('a-scene');
},
tick: function(time, deltaTime) // deltaTime: milliseconds
{
let deltaSeconds = deltaTime/1000;
let eventFrequency = 0.75;
if ( Math.random() < eventFrequency * deltaSeconds )
{
console.log( "Firework!" );
let angle = THREE.Math.degToRad(270);
let angleSpread = THREE.Math.degToRad(90);
let angleRadians = angle + randomNormal(-angleSpread, angleSpread);
let radius = 9;
let x = radius * Math.cos(angleRadians);
let y = 0;
let z = radius * Math.sin(angleRadians);
let firework = document.createElement("a-entity");
firework.setAttribute("firework", "");
firework.setAttribute("position", {"x":x, "y":y, "z":z} );
// random flight parameters
let maxHeight = randomNormal(4, 8);
firework.setAttribute("firework", "maxHeight", maxHeight );
let riseTime = randomNormal(2, 3);
firework.setAttribute("firework", "riseTime", riseTime );
this.sceneElement.appendChild( firework );
// chance of multi-firework
// 0.10 = 1/10 double, 1/100 triple, 1/1000 quadruple, etc.
// 0.20 = 1/5 double, 1/25 triple, 1/125 quadruple, etc.
while ( Math.random() < 0.15 )
{
let fireworkExtra = document.createElement("a-entity");
fireworkExtra.setAttribute("firework", "");
// synchronize position, height, time to explosion
fireworkExtra.setAttribute("position", {"x":x, "y":y, "z":z} );
fireworkExtra.setAttribute("firework", "maxHeight", maxHeight );
fireworkExtra.setAttribute("firework", "riseTime", riseTime );
this.sceneElement.appendChild( fireworkExtra );
}
}
}
});
AFRAME.registerComponent('firework',
{
schema:
{
color: {type: 'string', default: ""},
maxHeight: {type: 'number', default: 4.5},
riseTime: {type: 'number', default: 2.5} // time to reach maxHeight
},
randomColor: function()
{
let colorList = ["red", "orange", "gold", "green", "cyan", "#0088FF", "violet"];
let colorIndex = Math.floor( colorList.length * Math.random() );
return colorList[colorIndex];
},
init: function()
{
// set random color if none already set
if ( this.data.color == "" )
this.data.color = this.randomColor();
// set up movement animation
let p = this.el.object3D.position;
this.el.setAttribute("animation__position", "property", "position");
this.el.setAttribute("animation__position", "from", {"x":p.x, "y":p.y, "z":p.z});
this.el.setAttribute("animation__position", "to", {"x":p.x, "y":p.y + this.data.maxHeight, "z":p.z});
this.el.setAttribute("animation__position", "dur", this.data.riseTime * 1000);
this.el.setAttribute("animation__position", "easing", "easeOutQuad"); // fast at start, then slower
// trail effect ----------------------------------------------------------
this.particleTrail = document.createElement("a-image");
this.particleTrail.setAttribute("position", "0 -0.1 0");
this.particleTrail.setAttribute("width", "0.5");
this.particleTrail.setAttribute("height", "0.5");
this.particleTrail.setAttribute("scale", "1 1 1");
this.particleTrail.setAttribute("src", "#fireballSheet");
this.particleTrail.setAttribute("spritesheet-animation", "rows: 1; columns: 8; frameDuration: 0.08; loop: true;");
this.particleTrail.setAttribute("material", "blending: additive; transparent: true; opacity: 1;");
this.particleTrail.setAttribute("animation__fade", "property", "material.opacity");
this.particleTrail.setAttribute("animation__fade", "from", 1.0);
this.particleTrail.setAttribute("animation__fade", "to", 0.2);
this.particleTrail.setAttribute("animation__fade", "dur", this.data.riseTime * 1000);
this.particleTrail.setAttribute("animation__fade", "easing", "easeOutQuad"); // slow at start, then fast
this.particleTrail.setAttribute("animation__shrink", "property", "scale");
this.particleTrail.setAttribute("animation__shrink", "from", "1 1 1");
this.particleTrail.setAttribute("animation__shrink", "to", "0.25 0.25 0.25");
this.particleTrail.setAttribute("animation__shrink", "dur", this.data.riseTime * 1000);
this.particleTrail.setAttribute("animation__shrink", "easing", "easeOutQuad"); // slow at start, then fast
this.el.appendChild(this.particleTrail);
// burst effect ----------------------------------------------------------
this.particleBurst = document.createElement("a-entity");
this.particleBurst.setAttribute("spe-particles", "");
this.particleBurst.setAttribute("spe-particles", "texture", "images/particles/sparkle.png");
this.particleBurst.setAttribute("spe-particles", "blending", "additive");
this.particleBurst.setAttribute("spe-particles", "distribution", "sphere");
this.particleBurst.setAttribute("spe-particles", "radius", 0.01);
// particles both shrink and fade during last moments
this.particleBurst.setAttribute("spe-particles", "opacity", [1, 1, 1, 0]);
// this.particleBurst.setAttribute("spe-particles", "size", [1, 1, 1, 0]);
let particleCount = Math.floor( randomUniform(100, 999) );
this.particleBurst.setAttribute("spe-particles", "particleCount", particleCount);
// change of particle burst vs. particle stream
if ( Math.random() < 0.80 )
this.particleBurst.setAttribute("spe-particles", "activeMultiplier", particleCount);
else
this.particleBurst.setAttribute("spe-particles", "activeMultiplier", particleCount/128);
// note: when distribution = 'sphere', only first component of velocity vectors are used
let baseVelocity = randomUniform(1, 2);
this.particleBurst.setAttribute("spe-particles", "velocity", {"x":baseVelocity, "y":0, "z":0});
this.particleBurst.setAttribute("spe-particles", "velocitySpread", {"x":0.5, "y":0, "z":0});
this.particleBurst.setAttribute("spe-particles", "drag", 1.00);
this.particleBurst.setAttribute("spe-particles", "randomizeVelocity", true);
// add a bit of gravity
this.particleBurst.setAttribute("spe-particles", "accelerationDistribution", "box");
this.particleBurst.setAttribute("spe-particles", "acceleration", {"x":0, "y":-0.2, "z":0});
this.particleBurst.setAttribute("spe-particles", "accelerationSpread", {"x":0, "y":0.2, "z":0});
// how long will particles last?
this.burstDuration = randomUniform(1.0, 3.0);
this.particleBurst.setAttribute("spe-particles", "maxAge", this.burstDuration);
this.particleBurst.setAttribute("spe-particles", "maxAgeSpread", this.burstDuration/4);
// duration = maximum amount of time to emit particles;
// should be less than smallest max age to prevent second burst
this.particleBurst.setAttribute("spe-particles", "duration", 0.5);
// default: steady color
let colorArray = ["white", this.data.color, this.data.color, this.data.color];
// random chance of color shifting
if ( Math.random() < 0.25 )
{
let color2 = this.data.color;
// make sure second color is different from first
while (color2 == this.data.color)
{
color2 = this.randomColor();
}
colorArray = ["white", this.data.color, color2, color2];
}
this.particleBurst.setAttribute("spe-particles", "color", colorArray);
// disable burst effect for now;
// will be enabled (in tick method) after reaching max height
this.particleBurst.setAttribute("spe-particles", "enabled", false);
this.el.appendChild(this.particleBurst);
this.elapsedTime = 0;
this.burstStart = false;
// audio effects
this.launchSound = document.getElementById("launchSound");
this.launchSound.components.sound.playSound();
if ( this.burstDuration < 1.6 || particleCount < 300 )
this.burstSound = document.getElementById("burst1Sound");
else if ( this.burstDuration < 2.4 )
this.burstSound = document.getElementById("burst2Sound");
else // this.burstDuration < 3.0
this.burstSound = document.getElementById("burst3Sound");
},
tick: function(time, deltaTime)
{
this.elapsedTime += deltaTime/1000;
// firework has reached max height, start burst effect
if (this.elapsedTime > this.data.riseTime && this.burstStart == false)
{
this.burstStart = true;
this.particleBurst.setAttribute("spe-particles", "enabled", true);
this.el.removeChild( this.particleTrail );
this.burstSound.components.sound.playSound();
}
// firework finished; remove self from scene
// need to take into account particle age variation (1.25 factor)
if (this.elapsedTime > this.data.riseTime + this.burstDuration * 1.25)
{
let element = this.el;
element.parentNode.removeChild(element);
}
}
});
// runs when user clicks button
AFRAME.registerComponent('start',
{
init: function()
{
// block pointer events until buttonEnter is clicked
let uiDiv = document.getElementById("uiDiv");
uiDiv.style["pointer-events"] = "auto";
// set up background blocker
uiDiv.style["background-color"] = "rgba(0, 0, 0, 0.75)";
// indicate clickable with hand cursor (desktop)
let buttonEnter = document.getElementById("buttonEnter");
buttonEnter.style.cursor = "pointer";
// fade-in functionality
let fadeIn = function()
{
// allow point events again
uiDiv.style["pointer-events"] = "none";
// sounds can only be triggered after a mouse interaction
let soundPlayer = document.querySelector("#ambientSound");
soundPlayer.components.sound.playSound();
// remove startButton
buttonEnter.parentNode.remove(buttonEnter);
// fade out uiDiv background
uiDiv.style["background-color"] = "rgba(0, 0, 0, 0.0)";
uiDiv.style["transition"] = "background-color 1000ms linear";
// activate the spawner
let camera = document.querySelector("#camera");
camera.setAttribute("spawner", "");
}
// activate for desktop/touchscreen
buttonEnter.addEventListener('touchstart', fadeIn);
buttonEnter.addEventListener('mousedown', fadeIn);
}
});
</script>
<a-scene environment="preset: starry; fog: 0; flatShading: false; groundTexture: walkernoise; groundColor: #000000; groundColor2: #00ff00; grid: none" start>
<a-assets>
<img id="fireball" src="images/fireball.png"/>
<img id="fireballSheet" src="images/fireball-up.png"/>
<audio id="ambient" src="audio/ambient-night.mp3" preload="auto"></audio>
<audio id="launch" src="audio/rocket-launch.wav" preload="auto"></audio>
<audio id="burst1" src="audio/firework-burst-short.wav" preload="auto"></audio>
<audio id="burst2" src="audio/firework-burst-medium.wav" preload="auto"></audio>
<audio id="burst3" src="audio/firework-burst-long.wav" preload="auto"></audio>
</a-assets>
<a-entity id="camera" camera look-controls position="0 1.6 0" ></a-entity>
<a-entity id="ambientSound" sound="src: #ambient; loop: true;"></a-entity>
<a-entity id="launchSound" sound="src: #launch; loop: false; poolSize: 4; volume: 0.025;"></a-entity>
<a-entity id="burst1Sound" sound="src: #burst1; loop: false; poolSize: 4; volume: 0.025;"></a-entity>
<a-entity id="burst2Sound" sound="src: #burst2; loop: false; poolSize: 4; volume: 0.025;"></a-entity>
<a-entity id="burst3Sound" sound="src: #burst3; loop: false; poolSize: 4; volume: 0.025;"></a-entity>
</a-scene>
</body>
</html>