-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
241 lines (183 loc) · 5.19 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
import './style.css'
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { Water } from 'three/examples/jsm/objects/Water.js';
import { Sky } from 'three/examples/jsm/objects/Sky.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
let camera, scene, renderer;
let controls, water, sun;
const loader = new GLTFLoader();
function random(min, max) {
return Math.random() * (max - min) + min;
}
class Boat {
constructor(){
loader.load("assets/boat/scene.gltf", (gltf) => {
scene.add( gltf.scene )
gltf.scene.scale.set(3, 3, 3)
gltf.scene.position.set(5,13,50)
gltf.scene.rotation.y = 1.5
this.boat = gltf.scene
this.speed = {
vel: 0,
rot: 0
}
})
}
stop(){
this.speed.vel = 0
this.speed.rot = 0
}
update(){
if(this.boat){
this.boat.rotation.y += this.speed.rot
this.boat.translateX(this.speed.vel)
}
}
}
const boat = new Boat()
class Trash{
constructor(_scene){
scene.add( _scene )
_scene.scale.set(1.5, 1.5, 1.5)
if(Math.random() > .6){
_scene.position.set(random(-100, 100), -.5, random(-100, 100))
}else{
_scene.position.set(random(-500, 500), -.5, random(-1000, 1000))
}
this.trash = _scene
}
}
async function loadModel(url){
return new Promise((resolve, reject) => {
loader.load(url, (gltf) => {
resolve(gltf.scene)
})
})
}
let boatModel = null
async function createTrash(){
if(!boatModel){
boatModel = await loadModel("assets/trash/scene.gltf")
}
return new Trash(boatModel.clone())
}
let trashes = []
const TRASH_COUNT = 500
init();
animate();
async function init() {
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 1, 20000 );
camera.position.set( 30, 30, 100 );
sun = new THREE.Vector3();
// Water
const waterGeometry = new THREE.PlaneGeometry( 10000, 10000 );
water = new Water(
waterGeometry,
{
textureWidth: 512,
textureHeight: 512,
waterNormals: new THREE.TextureLoader().load( 'assets/waternormals.jpg', function ( texture ) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
} ),
sunDirection: new THREE.Vector3(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 3.7,
fog: scene.fog !== undefined
}
);
water.rotation.x = - 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 = {
elevation: 2,
azimuth: 180
};
const pmremGenerator = new THREE.PMREMGenerator( renderer );
function updateSun() {
const phi = THREE.MathUtils.degToRad( 90 - parameters.elevation );
const theta = THREE.MathUtils.degToRad( parameters.azimuth );
sun.setFromSphericalCoords( 1, phi, theta );
sky.material.uniforms[ 'sunPosition' ].value.copy( sun );
water.material.uniforms[ 'sunDirection' ].value.copy( sun ).normalize();
scene.environment = pmremGenerator.fromScene( sky ).texture;
}
updateSun();
controls = new OrbitControls( camera, renderer.domElement );
controls.maxPolarAngle = Math.PI * 0.495;
controls.target.set( 0, 10, 0 );
controls.minDistance = 40.0;
controls.maxDistance = 200.0;
controls.update();
const waterUniforms = water.material.uniforms;
for(let i = 0; i < TRASH_COUNT; i++){
const trash = await createTrash()
trashes.push(trash)
}
window.addEventListener( 'resize', onWindowResize );
window.addEventListener( 'keydown', function(e){
if(e.key == "ArrowUp"){
boat.speed.vel = 1
}
if(e.key == "ArrowDown"){
boat.speed.vel = -1
}
if(e.key == "ArrowRight"){
boat.speed.rot = -0.1
}
if(e.key == "ArrowLeft"){
boat.speed.rot = 0.1
}
})
window.addEventListener( 'keyup', function(e){
boat.stop()
})
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function isColliding(obj1, obj2){
return (
Math.abs(obj1.position.x - obj2.position.x) < 15 &&
Math.abs(obj1.position.z - obj2.position.z) < 15
)
}
function checkCollisions(){
if(boat.boat){
trashes.forEach(trash => {
console.log("hello")
if(trash.trash){
if(isColliding(boat.boat, trash.trash)){
scene.remove(trash.trash)
}
}
})
}
}
function animate() {
requestAnimationFrame( animate );
render();
boat.update()
checkCollisions()
}
function render() {
water.material.uniforms[ 'time' ].value += 1.0 / 60.0;
renderer.render( scene, camera );
}