-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
251 lines (224 loc) · 5.48 KB
/
index.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var getNormal = require('gl-mat3/normal-from-mat4')
var DisplayTreeNode = require('display-tree')
var quatIdentity = require('gl-quat/identity')
var identity4 = require('gl-mat4/identity')
var identity3 = require('gl-mat3/identity')
var multiply = require('gl-mat4/multiply')
var copy4 = require('gl-mat4/copy')
var copy3 = require('gl-mat3/copy')
var inherits = require('inherits')
var rotateX = require('gl-quat/rotateX')
var rotateY = require('gl-quat/rotateY')
var rotateZ = require('gl-quat/rotateZ')
module.exports = SceneTreeNode
inherits(SceneTreeNode, DisplayTreeNode)
function SceneTreeNode (data) {
if (!(this instanceof SceneTreeNode)) {
return new SceneTreeNode(data)
}
data = data || {}
DisplayTreeNode.call(this, data)
this._eachSorter = null
this.normalMatrix = identity4(new Float32Array(9))
this.modelMatrix = identity4(new Float32Array(16))
this.baseModelMatrix = identity4(new Float32Array(16))
this.matrixDirty = true
this.matrixIdentity = !(
data.rotation ||
data.position ||
data.scale || typeof data.scale === 'number'
)
data.position = data.position || new Float32Array(3)
data.scale = typeof data.scale === 'number'
? defaultScale(data.scale)
: data.scale || defaultScale(1)
if (!data.rotation) {
data.rotation = new Float32Array(4)
data.rotation[3] = 1
}
}
SceneTreeNode.prototype.tick = function () {
if (!this._eachSorter) this._eachSorter = this.list()
var nodes = this._eachSorter()
tickNode(this)
for (var i = 0; i < nodes.length; i++) {
tickNode(nodes[i])
}
}
function tickNode (node) {
var data = node.data
var model = node.modelMatrix
var baseModel = node.baseModelMatrix
var normalMatrix = node.normalMatrix
if (node.matrixIdentity) {
// baseModel/model default to an identity matrix, so we can safely ignore
// them here as matrixIdentity is only true if the position/rotation/scale
// has never been manipulated.
if (node.parent && node.parent.modelMatrix) {
copy3(normalMatrix, node.parent.normalMatrix)
} else {
identity3(normalMatrix)
}
} else {
if (node.matrixDirty) {
identity4(baseModel)
var rotation = data.rotation
var position = data.position
var scaleDat = data.scale
fromRotationTranslationScale(baseModel, rotation, position, scaleDat)
}
if (node.parent && node.parent.modelMatrix) {
multiply(model, node.parent.modelMatrix, baseModel)
} else {
copy4(model, baseModel)
}
getNormal(normalMatrix, model)
}
node.matrixDirty = false
}
SceneTreeNode.prototype.setPosition = function (x, y, z) {
var pos = this.data.position
this.matrixDirty = true
this.matrixIdentity = false
if (Array.isArray(x)) {
pos[0] = x[0]
pos[1] = x[1]
pos[2] = x[2]
} else {
pos[0] = x
pos[1] = y
pos[2] = z
}
return this
}
SceneTreeNode.prototype.setRotation = function (x, y, z, w) {
var rot = this.data.rotation
this.matrixDirty = true
this.matrixIdentity = false
if (Array.isArray(x)) {
rot[0] = x[0]
rot[1] = x[1]
rot[2] = x[2]
rot[3] = x[3]
} else {
rot[0] = x
rot[1] = y
rot[2] = z
rot[3] = w
}
return this
}
SceneTreeNode.prototype.setEuler = function (x, y, z, order) {
var rot = this.data.rotation
this.matrixDirty = true
this.matrixIdentity = false
quatIdentity(rot)
var X, Y, Z, O
if (Array.isArray(x)) {
X = x[0]
Y = x[1]
Z = x[2]
O = y || ''
} else {
X = x
Y = y
Z = z
O = order || ''
}
switch (O.toLowerCase()) {
case 'xzy':
rotateX(rot, rot, X)
rotateZ(rot, rot, Y)
rotateY(rot, rot, Z)
break
case 'yxz':
rotateY(rot, rot, X)
rotateX(rot, rot, Y)
rotateZ(rot, rot, Z)
break
case 'yzx':
rotateY(rot, rot, X)
rotateZ(rot, rot, Y)
rotateX(rot, rot, Z)
break
case 'zxy':
rotateZ(rot, rot, X)
rotateX(rot, rot, Y)
rotateY(rot, rot, Z)
break
case 'zyx':
rotateZ(rot, rot, X)
rotateY(rot, rot, Y)
rotateX(rot, rot, Z)
break
default:
rotateX(rot, rot, X)
rotateY(rot, rot, Y)
rotateZ(rot, rot, Z)
break
}
return this
}
SceneTreeNode.prototype.setScale = function (x, y, z) {
var scale = this.data.scale
this.matrixDirty = true
if (Array.isArray(x)) {
scale[0] = x[0]
scale[1] = x[1]
scale[2] = x[2]
} else
if (arguments.length === 1) {
scale[0] =
scale[1] =
scale[2] = x
} else {
scale[0] = x
scale[1] = y
scale[2] = z
}
return this
}
function defaultScale (n) {
var scale = new Float32Array(3)
scale[0] = n
scale[1] = n
scale[2] = n
return scale
}
// From @toji's gl-matrix, pulled out to save some time :')
// http://glmatrix.net/docs/mat4.html#.fromRotationTranslationScale
function fromRotationTranslationScale (out, q, v, s) {
var x = q[0], y = q[1], z = q[2], w = q[3],
x2 = x + x,
y2 = y + y,
z2 = z + z,
xx = x * x2,
xy = x * y2,
xz = x * z2,
yy = y * y2,
yz = y * z2,
zz = z * z2,
wx = w * x2,
wy = w * y2,
wz = w * z2,
sx = s[0],
sy = s[1],
sz = s[2]
out[0] = (1 - (yy + zz)) * sx
out[1] = (xy + wz) * sx
out[2] = (xz - wy) * sx
out[3] = 0
out[4] = (xy - wz) * sy
out[5] = (1 - (xx + zz)) * sy
out[6] = (yz + wx) * sy
out[7] = 0
out[8] = (xz + wy) * sz
out[9] = (yz - wx) * sz
out[10] = (1 - (xx + yy)) * sz
out[11] = 0
out[12] = v[0]
out[13] = v[1]
out[14] = v[2]
out[15] = 1
return out
}