-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
539 lines (448 loc) · 20.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
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
537
538
539
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<!-- -->
<link type="text/css" rel="stylesheet" href="style.css">
<link rel = "icon" type = "image/png" href = "./favicon.png" >
<title>BackToGolfTour!</title>
</head>
<body>
<!-- -->
<canvas id="renderCanvas"></canvas>
<script type="module">
import * as THREE from '/lib/three.module.js';
import { FlyControls } from '/lib/FlyControls.js'
const canvas = document.querySelector( "#renderCanvas" );
let renderer = null, scene = null, camera = null;
/** Render Settings **/
renderer = new THREE.WebGLRenderer( {canvas: canvas, antialias: true, } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio( Math.min( window.devicePixelRatio, 2 ) );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 0.60;
/** Scene **/
scene = new THREE.Scene();
// Axes Helper, red(x), blue(z), green(y)
//const axesHelper = new THREE.AxesHelper( 5 );
//scene.add( axesHelper );
/** Camera **/
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.y = 4;
camera.rotation.y = Math.PI /2;
camera.position.x = 15;
scene.add( camera );
// Controls
const controls = new FlyControls( camera, renderer.domElement );
controls.movementSpeed = 10;
controls.rollSpeed = 0.5;
controls.autoForward = false;
controls.dragToLook = true;
// cubemap
{
const loader = new THREE.CubeTextureLoader();
const texture = loader.load([
'../images/skyCubemap/right_sky.png',
'../images/skyCubemap/left_sky.png',
'../images/skyCubemap/top_sky.png',
'../images/skyCubemap/bottom_sky.png',
'../images/skyCubemap/front_sky.png',
'../images/skyCubemap/back_sky.png'
]);
scene.background = texture;
}
//AUDIO
const listener = new THREE.AudioListener();
camera.add( listener );
const vientoSound = new THREE.Audio( listener );
const audioLoader = new THREE.AudioLoader();
audioLoader.load( '../sounds/mixkit-meadow-wind-light-1166.wav', function( buffer ) {
vientoSound.setBuffer( buffer );
vientoSound.setLoop( true );
vientoSound.setVolume( 5.1 );
vientoSound.play();
});
const engineAudioMesh = new THREE.Object3D();
scene.add( engineAudioMesh );
engineAudioMesh.add( vientoSound );
/** Lights **/
// Ambient light
const ambientLight = new THREE.AmbientLight( '#ffffff', 0.5 );
scene.add( ambientLight );
// Directional light
const sunLight = new THREE.DirectionalLight( '#ffffff', 0.5 );
sunLight.position.set( 2, 7, 0 );
scene.add( sunLight );
/* Textures && NormalMap*/
const loadManager = new THREE.LoadingManager();
const textureLoader = new THREE.TextureLoader(loadManager);
const pastoTexture = textureLoader.load('./images/greengrass.jpg');
const aguaTexture = textureLoader.load('./images/water.jpg');
const sueloTexture = textureLoader.load('./images/ground.jpg');
sueloTexture.repeat.set(1, 0.2);
pastoTexture.repeat.set(0.9, 0.9);
const pelotaTexture = textureLoader.load('./images/pelota.png');
/** Mesh && Geometry**/
///////////////////////////////////////////////////////////////////////////////// INICIO BANDERA DE GOLF
const banderaGroup = new THREE.Group();
//La varilla de la bandera
const banderaMesh = new THREE.Mesh(
new THREE.CylinderBufferGeometry( 0.03, 0.03, 2.5, 32 ),
new THREE.MeshStandardMaterial( { color: 'white' } )
);
banderaMesh.position.y = 0.76;
//El hoyo de la bandera
const hoyoMesh = new THREE.Mesh(
new THREE.CylinderBufferGeometry( 0.2, 0.2, 0.5, 32, 32, true ),
new THREE.MeshStandardMaterial( { color: '#A0522D', side: THREE.DoubleSide } )
);
hoyoMesh.position.y = -0.25+0.001;
const tapaMesh = new THREE.Mesh(
new THREE.CircleGeometry( 0.2, 32 ),
new THREE.MeshBasicMaterial( { color: '#A0522D', side: THREE.DoubleSide } )
);
tapaMesh.rotation.x = Math.PI / 2;
tapaMesh.position.y = -0.5;
const banderaOndulante = new THREE.Mesh(
new THREE.PlaneGeometry( 5/6,3/6, 20, 20),
new THREE.MeshStandardMaterial({ color: 'red', wireframe:false, side: THREE.DoubleSide})
);
banderaOndulante.position.set( 0.4, 1.75, 0 );
banderaGroup.add( hoyoMesh, tapaMesh, banderaMesh, banderaOndulante );
// Comentar para quitar de la escena.
scene.add( banderaGroup );
function banderaWind( elapsedTime ){
banderaOndulante.geometry.vertices.map( vertices => {
const MovX1 = 0.5 * Math.sin( vertices.x * ( 2/6 ) + elapsedTime );
const MovX2 = 0.25 * Math.sin(vertices.x * ( 3/6 ) + elapsedTime * 2/6 );
const MovY1 = 0.1 * Math.sin(vertices.y * ( 5/6 ) + elapsedTime * 0.5/6 );
const MovB = ( vertices.x + 2.5/6 ) / ( 5/6 );
vertices.z = ( MovX1 + MovX2 + MovY1 ) * MovB;
} );
banderaOndulante.geometry.verticesNeedUpdate = true;
}
// FIN BANDERA DE GOLF ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////// INICIO ARBOLES
class Pino{
constructor( _x = 0, _y = 0, _z = 0 ){
const arbolGroup = new THREE.Group();
const troncoMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 0.3, 1.5, 0.3 ),
new THREE.MeshStandardMaterial( {color: '#8B4513'} )
);
const copasMaterial = new THREE.MeshStandardMaterial( {color:'green'} );
const copasInfMesh = new THREE.Mesh(
new THREE.ConeBufferGeometry( 1, 1.5, 4 ),
copasMaterial
);
const copasMedMesh = new THREE.Mesh(
new THREE.ConeBufferGeometry( 0.7, 1, 4 ),
copasMaterial
);
const copasSupMesh = new THREE.Mesh(
new THREE.ConeBufferGeometry( 0.5, 1, 4 ),
copasMaterial
);
troncoMesh.position.y = 0.5;
copasSupMesh.position.y = 2.8;
copasInfMesh.position.y = 1.5;
copasMedMesh.position.y = 2.2;
arbolGroup.add( troncoMesh, copasInfMesh, copasMedMesh, copasSupMesh );
arbolGroup.position.x = _x;
arbolGroup.position.y = _y;
arbolGroup.position.z = _z;
scene.add( arbolGroup );
}
}
class Arbol{
constructor( _x = 0, _y = 0, _z = 0 ){
const arbolGroup = new THREE.Group();
const troncoMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 0.3 ,1.5, 0.3 ),
new THREE.MeshStandardMaterial( {color: '#8B4513'} )
);
const copasMaterial = new THREE.MeshStandardMaterial( {color:'green'} );
const copasMesh = new THREE.Mesh(
new THREE.SphereBufferGeometry( 1, 10, 10 ),
copasMaterial
);
const copasMesh2 = new THREE.Mesh(
new THREE.SphereBufferGeometry( 0.7, 10, 10 ),
copasMaterial
);
const copasMesh3 = new THREE.Mesh(
new THREE.SphereBufferGeometry( 0.5, 10, 10 ),
copasMaterial
);
troncoMesh.position.y = 0;
copasMesh3.position.y = 3.0;
copasMesh.position.y = 1.5;
copasMesh2.position.y = 2.2;
arbolGroup.add( troncoMesh, copasMesh, copasMesh2, copasMesh3 );
arbolGroup.position.x = _x;
arbolGroup.position.y = _y;
arbolGroup.position.z = _z;
scene.add( arbolGroup );
}
}
// Comentar para quitar de la escena.
let bosques = [];
bosques.push( new Arbol(-16,0,-9) );
bosques.push( new Arbol(-14,0,-9) );
bosques.push( new Arbol(-15.5,0,-7) );
bosques.push( new Arbol(-16,0,9) );
bosques.push( new Arbol(-16,0,7) );
bosques.push( new Arbol(-14,0,9.5) );
bosques.push( new Arbol(0,0,-9) );
bosques.push( new Arbol(5,0,-8.5) );
bosques.push( new Arbol(3,0,-9) );
bosques.push( new Arbol(-3,0,-8) );
bosques.push( new Arbol(-8,0,-9.5) );
bosques.push( new Arbol(-10,0,-9.5) );
bosques.push( new Pino(-7,0,0) );
bosques.push( new Pino(-9,1.5,-6) );
bosques.push( new Pino(-9,1.5,-3) );
bosques.push( new Pino(-11,1.5,-2) );
bosques.push( new Pino(-11,1.5,2) );
bosques.push( new Pino(-13,1.5,4) );
bosques.push( new Pino(-10,1.5,5) );
bosques.push( new Pino(-12,1,7) );
// FIN ARBOLES ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////// INICIO ARBUSTOS
class Arbusto{
constructor( _x, _y, _z ){
const arbustoMesh = new THREE.Mesh(
new THREE.SphereBufferGeometry( 7 ),
new THREE.MeshStandardMaterial( { color: '#0FFF00' } )
);
arbustoMesh.rotation.x = Math.PI /2;
arbustoMesh.scale.set(0.1,0.1,0.1);
arbustoMesh.position.y = _y;
arbustoMesh.position.x = _x;
arbustoMesh.position.z = _z;
scene.add( arbustoMesh );
}
}
// Comentar para quitar de la escena.
let arbustos = [];
arbustos.push( new Arbusto(7.5,0.5,8) );
arbustos.push( new Arbusto(7.5,0.1,7) );
arbustos.push( new Arbusto(7,0.1,9) );
arbustos.push( new Arbusto(7.5,0.5,-8) );
arbustos.push( new Arbusto(7.5,0.1,-7) );
arbustos.push( new Arbusto(7,0.1,-9) );
// FIN ARBUSTOS ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////// INICIO PELOTA DE GOLF
var status = false, pelotavelocidad = 0;
const pelotaMesh = new THREE.Mesh(
new THREE.SphereBufferGeometry( 0.1, 32, 32 ),
new THREE.MeshStandardMaterial( { roughness: 0.7, normalMap: pelotaTexture } )
);
pelotaMesh.position.y = 0.1;
// Animacion de la pelota al acercarse al hoyo. -Es necesario Teclado-
window.addEventListener( 'keydown', ( e ) => {
if( e.key == 'L' || e.key == 'l' ){
if( !status ){
scene.add( pelotaMesh );
pelotaMesh.position.z = 8;
pelotavelocidad = ( Math.floor( Math.random() * 40 ) + 1 ) / 100;
status = true;
}else{
( pelotaMesh.position.z <= 0.2 && pelotaMesh.position.z >= -0.2 ) ? alert('Hoyo en uno!') : alert('Fallaste!');
status = false;
}
}
} );
function pelotaMovimiento( pelotaMesh ){
if( status ){
if( pelotaMesh.position.z < -8.5 ){
pelotavelocidad = 0;
}
pelotaMesh.position.z -= pelotavelocidad;
pelotaMesh.rotation.x -= pelotavelocidad;
}
}
// FIN PELOTA DE GOLF ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////// INICIO NUBES
class Nube{
constructor( _x, _y, _z ){
const nubeMesh = new THREE.Mesh(
new THREE.TorusKnotBufferGeometry( 5, 7, 22, 20 ),
new THREE.MeshStandardMaterial()
);
nubeMesh.scale.set( 0.3, 0.1, 0.2 );
nubeMesh.position.x = _x;
nubeMesh.position.y = _y;
nubeMesh.position.z = _z;
scene.add( nubeMesh );
}
}
// Comentar para quitar de la escena.
let nubes = [];
nubes.push( new Nube(0, 13, -2) );
nubes.push( new Nube(10, 13, 2) );
nubes.push( new Nube(-10, 13, 2) );
// FIN NUBES ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////// INICIO TERRENO
const greenMesh = new THREE.Mesh(
new THREE.RingBufferGeometry( 0.2, 4, 42, 1 ),
new THREE.MeshStandardMaterial( { color: '#43A047' , map: pastoTexture} )
);
greenMesh.rotation.x = -Math.PI / 2;
// Comentar para quitar de la escena.
scene.add( greenMesh );
// Terreno
const terrenoGroup = new THREE.Group();
const terrenoMaterial = new THREE.MeshStandardMaterial( { color: '#1B5E15' } );
const terrenoSuperiorMesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 25, 10 ),
terrenoMaterial
);
terrenoSuperiorMesh.rotation.x = -Math.PI / 2;
terrenoSuperiorMesh.position.y -= 0.009;
terrenoSuperiorMesh.position.z = -5.2;
const terrenoInferiorMesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 25, 10 ),
terrenoMaterial
);
terrenoInferiorMesh.rotation.x = -Math.PI / 2;
terrenoInferiorMesh.position.y -= 0.009;
terrenoInferiorMesh.position.z = 5.2;
const terrenoMedioMesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 15, 0.4 ),
terrenoMaterial
);
terrenoMedioMesh.rotation.x = -Math.PI / 2;
terrenoMedioMesh.position.y -= 0.009;
terrenoMedioMesh.position.x = -5;
const terrenoMedioMesh2 = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 5, 0.4 ),
terrenoMaterial
);
terrenoMedioMesh2.rotation.x = -Math.PI / 2;
terrenoMedioMesh2.position.x = 10;
terrenoMedioMesh2.position.y -= 0.009;
terrenoGroup.add( terrenoSuperiorMesh, terrenoInferiorMesh, terrenoMedioMesh, terrenoMedioMesh2 );
terrenoGroup.position.x = -4;
// Comentar para quitar de la escena.
scene.add( terrenoGroup );
// Colinas
const colinaGroup = new THREE.Group();
const colinaMaterial = new THREE.MeshStandardMaterial( { color: '#1B5E20' } );
const points = [];
for ( let i = 0; i < 10; i++ ) {
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 10 + 5, ( i - 5 ) * 2 ) );
}
const colinaIzqMesh = new THREE.Mesh(
new THREE.LatheBufferGeometry( points, 30, Math.PI *2 , Math.PI ),
colinaMaterial
);
const colinaDerMesh = new THREE.Mesh(
new THREE.LatheBufferGeometry( points, 30, Math.PI *2 , Math.PI ),
colinaMaterial
);
colinaIzqMesh.rotation.z = Math.PI / 2;
colinaDerMesh.rotation.z = Math.PI / 2;
colinaDerMesh.rotation.y = Math.PI;
colinaDerMesh.position.x = -16;
colinaGroup.add( colinaDerMesh, colinaIzqMesh );
colinaGroup.scale.set( 0.4, 0.2, 0.3 );
colinaGroup.position.y = -1.2;
colinaGroup.rotation.y = Math.PI /2 ;
colinaGroup.position.z = -0.5;
colinaGroup.position.x = -12;
// Comentar para quitar de la escena.
scene.add( colinaGroup );
const colinaMesh = colinaGroup.clone( true );
colinaMesh.position.x = -7;
colinaMesh.rotation.y = Math.PI / 5;
colinaMesh.position.z = -6;
// Comentar para quitar de la escena.
scene.add( colinaMesh );
// Tierra costado
const bordeTierraGroup = new THREE.Group();
const tierraMaterial = new THREE.MeshStandardMaterial( { color:'#8B3000', normalMap: sueloTexture } );
const tierraFrenteMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 1, 1.8, 20.4 ),
tierraMaterial
);
tierraFrenteMesh.position.x = 8;
tierraFrenteMesh.position.y -= 0.919;
const tierraFondoMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 1, 1.8, 20.4 ),
tierraMaterial
);
tierraFondoMesh.position.x = -16;
tierraFondoMesh.position.y -= 0.919;
const tierraCostadoIzqMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 1, 1.8, 23 ),
tierraMaterial
);
tierraCostadoIzqMesh.position.x = -4;
tierraCostadoIzqMesh.rotation.y = Math.PI/2;
tierraCostadoIzqMesh.position.y -= 0.919;
tierraCostadoIzqMesh.position.z = 9.7;
const tierraCostadoDerMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 1, 1.8, 23 ),
tierraMaterial
);
tierraCostadoDerMesh.position.x = -4;
tierraCostadoDerMesh.rotation.y = Math.PI/2;
tierraCostadoDerMesh.position.y -= 0.919;
tierraCostadoDerMesh.position.z = -9.7;
const tierraBajoMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 20.2, 0.5, 23 ),
tierraMaterial
);
tierraBajoMesh.position.x = -4;
tierraBajoMesh.rotation.y = Math.PI/2;
tierraBajoMesh.position.y -= 1.59;
bordeTierraGroup.add( tierraFrenteMesh, tierraFondoMesh, tierraCostadoIzqMesh, tierraCostadoDerMesh, tierraBajoMesh );
// Comentar para quitar de la escena.
scene.add( bordeTierraGroup );
// FIN TERRENO ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////// INICIO AGUA
const aguaMaterial = {
clearcoat: 2.0,
clearcoatRoughness: 0.5,
metalness:0.7,
roughness: 0.7,
color: '#1490DA',
normalMap: aguaTexture,
};
const aguaPlaneMesh = new THREE.Mesh(
new THREE.BoxBufferGeometry( 5, 20.4 ),
new THREE.MeshPhysicalMaterial( aguaMaterial )
);
aguaPlaneMesh.rotation.x = -Math.PI / 2;
aguaPlaneMesh.position.x = 11;
aguaPlaneMesh.position.y = -1.2;
scene.add( aguaPlaneMesh );
// FIN AGUA ///////////////////////////////////////////////////////////////////////////////
/** Animate **/
const clock = new THREE.Clock();
const animate = () => {
const elapsedTime = clock.getElapsedTime();
banderaWind( elapsedTime );
pelotaMovimiento( pelotaMesh );
engineAudioMesh.position.set(camera.position.x,camera.position.y,camera.position.z);
renderer.render( scene, camera );
// Update for controls
controls.update(0.01)
// Target => 60 fps
window.requestAnimationFrame( animate );
};
animate();
// Resize
window.addEventListener('resize', () =>
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio( Math.min( window.devicePixelRatio, 2 ) );
});
</script>
</body>
</html>