-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
357 lines (304 loc) · 11 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My first three.js app</title>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<div id="stats"></div>
<div id="main"></div>
</body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/17.2.0/Tween.min.js"></script>
<script src="./handler.js"></script>
<script type="module">
import * as THREE from 'https://cdn.skypack.dev/[email protected]'
import { OrbitControls } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/controls/OrbitControls.js'
import { GLTFLoader } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/loaders/GLTFLoader.js'
import { Lensflare, LensflareElement } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/objects/Lensflare.js'
let scene, renderer, camera
let cameraControl, stats
let mixers = []
let creeperObj
let allCreaper = []
let tween, tweenBack
let modelMesh
let keysPressed = {}
let bullets = []
const cameraRange = 10 // 模型與相機之間前後的距離
const cameraHeight = 5 // 模型與相機之間的高度
const speed = 20 // 移動速度
const rotateSpeed = 0.05 // 旋轉速度
class Creeper {
constructor() {
// 宣告頭、身體、腳幾何體大小
const headGeo = new THREE.BoxGeometry(40, 40, 40)
const bodyGeo = new THREE.BoxGeometry(40, 80, 20)
const footGeo = new THREE.BoxGeometry(20, 30, 20)
// 苦力怕臉部貼圖
const headMap = new THREE.TextureLoader().load('../assets/img/creeper_face.png')
// 苦力怕皮膚貼圖
const skinMap = new THREE.TextureLoader().load('../assets/img/creeper.png')
// 身體與腳的材質設定
const skinMat = new THREE.MeshPhongMaterial({
map: skinMap, // 皮膚貼圖
})
// 準備頭部與臉的材質
const headMaterials = []
for (let i = 0; i < 6; i++) {
let map
if (i === 4) map = headMap
else map = skinMap
headMaterials.push(new THREE.MeshPhongMaterial({ map: map }))
}
// 頭
this.head = new THREE.Mesh(headGeo, headMaterials)
this.head.position.set(0, 60, 0)
this.head.rotation.y = 0.5 // 稍微的擺頭
// 身體
this.body = new THREE.Mesh(bodyGeo, skinMat)
this.body.position.set(0, 0, 0)
// 四隻腳
this.foot1 = new THREE.Mesh(footGeo, skinMat)
this.foot1.position.set(-10, -55, 20)
this.foot2 = this.foot1.clone() // 剩下三隻腳都複製第一隻的 Mesh
this.foot2.position.set(-10, -55, -20)
this.foot3 = this.foot1.clone()
this.foot3.position.set(10, -55, 20)
this.foot4 = this.foot1.clone()
this.foot4.position.set(10, -55, -20)
// 將四隻腳組合為一個 group
this.feet = new THREE.Group()
this.feet.add(this.foot1)
this.feet.add(this.foot2)
this.feet.add(this.foot3)
this.feet.add(this.foot4)
// 將頭、身體、腳組合為一個 group
this.creeper = new THREE.Group()
this.creeper.add(this.head)
this.creeper.add(this.body)
this.creeper.add(this.feet)
// 苦力怕投影設定,利用 traverse 遍歷各個子元件設定陰影
this.creeper.traverse(function (object) {
if (object instanceof THREE.Mesh) {
object.castShadow = true
object.receiveShadow = true
}
})
}
}
// 添加光線
function addLight() {
// 設置環境光提供輔助柔和白光
const ambientLight = new THREE.AmbientLight(0x404040)
scene.add(ambientLight)
const spotLight = new THREE.PointLight(0xffffff, 2)
spotLight.position.set(0, 600, 0)
spotLight.castShadow = true
spotLight.shadow.mapSize.width = 1024
spotLight.shadow.mapSize.height = 1024
// 光源輔助器
spotLight.shadow.camera.near = 0.1
spotLight.shadow.camera.far = 100000
spotLight.shadow.camera.fov = 90
scene.add(spotLight)
var textureFlare2 = new THREE.TextureLoader().load('./20200630103311828.png')
var textureFlare0 = new THREE.TextureLoader().load('./20200630103304387.png')
var lensFlare = new Lensflare()
lensFlare.addElement(new LensflareElement(textureFlare0, 1000, 0.0, new THREE.Color(0xffffff)))
lensFlare.addElement(new LensflareElement(textureFlare2, 60, 0.6, new THREE.Color(0xffff00)))
lensFlare.addElement(new LensflareElement(textureFlare2, 70, 0.7, new THREE.Color(0x00ffff)))
lensFlare.addElement(new LensflareElement(textureFlare2, 120, 0.9, new THREE.Color(0x00ff00)))
lensFlare.addElement(new LensflareElement(textureFlare2, 70, 1.0, new THREE.Color(0x0000ff)))
lensFlare.position.copy(spotLight.position)
// scene.add(lensFlare)
}
// 生成苦力怕並加到場景
function createCreeper() {
creeperObj = new Creeper()
creeperObj.creeper.position.set(THREE.Math.randInt(-500, 500), 100, THREE.Math.randInt(-500, 500))
scene.add(creeperObj.creeper)
allCreaper.push(creeperObj.creeper)
}
// 生成鳳凰
function createPhoenix() {
const loader = new GLTFLoader()
loader.load(
'./models/phoenix/scene.gltf',
function (gltf) {
camera.position.set(-15, 105, 0) // 相機位置
scene.add(camera)
gltf.scene.scale.set(0.01, 0.01, 0.01)
gltf.scene.position.set(camera.position.x + cameraRange, camera.position.y - cameraHeight, camera.position.z)
gltf.scene.rotation.copy(camera.rotation)
scene.add(gltf.scene) // 將模型引入three
gltf.scene.traverse(function (node) {
if (node.isMesh) {
node.castShadow = true
node.receiveShadow = true
}
})
modelMesh = gltf.scene
// 調用動畫
const mixer = new THREE.AnimationMixer(gltf.scene)
mixer.clipAction(gltf.animations[0]).play()
mixers.push(mixer)
},
undefined,
function (error) {
console.error(error)
}
)
}
// 建立粒子系統(雪花)
function createPoints() {
const texture = new THREE.TextureLoader().load('./20107572piCtGLYX4z.png')
const material = new THREE.PointsMaterial({
size: 50,
map: texture,
blending: THREE.AdditiveBlending,
depthTest: false,
transparent: true,
opacity: 0.7,
})
let vertices = []
for (let i = 0; i < 100000; i++) {
const x = THREE.Math.randInt(-50000, 50000)
const y = THREE.Math.randInt(-50000, 50000)
const z = THREE.Math.randInt(-50000, 50000)
const point = new THREE.Vector3(x, y, z)
vertices.push(point)
}
const geometry = new THREE.BufferGeometry().setFromPoints(vertices)
const points = new THREE.Points(geometry, material)
scene.add(points)
}
// 發射
function attack() {
const geometry = new THREE.BoxGeometry(1, 1, 1)
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
const cube = new THREE.Mesh(geometry, material)
cube.position.copy(modelMesh.position)
cube.rotation.copy(modelMesh.rotation)
scene.add(cube)
bullets.push(cube)
}
// 創建天空場景
function createSkybox() {
let materialArray = []
let texture_ft = new THREE.TextureLoader().load('./penguins/yonder_ft.jpg')
let texture_bk = new THREE.TextureLoader().load('./penguins/yonder_bk.jpg')
let texture_up = new THREE.TextureLoader().load('./penguins/yonder_up.jpg')
let texture_dn = new THREE.TextureLoader().load('./penguins/yonder_dn.jpg')
let texture_rt = new THREE.TextureLoader().load('./penguins/yonder_rt.jpg')
let texture_lf = new THREE.TextureLoader().load('./penguins/yonder_lf.jpg')
materialArray.push(new THREE.MeshBasicMaterial({ map: texture_ft }))
materialArray.push(new THREE.MeshBasicMaterial({ map: texture_bk }))
materialArray.push(new THREE.MeshBasicMaterial({ map: texture_up }))
materialArray.push(new THREE.MeshBasicMaterial({ map: texture_dn }))
materialArray.push(new THREE.MeshBasicMaterial({ map: texture_rt }))
materialArray.push(new THREE.MeshBasicMaterial({ map: texture_lf }))
for (let i = 0; i < 6; i++) materialArray[i].side = THREE.BackSide
let skyboxGeo = new THREE.BoxGeometry(100000, 100000, 100000)
let skybox = new THREE.Mesh(skyboxGeo, materialArray)
scene.add(skybox)
}
function initStats() {
const stats = new Stats()
stats.setMode(0)
document.getElementById('stats').appendChild(stats.domElement)
return stats
}
const clock = new THREE.Clock()
function init() {
scene = new THREE.Scene()
camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 100000)
// camera.position.set(-15, 105, 0) // 相機位置
let axes = new THREE.AxesHelper(20)
scene.add(axes)
stats = initStats()
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.shadowMap.enabled = true
renderer.setClearColor(0x111111)
renderer.shadowMap.type = 2 // THREE.PCFSoftShadowMap
cameraControl = new OrbitControls(camera, renderer.domElement)
cameraControl.enableDamping = true // 啟用阻尼效果
cameraControl.dampingFactor = 0.25 // 阻尼系數
for (let i = 0; i < 50; i++) {
createCreeper()
}
createSkybox()
createPhoenix()
createPoints()
addLight()
// createCreeper()
// 監聽鍵盤事件
document.addEventListener('keyup', onKeyUp)
document.addEventListener('keypress', onKeyPress)
document.addEventListener('keydown', onKeyDown)
// 將渲染器的 DOM 綁到網頁上
document.getElementById('main').appendChild(renderer.domElement)
}
// 控制移動
function mainHandler() {
TWEEN.removeAll()
bullets = bulletHandler(THREE, bullets, allCreaper, scene, renderer)
cameraHandler(cameraControl, modelMesh)
moveHandler(THREE, scene, modelMesh, camera, keysPressed, cameraHeight, cameraRange, speed, rotateSpeed)
}
// 鬆開按鍵
function onKeyUp(event) {
delete keysPressed[event.key]
}
// 按住按鈕
function onKeyPress(event) {
keysPressed[event.key] = true
}
// 按下按鈕
function onKeyDown(event) {
if (event.key === ' ') {
attack()
}
}
function animations() {
var delta = clock.getDelta()
for (var i = 0; i < mixers.length; i++) {
// 重復播放動畫
mixers[i].update(delta)
}
}
function render() {
cameraControl.update() // 需設定 update
animations()
TWEEN.update()
requestAnimationFrame(render)
mainHandler()
renderer.render(scene, camera)
stats.update()
}
// 監聽螢幕寬高變化來做簡單 RWD 設定
window.addEventListener('resize', function () {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
})
init()
render()
</script>
<style>
#stats {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display: block;
}
</style>