/
day23.js
547 lines (467 loc) · 16.4 KB
/
day23.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
let scene, renderer, camera
let cameraControl, stats, gui
let usagiObj
let Speed = 0
// Cannon.js
var world
let groundBody,groundCM,gripperRGroup,gripperRCM
let friction = 0.5
let restitution = 0.7
// Usagi初始位置
let usagi_init_x = 0
let usagi_init_y = 5.1
let usagi_init_z = 0
// 夾爪初始位置
let gripperR_init_x = -3.8
let gripperR_init_y = 9.5
let gripperR_init_z = 1
let gripperL_init_x = -6.8
let gripperL_init_y = 9.5
let gripperL_init_z = 1
class Usagi{
constructor(){
const headGeo = new THREE.BoxGeometry(1.5, 1, 1);
const earGeo = new THREE.BoxGeometry(0.25, 0.75, 0.25);
//設定 Usagi 圖案
const headTexture = new THREE.TextureLoader().load('images/Usagi_face.jpg')
const earTexture = new THREE.TextureLoader().load('images/Usagi_ear.jpg')
// 每一面同圖案
const earMaterial = new THREE.MeshLambertMaterial({map:earTexture})
// 只給一面有臉
const headMaterials = []
for (let i = 0; i < 6; i++) {
let map
if (i === 4){
map = headTexture
}
else map = earTexture
headMaterials.push(new THREE.MeshStandardMaterial({ map: map }))
}
//設定部位
this.head = new THREE.Mesh(headGeo,headMaterials)
this.head.position.set(0,0,0)
this.ear1 = new THREE.Mesh(earGeo,earMaterial)
this.ear1.position.set(-0.25,0.5,0)
this.ear2 = new THREE.Mesh(earGeo,earMaterial)
this.ear2.position.set(0.25,0.5,0)
// 組合
this.usagi = new THREE.Group()
this.usagi.add(this.head)
this.usagi.add(this.ear1)
this.usagi.add(this.ear2)
this.usagi.traverse(function(object) {
if (object instanceof THREE.Mesh) {
object.castShadow = true
object.receiveShadow = true
}
})
}
}
// 加入場景
function createUsagi(){
usagiObj = new Usagi()
usagiObj.usagi.position.x = 0
usagiObj.usagi.position.y = 0
usagiObj.usagi.position.z = 12
scene.add(usagiObj.usagi)
}
// 建立監測器
function initStats() {
const stats = new Stats()
stats.setMode(0)
document.getElementById('stats').appendChild(stats.domElement)
return stats
}
// 初始化
function init() {
scene = new THREE.Scene()
camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
1000
)
camera.position.set(0, 10, 30)
camera.lookAt(scene.position)
statsUI = initStats()
// 建立渲染器
renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
// 設定背景顏色
renderer.setClearColor(0xeeeeee, 1.0)
renderer.shadowMap.enabled = true
renderer.outputEncoding = THREE.sRGBEncoding;
// 陰影貼圖種類
renderer.shadowMap.type = 2
// 設置環境光提供輔助柔和白光
let ambientLight = new THREE.AmbientLight(0x404040)
scene.add(ambientLight)
// 設置聚光燈幫忙照亮物體
let spotLight = new THREE.SpotLight(0xf0f0f0)
spotLight.position.set(-10, 30, 20)
scene.add(spotLight)
// 建立光源
let pointLight = new THREE.PointLight(0xccffcc, 1, 100)
pointLight.castShadow = true // 投影
pointLight.position.set(-30, 30, 30)
scene.add(pointLight)
//建立軌道控制器
cameraControl = new THREE.OrbitControls(camera, renderer.domElement)
// 啟用阻尼效果
cameraControl.enableDamping = true
// 阻尼系數
cameraControl.dampingFactor = 0.05
//物件加入場景
createUsagi()
// 將渲染器的 DOM 綁到網頁上
document.body.appendChild(renderer.domElement)
}
function initCannon(){
// 建立Cannon世界
world = new CANNON.World()
// 設定重力場為 y 軸 -9.8 m/s²
world.gravity.set(0, -9.8, 0)
// 碰撞偵測
world.broadphase = new CANNON.NaiveBroadphase()
// 建立地板剛體
let groundShape = new CANNON.Plane()
groundCM = new CANNON.Material()
groundBody = new CANNON.Body({
mass: 0,
shape: groundShape,
material: groundCM
})
// setFromAxisAngle 地板沿著X軸轉90度
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2)
world.add(groundBody)
// 地板網格
let groundGeometry = new THREE.PlaneGeometry(30, 30, 30)
let groundtexture = new THREE.TextureLoader().load('images/map.png')
let groundMaterial = new THREE.MeshLambertMaterial({
map:groundtexture,side: THREE.DoubleSide,
})
let ground = new THREE.Mesh(groundGeometry, groundMaterial)
ground.rotation.x = -Math.PI / 2
ground.receiveShadow = true
scene.add(ground)
// gripperR 剛體
let gripperRShape = new CANNON.Box(new CANNON.Vec3(0.6, 0.1, 0.3))
gripperRCM = new CANNON.Material()
gripperRBody = new CANNON.Body({
mass: 0,
shape: gripperRShape,
position: new CANNON.Vec3(gripperR_init_x, gripperR_init_y, gripperR_init_z),
material: gripperRCM
})
world.add(gripperRBody)
// gripperR 網格
const gripperRGeo = new THREE.BoxGeometry(1.2, 0.2, 0.6);
const gripperRMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
gripperR = new THREE.Mesh(gripperRGeo, gripperRMat);
gripperR.castShadow = true
scene.add(gripperR);
// gripperRTop 剛體
let gripperRTopShape = new CANNON.Box(new CANNON.Vec3(0.2, 0.5, 0.25))
gripperRTopCM = new CANNON.Material()
gripperRTopBody = new CANNON.Body({
mass: 5,
shape: gripperRTopShape,
position: new CANNON.Vec3(gripperR_init_x+0.4, gripperR_init_y+0.5, gripperR_init_z),
material: gripperRTopCM
})
world.add(gripperRTopBody)
// gripperRTop 網格
const gripperRTopGeo = new THREE.BoxGeometry(0.4, 1, 0.5);
const gripperRTopMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
gripperRTop = new THREE.Mesh(gripperRTopGeo, gripperRTopMat);
gripperRTop.castShadow = true
scene.add(gripperRTop)
// gripperR 網格組裝
gripperRBodyJoint = new CANNON.LockConstraint(gripperRBody,gripperRTopBody)
world.addConstraint(gripperRBodyJoint)
// gripperL剛體
let gripperLShape = new CANNON.Box(new CANNON.Vec3(0.6, 0.1, 0.25))
let gripperLCM = new CANNON.Material()
gripperLBody = new CANNON.Body({
mass: 0,
shape: gripperLShape,
position: new CANNON.Vec3(gripperL_init_x, gripperL_init_y, gripperL_init_z),
material: gripperLCM,
})
world.add(gripperLBody)
// gripperL網格
const gripperLGeo = new THREE.BoxGeometry(1.2, 0.2, 0.5);
const gripperLMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
gripperL = new THREE.Mesh(gripperLGeo, gripperLMat);
gripperL.castShadow = true
scene.add(gripperL);
// gripperLTop 剛體
let gripperLTopShape = new CANNON.Box(new CANNON.Vec3(0.2, 0.5, 0.25))
gripperLTopCM = new CANNON.Material()
gripperLTopBody = new CANNON.Body({
mass: 5,
shape: gripperLTopShape,
position: new CANNON.Vec3(gripperL_init_x-0.4, gripperL_init_y+0.5, gripperL_init_z),
material: gripperLTopCM
})
world.add(gripperLTopBody)
// gripperLTop 網格
const gripperLTopGeo = new THREE.BoxGeometry(0.4, 1, 0.5);
const gripperLTopMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
gripperLTop = new THREE.Mesh(gripperLTopGeo, gripperLTopMat);
gripperLTop.castShadow = true
scene.add(gripperLTop)
// gripperL 網格組裝
gripperLBodyJoint = new CANNON.LockConstraint(gripperLBody,gripperLTopBody)
world.addConstraint(gripperLBodyJoint)
}
function initClawMachineCannon(){
// 建立機台夾層地板剛體
let machineBottom = new CANNON.Box(new CANNON.Vec3(3.5, 0.1, 3))
let machineBottomMaterial = new CANNON.Material()
machineBottomBody = new CANNON.Body({
shape: machineBottom,
position: new CANNON.Vec3(-0.5, 4.5, 0),
material: machineBottomMaterial,
})
world.add(machineBottomBody)
// 機台夾層地板網格
const machineBottomGeo = new THREE.BoxGeometry(6.8, 0.2, 6);
const machineBottomMat = new THREE.MeshPhongMaterial({
color: 0x000000,
});
machine = new THREE.Mesh(machineBottomGeo, machineBottomMat);
machine.castShadow = true
scene.add(machine);
// 右牆壁剛體
let wallshape = new CANNON.Box(new CANNON.Vec3(0.1, 2.7, 3))
let wallMaterial = new CANNON.Material()
wallBody = new CANNON.Body({
shape: wallshape,
position: new CANNON.Vec3(3, 7.2, 0),
material: wallMaterial,
})
world.add(wallBody)
// 右牆壁網格
const wallGeo = new THREE.BoxGeometry(0.2, 5.4, 6);
const wallMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
wall = new THREE.Mesh(wallGeo, wallMat);
wall.castShadow = true
scene.add(wall);
// 左牆壁剛體
let wallLshape = new CANNON.Box(new CANNON.Vec3(0.1, 4.5, 3))
let wallLMaterial = new CANNON.Material()
wallLBody = new CANNON.Body({
shape: wallLshape,
position: new CANNON.Vec3(-7.5, 5.4, 0),
material: wallLMaterial,
})
world.add(wallLBody)
// 左牆壁網格
const wallLGeo = new THREE.BoxGeometry(0.2, 9, 6);
const wallLMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
wallL = new THREE.Mesh(wallLGeo, wallLMat);
wallL.castShadow = true
scene.add(wallL);
// 檔板剛體
let swallshape = new CANNON.Box(new CANNON.Vec3(0.1, 2.5, 3))
let swallMaterial = new CANNON.Material()
swallBody = new CANNON.Body({
shape: swallshape,
position: new CANNON.Vec3(-4, 3.4, 0),
material: swallMaterial,
})
world.add(swallBody)
//檔板網格
const swallGeo = new THREE.BoxGeometry(0.2, 5, 6);
const swallMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
swall = new THREE.Mesh(swallGeo, swallMat);
swall.castShadow = true
scene.add(swall);
// 掉落物地板剛體
let floorshape = new CANNON.Box(new CANNON.Vec3(1.8, 0.1, 3))
let floorMaterial = new CANNON.Material()
floorBody = new CANNON.Body({
shape: floorshape,
position: new CANNON.Vec3(-5.8, 1, 0),
material: floorMaterial,
})
world.add(floorBody)
//掉落物地板網格
const floorGeo = new THREE.BoxGeometry(3.6, 0.2, 6);
const floorMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
floor = new THREE.Mesh(floorGeo, floorMat);
floor.castShadow = true
scene.add(floor);
// 背板剛體
let backwallshape = new CANNON.Box(new CANNON.Vec3(5.2, 2.8, 0.1))
let backwallMaterial = new CANNON.Material()
backwallBody = new CANNON.Body({
shape: backwallshape,
position: new CANNON.Vec3(-2.2, 7.1, -3),
material: backwallMaterial,
})
world.add(backwallBody)
// 背板網格
const backwallGeo = new THREE.BoxGeometry(10.5, 5.6, 0.2);
const backwallMat = new THREE.MeshPhongMaterial({
color: 0x1C4265,
});
backwall = new THREE.Mesh(backwallGeo, backwallMat);
backwall.castShadow = true
scene.add(backwall);
// 前玻璃剛體
let frontwallshape = new CANNON.Box(new CANNON.Vec3(5.2, 2.8, 0.1))
let frontwallMaterial = new CANNON.Material()
frontwallBody = new CANNON.Body({
shape: frontwallshape,
position: new CANNON.Vec3(-2.2, 7.1, 3),
material: frontwallMaterial,
})
world.add(frontwallBody)
// 前玻璃網格
const frontwallGeo = new THREE.BoxGeometry(10.5, 5.6, 0.2);
const frontwallMat = new THREE.MeshPhongMaterial({
color: 0x1C4265,
transparent:true,
opacity:0.3,
});
frontwall = new THREE.Mesh(frontwallGeo, frontwallMat);
frontwall.castShadow = true
scene.add(frontwall);
}
function initUsagiCannon(){
// 建立Usagi A 剛體 Sphere(radius)
let UsagiAShape = new CANNON.Box(new CANNON.Vec3(0.75, 0.1, 0.5))
UsagiACM = new CANNON.Material()
UsagiABody = new CANNON.Body({
mass: 1,
shape: UsagiAShape,
position: new CANNON.Vec3(usagi_init_x,usagi_init_y+0.4,usagi_init_z),
material: UsagiACM,
})
world.add(UsagiABody)
// Usagi A網格
const UsagiAGeo = new THREE.BoxGeometry(1.5, 0.2, 1);
const UsagiAMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
UsagiA = new THREE.Mesh(UsagiAGeo, UsagiAMat);
UsagiA.castShadow = true
// scene.add(UsagiA);
// 建立Usagi B 剛體 Sphere(radius)
let UsagiBShape = new CANNON.Box(new CANNON.Vec3(0.75, 0.1, 0.5))
let UsagiBCM = new CANNON.Material()
UsagiBBody = new CANNON.Body({
mass: 1,
shape: UsagiBShape,
position: new CANNON.Vec3(usagi_init_x, usagi_init_y-0.4, usagi_init_z),
material: UsagiBCM,
})
world.add(UsagiBBody)
// Usagi B網格
const UsagiBGeo = new THREE.BoxGeometry(1.5, 0.2, 1);
const UsagiBMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
UsagiB = new THREE.Mesh(UsagiBGeo, UsagiBMat);
UsagiB.castShadow = true
// scene.add(UsagiB);
// 建立Usagi C 剛體 Sphere(radius)
let UsagiCShape = new CANNON.Box(new CANNON.Vec3(0.25, 0.3, 0.25))
let UsagiCCM = new CANNON.Material()
UsagiCBody = new CANNON.Body({
mass: 1,
shape: UsagiCShape,
position: new CANNON.Vec3(usagi_init_x, usagi_init_y, usagi_init_z),
material: UsagiCCM,
})
world.add(UsagiCBody)
// Usagi C網格
const UsagiCGeo = new THREE.BoxGeometry(0.5, 0.6, 0.5);
const UsagiCMat = new THREE.MeshPhongMaterial({
color: 0x4287f5,
});
UsagiC = new THREE.Mesh(UsagiCGeo, UsagiCMat);
UsagiC.castShadow = true
// scene.add(UsagiC);
// Usagi 剛體組裝
UsagiBodyJoint1 = new CANNON.LockConstraint(UsagiABody,UsagiBBody)
world.addConstraint(UsagiBodyJoint1)
UsagiBodyJoint2 = new CANNON.LockConstraint(UsagiBBody,UsagiCBody)
world.addConstraint(UsagiBodyJoint2)
}
const timeStep = 1.0 / 60.0 // seconds
function render() {
world.step(timeStep)
// 複製剛體位址到物體位置
// UsagiA.position.copy(UsagiABody.position)
// UsagiA.quaternion.copy(UsagiABody.quaternion)
// UsagiB.position.copy(UsagiBBody.position)
// UsagiB.quaternion.copy(UsagiBBody.quaternion)
// UsagiC.position.copy(UsagiCBody.position)
// UsagiC.quaternion.copy(UsagiCBody.quaternion)
usagiObj.usagi.position.copy(UsagiCBody.position)
usagiObj.usagi.quaternion.copy(UsagiCBody.quaternion)
statsUI.update()
TWEEN.update()
cameraControl.update()
requestAnimationFrame(wall_load)
requestAnimationFrame(gripper_load)
requestAnimationFrame(render)
renderer.render(scene, camera)
}
function wall_load() {
// 機台
machine.position.copy(machineBottomBody.position)
machine.quaternion.copy(machineBottomBody.quaternion)
wall.position.copy(wallBody.position)
wall.quaternion.copy(wallBody.quaternion)
wallL.position.copy(wallLBody.position)
wallL.quaternion.copy(wallLBody.quaternion)
floor.position.copy(floorBody.position)
floor.quaternion.copy(floorBody.quaternion)
swall.position.copy(swallBody.position)
swall.quaternion.copy(swallBody.quaternion)
backwall.position.copy(backwallBody.position)
backwall.quaternion.copy(backwallBody.quaternion)
frontwall.position.copy(frontwallBody.position)
frontwall.quaternion.copy(frontwallBody.quaternion)
}
function gripper_load(){
gripperRTop.position.copy(gripperRTopBody.position)
gripperRTop.quaternion.copy(gripperRTopBody.quaternion)
gripperR.position.copy(gripperRBody.position)
gripperR.quaternion.copy(gripperRBody.quaternion)
gripperLTop.position.copy(gripperLTopBody.position)
gripperLTop.quaternion.copy(gripperLTopBody.quaternion)
gripperL.position.copy(gripperLBody.position)
gripperL.quaternion.copy(gripperLBody.quaternion)
}
// 監聽螢幕寬高來做簡單 RWD 設定
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
})
init()
initCannon()
initUsagiCannon()
initClawMachineCannon()
render()