-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathheadless-gltf-loader.js
88 lines (66 loc) · 2.07 KB
/
headless-gltf-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
83
84
85
86
87
88
/**
* 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
})
this.objects = [ground]
const GLTFLoader = new Loaders.GLTFLoader()
GLTFLoader.load(path.resolve(__dirname, '../assets/glb/suzanne.glb')).then(gltf => {
const child = gltf.scene.children[0]
const suzanne = new ExtendedObject3D()
suzanne.add(child)
suzanne.position.set(0, 5, 0)
const physicsOptions = {
addChildren: false,
shape: 'hacd' // or any other shape you want
}
this.factory.add.existing(suzanne)
this.physics.add.existing(suzanne, physicsOptions)
suzanne.body.setFriction(1)
this.objects.push(suzanne)
})
// 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)
const suzanne = this.objects[1]
if (!suzanne) return // suzanne has not been parsed yet
const y = suzanne.position.y.toFixed(2)
// watch the y position fall down from 5 to the ground
if (y > 2) console.log('y:', suzanne.body.position.y.toFixed(2))
// TODO
// send new positions to the client
}
}
// wait for Ammo to be loaded
_ammo().then(ammo => {
globalThis.Ammo = ammo
// start server scene
new ServerScene()
})