-
Notifications
You must be signed in to change notification settings - Fork 97
/
fbx-loader-and-animations-hitbox.html
145 lines (124 loc) Β· 4.85 KB
/
fbx-loader-and-animations-hitbox.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
<!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>FBX Loader and Animations (with Hitbox)</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/phaser.min.js?ver=3.52.0"></script>
<script src="/lib/enable3d/enable3d.phaserExtension.0.25.4.min.js"></script>
</head>
<body>
<div id="info-text">
The cube on the left hand has a physics body.<br />The cube on the right hand has no physics body.
</div>
<script>
const { enable3d, Scene3D, Canvas, THREE, ExtendedObject3D } = ENABLE3D
class MainScene extends Scene3D {
constructor() {
super({ key: 'MainScene' })
}
init() {
this.accessThirdDimension()
}
create() {
this.third.warpSpeed()
this.third.camera.position.set(20, 20, 40)
this.third.physics.debug.enable()
// These assets are from mixamo.com
// The the Idle.fbx contains the skin and the idle animation.
const robot = new ExtendedObject3D()
const animations = ['Jumping', 'LookingAround', 'Running', 'BodyJabCross', 'HipHopDancing']
const pos = { x: 0, y: 5, z: 0 }
let boxWithoutPhysics
this.third.load.fbx('/assets/fbx/Idle.fbx').then(object => {
robot.add(object)
this.third.animationMixers.add(robot.anims.mixer)
robot.anims.add('Idle', object.animations[0])
robot.anims.play('Idle')
// attach a cube to the left hand
robot.traverse(child => {
if (child.name === 'mixamorigLeftHandIndex1') {
// box with physics
if (!this.box) {
// collisionFlags 2 = kinematic
// collisionFlags 6 = kinematic and ghost
this.box = this.third.physics.add.box({ width: 1, height: 1, depth: 1, collisionFlags: 6 })
this.box.customParams = { child }
}
}
if (child.name === 'mixamorigRightHandIndex1') {
// box without physics
if (!boxWithoutPhysics) {
boxWithoutPhysics = this.third.add.box({ width: 1, height: 1, depth: 1 })
// execute this later
Promise.resolve().then(() => {
// get bone position
const pos = new THREE.Vector3()
child.getWorldPosition(pos)
// copy bone position to box
boxWithoutPhysics.position.copy(pos)
// Attach box to bone
child.attach(boxWithoutPhysics)
})
}
}
})
robot.traverse(child => {
if (child.isMesh) child.castShadow = child.receiveShadow = true
})
robot.scale.set(0.05, 0.05, 0.05)
robot.position.set(pos.x, pos.y, pos.z)
this.third.add.existing(robot)
this.third.physics.add.existing(robot, { shape: 'box', offset: { y: -0.5 } })
// load more animations
animations.forEach(key => {
if (key === 'Idle') return
this.third.load.fbx(`/assets/fbx/${key}.fbx`).then(object => {
robot.anims.add(key, object.animations[0])
})
})
this.time.addEvent({
delay: 2500,
loop: true,
callback: () => {
const anim = Phaser.Math.RND.pick(animations)
console.log(`Set animation ${anim}`)
robot.anims.play(anim, 350)
}
})
})
}
update() {
if (this.box && this.box.body) {
const child = this.box.customParams.child
const pos = child.getWorldPosition(new THREE.Vector3())
this.box.position.copy(pos)
this.box.rotation.copy(child.rotation)
this.box.body.needUpdate = true
}
}
}
const config = {
type: Phaser.WEBGL,
transparent: true,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: window.innerWidth * Math.max(1, window.devicePixelRatio / 2),
height: window.innerHeight * Math.max(1, window.devicePixelRatio / 2)
},
scene: [MainScene],
...Canvas()
}
window.addEventListener('load', () => {
enable3d(() => new Phaser.Game(config)).withPhysics('/lib/ammo/kripken')
})
</script>
</body>
</html>