-
Notifications
You must be signed in to change notification settings - Fork 97
/
medieval-fantasy-book-standalone.html
334 lines (295 loc) Β· 10.4 KB
/
medieval-fantasy-book-standalone.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Medieval Fantasy Book</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.1.1"></script>
<script src="/lib/enable3d/enable3d.framework.0.25.4.min.js"></script>
</head>
<body>
<div id="info-text">Use WASD, SPACE and your Mouse.<br />Try to play it on your mobile device :)</div>
<script>
const {
Project,
PhysicsLoader,
Scene3D,
ExtendedObject3D,
THREE,
JoyStick,
ThirdPersonControls,
PointerLock,
PointerDrag
} = ENABLE3D
/**
* Is touch device?
*/
const isTouchDevice = 'ontouchstart' in window
class MainScene extends Scene3D {
constructor() {
super('MainScene')
}
init() {
this.renderer.setPixelRatio(Math.max(1, window.devicePixelRatio / 2))
this.canJump = true
this.move = false
this.moveTop = 0
this.moveRight = 0
}
async preload() {
/**
* Medieval Fantasy Book by Pixel (https://sketchfab.com/stefan.lengyel1)
* https://sketchfab.com/3d-models/medieval-fantasy-book-06d5a80a04fc4c5ab552759e9a97d91a
* Attribution 4.0 International (CC BY 4.0)
*/
const book = this.load.preload('book', '/assets/glb/book.glb')
/**
* box_man.glb by Jan BlΓ‘ha
* https://github.com/swift502/Sketchbook
* CC-0 license 2018
*/
const man = this.load.preload('man', '/assets/glb/box_man.glb')
await Promise.all([book, man])
}
async create() {
const { lights } = await this.warpSpeed('-ground', '-orbitControls')
const { hemisphereLight, ambientLight, directionalLight } = lights
const intensity = 1
hemisphereLight.intensity = intensity
ambientLight.intensity = intensity
directionalLight.intensity = intensity
// this.physics.debug.enable()
const addBook = async () => {
const object = await this.load.gltf('book')
const scene = object.scenes[0]
const book = new ExtendedObject3D()
book.name = 'scene'
book.add(scene)
this.add.existing(book)
// add animations
// sadly only the flags animations works
object.animations.forEach((anim, i) => {
book.mixer = this.animationMixers.create(book)
// overwrite the action to be an array of actions
book.action = []
book.action[i] = book.mixer.clipAction(anim)
book.action[i].play()
})
book.traverse(child => {
if (child.isMesh) {
child.castShadow = child.receiveShadow = false
child.material.metalness = 0
child.material.roughness = 1
if (/mesh/i.test(child.name)) {
this.physics.add.existing(child, {
shape: 'concave',
mass: 0,
collisionFlags: 1,
autoCenter: false
})
child.body.setAngularFactor(0, 0, 0)
child.body.setLinearFactor(0, 0, 0)
}
}
})
}
const addMan = async () => {
const object = await this.load.gltf('man')
const man = object.scene.children[0]
this.man = new ExtendedObject3D()
this.man.name = 'man'
this.man.rotateY(Math.PI + 0.1) // a hack
this.man.add(man)
this.man.rotation.set(0, Math.PI * 1.5, 0)
this.man.position.set(35, 0, 0)
// add shadow
this.man.traverse(child => {
if (child.isMesh) {
child.castShadow = child.receiveShadow = false
// https://discourse.threejs.org/t/cant-export-material-from-blender-gltf/12258
child.material.roughness = 1
child.material.metalness = 0
}
})
/**
* Animations
*/
this.animationMixers.add(this.man.animation.mixer)
object.animations.forEach(animation => {
if (animation.name) {
this.man.animation.add(animation.name, animation)
}
})
this.man.animation.play('idle')
/**
* Add the player to the scene with a body
*/
this.add.existing(this.man)
this.physics.add.existing(this.man, {
shape: 'sphere',
radius: 0.25,
width: 0.5,
offset: { y: -0.25 }
})
this.man.body.setFriction(0.8)
this.man.body.setAngularFactor(0, 0, 0)
// https://docs.panda3d.org/1.10/python/programming/physics/bullet/ccd
this.man.body.setCcdMotionThreshold(1e-7)
this.man.body.setCcdSweptSphereRadius(0.25)
/**
* Add 3rd Person Controls
*/
this.controls = new ThirdPersonControls(this.camera, this.man, {
offset: new THREE.Vector3(0, 1, 0),
targetRadius: 3
})
// set initial view to 90 deg theta
this.controls.theta = 90
/**
* Add Pointer Lock and Pointer Drag
*/
if (!isTouchDevice) {
let pl = new PointerLock(this.canvas)
let pd = new PointerDrag(this.canvas)
pd.onMove(delta => {
if (pl.isLocked()) {
this.controls.update(delta.x * 2, delta.y * 2)
}
})
}
}
addBook()
addMan()
/**
* Add Keys
*/
this.keys = {
w: { isDown: false },
a: { isDown: false },
s: { isDown: false },
d: { isDown: false },
space: { isDown: false }
}
const press = (e, isDown) => {
e.preventDefault()
const { keyCode } = e
switch (keyCode) {
case 87: // w
this.keys.w.isDown = isDown
break
case 38: // arrow up
this.keys.w.isDown = isDown
break
case 32: // space
this.keys.space.isDown = isDown
break
}
}
document.addEventListener('keydown', e => press(e, true))
document.addEventListener('keyup', e => press(e, false))
/**
* Add joystick
*/
if (isTouchDevice) {
const joystick = new JoyStick()
const axis = joystick.add.axis({
styles: { left: 35, bottom: 35, size: 100 }
})
axis.onMove(event => {
/**
* Update Camera
*/
const { top, right } = event
this.moveTop = top * 3
this.moveRight = right * 3
})
const buttonA = joystick.add.button({
letter: 'A',
styles: { right: 35, bottom: 110, size: 80 }
})
buttonA.onClick(() => this.jump())
const buttonB = joystick.add.button({
letter: 'B',
styles: { right: 110, bottom: 35, size: 80 }
})
buttonB.onClick(() => (this.move = true))
buttonB.onRelease(() => (this.move = false))
}
setTimeout(() => {
const placeholder = document.getElementById('welcome-game-placeholder')
if (placeholder) placeholder.remove()
}, 500)
}
jump() {
if (!this.man || !this.canJump) return
this.canJump = false
this.man.animation.play('jump_running', 500, false)
setTimeout(() => {
this.canJump = true
this.man.animation.play('idle')
}, 650)
this.man.body.applyForceY(6)
}
update(time, delta) {
if (this.man && this.man.body) {
/**
* Update Controls
*/
this.controls.update(this.moveRight * 2, -this.moveTop * 2)
/**
* Player Turn
*/
const speed = 4
const v3 = new THREE.Vector3()
const rotation = this.camera.getWorldDirection(v3)
const theta = Math.atan2(rotation.x, rotation.z)
const rotationMan = this.man.getWorldDirection(v3)
const thetaMan = Math.atan2(rotationMan.x, rotationMan.z)
this.man.body.setAngularVelocityY(0)
const l = Math.abs(theta - thetaMan)
let rotationSpeed = isTouchDevice ? 2 : 4
let d = Math.PI / 24
if (l > d) {
if (l > Math.PI - d) rotationSpeed *= -1
if (theta < thetaMan) rotationSpeed *= -1
this.man.body.setAngularVelocityY(rotationSpeed)
}
/**
* Player Move
*/
if (this.keys.w.isDown || this.move) {
if (this.man.animation.current === 'idle' && this.canJump) this.man.animation.play('run')
const x = Math.sin(theta) * speed,
y = this.man.body.velocity.y,
z = Math.cos(theta) * speed
this.man.body.setVelocity(x, y, z)
} else {
if (this.man.animation.current === 'run' && this.canJump) this.man.animation.play('idle')
}
/**
* Player Jump
*/
if (this.keys.space.isDown && this.canJump) {
this.jump()
}
}
}
}
window.addEventListener('load', () => {
PhysicsLoader('/lib/ammo/kripken', () => {
const project = new Project({
antialias: false,
maxSubSteps: 10,
fixedTimeStep: 1 / 120,
scenes: [MainScene]
})
})
})
</script>
</body>
</html>