forked from enable3d/enable3d-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbx-loader-and-animations.html
106 lines (88 loc) · 3.42 KB
/
fbx-loader-and-animations.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
<!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</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.0.0"></script>
<script src="/lib/phaser.min.js"></script>
<script src="/lib/enable3d.phaserExtension.0.19.1.min.js"></script>
</head>
<body>
<div id="info-text">
Loads the robot, animates it and adds a cube (or any mesh you want) to its left and.
</div>
<script>
const { enable3d, Scene3D, Canvas, 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)
// 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 }
this.third.load.fbx('/assets/fbx/Idle.fbx').then(object => {
robot.add(object)
console.log(this.third)
this.third.animationMixers.add(robot.animation.mixer)
robot.animation.add('Idle', object.animations[0])
robot.animation.play('Idle')
// attach a cube to the left hand
robot.traverse(child => {
if (child.name === 'mixamorigLeftHandIndex1') {
child.add(this.third.add.box({ width: 20, height: 20, depth: 20 }))
}
})
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.animation.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.animation.play(anim, 350)
}
})
})
}
}
const config = {
type: Phaser.WEBGL,
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')
})
</script>
</body>
</html>