-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·284 lines (250 loc) · 9.82 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>MinJae THREE.js Project</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
body {
font-family: Helvetica, Arial, sans-serif;
background-color: #fff;
color: #000;
margin: 0px;
height: 100%;
}
</style>
</head>
<body>
<script src="js/three.min.js"></script>
<script src="js/PointerLockControl.js"></script>
<script src="js/VREffects.js"></script>
<script src="js/VRControls.js"></script>
<script src="js/webVR.js"></script>
<script src="js/minjae.noise.js"></script>
<div id = "blocker">
<div style= "margin: 10pt" id="instructions">
<span style="font-size:32px"><b>Click to play</b></span><br>
Move your mouse or look around
</div>
</div>
<script>
var effect, ctr, noiseScale, subdivs, waterlevel;
var fireworks, fireworkInterval;
var light, bufferTexture, camera, scene, vrcontrols, controls, renderer;
var noise, terrain, geometry, water, terrainBaseHue;
var blocker = document.getElementById( 'blocker' );
var instructions = document.getElementById( 'instructions' );
// using code from Trung's demo for PointerLockControl
// http://www.html5rocks.com/en/tutorials/pointerlock/intro/
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
if ( havePointerLock ) {
var element = document.body;
var pointerlockchange = function ( event ) {
if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {
controls.enabled = true;
blocker.style.display = 'none';
} else {
controls.enabled = false;
blocker.style.display = '-webkit-box';
blocker.style.display = '-moz-box';
blocker.style.display = 'box';
instructions.style.display = '';
}
};
var pointerlockerror = function ( event ) {
instructions.style.display = '';
};
// Hook pointer lock state change events
document.addEventListener( 'pointerlockchange', pointerlockchange, false );
document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );
document.addEventListener( 'pointerlockerror', pointerlockerror, false );
document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );
instructions.style.display = 'none';
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
instructions.addEventListener( 'click', function ( event ) {
instructions.style.display = 'none';
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
}, false );
} else {
instructions.innerHTML = 'Your browser doesn\'t seem to support Pointer Lock API';
}
init();
animate();
if ( WEBVR.isAvailable() === false ) {
instructions.appendChild( WEBVR.getMessage() );
}
function init() {
//camera
camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 1, 1000 );
//scene
scene = new THREE.Scene();
//controls
controls = new THREE.PointerLockControls( camera );
scene.add( controls.getObject() );
//light
light = new THREE.HemisphereLight( 0xeeeeff, 0x777788, 0.75 );
light.position.set( 0.5, 1, 0.75 );
scene.add( light );
//raycaster
raycaster = new THREE.Raycaster();
//terrain
const terrainSideLength = 2000;
subdivs = 20;
noiseScale = 200;
waterlevel = 2.7;
noise = new noiseGen(subdivs, noiseScale);
geometry = new THREE.PlaneGeometry( terrainSideLength, terrainSideLength, subdivs, subdivs );
terrainBaseHue = Math.random();
generateTerrain();
geometry.rotateX( - Math.PI / 2 );
geometry.rotateY( Math.PI*Math.random());
terrain = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({vertexColors: THREE.VertexColors}));
water = new THREE.Mesh(new THREE.PlaneGeometry(terrainSideLength,terrainSideLength,0,0).rotateX( - Math.PI / 2 ),
new THREE.MeshBasicMaterial({ color: 0x3333ee}));
terrain.position.setY(-noiseScale);
water.position.setY(-noiseScale/waterlevel);
scene.add( terrain );
scene.add( water );
//fireworks
fireworks = new Array();
ctr = new THREE.Vector3();
fireworkInterval = 3000;
var timer = setInterval(shootFireworks, fireworkInterval);
//VR STUFF
if ( WEBVR.isAvailable() === true ) {
vrcontrols = new THREE.VRControls( camera );
document.body.appendChild( WEBVR.getButton( effect ) );
}
//renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor( 0xffffff );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight);
effect = new THREE.VREffect( renderer );
document.body.appendChild( renderer.domElement );
}
function shootFireworks() {
var scalar = 1000;
var fwg = new THREE.Geometry();
var fwm = new THREE.PointsMaterial( { size: 25, vertexColors: THREE.VertexColors} );
fwg.vertices.push(new THREE.Vector3());
fwg.colors[0] = randomColor();
var fw = new THREE.Points(fwg, fwm);
fw.position.add(new THREE.Vector3((Math.random()-0.5)*scalar, water.position.y, (Math.random()-0.5)*scalar));
scene.updateMatrixWorld();
fireworks.push(fw);
scene.add(fw);
}
function generateFireworkExplosion(fwi) {
fireworks[fwi].geometry.vertices.splice(0, fireworks[fwi].geometry.vertices.length);
var baseColor = randomColor();
for (var i = 0; i < 12; i ++) {
newvtx = new THREE.Vector3();
newvtx.setX(fireworks[fwi].position.x);
newvtx.setY(fireworks[fwi].position.y);
newvtx.setZ(fireworks[fwi].position.z);
fireworks[fwi].geometry.vertices.push(newvtx);
fireworks[fwi].geometry.colors[i] = variedColor(baseColor);
}
fireworks[fwi].geometry.verticesNeedUpdate = true;
fireworks[fwi].geometry.dynamic = true;
scene.remove(fireworks[fwi]);
scene.add(fireworks[fwi]);
}
function tickFireworks() {
var deleteIdx = new Array();
for(var i = 0; i < fireworks.length; i++) {
var fwheight = fireworks[i].position.y;
if (fireworks[i].geometry.vertices.length === 1 && fwheight > 500) {
generateFireworkExplosion(i);
} else if (fireworks[i].geometry.vertices.length === 1) {
fireworks[i].position.y += 4;
} else if(fireworks[i].geometry.vertices.length > 1) {
fireworkExplosionTick(fireworks[i]);
fireworks[i].position.y += 2;
}
if(fireworks[i].position.y >= 700) {
deleteIdx.push(i);
}
}
for(var i = 0; i < deleteIdx.length; i++) {
scene.remove(fireworks[deleteIdx[i]]);
fireworks.splice(deleteIdx[i], 1);
}
}
function fireworkExplosionTick (fwobj) {
fwobj.geometry.verticesNeedUpdate = true;
var deleteIdx = new Array();
var fwctr = fwobj.position;
var movementScalar = 2;
var disappearingDist = 100;
for (var i = 0; i < fwobj.geometry.vertices.length; i++) {
var dis2ctr = ctr.distanceTo(fwobj.geometry.vertices[i]);
if (dis2ctr >= disappearingDist) {
deleteIdx.push(i);
} else {
fwobj.geometry.colors[i].setHSL(fwobj.geometry.colors[i].getHSL().h,fwobj.geometry.colors[i].getHSL().s + Math.random(), (dis2ctr/disappearingDist));
}
fwobj.geometry.vertices[i].add(movementScalar * fwobj.geometry.vertices[i]);
}
for(var i = 0; i < deleteIdx.length; i++) {
fwobj.geometry.vertices.splice(deleteIdx[i], 1);
}
}
function variedColor(baseColor) {
var variationScalar = 0.1;
var result = baseColor.clone().offsetHSL(Math.random()*variationScalar,Math.random()*variationScalar,Math.random()*variationScalar);
return result;
}
function randomColor() {
var c = new THREE.Color();
c.setHSL(Math.random(),Math.random(),Math.random());
return c;
}
function generateTerrain() {
var faceCount = subdivs*subdivs*2;
var vertexIdx;
for (var i = 0; i < geometry.vertices.length; i++) {
geometry.vertices[i].z = noise.getHeightAt(i) ;
}
for(var i = 0; i < faceCount; i++) {
vertexIdx = Math.floor(i/4);
geometry.faces[i].vertexColors[0] = heightBasedColor(geometry.vertices[geometry.faces[i].a].z);
geometry.faces[i].vertexColors[1] = heightBasedColor(geometry.vertices[geometry.faces[i].b].z);
geometry.faces[i].vertexColors[2] = heightBasedColor(geometry.vertices[geometry.faces[i].c].z);
}
}
function heightBasedColor(height) {
var a = (noiseScale + height) / 2;
var h = terrainBaseHue + (noiseScale - a)/noiseScale;
var s = a/noiseScale - 0.2;
var l = (1 + Math.sin(2*Math.PI/noiseScale*a)/2);
var c = new THREE.Color();
c.setHSL(h,s,l);
return c;
}
function animate() {
tickFireworks();
var center = new THREE.Vector2();
raycaster.setFromCamera(center, camera);
requestAnimationFrame( animate );
if ( WEBVR.isAvailable() === true ) vrcontrols.update();
effect.render(scene, camera);
}
//making the window resizable
//source: http://stackoverflow.com/questions/20290402/three-js-resizing-canvas
window.addEventListener('resize', onResize, false);
function onResize(){
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>