-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathheadless-fbx-loader.js
82 lines (63 loc) · 1.9 KB
/
headless-fbx-loader.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
/**
* TypeScript Example: https://github.com/enable3d/ammo-on-nodejs-example
*/
var _ammo = require('@enable3d/ammo-on-nodejs/ammo/ammo.js')
const { Physics, ServerClock, Loaders, ExtendedObject3D } = require('@enable3d/ammo-on-nodejs')
const path = require('path')
class ServerScene {
constructor() {
this.init()
this.create()
}
init() {
// test if we have access to Ammo
console.log('Ammo', new Ammo.btVector3(1, 2, 3).y() === 2)
// init the Physics
this.physics = new Physics()
this.factory = this.physics.factory
}
create() {
const ground = this.physics.add.box({
name: 'ground',
width: 40,
depth: 40,
collisionFlags: 2,
mass: 0
})
const FBXLoader = new Loaders.FBXLoader()
FBXLoader.load(path.resolve(__dirname, '../assets/fbx/Idle.fbx')).then(fbx => {
const robot = new ExtendedObject3D()
robot.name = 'robot'
robot.add(fbx)
robot.scale.set(0.05, 0.05, 0.05)
robot.position.set(0, 10, 0)
const physicsOptions = {
addChildren: false,
shape: 'hull' // or any other shape you want
}
this.physics.add.existing(robot, physicsOptions)
this.robot = robot
})
// clock
const clock = new ServerClock()
// for debugging you disable high accuracy
// high accuracy uses much more cpu power
if (process.env.NODE_ENV !== 'production') clock.disableHighAccuracy()
clock.onTick(delta => this.update(delta))
}
update(delta) {
this.physics.update(delta * 1000)
if (!this.robot) return // robot has not been parsed yet
const y = this.robot.position.y.toFixed(2)
// watch the y position fall down from 5 to the ground
if (y > 2) console.log('y:', y)
// TODO
// send new positions to the client
}
}
// wait for Ammo to be loaded
_ammo().then(ammo => {
globalThis.Ammo = ammo
// start server scene
new ServerScene()
})