-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12D-boat3_no_test.js
491 lines (390 loc) · 14.2 KB
/
12D-boat3_no_test.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
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js';
import { Water } from 'three/examples/jsm/objects/Water';
const waterTexture = document.getElementById("img-material")
const lensflareTexture0 = document.getElementById("img-material-1")
const lensflareTexture1 = document.getElementById("img-material-2")
import { Sky } from 'three/examples/jsm/objects/Sky';
import { Lensflare, LensflareElement } from 'three/examples/jsm/objects/Lensflare.js';
let renderer, camera, scene, light, meshes;
let mixer,mixer2,mixer3,mixer4;
let plane, model;
let model1;
let pointLight,ambientLight,dirLight;
let clock = new THREE.Clock();
let axesHelper;
let controls;
var username = prompt("Username, please.")
var serverPath = "https://kaoyan1.eu.pythonanywhere.com/threejs/get_mousedata";
$.getJSON(serverPath,
{username: username,
timeMark: Date.now(),
objname: "homepage",
action: "landing",
x: 0,
y: 0 },
function(data) {console.log(data)}
)
function mousetrack (event) {
$.getJSON(serverPath,
{username: username,
timeMark: Date.now(),
objname: "view",
action: event.type,
x: event.clientX,
y: event.clientY },
function(data) {console.log(data)})
}
window.addEventListener("mousedown", mousetrack, false);
window.addEventListener("mousemove", mousetrack, false);
window.addEventListener("mouseup", mousetrack, false);
initRenderer();
initCamera();
const vec1 = new THREE.Vector3(160,30,200);//终点flag坐标
function dist(x0,y0,z0){
const vec2 = new THREE.Vector3(x0, y0, z0);
var distance = vec1.distanceTo(vec2); //到终点的距离
if (distance <= 31){
console.log('Arrive!!');
$("body").html('<center>Thank you!</center>');
}
console.log(distance);
}
const vec_boat = new THREE.Vector3(20, 0, 70);
function dist_boat(x0,y0,z0,keypress){
const vec2 = new THREE.Vector3(x0, y0, z0);
var distance_boat = vec2.distanceTo(vec_boat);
console.log(distance_boat);
$.getJSON(serverPath,
{username: username,
timeMark: Date.now(),
objname: "boat_distance",
action: keypress,
x: distance_boat,
y: 0 },
function(data) {console.log(data)}
)
}
initScene();
initLight();
initMeshes();
enableShadow();
initAxesHelper();
initControls();
window.addEventListener('resize', function(){//渲染结果随着窗体的变化而变化(浏览器变窄了,渲染窗口也变窄)
camera.aspect = window.innerWidth/this.window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, this.window.innerHeight);
})
/* ------------------场景三要素初始化------------------- */
function initRenderer() {
renderer = new THREE.WebGL1Renderer({ alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild(renderer.domElement);
}
function initCamera(){
camera = new THREE.PerspectiveCamera(40,window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(300,120,505);
camera.lookAt(200,8,200);
}
function initScene(){
scene = new THREE.Scene();
scene.background = new THREE.Color(0xCAE1FF);
}
/* ------------------场景三要素初始化------------------- */
/* ------------------灯光------------------- */
function initLight(){
ambientLight = new THREE.AmbientLight(0xffffff, .8);
scene.add(ambientLight);
dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.color.setHSL(.1, 1, .95);
dirLight.position.set(-1, 1.75, 1);
dirLight.position.multiplyScalar(30);
scene.add(dirLight);
// 太阳点光源
pointLight = new THREE.PointLight(0xffffff, 1.2, 2000);
pointLight.color.setHSL(.995, .5, .9);
pointLight.position.set(0, 45, -2000);
const textureLoader = new THREE.TextureLoader();
const textureFlare0 = textureLoader.load(lensflareTexture0);
const textureFlare1 = textureLoader.load(lensflareTexture1);
// 镜头光晕
const lensflare = new Lensflare();
lensflare.addElement(new LensflareElement( textureFlare0, 600, 0, pointLight.color));
lensflare.addElement(new LensflareElement( textureFlare1, 60, .6));
lensflare.addElement(new LensflareElement( textureFlare1, 70, .7));
lensflare.addElement(new LensflareElement( textureFlare1, 120, .9));
lensflare.addElement(new LensflareElement( textureFlare1, 70, 1));
pointLight.add(lensflare);
scene.add(pointLight);
}
/* ------------------灯光------------------- */
/* ------------------加载3维模型------------------- */
function initMeshes(){
//plane
plane = new THREE.Mesh(
new THREE.PlaneGeometry(5000, 5000),
new THREE.MeshPhongMaterial({
color:0x999999,
transparent: true
})
);
plane.rotation.x = - Math.PI / 2;
//scene.add(plane);
//model
//flag
const flag_loader = new GLTFLoader();
flag_loader.load('models/gltb/low_poly_golf_flag_animated.glb',function(gltf){
model = gltf.scene;
scene.add(model);
model.position.set(160, 30, 200);
model.rotation.y = 45;
mixer4 = new THREE.AnimationMixer(model);
mixer4.clipAction(gltf.animations[0]).play();
gltf.scene.traverse( function(object){
// console.log(object.type);
if(object.isMesh){
//console.log(object);
//object.castShadow = true;
object.material.roughness = .6;
}
})
model.scale.set(20,20,20);
//模型放大
render();
});
//二号小岛
const island_loader_2 = new GLTFLoader();
island_loader_2.load('models/gltb/island.glb',function(gltf){
model = gltf.scene;
scene.add(model);
model.position.set(200, -1, 200);
model.rotation.y = - 10;
gltf.scene.traverse( function(object){
if(object.isMesh){
object.material.roughness = .6;
}
})
model.scale.set(20,20,20);
render();
});
//小船
const smallship_loader = new GLTFLoader();
smallship_loader.load('models/gltb/BoatWSail_small.glb',function(gltf){
model1 = gltf.scene;
scene.add(model1);
model1.position.set(-150, 0, 150);
model1.rotation.y = -30;
gltf.scene.traverse( function(object){
// console.log(object.type);
if(object.isMesh){
//console.log(object);
object.castShadow = true;
}
})
model1.scale.set(22,22,22);
render();
});
//大船
const bigship_loader = new GLTFLoader();
bigship_loader.load('models/gltb/Sail ship_big.glb',function(gltf){
model = gltf.scene;
scene.add(model);
model.position.set(20, 0, 70);
model.rotation.y = - 30;
gltf.scene.traverse( function(object){
// console.log(object.type);
if(object.isMesh){
//console.log(object);
object.castShadow = true;
}
})
model.scale.set(10,10,10);
//render();
});
//火烈鸟
const bird_loader = new GLTFLoader();
bird_loader.load('models/gltb/flamingo.glb',function(gltf){
const mesh = gltf.scene.children[0];
mesh.scale.set(.35, .35, .35);
mesh.position.set(-100, 80, -300);
mesh.rotation.y = - 1;
mesh.castShadow = true;
scene.add(mesh);
const bird2 = mesh.clone();
bird2.position.set(150, 80, -500);
scene.add(bird2);
mixer = new THREE.AnimationMixer(mesh);
mixer.clipAction(gltf.animations[0]).setDuration(1.2).play();
//this.mixers.push(mixer);
mixer2 = new THREE.AnimationMixer(bird2);
mixer2.clipAction(gltf.animations[0]).setDuration(1.8).play();
//this.mixers.push(mixer2);
// scene.add(gltf.scene);
gltf.scene.traverse( function(object){
console.log(object.type);
if(object.isMesh){
console.log(object);
object.castShadow = true;
}
})
console.log(gltf);
render();
});
}
// 海
const waterGeometry = new THREE.PlaneGeometry(10000, 10000);
//const waterGeometry = new THREE.BoxGeometry(10000, 10000, 10000);
const water = new Water(waterGeometry, {
textureWidth: 512,
textureHeight: 512,
//transparent: true,
waterNormals: new THREE.TextureLoader().load(waterTexture, texture => {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
}),
sunDirection: new THREE.Vector3(),
sunColor: 0xfffff0,
waterColor: 0x0072ff,
distortionScale: 4,
fog: scene.fog !== undefined
});
water.rotation.x = - Math.PI / 2;
//water.depthTest = false;
// water.opacity = .2;
//water.position.set(0, -5000, 0);
scene.add(water);
// 天空
const sky = new Sky();
sky.scale.setScalar(10000);
scene.add(sky);
const skyUniforms = sky.material.uniforms;
skyUniforms['turbidity'].value = 20;
skyUniforms['rayleigh'].value = 2;
skyUniforms['mieCoefficient'].value = 0.005;
skyUniforms['mieDirectionalG'].value = 0.8;
// 太阳
const sun = new THREE.Vector3();
const pmremGenerator = new THREE.PMREMGenerator(renderer);
const phi = THREE.MathUtils.degToRad(88);
const theta = THREE.MathUtils.degToRad(180);
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;
const manager = new THREE.LoadingManager();
manager.onProgress = async(url, loaded, total) => {
if (Math.floor(loaded / total * 100) === 100) {
this.setState({ loadingProcess: Math.floor(loaded / total * 100) });
Animations.animateCamera(camera, controls, { x: 0, y: 40, z: 140 }, { x: 0, y: 0, z: 0 }, 4000, () => {
this.setState({ sceneReady: true });
});
} else {
this.setState({ loadingProcess: Math.floor(loaded / total * 100) });
}
};
/* ------------------加载3维模型------------------- */
/* ------------------坐标轴------------------- */
function initAxesHelper(){
axesHelper = new THREE.AxesHelper(1);
scene.add(axesHelper);
}
/* ------------------坐标轴------------------- */
/* ------------------阴影------------------- */
function enableShadow(){
renderer.shadowMap.enabled = true;
dirLight.castShadow = true;
pointLight.castShadow = true;
//ambientLight.castShadow = true;
plane.receiveShadow = true;
};
/* ------------------阴影------------------- */
/* ------------------控制器------------------- */
function initControls(){
controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(160, 30, 200);
controls.enableDamping = true;
controls.autoRotate=false;
controls.enablePan = false;
controls.maxPolarAngle = 1.5;
//controls.minDistance = 50;
//controls.maxDistance = 1200;
window.addEventListener("keydown",function(e){
keyDownWalk(e)
})
window.addEventListener("keyup",function(e){
keyUpPalse(e)
})
controls.update();
}
function keyDownWalk(e) {
let keyCode = e.keyCode;
let position = model1.position, rotation = model1.rotation.y;
switch (keyCode) {
case 87: //w
position.x += 1.5 * Math.cos(rotation - Math.PI/2);
position.z += 1.5 * Math.sin(-rotation + Math.PI/2);
dist(model1.position.x,model1.position.y,model1.position.z);
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyW_down');
break;
case 65: //a
position.x += 1.5 * Math.cos(rotation);
position.z += 1.5 * Math.sin(-rotation);
dist(model1.position.x,model1.position.y,model1.position.z);
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyA_down');
break;
case 68: //d
position.x -= 1.5 * Math.cos(rotation);
position.z -= 1.5 * Math.sin(-rotation);
dist(model1.position.x,model1.position.y,model1.position.z);
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyD_down');
break;
case 83: //s
position.x += 1.5 * Math.cos(rotation + Math.PI/2);
position.z += 1.5 * Math.sin(-rotation - Math.PI/2);
dist(model1.position.x,model1.position.y,model1.position.z);
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyS_down');
break;
default:
break;
}
}
function keyUpPalse(e) {
let keyCode = e.keyCode;
switch (keyCode) {
case 87: //w
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyW_up');
break;
case 65: //a
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyA_up');
break;
case 68: //d
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyD_up');
break;
case 83: //s
dist_boat(model1.position.x,model1.position.y,model1.position.z,'KeyS_up');
break;
default:
break;
}
}
/* ------------------控制器------------------- */
/* ------------------渲染器------------------- */
function render(){
//const time = performance.now() / 3000;
const delta = clock.getDelta();
requestAnimationFrame( render );
//camera.lookAt(model1.position);
// spotLight.rotation.y += 0.01;
// spotLight.position.x = Math.cos( time ) * 25;
// spotLight.position.z = Math.sin( time ) * 25;
renderer.render(scene,camera);
//lightHelper.update();
mixer.update(delta);
mixer2.update(delta);
mixer4.update(delta);
}
/* ------------------渲染器------------------- */