-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exhibition Room (HW2).html
396 lines (341 loc) · 12.6 KB
/
Exhibition Room (HW2).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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>★星爆氣流展★</title>
<link rel="shortcut icon" href="https://i.imgur.com/jnjsKvZ.png">
</head>
<script src="https://cdn.jsdelivr.net/npm/three@0.113.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.113.0/examples/js/controls/OrbitControls.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<div id="info">
<a href="texture/HW2/2D design layout.png">2D Design Layout</a><br>
<button id="camera" style="width:10%">camera</button>
<button id="camera1" style="width:10%">camera1</button>
<button id="camera2" style="width:10%">camera2</button>
<button id="roomLight" style="width:10%">Room Light</button><br>
<button id="light" style="width:10%">Exhibits Light</button>
<input id='light_range' type='range' min=0 max=1 value=0 step="any"><br>
</div>
<style>
body {
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
}
</style>
<body>
<script>
class Light {
constructor(x, z) {
this.on = false;
this.x = x;
this.z = z;
this.obj = this.makeLight();
this.lamoshade = this.makeLampshade();
}
get isOn() {
return this.on;
}
toggle() {
this.on = !this.on;
this.update();
}
update() {
if (this.on) {
this.obj.intensity = 1;
this.obj.castShadow = true;
} else {
this.obj.intensity = 0;
this.obj.castShadow = false;
}
}
makeLight(){
let light = new THREE.SpotLight(0xdddddd); //ffffff,圖畫掛上去之後再看看要不要調整(太亮之類的)。
light.position.set(this.x, 250, this.z);
light.target.position.set(this.x, 0, this.z);
light.target.updateMatrixWorld();
light.angle = Math.PI/6;
light.penumbra = 0.25;
light.intensity = 0;
light.castShadow = true; //太多燈了,light超過15個three.js承受不了!
light.shadow.mapSize.width = 512;
light.shadow.mapSize.height = 512;
light.shadow.camera.near = 50
light.shadow.camera.far = 400;
light.shadow.camera.fov = 60;
light.shadow.bias = 0.001;
scene.add(light);
//scene.add(new THREE.SpotLightHelper(light));
//scene.add(new THREE.CameraHelper (light.shadow.camera));
return light;
}
makeLampshade(){
let points = [];
for ( let i = 0; i < 10; i ++ ) {
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 15 + 3, ( i - 5 ) * 2 ) );
}
let geometry = new THREE.LatheGeometry(points, 20);
let material = new THREE.MeshPhongMaterial({color: 0xffffff});
let lathe = new THREE.Mesh(geometry, material);
lathe.position.set(this.x, 250, this.z);
lathe.rotation.x = -Math.PI;
scene.add(lathe);
return lathe;
}
}
var renderer, scene;
var camera, camera1, camera2;
var controls, controls1, controls2;
var roomLight, l1, l2, l3, l4, l5, l7, l8, l9, l10, l11, l12, l13, l14;
var toggleRoom = true;
var whichCamera = 0;
init();
animate();
$('#camera').click (function() {
camera.position.set(0, 500, 0);
// 不可使用camera.lookAt (vetcor),因為會被OrbitControls給覆蓋掉,應該直接更改OrbitControls裡的target
controls.target = new THREE.Vector3(0, 0, 0);
whichCamera = 0;
});
$('#camera1').click (function() {
camera1.position.set(478, 108, 236);
controls1.target = new THREE.Vector3(137, 0, 1);
whichCamera = 1;
});
$('#camera2').click (function() {
camera2.position.set(79, 71, 35);
controls2.target = new THREE.Vector3(-11, 9, 196);
whichCamera = 2;
});
$('#roomLight').click (function() {
toggleRoom = !toggleRoom;
});
$('#light').click (function() {
l1.toggle(); l2.toggle();
l3.toggle(); l4.toggle();
l5.toggle(); l6.toggle();
l7.toggle(); l8.toggle();
l9.toggle(); l10.toggle();
l11.toggle(); l12.toggle();
l13.toggle(); l14.toggle();
});
$('#light_range').change( function(){
l1.obj.intensity = $(this).val(); l2.obj.intensity = $(this).val();
l3.obj.intensity = $(this).val(); l4.obj.intensity = $(this).val();
l5.obj.intensity = $(this).val(); l6.obj.intensity = $(this).val();
l7.obj.intensity = $(this).val(); l8.obj.intensity = $(this).val();
l9.obj.intensity = $(this).val(); l10.obj.intensity = $(this).val();
l11.obj.intensity = $(this).val(); l12.obj.intensity = $(this).val();
l13.obj.intensity = $(this).val(); l14.obj.intensity = $(this).val();
});
function init() {
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x404040);
document.body.appendChild(renderer.domElement);
// camera
camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 1, 1500);
camera1 = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 1, 1500);
camera2 = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 1, 1500);
camera.position.set(0, 500, 0);
camera1.position.set(478, 108, 236);
camera2.position.set(79, 71, 35);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls1 = new THREE.OrbitControls(camera1, renderer.domElement);
controls2 = new THREE.OrbitControls(camera2, renderer.domElement);
scene = new THREE.Scene();
let axes = new THREE.AxisHelper(500);
scene.add(axes);
let gridXZ = new THREE.GridHelper(200, 20, 'red', 'white');
//scene.add(gridXZ);
// exhibition room
var room = build();
scene.add(room); //(unitize (room, 1.5));
// lighting
var light = new THREE.AmbientLight(0x505050); // soft white light
scene.add(light);
// room light
roomLight = new THREE.SpotLight(0xffffff);
roomLight.angle = Math.PI / 2.5;
roomLight.penumbra = 0.25;
roomLight.castShadow = true;
roomLight.shadow.mapSize.width = 1024;
roomLight.shadow.mapSize.height = 1024;
roomLight.shadow.camera.near = 200;
roomLight.shadow.camera.far = 500;
roomLight.shadow.camera.fov = 150;
roomLight.shadow.bias = 0.001;
roomLight.position.set(-25, 450, 25);
scene.add(roomLight);
//scene.add(new THREE.SpotLightHelper(roomLight));
//scene.add ( new THREE.CameraHelper (roomLight.shadow.camera) );
// exhibits light (編號從左上開始,依序往右邊算,接著再下一排)
l1 = new Light(-350, -230);
l2 = new Light(-225, -210);
l3 = new Light(125, -210);
l4 = new Light(254, -210);
l5 = new Light(-420, -50);
l6 = new Light(-260, -50);
l7 = new Light(80, -50);
l8 = new Light(290, -50);
l9 = new Light(-420, 80);
l10 = new Light(-260, 80);
l11 = new Light(80, 80);
l12 = new Light(290, 80);
l13 = new Light(-320, 210);
l14 = new Light(218, 210);
window.addEventListener('resize', onWindowResize, false);
}
function build() {
var space = new THREE.Object3D();
// floor
var loader = new THREE.TextureLoader();
loader.setCrossOrigin ('');
var floorTexture = loader.load('texture/HW2/floor.jpg');
floorTexture.wrapS = THREE.RepeatWrapping;
floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set(6, 4);
var floor = new THREE.Mesh(new THREE.PlaneGeometry(1000, 580, 140, 140), new THREE.MeshPhongMaterial({
map: floorTexture,
side: THREE.DoubleSide
}));
floor.rotation.x = -Math.PI / 2;
space.add(floor);
// wall
var wallMat = new THREE.MeshPhongMaterial({color: 0xffffff});
var wall_negz = new THREE.Mesh(new THREE.PlaneGeometry(1000, 150, 140, 140), wallMat);
var wall_posz = wall_negz.clone();
var wall_posx = new THREE.Mesh(new THREE.PlaneGeometry(480, 150, 140, 140), wallMat);
var wall_negx = new THREE.Mesh(new THREE.PlaneGeometry(580, 150, 140, 140), wallMat);
wall_negz.position.set(0, 75, -290);
wall_posz.rotation.x = Math.PI;
wall_posz.position.set(0, 75, 290);
wall_posz.rotation.z = Math.PI;
wall_negx.position.set(-500, 75, 0);
wall_negx.rotation.y = Math.PI / 2;
wall_posx.position.set(500, 75, -50);
wall_posx.rotation.y = -Math.PI / 2;
space.add(wall_negz);
space.add(wall_posz);
space.add(wall_negx);
space.add(wall_posx);
// compartment
var compartment = new THREE.Object3D();
var compartmentMat = wallMat.clone();
var compartment1 = new THREE.Mesh(new THREE.BoxGeometry(332.8, 150, 15), compartmentMat); //左邊中間
var compartment2 = new THREE.Mesh(new THREE.BoxGeometry(15, 150, 167.6), compartmentMat); //左邊下方
var compartment3 = new THREE.Mesh(new THREE.BoxGeometry(15, 150, 136.1), compartmentMat); //左邊上方L型直
var compartment4 = new THREE.Mesh(new THREE.BoxGeometry(113.7, 150, 15), compartmentMat); //左邊上方L型橫
var compartment5 = new THREE.Mesh(new THREE.BoxGeometry(15, 150, 160), compartmentMat); //右邊下方
var compartment6 = new THREE.Mesh(new THREE.BoxGeometry(15, 150, 171.1), compartmentMat); //右邊上方靠牆直
var compartment7 = new THREE.Mesh(new THREE.BoxGeometry(174.8, 150, 15), compartmentMat); //右邊上方接下來第2個橫
var compartment8 = new THREE.Mesh(new THREE.BoxGeometry(15, 150, 173.3), compartmentMat); //右邊上方接下來第3個直
var compartment9 = new THREE.Mesh(new THREE.BoxGeometry(350.1, 150, 15), compartmentMat); //右邊上方接下來第4個橫
var door = new THREE.Mesh(new THREE.BoxGeometry(8, 150, 100), compartmentMat);
compartment1.position.set(-333.6, 75, 14.6);
compartment2.position.set(-257.7, 75, 206.2);
compartment3.position.set(-287, 75, -221.9);
compartment4.position.set(-237.6, 75, -146.4);
compartment5.position.set(151.9, 75, 210);
compartment6.position.set(188.6, 75, -204.4);
compartment7.position.set(101.2, 75, -146.4);
compartment8.position.set(13.8, 75, -79.7);
compartment9.position.set(181.3, 75, 14.4);
door.position.set(536, 75, 255);
door.rotation.y = -Math.PI / 4;
compartment.add(compartment1);
compartment.add(compartment2);
compartment.add(compartment3);
compartment.add(compartment4);
compartment.add(compartment5);
compartment.add(compartment6);
compartment.add(compartment7);
compartment.add(compartment8);
compartment.add(compartment9);
compartment.add(door);
// bench
var bench1 = new THREE.Object3D();
var benchMat = new THREE.MeshPhongMaterial({color: 0xffc160});
var top = new THREE.Mesh(new THREE.BoxGeometry(158, 4, 40), benchMat);
var base_R = new THREE.Mesh(new THREE.BoxGeometry(4, 43, 40), benchMat);
var base_L = base_R.clone();
top.position.y = 45;
base_R.position.set(77, 21.5, 0);
base_L.position.set(-77, 21.5, 0);
bench1.add(top);
bench1.add(base_R);
bench1.add(base_L);
var bench2 = bench1.clone();
bench1.position.set(-50, 0, 175);
bench2.position.set(350, 0, -132);
bench2.rotation.y = -Math.PI / 2;
////////////////////
space.add(compartment);
space.add(bench1);
space.add(bench2);
// shadow related
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
space.traverse (
function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.castShadow = true;
mesh.receiveShadow = true;
}
}
);
compartment.traverse (
function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.castShadow = false;
}
}
);
floor.castShadow = false;
wall_negz.castShadow = false;
wall_posz.castShadow = false;
wall_negx.castShadow = false;
wall_posx.castShadow = false;
return space;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
roomLight.intensity = (toggleRoom === true) ? 1 : 0;
roomLight.castShadow = (toggleRoom === true) ? true : false;
requestAnimationFrame(animate);
render(whichCamera);
}
function render(which) {
switch (which) {
case 0:
renderer.render(scene, camera);
controls.update();
//console.log(controls.target);
//console.log(camera.position);
break;
case 1:
renderer.render(scene, camera1);
controls1.update();
break;
case 2:
renderer.render(scene, camera2);
controls2.update();
break;
}
}
</script>
</body>
</html>