-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
193 lines (143 loc) · 5.04 KB
/
app.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
import * as THREE from 'three'
import {
OrbitControls
} from "three/examples/jsm/controls/OrbitControls";
import {
PointerLockControls
} from "three/examples/jsm/controls/PointerLockControls"
import { IFCLoader } from "web-ifc-three/IFCLoader";
//Creates the Three.js scene
const scene = new THREE.Scene();
const ifcLoader = new IFCLoader();
ifcLoader.load(
"./05.ifc",
(ifcModel) => scene.add(ifcModel));
//Object to store the size of the viewport
const size = {
width: window.innerWidth,
height: window.innerHeight,
};
//Creates the camera (point of view of the user)
const aspect = size.width / size.height;
const camera = new THREE.PerspectiveCamera(75, aspect);
camera.position.z = 10;
camera.position.y = 1.5;
camera.position.x = 12;
//Creates the lights of the scene
const lightColor = 0xffffff;
const ambientLight = new THREE.AmbientLight(lightColor, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(lightColor, 1);
directionalLight.position.set(0, 10, 0);
directionalLight.target.position.set(-5, 0, 0);
scene.add(directionalLight);
scene.add(directionalLight.target);
//Sets up the renderer, fetching the canvas of the HTML
const threeCanvas = document.getElementById("three-canvas");
const renderer = new THREE.WebGLRenderer({
canvas: threeCanvas,
alpha: true
});
renderer.setSize(size.width, size.height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
//Creates grids and axes in the scene
const grid = new THREE.GridHelper(50, 30);
scene.add(grid);
const axes = new THREE.AxesHelper();
axes.material.depthTest = false;
axes.renderOrder = 1;
scene.add(axes);
//Creates the orbit controls (to navigate the scene)
// const controls = new OrbitControls(camera, threeCanvas);
// controls.enableDamping = true;
// controls.target.set(-2, 0, 0);
const controls = new PointerLockControls(camera, document.documentElement);
window.addEventListener("click", () => {
controls.lock();
});
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
const velocity = new THREE.Vector3();
const direction = new THREE.Vector3();
const onKeyDown = (e) => {
switch (e.code) {
case "KeyW":
moveForward = true;
console.log(moveForward)
break;
case "KeyS":
moveBackward = true;
break;
case "KeyA":
moveLeft = true;
break;
case "KeyD":
moveRight = true;
break;
}
};
const onKeyUp = (e) => {
switch (e.code) {
case "KeyW":
moveForward = false;
console.log(moveForward)
break;
case "KeyS":
moveBackward = false;
break;
case "KeyA":
moveLeft = false;
break;
case "KeyD":
moveRight = false;
break;
case "KeyQ":
camera.position.set(camera.position.x, camera.position.y + 3.6,camera.position.z)
break;
case "KeyE":
camera.position.set(camera.position.x, camera.position.y - 3.6,camera.position.z)
break;
}
};
document.addEventListener("keydown", onKeyDown);
document.addEventListener("keyup", onKeyUp);
let prevTime = performance.now();
function animate() {
// rectAreaLight.color.r = (rectAreaLight.color.r+ 0.01)
// rectAreaLight.color.b = (rectAreaLight.color.b+ 0.01)
// rectAreaLight.intensity = 1
requestAnimationFrame(animate);
const time = performance.now();
// 前進後進判定
direction.z = Number(moveForward) - Number(moveBackward);
direction.x = Number(moveRight) - Number(moveLeft);
//ポインターがONになったら
if (controls.isLocked) {
const delta = (time - prevTime) / 1000;
// decline
velocity.z -= velocity.z * 0.5 * delta;
velocity.x -= velocity.x * 0.5 * delta;
if (moveForward || moveBackward) {
velocity.z -= direction.z * 10 * delta;
}
if (moveRight || moveLeft) {
velocity.x -= direction.x * 10 * delta;
}
controls.moveForward(-velocity.z * delta);
controls.moveRight(-velocity.x * delta);
}
prevTime = time;
renderer.render(scene, camera);
}
// initialize();
animate();
//Adjust the viewport to the size of the browser
// window.addEventListener("resize", () => {
// size.width = window.innerWidth;
// size.height = window.innerHeight;
// camera.aspect = size.width / size.height;
// camera.updateProjectionMatrix();
// renderer.setSize(size.width, size.height);
// });