-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathSkinnedMesh.js
More file actions
370 lines (245 loc) · 8.87 KB
/
SkinnedMesh.js
File metadata and controls
370 lines (245 loc) · 8.87 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { Mesh } from './Mesh.js';
import { Box3 } from '../math/Box3.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Sphere } from '../math/Sphere.js';
import { Vector3 } from '../math/Vector3.js';
import { Vector4 } from '../math/Vector4.js';
import { Ray } from '../math/Ray.js';
import { AttachedBindMode, DetachedBindMode } from '../constants.js';
import { warn } from '../utils.js';
const _baseVector = /*@__PURE__*/ new Vector4();
const _skinIndex = /*@__PURE__*/ new Vector4();
const _skinWeight = /*@__PURE__*/ new Vector4();
const _vector4 = /*@__PURE__*/ new Vector4();
const _matrix4 = /*@__PURE__*/ new Matrix4();
const _vertex = /*@__PURE__*/ new Vector3();
const _sphere = /*@__PURE__*/ new Sphere();
const _inverseMatrix = /*@__PURE__*/ new Matrix4();
const _ray = /*@__PURE__*/ new Ray();
/**
* A mesh that has a {@link Skeleton} that can then be used to animate the
* vertices of the geometry with skinning/skeleton animation.
*
* Next to a valid skeleton, the skinned mesh requires skin indices and weights
* as buffer attributes in its geometry. These attribute define which bones affect a single
* vertex to a certain extend.
*
* Typically skinned meshes are not created manually but loaders like {@link GLTFLoader}
* or {@link FBXLoader } import respective models.
*
* @augments Mesh
* @demo scenes/bones-browser.html
*/
class SkinnedMesh extends Mesh {
/**
* Constructs a new skinned mesh.
*
* @param {BufferGeometry} [geometry] - The mesh geometry.
* @param {Material|Array<Material>} [material] - The mesh material.
*/
constructor( geometry, material ) {
super( geometry, material );
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isSkinnedMesh = true;
this.type = 'SkinnedMesh';
/**
* `AttachedBindMode` means the skinned mesh shares the same world space as the skeleton.
* This is not true when using `DetachedBindMode` which is useful when sharing a skeleton
* across multiple skinned meshes.
*
* @type {(AttachedBindMode|DetachedBindMode)}
* @default AttachedBindMode
*/
this.bindMode = AttachedBindMode;
/**
* The base matrix that is used for the bound bone transforms.
*
* @type {Matrix4}
*/
this.bindMatrix = new Matrix4();
/**
* The base matrix that is used for resetting the bound bone transforms.
*
* @type {Matrix4}
*/
this.bindMatrixInverse = new Matrix4();
/**
* The bounding box of the skinned mesh. Can be computed via {@link SkinnedMesh#computeBoundingBox}.
*
* @type {?Box3}
* @default null
*/
this.boundingBox = null;
/**
* The bounding sphere of the skinned mesh. Can be computed via {@link SkinnedMesh#computeBoundingSphere}.
*
* @type {?Sphere}
* @default null
*/
this.boundingSphere = null;
}
/**
* Computes the bounding box of the skinned mesh, and updates {@link SkinnedMesh#boundingBox}.
* The bounding box is not automatically computed by the engine; this method must be called by your app.
* If the skinned mesh is animated, the bounding box should be recomputed per frame in order to reflect
* the current animation state.
*/
computeBoundingBox() {
const geometry = this.geometry;
if ( this.boundingBox === null ) {
this.boundingBox = new Box3();
}
this.boundingBox.makeEmpty();
const positionAttribute = geometry.getAttribute( 'position' );
for ( let i = 0; i < positionAttribute.count; i ++ ) {
this.getVertexPosition( i, _vertex );
this.boundingBox.expandByPoint( _vertex );
}
}
/**
* Computes the bounding sphere of the skinned mesh, and updates {@link SkinnedMesh#boundingSphere}.
* The bounding sphere is automatically computed by the engine once when it is needed, e.g., for ray casting
* and view frustum culling. If the skinned mesh is animated, the bounding sphere should be recomputed
* per frame in order to reflect the current animation state.
*/
computeBoundingSphere() {
const geometry = this.geometry;
if ( this.boundingSphere === null ) {
this.boundingSphere = new Sphere();
}
this.boundingSphere.makeEmpty();
const positionAttribute = geometry.getAttribute( 'position' );
for ( let i = 0; i < positionAttribute.count; i ++ ) {
this.getVertexPosition( i, _vertex );
this.boundingSphere.expandByPoint( _vertex );
}
}
copy( source, recursive ) {
super.copy( source, recursive );
this.bindMode = source.bindMode;
this.bindMatrix.copy( source.bindMatrix );
this.bindMatrixInverse.copy( source.bindMatrixInverse );
this.skeleton = source.skeleton;
if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
return this;
}
raycast( raycaster, intersects ) {
const material = this.material;
const matrixWorld = this.matrixWorld;
if ( material === undefined ) return;
// test with bounding sphere in world space
if ( this.boundingSphere === null ) this.computeBoundingSphere();
_sphere.copy( this.boundingSphere );
_sphere.applyMatrix4( matrixWorld );
if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
// convert ray to local space of skinned mesh
_inverseMatrix.copy( matrixWorld ).invert();
_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
// test with bounding box in local space
if ( this.boundingBox !== null ) {
if ( _ray.intersectsBox( this.boundingBox ) === false ) return;
}
// test for intersections with geometry
this._computeIntersections( raycaster, intersects, _ray );
}
getVertexPosition( index, target ) {
super.getVertexPosition( index, target );
this.applyBoneTransform( index, target );
return target;
}
/**
* Binds the given skeleton to the skinned mesh.
*
* @param {Skeleton} skeleton - The skeleton to bind.
* @param {Matrix4} [bindMatrix] - The bind matrix. If no bind matrix is provided,
* the skinned mesh's world matrix will be used instead.
*/
bind( skeleton, bindMatrix ) {
this.skeleton = skeleton;
if ( bindMatrix === undefined ) {
this.updateMatrixWorld( true );
this.skeleton.calculateInverses();
bindMatrix = this.matrixWorld;
}
this.bindMatrix.copy( bindMatrix );
this.bindMatrixInverse.copy( bindMatrix ).invert();
}
/**
* This method sets the skinned mesh in the rest pose).
*/
pose() {
this.skeleton.pose();
}
/**
* Normalizes the skin weights which are defined as a buffer attribute
* in the skinned mesh's geometry.
*/
normalizeSkinWeights() {
const vector = new Vector4();
const skinWeight = this.geometry.attributes.skinWeight;
for ( let i = 0, l = skinWeight.count; i < l; i ++ ) {
vector.fromBufferAttribute( skinWeight, i );
const scale = 1.0 / vector.manhattanLength();
if ( scale !== Infinity ) {
vector.multiplyScalar( scale );
} else {
vector.set( 1, 0, 0, 0 ); // do something reasonable
}
skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
}
}
updateMatrixWorld( force ) {
super.updateMatrixWorld( force );
if ( this.bindMode === AttachedBindMode ) {
this.bindMatrixInverse.copy( this.matrixWorld ).invert();
} else if ( this.bindMode === DetachedBindMode ) {
this.bindMatrixInverse.copy( this.bindMatrix ).invert();
} else {
warn( 'SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
}
}
/**
* Applies the bone transform associated with the given index to the given
* vector. Can be used to transform positions or direction vectors by providing
* a Vector4 with 1 or 0 in the w component respectively. Returns the updated vector.
*
* @param {number} index - The vertex index.
* @param {Vector3|Vector4} target - The target object that is used to store the method's result.
* @return {Vector3|Vector4} The updated vertex attribute data.
*/
applyBoneTransform( index, target ) {
const skeleton = this.skeleton;
const geometry = this.geometry;
_skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
_skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );
if ( target.isVector4 ) {
_baseVector.copy( target );
target.set( 0, 0, 0, 0 );
} else {
_baseVector.set( ...target, 1 );
target.set( 0, 0, 0 );
}
_baseVector.applyMatrix4( this.bindMatrix );
for ( let i = 0; i < 4; i ++ ) {
const weight = _skinWeight.getComponent( i );
if ( weight !== 0 ) {
const boneIndex = _skinIndex.getComponent( i );
_matrix4.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );
target.addScaledVector( _vector4.copy( _baseVector ).applyMatrix4( _matrix4 ), weight );
}
}
if ( target.isVector4 ) {
// ensure the homogenous coordinate remains unchanged after vector operations
target.w = _baseVector.w;
}
return target.applyMatrix4( this.bindMatrixInverse );
}
}
export { SkinnedMesh };