-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
234 lines (212 loc) · 8.57 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ray Dongho Kim_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: Monospace;
background-color: #101010;
color: #fff;
margin: 0px;
overflow: hidden;
}
a {
color: #f00;
}
</style>
</head>
<body>
<script src="js/threejs/three.min.js"></script>
<script src="js/controls/PointerLockControl.js"></script>
<script src="js/controls/VRControls.js"></script>
<script src="js/effects/VREffect.js"></script>
<script src="js/vr/WebVR.js"></script>
<script>
if ( WEBVR.isAvailable() === false ) {
document.body.appendChild( WEBVR.getMessage() );
}
var terrain;
var planegeometry;
var clock = new THREE.Clock();
var container;
var camera, scene, raycaster, renderer;
var effect, controls;
var room;
var p = [];
for (var j = 0; j < 1000; j++) {
p.push(new THREE.Vector2(Math.random()*100, Math.random()*100));
}
var particle;
var blasted;
var particles = [];
var velocityx = [];
var velocityy = [];
var velocityz = [];
var lifetimes = [];
var firsttime = [];
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
container.appendChild( info );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
scene.add( camera );
// creating terrain=================================================================================================================
planegeometry = new THREE.PlaneGeometry(2000, 2000, 64, 64);
planegeometry.rotateX( - Math.PI / 2 );
var planematerial = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: false, vertexColors: THREE.VertexColors} );
var color;
for ( var i = 0; i < planegeometry.vertices.length; i++) {
planegeometry.vertices[i].y = perlinNoise(planegeometry.vertices[i].x, planegeometry.vertices[i].z);
color = new THREE.Color( 0xff0000 );
color.r = 100/255;
color.g = planegeometry.vertices[i].y/70;
color.b = 72/255;
planegeometry.colors[i] = color;
}
var face, vertexIndex, numberOfSides;
var faceIndices = [ 'a', 'b', 'c', 'd' ];
for ( var i = 0; i < planegeometry.faces.length; i++ ) {
face = planegeometry.faces[ i ];
numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
for( var j = 0; j < numberOfSides; j++ ) {
vertexIndex = face[ faceIndices[ j ] ];
face.vertexColors[ j ] = this.planegeometry.colors[vertexIndex];
}
}
terrain = new THREE.Mesh(planegeometry, planematerial);
terrain.position.x = 20;
terrain.position.y -= 100;
scene.add(terrain);
var light = new THREE.PointLight( 0xffffff, 1.4 );
light.position.set( 50, 150, -50 );
scene.add( light );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setClearColor( 0x040027 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.sortObjects = false;
container.appendChild( renderer.domElement );
controls = new THREE.VRControls( camera );
effect = new THREE.VREffect( renderer );
if ( navigator.getVRDisplays ) {
navigator.getVRDisplays()
.then( function ( displays ) {
effect.setVRDisplay( displays[ 0 ] );
controls.setVRDisplay( displays[ 0 ] );
} )
.catch( function () {
// no displays
} );
document.body.appendChild( WEBVR.getButton( effect ) );
}
//
window.addEventListener( 'resize', onWindowResize, false );
}
// Perlin Noise Function Implementing ================================================================================================================================
function perlinNoise(x, y) {
var unitx = Math.floor(x);
var unity = Math.floor(y);
var width = 1000;
var gradV1 = p[Math.abs((unitx+350+(unity+350)*width)%623)];
var gradV2 = p[Math.abs((unitx+350+1+(unity+350)*width)%792)];
var gradV3 = p[Math.abs((unitx+350+(unity+1+350)*width)%687)];
var gradV4 = p[Math.abs((unitx+350+1+(unity+351)*width)%723)];
var dot1 = gradV1.x * (x-unitx) + gradV1.y * (y-unity);
var dot2 = gradV2.x * (1-(x-unitx)) + gradV2.y * (y-unity);
var dot3 = gradV3.x * (x-unitx) + gradV3.y * (1-(y-unity));
var dot4 = gradV4.x * (1-(x-unitx)) + gradV4.y * (1-(y-unity));
var x1 = lerp(dot1, dot2, (x-unitx));
var x2 = lerp(dot3, dot4, (x-unitx));
var y1 = lerp(x1, x2, (y-unity));
return y1;
}
//====================================================================================================================================================================
// Lerp Function ====================================================================================================================================================================
function lerp(x, y, w) {
return (x + w*(y-x));
}
//===================================================================================================================================================================================
//Tick function=======================================================================================================================================================================
function tick() {
if (Math.random() < 0.07) {
particle = new THREE.Mesh( new THREE.SphereGeometry( 2, 16, 16 ), new THREE.MeshPhongMaterial( { color: (Math.random() * 0xffff00+0xffffff)/2, shininess: 100} ));
particle.position.x = Math.random() * 2000 - 1000;
particle.position.y = -100;
particle.position.z = Math.random() * 2000 - 1000;
particles.push(particle);
velocityx.push(0);
velocityy.push(7 - Math.random()/5);
velocityz.push(0);
lifetimes.push(160-Math.random()*10);
firsttime.push(1);
scene.add(particle);
}
for (var i = 0; i < particles.length; i++) {
if (lifetimes[i] <= 0) {
if (firsttime[i] == 1) {
blast(particles[i].position.x, particles[i].position.y+velocityy[i], particles[i].position.z);
}
scene.remove(particles[i]);
particles.splice(i, 1);
velocityx.splice(i, 1);
velocityy.splice(i, 1);
velocityz.splice(i, 1);
lifetimes.splice(i, 1);
firsttime.splice(i, 1);
} else {
particles[i].position.x += velocityx[i];
particles[i].position.y += velocityy[i];
particles[i].position.z += velocityz[i];
velocityy[i] -= 0.05;
lifetimes[i]--;
}
}
}
//===================================================================================================================================================================================
//blast function=====================================================================================================================================================================
function blast(x, y, z) {
for (var i = 0; i < 30; i++) {
blasted = new THREE.Mesh( new THREE.SphereGeometry( 1.5, 4, 4 ), new THREE.MeshPhongMaterial( { color: (Math.random() * 0xffff00+0xffffff)/2, shininess: 20} ));
particles.push(blasted);
blasted.position.x = x
blasted.position.y = y
blasted.position.z = z
velocityx.push(Math.random());
velocityy.push(Math.random());
velocityz.push(Math.random());
lifetimes.push(90-Math.random()*30);
firsttime.push(0);
scene.add(blasted);
}
}
//===========================================================================================================================================================================
// for mouse click motions===================================================================================================
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
effect.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
effect.requestAnimationFrame( animate );
render();
}
function render() {
tick();
controls.update();
effect.render( scene, camera );
}
</script>
</body>
</html>