-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwebgl_wave.js
536 lines (413 loc) · 15.4 KB
/
webgl_wave.js
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
Function: WebGl Animation for the homepage
Usage: The function takes 2 arguments, the "action" that can be : "init", "kill", "play" and "pause" and the "targe" div where the script creates the canvas element
*/
var SEPARATION = 125, AMOUNTX = 35, AMOUNTY = 35;
var container, stats;
var camera, scene, renderer;
var particles, particle, count = 0, particles_globe = [];
var mouseX = 0, mouseY = 0;
var objTo;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var animation_type;
var rotation_speed = 0.002;
var timeout = null;
var $viewPort = $(document);
var $_body = $('body');
var $_html = $('html');
function webglWave(action, target){
if( $_body.hasClass('ismobile') || $_html.hasClass('ie9'))
return false;
/* Logic Control to call the animation */
if(action == 'init'){
if(target == 'page404'){
initGlobeError(target);
addRemoveListeners(true);
animateGlobeError();
}
else{
/* Randomize the animation that is generated */
//animation_type = Math.floor( (Math.random() * 2) + 0 );
animation_type = 1;
if(animation_type == 0){
initWave(target);
addRemoveListeners(true);
animateWave();
}
else if(animation_type == 1){
initGlobe(target);
addRemoveListeners(true);
animateGlobe();
}
}
}
else if(action == 'kill'){
if(target == 'page404'){
stopAnimationGlobeError(animation_id);
killAnimationGlobeError();
addRemoveListeners(false);
}
else{
if(animation_type == 0 ){
stopAnimationWave(animation_id);
killAnimationWave();
addRemoveListeners(false);
}
else if(animation_type == 1){
stopAnimationGlobe(animation_id);
killAnimationGlobe();
addRemoveListeners(false);
}
}
}
else if(action == 'play'){
if(target == 'page404'){
addRemoveListeners(true);
animateGlobeError();
}
else{
if(animation_type == 0){
addRemoveListeners(true);
animateWave();
}
else if(animation_type == 1){
addRemoveListeners(true);
animateGlobe();
}
}
}
else if(action == 'stop'){
if(target == 'page404'){
addRemoveListeners(false);
stopAnimationGlobeError(animation_id);
}
else{
if(animation_type == 0){
addRemoveListeners(false);
stopAnimationWave(animation_id);
}
else if(animation_type == 1){
addRemoveListeners(false);
stopAnimationGlobe(animation_id);
}
}
}
}
/* WAVE */
function initWave(target) {
/* Creation of the canvas element in the "target" div */
objTo = document.getElementById(target);
container = document.getElementById('webgl-canvas');
objTo.appendChild( container );
/* Camera and scene creation */
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;
camera.position.y = 100;
camera.position.y = 1000;
scene = new THREE.Scene();
/* Particles Creation */
particles = new Array();
var PI2 = Math.PI * 2;
var i = 0;
for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
var material = new THREE.SpriteCanvasMaterial( {
color: 0xffffff,
transparent : true,
program: function ( context ) {
context.beginPath();
context.arc( 0, 0, 0.5, 0, PI2, true );
context.fill();
}
} );
particle = particles[ i ++ ] = new THREE.Sprite( material );
particle.position.x = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 );
particle.position.z = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 );
scene.add( particle );
particle.material.opacity = 0.4;
}
}
/* Renderer Creation */
renderer = new THREE.CanvasRenderer({ alpha: true });
renderer.setClearColor( 0x0000, 0);
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
// stats = new Stats();
// stats.domElement.style.position = 'absolute';
// stats.domElement.style.top = '0px';
// container.appendChild( stats.domElement );
}
/* User interaction */
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
/* Wave animation start */
function animateWave() {
animation_id = requestAnimationFrame( animateWave );
renderWave();
//stats.update();
}
/* Wave Animation */
function renderWave() {
camera.position.x += ( mouseX - camera.position.x ) * .01;
camera.position.y += ( mouseY - camera.position.y ) * .005;
camera.lookAt( scene.position );
var i = 0;
for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
particle = particles[ i++ ];
particle.position.y = ( Math.sin( ( ix + count ) * 0.3 ) * 50 ) + ( Math.sin( ( iy + count ) * 0.5 ) * 50 );
particle.scale.x = particle.scale.y = ( Math.sin( ( ix + count ) * 0.3 ) + 1 ) * 4 + ( Math.sin( ( iy + count ) * 0.5 ) + 1 ) * 4;
opacity = (Math.abs(particle.position.y) /100);
if(opacity < 0.5)
opacity = 0.5;
if (opacity > 1)
opacity = 1;
particle.material.opacity = opacity;
}
}
renderer.render( scene, camera );
count += 0.03;
}
/* Clear the scene and kill all objects */
function killAnimationWave(){
if (scene) {
while (scene.children.length > 0) {
scene.remove(scene.children[scene.children.length - 1]);
}
}
$('#webgl-canvas > canvas').remove();
}
/* Pause the animation*/
function stopAnimationWave(animation_id){
cancelAnimationFrame(animation_id);
}
/* GLOBE */
function initGlobe(target) {
/* Creation of the canvas element in the "target" div */
objTo = document.getElementById(target);
container = document.getElementById('webgl-canvas');
objTo.appendChild( container );
/* Camera and scene creation */
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 500;
scene = new THREE.Scene();
/* Particles Creation */
var PI2 = Math.PI * 2;
var program = function ( context ) {
context.beginPath();
context.arc( 0, 0, 25, 0, PI2, true );
context.fill();
}
var PI2 = Math.PI * 2;
for ( var i = 0; i < 500; i ++ ) {
var material = new THREE.SpriteCanvasMaterial( {
color: 0xffffff,
transparent : true,
program: function ( context ) {
context.beginPath();
context.arc( 0, 0, 0.5, 0, PI2, true );
context.fill();
}
} );
particle = new THREE.Sprite( material );
particle.position.x = Math.random() * 2 - 1;
particle.position.y = Math.random() * 2 - 1;
particle.position.z = Math.random() * 2 - 1;
particle.position.normalize();
particle.position.multiplyScalar( Math.random() * 10 + 450 );
particle.scale.multiplyScalar( 4 + Math.random()*2 );
particle.material.opacity = 0.1;
scene.add( particle );
particles_globe.push(particle);
}
/* Particles Creation */
for (var i = 0; i < 500; i++) {
var geometry = new THREE.Geometry();
var vertex = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
vertex.normalize();
vertex.multiplyScalar( 450 );
geometry.vertices.push( vertex );
var vertex2 = vertex.clone();
vertex2.multiplyScalar( Math.random() * 0.3 + 1 );
geometry.vertices.push( vertex2 );
var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.3 } ) );
scene.add( line );
}
/* Renderer Creation */
renderer = new THREE.CanvasRenderer({ alpha: true });
renderer.setClearColor( 0x0000, 0);
renderer.setSize( window.innerWidth , window.innerHeight );
container.appendChild( renderer.domElement );
}
/* Globe animation start */
function animateGlobe() {
animation_id = requestAnimationFrame( animateGlobe );
renderGlobe();
}
/* Globe Animation */
function renderGlobe() {
var $webglCanvas = $('body:hover');
var x = camera.position.x, y = camera.position.y, z = camera.position.z;
if ($webglCanvas.length != 0 && timeout != null) {
camera.position.x += (( mouseX - camera.position.x ) * .05) ;
}
else{
camera.position.x = x * Math.cos(rotation_speed) - z * Math.sin(rotation_speed);
camera.position.z = z * Math.cos(rotation_speed) + x * Math.sin(rotation_speed);
}
camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05 ;
$viewPort.on('mousemove', function() {
if (timeout !== null) {
clearTimeout(timeout);
}
timeout = setTimeout(function() {
timeout = null;
}, 600);
});
camera.lookAt( scene.position );
var i = 0;
for ( var i = 0; i < particles_globe.length ; i ++ ) {
particle = particles_globe[ i++ ];
temp = ( Math.sin( ( i + count ) * 0.3 ) * 50 ) + ( Math.sin( ( i + count ) * 0.5 ) * 0.50 );
opacity = (Math.abs(temp) /50) + 0.1
if (opacity > 1)
opacity = 1;
particle.material.opacity = opacity;
}
renderer.render( scene, camera );
count += 0.1;
renderer.render( scene, camera );
}
/* Clear the scene and kill all objects */
function killAnimationGlobe(){
if (scene) {
while (scene.children.length > 0) {
scene.remove(scene.children[scene.children.length - 1]);
}
}
$('#webgl-canvas > canvas').remove();
}
/* Pause the animation*/
function stopAnimationGlobe(animation_id){
cancelAnimationFrame(animation_id);
}
/* GLOBE ERROR*/
function initGlobeError(target) {
/* Creation of the canvas element in the "target" div */
objTo = document.getElementById(target);
container = document.getElementById('webgl-canvas');
objTo.appendChild( container );
/* Camera and scene creation */
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 500;
scene = new THREE.Scene();
/* Particles Creation */
var PI2 = Math.PI * 2;
var program = function ( context ) {
context.beginPath();
context.arc( 0, 0, 25, 0, PI2, true );
context.fill();
}
var PI2 = Math.PI * 2;
for ( var i = 0; i < 250; i ++ ) {
var material = new THREE.SpriteCanvasMaterial( {
color: 0xffffff,
transparent : true,
program: function ( context ) {
context.beginPath();
context.arc( 0, 0, 0.5, 0, PI2, true );
context.fill();
}
} );
particle = new THREE.Sprite( material );
particle.position.x = Math.random() * 2 - 1;
particle.position.y = Math.random() * 2 - 1;
particle.position.z = Math.random() * 2 - 1;
particle.position.normalize();
particle.position.multiplyScalar( Math.random() * 10 + 450 );
particle.scale.multiplyScalar( 4 + Math.random()*2 );
particle.material.opacity = 0.1;
scene.add( particle );
particles_globe.push(particle);
}
/* Renderer Creation */
renderer = new THREE.CanvasRenderer({ alpha: true });
renderer.setClearColor( 0x0000, 0);
renderer.setSize( window.innerWidth , window.innerHeight );
container.appendChild( renderer.domElement );
}
/* Globe animation start */
function animateGlobeError() {
animation_id = requestAnimationFrame( animateGlobeError );
renderGlobeError();
}
/* Globe Animation */
function renderGlobeError() {
var rotation_speed = 0.002;
var x = camera.position.x, y = camera.position.y, z = camera.position.z;
camera.position.x = x * Math.cos(rotation_speed) + z * Math.sin(rotation_speed);
camera.position.z = z * Math.cos(rotation_speed) - x * Math.sin(rotation_speed);
camera.lookAt( scene.position );
renderer.render( scene, camera );
count += 0.1;
renderer.render( scene, camera );
}
/* Clear the scene and kill all objects */
function killAnimationGlobeError(){
if (scene) {
while (scene.children.length > 0) {
scene.remove(scene.children[scene.children.length - 1]);
}
}
$('#webgl-canvas > canvas').remove();
}
/* Pause the animation*/
function stopAnimationGlobeError(animation_id){
cancelAnimationFrame(animation_id);
}
/* GLOBAL FUNCTIONS */
/* User Listeners - takes an argument (true or false) to turn on and off as needed */
function addRemoveListeners(action){
if(action){
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
window.addEventListener( 'resize', onWindowResize, false );
}
else{
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'touchstart', onDocumentTouchStart, false );
document.removeEventListener( 'touchmove', onDocumentTouchMove, false );
window.removeEventListener( 'resize', onWindowResize, false );
}
}
/* User interaction */
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
/*mouseY = event.clientY - windowHalfY;*/
mouseY = event.clientY + 150;
}
/* User interaction */
function onDocumentTouchStart( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
/*mouseY = event.touches[ 0 ].pageY - windowHalfY;*/
mouseY = - event.touches[ 0 ].pageY;
}
}
/* User interaction */
function onDocumentTouchMove( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
/* mouseY = event.touches[ 0 ].pageY - windowHalfY;*/
mouseY = - event.touches[ 0 ].pageY;
}
}