-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
380 lines (301 loc) · 11.8 KB
/
index.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!DOCTYPE html>
<html lang="en">
<head>
<title>Project1</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<script src="js/three.js"></script>
<script src="js/three.min.js"></script>
<script src="js/Detector.js"></script>
<script src="js/NoiseGenerator.js"></script>
<script src="js/MaterialsGenerator.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/VRControls.js"></script>
<script src="js/VREffects.js"></script>
<script src="js/WebVR.js"></script>
<div id="container"></div>
<script>
if ( !Detector.webgl ) {
Detector.addGetWebGLMessage();
document.getElementById('container').innerHTML = "";
}
if ( WEBVR.isAvailable() === false ) {
document.body.appendChild( WEBVR.getMessage() );
}
var camera, controls, effect, scene, renderer, orbit_controls;
var mesh, texture;
var vr_enabled = false;
var all_fireworks = [];
var firework_id = 0;
var worldWidth = 256;
var worldDepth = 256;
init();
animate();
function init() {
//to regenerate scene
var regen_button = makeRegenerateButton();
document.body.appendChild(regen_button);
//setting up core objects
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 2000);
camera.position.x = 300;
scene = new THREE.Scene();
//mountain terrain
var plane = new THREE.PlaneBufferGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1);
plane.rotateX( - Math.PI / 2 );
var vertices = plane.attributes.position.array;
var p_data = generateRandomArray(worldWidth);
var heightMap = createHeightMap(worldWidth, worldDepth, p_data);
camera.position.y = heightMap[ worldWidth / 2 + worldDepth / 2 * worldWidth ] * 8 + 100;
for (var i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) {
vertices[j + 1] = heightMap[i] * 8;
};
plane.attributes.position.array = vertices;
var mountain_texture = new THREE.TextureLoader().load("images/night-mountain.jpg");
mountain_texture.wrapS = THREE.ClampToEdgeWrapping;
mountain_texture.wrapT = THREE.ClampToEdgeWrapping;
var mountain_material = new THREE.MeshBasicMaterial( {
map: mountain_texture
});
var mountain_mesh = new THREE.Mesh( plane, mountain_material );
mountain_mesh.position.set(0, -200, 0);
scene.add( mountain_mesh );
//adding sky and stars
var sky = new THREE.SphereGeometry(1000, 80, 80);
var sky_material = new THREE.MeshBasicMaterial( {color: 0x000000});
var star_geometry = new THREE.Geometry();
var starMap = generateStars(5000);
var star_material = new THREE.ParticleBasicMaterial({
color: 0xffffff,
map: new THREE.TextureLoader().load("images/particle.jpg"),
size: 5
});
for (var i = 0; i < starMap.length; i += 3) {
var v = new THREE.Vector3(starMap[i], starMap[i + 1], starMap[i + 2]);
star_geometry.vertices.push(v);
}
var star_system = new THREE.ParticleSystem( star_geometry, star_material );
scene.add( star_system );
var sky_mesh = new THREE.Mesh( sky, sky_material);
sky_mesh.material.side = THREE.BackSide;
scene.add( sky_mesh );
//adding moon with lighting
var moon_geometry = new THREE.SphereGeometry(10, 50, 50);
var moon_material = new THREE.MeshBasicMaterial({
color: 0xfee5ac
});
var moon = new THREE.Mesh(moon_geometry, moon_material);
moon.position.set(camera.position.x + 30, camera.position.y + 40, camera.position.z + 10);
moon.material.side = THREE.BackSide;
scene.add(moon);
var moonlight = new THREE.PointLight(0xfee5ac, 10, 0, 2);
moonlight.position.set(camera.position.x + 60, camera.position.y + 40, camera.position.z + 100);
scene.add( moonlight );
//generating fireworks
createFireworks(5);
//setting up renderer
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0x87cefa );
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
//setting up orbit controls
orbit_controls = new THREE.OrbitControls(camera, renderer.domElement);
orbit_controls.addEventListener('change', render);
orbit_controls.enableZoom = false;
var container = document.getElementById("container");
container.innerHTML = "";
container.appendChild(renderer.domElement);
//enable use with headset
if (navigator.getVRDisplays) {
vr_enabled = true;
controls = new THREE.VRControls( camera );
effect = new THREE.VREffect( renderer );
navigator.getVRDisplays()
.then((displays) => {
effect.setVRDisplay(displays[0]);
controls.setVRDisplay(displays[0]);
})
.catch(() => {});
}
if ( WEBVR.isAvailable() === true ) {
document.body.appendChild( WEBVR.getButton( effect ) );
}
//set up for resizing
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
if (navigator.getVRDisplays) {
effect.setSize(window.innerWidth, window.innerHeight);
} else {
renderer.setSize(window.innerWidth, window.innerHeight);
}
});
}
function animate() {
if (vr_enabled) {
effect.requestAnimationFrame(animate);
} else {
requestAnimationFrame( animate );
}
//create fireworks at arbitrary times
updateFireworks();
createFireworks(1);
//update orbit controls if mouse moved
if (vr_enabled) {
controls.update();
} else {
orbit_controls.update();
}
render();
}
function render() {
if (vr_enabled) {
effect.render(scene, camera);
} else {
renderer.render( scene, camera );
}
}
function createFireworks(num) {
for (var i = 0; i < num; i++) {
if (shouldMakeFirework()) { //generate firework with certain probability
var dest_point = [];
//random firework position
var x = THREE.Math.randInt(-1000, 1000);
var y = THREE.Math.randInt(150, 250);
var z = THREE.Math.randInt(-600, -100);
//randomly assign start and end vectors
var start = new THREE.Vector3(x, -4000, z);
var end = new THREE.Vector3(x, y, z);
var color = getRandomColor();
var fireworks_geometry = new THREE.Geometry();
fireworks_geometry.vertices.push(start);
fireworks_geometry.colors.push(getRandomColor());
fireworks_geometry.verticesNeedUpdate = true;
var fireworks_material = new THREE.PointsMaterial({
size: 10,
color: getRandomColor(),
opacity: 1,
vertexColors: true,
transparent: true
});
dest_point.push(end);
//create new firework object
var firework_obj = {
geometry: fireworks_geometry,
material: fireworks_material,
system: new THREE.Points( fireworks_geometry, fireworks_material ),
dest: dest_point,
name: "fireworks" + firework_id
};
firework_id++;
firework_obj.system.name = firework_obj.name;
all_fireworks.push(firework_obj);
scene.add( firework_obj.system ); //add new firework object to scene
}
}
}
function updateFireworks() {
for (var i = all_fireworks.length - 1; i >= 0; i--) {
var current_firework = all_fireworks[i];
var all_points = current_firework.geometry.vertices;
//if the particle is being shot up into the air: slower speed
if (all_points.length == 1) {
all_points[0].x += (all_fireworks[i].dest[0].x - all_points[0].x) / 100000;
all_points[0].y += (all_fireworks[i].dest[0].y - all_points[0].y) / 100000;
all_points[0].z += (all_fireworks[i].dest[0].z - all_points[0].z) / 100000;
} else {
//if the firework has already exploded: faster speed
for (var j = 0; j < all_points.length; j++) {
all_points[j].x += (all_fireworks[i].dest[j].x - all_points[j].x) / 50;
all_points[j].y += (all_fireworks[i].dest[j].y - all_points[j].y) / 50;
all_points[j].z += (all_fireworks[i].dest[j].z - all_points[j].z) / 50;
}
}
all_fireworks[i].geometry.vertices = all_points;
all_fireworks[i].geometry.verticesNeedUpdate = true;
all_fireworks[i].system = new THREE.Points( all_fireworks[i].geometry, all_fireworks[i].material);
if (all_points.length == 1) { //see if firework has reached its end destination
if (all_points[0].x - all_fireworks[i].dest[0].x < 0.000000000001 ||
all_points[0].y - all_fireworks[i].dest[0].y < 0.000000000001 ||
all_points[0].z - all_fireworks[i].dest[0].z < 0.000000000001) {
explode_firework(i);
}
} else { //slowly fade out the fireworks already in existence
all_fireworks[i].material.opacity -= 0.01
all_fireworks[i].material.colorsNeedUpdate = true;
if (all_fireworks[i].material.opacity <= 0.0) {
remove_firework(i);
}
}
}
}
function explode_firework(index) {
//called when particle reaches its end destination
var to_explode = all_fireworks[index];
var to_explode_pos = to_explode.dest[0];
var new_geometry = new THREE.Geometry();
var new_material = new THREE.PointsMaterial({
size: 5,
color: getRandomColor(),
opacity: 1.0
});
var new_dest = [];
for (var i = 0; i < 40; i++) {
//add 40 particles at the initial firework's end destination
var particle_color = getRandomColor();
var x = THREE.Math.randInt(to_explode_pos.x - 20, to_explode_pos.x + 20);
var y = THREE.Math.randInt(to_explode_pos.y - 20, to_explode_pos.y + 20);
var z = THREE.Math.randInt(to_explode_pos.z - 20, to_explode_pos.z + 20);
var x_end = THREE.Math.randInt(to_explode_pos.x - 100, to_explode_pos.x + 100);
var y_end = THREE.Math.randInt(to_explode_pos.y - 100, to_explode_pos.y + 100);
var z_end = THREE.Math.randInt(to_explode_pos.z - 100, to_explode_pos.z + 100);
//emulate structure of single particle fireworks
var start = new THREE.Vector3(x, y, z);
var end = new THREE.Vector3(x_end, y_end, z_end);
new_geometry.vertices.push(start);
new_geometry.colors.push(getRandomColor());
new_dest.push(end);
}
//update firework object with new particles
all_fireworks[index].geometry = new_geometry;
all_fireworks[index].geometry.verticesNeedUpdate = true;
all_fireworks[index].material = new_material;
all_fireworks[index].system = new THREE.Points( all_fireworks[index].geometry, all_fireworks[index].material );
all_fireworks[index].dest = new_dest;
all_fireworks[index].name = "fireworks" + firework_id;
firework_id++;
all_fireworks[index].system.name = all_fireworks[index].name;
scene.add( all_fireworks[index].system );
}
function remove_firework(i) { //remove firework from scene once it's reached its destination
var to_remove = all_fireworks[i];
var to_remove_obj = scene.getObjectByName( to_remove.name );
scene.remove( to_remove_obj );
all_fireworks.splice(i, 1);
}
function makeRegenerateButton() { //button to regenerate the scene at will
var button = document.createElement( 'button' );
button.style.position = 'absolute';
button.style.left = 'calc(50% - 50px)';
button.style.top = '20px';
button.style.width = '100px';
button.style.border = '0';
button.style.padding = '8px';
button.style.cursor = 'pointer';
button.style.backgroundColor = '#000';
button.style.color = '#fff';
button.style.fontFamily = 'sans-serif';
button.style.fontSize = '13px';
button.style.fontStyle = 'normal';
button.style.textAlign = 'center';
button.style.zIndex = '999';
button.textContent = 'Regenerate scene!';
button.onclick = function() {
init();
};
return button;
}
</script>
</body>
</html>