-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
260 lines (203 loc) · 6.31 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
import * as THREE from "https://unpkg.com/[email protected]/build/three.module.js";
import Stats from "https://unpkg.com/[email protected]/examples/jsm/libs/stats.module.js";
import { PointerLockControls } from "https://unpkg.com/[email protected]/examples/jsm/controls/PointerLockControls.js";
import { Water } from "https://unpkg.com/[email protected]/examples/jsm/objects/Water.js";
import { Sky } from "https://unpkg.com/[email protected]/examples/jsm/objects/Sky.js";
let container, stats;
let camera, scene, renderer;
let controls, water, sun, mesh, sprite;
const KEY_SENSITIVITY = 3;
const WAVE_INTENSITY = 2.5;
const MIN_HEIGHT = 15;
const ANGLE = Math.PI / 6;
let STARTED = false;
let velocity = {
x: 0,
y: 0,
z: 0,
};
init();
animate();
function init() {
container = document.getElementById("container");
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
55,
window.innerWidth / window.innerHeight,
1,
20000
);
camera.position.set(30, 15, 100);
sun = new THREE.Vector3();
// Water
const waterGeometry = new THREE.PlaneGeometry(100000, 100000);
water = new Water(waterGeometry, {
textureWidth: 512,
textureHeight: 512,
waterNormals: new THREE.TextureLoader().load(
"images/waternormals.jpg",
function (texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
}
),
alpha: 1.0,
sunDirection: new THREE.Vector3(),
sunColor: 0xffff00,
waterColor: 0x40a4df,
distortionScale: 0.7,
fog: scene.fog !== undefined,
});
water.rotation.x = (-9 * Math.PI) / 2;
water.rotation.z = (-9 * Math.PI) / 2;
scene.add(water);
// Skybox
const sky = new Sky();
sky.scale.setScalar(10000);
scene.add(sky);
const skyUniforms = sky.material.uniforms;
skyUniforms["turbidity"].value = 10;
skyUniforms["rayleigh"].value = 2;
skyUniforms["mieCoefficient"].value = 0.005;
skyUniforms["mieDirectionalG"].value = 0.8;
const parameters = {
inclination: 0.49,
azimuth: 0.205,
};
const pmremGenerator = new THREE.PMREMGenerator(renderer);
function updateSun() {
const theta = Math.PI * (parameters.inclination - 0.5);
const phi = 2 * Math.PI * (parameters.azimuth - 0.5);
sun.x = Math.cos(phi);
sun.y = Math.sin(phi) * Math.sin(theta);
sun.z = Math.sin(phi) * Math.cos(theta);
sky.material.uniforms["sunPosition"].value.copy(sun);
water.material.uniforms["sunDirection"].value.copy(sun).normalize();
scene.environment = pmremGenerator.fromScene(sky).texture;
}
updateSun();
//
const geometry = new THREE.BoxGeometry(30, 30, 30);
const loader = new THREE.TextureLoader();
const map = loader.load(
"images/jetski_transparent_square.png",
function (texture) {
loadSprite(texture);
},
undefined,
function (err) {
console.error("Error: " + err);
}
);
controls = new PointerLockControls(camera, document.body);
stats = new Stats();
container.appendChild(stats.dom);
window.addEventListener("resize", onWindowResize);
let startBtn = document.getElementById("start_btn");
startBtn.onclick = function() {
STARTED = true;
playAudio();
}
document.onkeydown = function (e) {
onKeyDown(e);
};
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
render();
stats.update();
//controls.update();
}
function render() {
const time = performance.now() * 0.001;
sprite.position.y = (Math.sin(3*time) + 1) * WAVE_INTENSITY + MIN_HEIGHT;
camera.position.y = sprite.position.y + 1.9;
camera.position.z += velocity.z;
sprite.position.z += velocity.z;
sprite.position.x += velocity.x;
camera.position.x += velocity.x;
velocity.z = updateVelocity(0, velocity.z);
velocity.x = updateVelocity(0, velocity.x);
bank(camera, sprite, 0);
water.material.uniforms["time"].value += 1.0 / 60.0;
renderer.render(scene, camera);
}
function playAudio() {
// Create an AudioListener
const listener = new THREE.AudioListener();
camera.add(listener);
// Create a global audio source
const sound = new THREE.Audio(listener);
// Load a sound and set it as the Audio object's buffer
const audioLoader = new THREE.AudioLoader();
audioLoader.load("sounds/jetski_motor.mp3", function (buffer) {
sound.setBuffer(buffer);
sound.setLoop(true);
sound.setVolume(1.8);
sound.play();
});
}
function onKeyDown(e) {
if (STARTED) {
switch (e.keyCode) {
case 37:
// LEFT
velocity.x = updateVelocity(-KEY_SENSITIVITY + velocity.x, velocity.x);
velocity.z = updateVelocity(-KEY_SENSITIVITY, velocity.z);
// bank left
bank(camera, sprite, ANGLE);
break;
case 65:
// LEFT
velocity.x = updateVelocity(-KEY_SENSITIVITY, velocity.x);
velocity.z = updateVelocity(-KEY_SENSITIVITY, velocity.z);
// bank left
bank(camera, sprite, ANGLE);
break;
case 38:
// FORWARD
velocity.z = updateVelocity(-KEY_SENSITIVITY, velocity.z);
// realign vertically
bank(camera, sprite, 0);
break;
case 87:
// FORWARD
velocity.z = updateVelocity(-KEY_SENSITIVITY, velocity.z);
// realign vertically
bank(camera, sprite, 0);
break;
case 39 :
// RIGHT
velocity.x = updateVelocity(KEY_SENSITIVITY + velocity.x, velocity.x);
velocity.z = updateVelocity(-KEY_SENSITIVITY + velocity.z, velocity.z);
// bank right
bank(camera, sprite, -ANGLE);
break;
case 68:
// RIGHT
velocity.x = updateVelocity(KEY_SENSITIVITY, velocity.x);
velocity.z = updateVelocity(-KEY_SENSITIVITY, velocity.z);
// bank right
bank(camera, sprite, -ANGLE);
break;
}
}
}
function loadSprite(texture) {
const spriteMaterial = new THREE.SpriteMaterial({
map: texture,
});
sprite = new THREE.Sprite(spriteMaterial);
sprite.position.x = 30;
sprite.position.z = 94;
sprite.scale.set(11, 8, 11);
scene.add(sprite);
}