-
Notifications
You must be signed in to change notification settings - Fork 16
/
skin.js
45 lines (38 loc) · 1.04 KB
/
skin.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
const Signal = require('signals')
const mat4 = require('pex-math/mat4')
function Skin(opts) {
this.type = 'Skin'
this.enabled = true
this.changed = new Signal()
this.entity = null
this.joints = []
this.jointMatrices = []
this.inverseBindMatrices = []
this.set(opts)
}
Skin.prototype.init = function(entity) {
this.entity = entity
}
Skin.prototype.set = function(opts) {
Object.assign(this, opts)
Object.keys(opts).forEach((prop) => this.changed.dispatch(prop))
if (opts.joints) {
this.jointMatrices.length = this.joints.length
for (let i = 0; i < this.joints.length; i++) {
this.jointMatrices[i] = this.jointMatrices[i] || mat4.create()
}
}
}
Skin.prototype.update = function() {
if (!this.enabled) return
for (let i = 0; i < this.joints.length; i++) {
const joint = this.joints[i]
const m = this.jointMatrices[i]
mat4.identity(m)
mat4.mult(m, joint.transform.modelMatrix)
mat4.mult(m, this.inverseBindMatrices[i])
}
}
module.exports = function createSkin(opts) {
return new Skin(opts)
}