-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
425 lines (324 loc) · 10.3 KB
/
main.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
import './style.css'
import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import {GUI} from 'dat.gui';
import {MathUtils} from 'three';
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 0.1, 1000);
let renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#bg'),
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
let clock = new THREE.Clock();
let controls = new OrbitControls(camera, renderer.domElement);
//controls.minPolarAngle = Math.PI/4;
controls.maxPolarAngle = Math.PI*0.395;
// set gui
const gui = new GUI();
// helper
//var axesHelper = new THREE.AxesHelper(40);
//scene.add(axesHelper);
var boxGeometry = new THREE.BoxGeometry(160, 2, 160, 50, 1, 50);
function getRandomVal(inf, sup){
let r = THREE.MathUtils.randFloat(inf, sup);
r = Math.ceil(r*10)/10;
return r;
}
let randomVal = getRandomVal(0.1, 3.9);
let velMarea = 0.9;
//alert(randomVal);
// audio analyser
var audio = new Audio("audio.mp3");
window.onload = (event) => {
audio.play();
};
// sounds
var context = new AudioContext();
var src = context.createMediaElementSource(audio);
var analyser = context.createAnalyser();
src.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 32;
var bufferLength = analyser.frequencyBinCount;
var dataArray = new Uint8Array(bufferLength);
console.log(dataArray);
let intensidadOleaje = 4*(dataArray[11]/255);
const uniformsData = {
u_time: {
type: 'f',
value: clock.getElapsedTime(),
},
u_random: {
type: 'f',
//value: randomVal,
value: intensidadOleaje,
},
u_velMarea: {
type: 'f',
value: velMarea,
}
};
// shader code
const mareaPlana_VS = `
// projectionMatrix, modelViewMatrix, position ya las definió threejs
uniform float u_time;
varying vec3 pos;
void main(){
vec4 result;
pos = position;
//result = vec4(position.x, 4.0*sin(position.z/4.0 + u_time) + 2.0*cos(position.x/6.0 + u_time) + position.y, position.z, 1.0);
//result = vec4(position.x, sin(position.z + u_time) + position.y, position.z, 1.0);
result = vec4(position.x, position.y, position.z, 1.0);
gl_Position = projectionMatrix
* modelViewMatrix
* result;
}
`;
const mareaTranquila_VS = `
// projectionMatrix, modelViewMatrix, position ya las definió threejs
// uniform = constantes con valores iniciales
uniform float u_time;
uniform float u_random;
varying vec3 pos;
void main(){
vec4 result;
pos = position;
result = vec4(
position.x, // positionX
u_random*sin(position.z/4.0 + 5.0*u_time)
+ u_random*cos(position.x/6.0 + u_time)
+ position.y, // positionY
position.z, // positionZ
1.0
);
//result = vec4(position.x, sin(position.z + u_time) + position.y, position.z, 1.0);
//result = vec4(position.x, position.y, position.z, 1.0);
gl_Position = projectionMatrix
* modelViewMatrix
* result;
}
`;
const mareaAgitada_VS = `
// projectionMatrix, modelViewMatrix, position ya las definió threejs
uniform float u_time;
varying vec3 pos;
void main(){
vec4 result;
pos = position;
//result = vec4(position.x, 4.0*sin(position.z/4.0 + u_time) + 2.0*cos(position.x/6.0 + u_time) + position.y, position.z, 1.0);
//result = vec4(position.x, sin(position.z + u_time) + position.y, position.z, 1.0);
//result = vec4(position.x, position.y, position.z, 1.0);
if (position.x <= -20.0 || position.x >= 20.0){
result = vec4(position.x, 8.0*sin(position.z/4.0 + u_time) + 2.0*cos(position.x/6.0 + u_time) + position.y, position.z, 1.0);
} else if (position.x >= -20.0 && position.x <= -10.0){
result = vec4(position.x, 6.0*sin(position.z/4.0 + u_time) + 2.0*cos(position.x/6.0 + u_time) + position.y, position.z, 1.0);
} else if (position.x >= 10.0 && position.x <= 20.0){
result = vec4(position.x, 6.0*sin(position.z/4.0 + u_time) + 2.0*cos(position.x/6.0 + u_time) + position.y, position.z, 1.0);
} else {
//result = vec4(position.x, position.y, position.z, 1.0);
result = vec4(position.x, 4.0*sin(position.z/4.0 + u_time) + 2.0*cos(position.x/6.0 + u_time) + position.y, position.z, 1.0);
}
gl_Position = projectionMatrix
* modelViewMatrix
* result;
}
`;
const colorNormal_FS = `
uniform float u_time;
uniform float u_velMarea;
varying vec3 pos;
void main(){
gl_FragColor = vec4(tan(pos.x)-8.0*sin(pos.z/4.0 - u_time/u_velMarea), 0.0, sin(1.5*u_time), 1.0);
}
`;
const colorNormal2_FS = `
uniform float u_time;
varying vec3 pos;
void main(){
gl_FragColor = vec4(abs(sin(u_time)), 0.0, tan(pos.z)-8.0*sin(pos.x/4.0 + u_time), 1.0);
}
`;
const colorPsico_FS = `
uniform float u_time;
varying vec3 pos;
void main(){
if (pos.x >= 0.0){
gl_FragColor = vec4(abs(sin(u_time)), -abs(sin(u_time)), abs(cos(u_time)), 1.0);
} else {
gl_FragColor = vec4(abs(cos(u_time)), abs(cos(u_time))-0.5, abs(sin(u_time)), 1.0);
}
}
`;
// creating materials
var material = new THREE.ShaderMaterial({
wireframe: false,
uniforms: uniformsData,
vertexShader: mareaTranquila_VS,
fragmentShader: colorNormal_FS,
});
var material2 = new THREE.ShaderMaterial({
wireframe: false,
uniforms: uniformsData,
vertexShader: mareaTranquila_VS,
fragmentShader: colorNormal2_FS,
});
material.setValues({wireframe:false}); // wireframe on/off
material.setValues({vertexShader:mareaTranquila_VS});
// creating ocean
var box = new THREE.Mesh(boxGeometry, material);
scene.add(box);
// GUI elements
gui.add(box.material, 'wireframe');
//gui.add(uniformsData.u_random, "value", -3.9, 3.9, 0.1).name('Fuerza del Oleaje');
gui.add(uniformsData.u_velMarea, "value", 0.1, 0.9, 0.01).name('Velocidad de la marea');
//gui.add(opc, 'isPsico');
var sphereGeometry = new THREE.SphereGeometry(6, 20, 20);
var sphere = new THREE.Mesh(sphereGeometry, material2);
sphere.position.set(0, 6, 0);
sphere.castShadow = true;
scene.add(sphere);
camera.position.set(-15, 15, 15);
camera.lookAt(0,0,0);
const light = new THREE.PointLight(0xffffff, 0.5, 10);
light.position.set(0, 20, 0);
scene.add(light);
// trayectoria blezier
// curva1
const curve1 = new THREE.CubicBezierCurve3(
new THREE.Vector3( -30, 30, 0 ),
new THREE.Vector3( -30, 30, -30 ),
new THREE.Vector3( -30, 20, -30 ),
new THREE.Vector3( 0, 20, -30 ),
);
const points1 = curve1.getPoints( 50 );
const b1Geometry = new THREE.BufferGeometry().setFromPoints( points1 );
const b1Material = new THREE.LineBasicMaterial( {color: 0xff0000} );
b1Material.transparent = false;
b1Material.opacity = 0.0;
// Create the final object to add to the scene
const curveObject1 = new THREE.Line( b1Geometry, b1Material );
scene.add(curveObject1);
// curva2
const curve2 = new THREE.CubicBezierCurve3(
new THREE.Vector3( 0, 20, -30 ),
new THREE.Vector3( 30, 20, -30 ),
new THREE.Vector3( 30, 30, -30 ),
new THREE.Vector3( 30, 30, 0 ),
);
const points2 = curve2.getPoints( 50 );
const b2Geometry = new THREE.BufferGeometry().setFromPoints( points2 );
const b2Material = new THREE.LineBasicMaterial( { color: 0x00ff00 } );
// Create the final object to add to the scene
const curveObject2 = new THREE.Line( b2Geometry, b2Material );
scene.add(curveObject2);
// curva3
const curve3 = new THREE.CubicBezierCurve3(
new THREE.Vector3( 30, 30, 0 ),
new THREE.Vector3( 30, 30, 30 ),
new THREE.Vector3( 30, 20, 30 ),
new THREE.Vector3( 0, 20, 30 ),
);
const points3 = curve3.getPoints( 50 );
const b3Geometry = new THREE.BufferGeometry().setFromPoints( points3 );
const b3Material = new THREE.LineBasicMaterial( { color: 0x0000ff } );
// Create the final object to add to the scene
const curveObject3 = new THREE.Line( b3Geometry, b3Material );
scene.add(curveObject3);
// curva4
const curve4 = new THREE.CubicBezierCurve3(
new THREE.Vector3( 0, 20, 30 ),
new THREE.Vector3( -30, 20, 30 ),
new THREE.Vector3( -30, 30, 30 ),
new THREE.Vector3( -30, 30, 0 ),
);
const points4 = curve4.getPoints( 50 );
const b4Geometry = new THREE.BufferGeometry().setFromPoints( points4 );
const b4Material = new THREE.LineBasicMaterial( { color: 0x0ffff0 } );
// Create the final object to add to the scene
const curveObject4 = new THREE.Line( b4Geometry, b4Material );
scene.add(curveObject4);
//curveObject4.visible = false;
//const linesBezier = new THREE.Group();
//linesBezier.add(curveObject1, curveObject2, curveObject3, curveObject4);
//linesBezier.visible = true;
//gui.add(linesBezier, 'visible');
curveObject1.visible = false;
gui.add(curveObject1, 'visible');
// --------------------------
// set camera points
const p1 = points1.slice(0, 50);
const p2 = points2.slice(0, 50);
const p3 = points3.slice(0, 50);
const p4 = points4.slice(0, 50);
let r = [];
r = [].concat(p1, p2, p3, p4);
//console.log(r);
function setBlezierPos(pos){
camera.position.set(r[pos].x, r[pos].y, r[pos].z);
}
controls.update();
function onWindowResize( event ) {
renderer.setSize( window.innerWidth, window.innerHeight );
}
onWindowResize();
window.addEventListener( 'resize', onWindowResize);
const onKeyDown = function(e){
switch(e.code){
case 'KeyM':
mode = (mode < 3)?mode + 1:0;
if (mode == 2){
controls.autoRotate = true;
controls.autoRotateSpeed = 3;
camera.position.set(0, 30, 0);
} else {
controls.autoRotate = false;
camera.position.set(-15, 15, 15);
}
break;
}
}
document.addEventListener('keydown', onKeyDown);
let step = 0;
let pointPosition = 0;
let mode = 0;
var last = 0;
function animate(now){
analyser.getByteFrequencyData(dataArray);
requestAnimationFrame(animate);
controls.update();
// actualiza uniforms
uniformsData.u_time.value = clock.getElapsedTime();
uniformsData.u_random.value = 10*((dataArray[8]/255)-0.5);
//step += 0.1;
if (curveObject1.visible){
curveObject2.visible = true;
curveObject3.visible = true;
curveObject4.visible = true;
} else {
curveObject2.visible = false;
curveObject3.visible = false;
curveObject4.visible = false;
}
if(!last || now - last >= 20*1000){
//console.log(dataArray[5]/255);
//console.log(dataArray);
}
switch(mode){
case 0:
camera.position.x = Math.cos(step) * 25;
camera.position.z = Math.sin(step) * 45;
camera.position.y = Math.sin(step + Math.PI/4) * 10 + 20;
step += 0.01;
break;
case 1:
setBlezierPos(pointPosition);
pointPosition += 1;
if (pointPosition > 199){
pointPosition = 0;
}
break;
}
renderer.render(scene, camera);
}
animate();