Skip to content

Commit 3cc6588

Browse files
authored
Separated SkinInstance class into its own file skin-instance.js (playcanvas#2394)
1 parent 525fc08 commit 3cc6588

File tree

8 files changed

+129
-124
lines changed

8 files changed

+129
-124
lines changed

src/deprecated.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ import { Model } from './scene/model.js';
6464
import { ParticleEmitter } from './scene/particle-system/particle-emitter.js';
6565
import { Picker } from './scene/pick.js';
6666
import { Scene } from './scene/scene.js';
67-
import { Skin, SkinInstance } from './scene/skin.js';
67+
import { Skin } from './scene/skin.js';
68+
import { SkinInstance } from './scene/skin-instance.js';
6869
import { StandardMaterial } from './scene/materials/standard-material.js';
6970

7071
import { Animation, Key, Node } from './anim/animation.js';

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export { MorphTarget } from './scene/morph-target.js';
9090
export { ParticleEmitter } from './scene/particle-system/particle-emitter.js';
9191
export { Picker } from './scene/pick.js';
9292
export { Scene } from './scene/scene.js';
93-
export { Skin, SkinInstance } from './scene/skin.js';
93+
export { Skin } from './scene/skin.js';
94+
export { SkinInstance } from './scene/skin-instance.js';
9495
export { Sprite } from './scene/sprite.js';
9596
export { StandardMaterial } from './scene/materials/standard-material.js';
9697
export { StencilParameters } from './scene/stencil-parameters.js';

src/resources/parser/glb-parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import { Model } from '../../scene/model.js';
3434
import { Morph } from '../../scene/morph.js';
3535
import { MorphInstance } from '../../scene/morph-instance.js';
3636
import { MorphTarget } from '../../scene/morph-target.js';
37-
import { Skin, SkinInstance } from '../../scene/skin.js';
37+
import { Skin } from '../../scene/skin.js';
38+
import { SkinInstance } from '../../scene/skin-instance.js';
3839
import { StandardMaterial } from '../../scene/materials/standard-material.js';
3940

4041
import { AnimCurve, AnimData, AnimTrack } from '../../anim/anim.js';

src/resources/parser/json-model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import { Model } from '../../scene/model.js';
2424
import { Morph } from '../../scene/morph.js';
2525
import { MorphInstance } from '../../scene/morph-instance.js';
2626
import { MorphTarget } from '../../scene/morph-target.js';
27-
import { Skin, SkinInstance } from '../../scene/skin.js';
27+
import { Skin } from '../../scene/skin.js';
28+
import { SkinInstance } from '../../scene/skin-instance.js';
2829

2930
var JSON_PRIMITIVE_TYPE = {
3031
"points": PRIMITIVE_POINTS,

src/scene/batching.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { LAYERID_WORLD, SPRITE_RENDERMODE_SIMPLE } from './constants.js';
1717
import { Mesh } from './mesh.js';
1818
import { MeshInstance } from './mesh-instance.js';
1919
import { Model } from './model.js';
20-
import { SkinInstance } from './skin.js';
20+
import { SkinInstance } from './skin-instance.js';
2121

2222
/**
2323
* @class

src/scene/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RENDERSTYLE_WIREFRAME } from './constants.js';
22
import { MeshInstance } from './mesh-instance.js';
33
import { MorphInstance } from './morph-instance.js';
4-
import { SkinInstance } from './skin.js';
4+
import { SkinInstance } from './skin-instance.js';
55

66
/**
77
* @class

src/scene/skin-instance.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { math } from '../math/math.js';
2+
import { Mat4 } from '../math/mat4.js';
3+
4+
import { FILTER_NEAREST, PIXELFORMAT_RGBA32F } from '../graphics/graphics.js';
5+
import { Texture } from '../graphics/texture.js';
6+
7+
var _invMatrix = new Mat4();
8+
9+
/**
10+
* @class
11+
* @name pc.SkinInstance
12+
* @classdesc A skin instance is responsible for generating the matrix palette that is used to
13+
* skin vertices from object space to world space.
14+
* @param {pc.Skin} skin - The skin that will provide the inverse bind pose matrices to
15+
* generate the final matrix palette.
16+
* @property {pc.GraphNode[]} bones An array of nodes representing each bone in this skin instance.
17+
*/
18+
function SkinInstance(skin) {
19+
this._dirty = true;
20+
21+
if (skin) {
22+
this.initSkin(skin);
23+
}
24+
}
25+
26+
Object.assign(SkinInstance.prototype, {
27+
28+
init: function (device, numBones) {
29+
30+
if (device.supportsBoneTextures) {
31+
32+
// texture size - roughly square that fits all bones, width is multiply of 3 to simplify shader math
33+
var numPixels = numBones * 3;
34+
var width = Math.ceil(Math.sqrt(numPixels));
35+
width = math.roundUp(width, 3);
36+
var height = Math.ceil(numPixels / width);
37+
38+
this.boneTexture = new Texture(device, {
39+
width: width,
40+
height: height,
41+
format: PIXELFORMAT_RGBA32F,
42+
mipmaps: false,
43+
minFilter: FILTER_NEAREST,
44+
magFilter: FILTER_NEAREST
45+
});
46+
47+
this.boneTexture.name = 'skin';
48+
this.matrixPalette = this.boneTexture.lock();
49+
50+
} else {
51+
this.matrixPalette = new Float32Array(numBones * 12);
52+
}
53+
},
54+
55+
initSkin: function (skin) {
56+
57+
this.skin = skin;
58+
59+
// Unique per clone
60+
this.bones = [];
61+
62+
var numBones = skin.inverseBindPose.length;
63+
this.init(skin.device, numBones);
64+
65+
this.matrices = [];
66+
for (var i = 0; i < numBones; i++) {
67+
this.matrices[i] = new Mat4();
68+
}
69+
},
70+
71+
uploadBones: function (device) {
72+
73+
// TODO: this is a bit strange looking. Change the Texture API to do a reupload
74+
if (device.supportsBoneTextures) {
75+
this.boneTexture.lock();
76+
this.boneTexture.unlock();
77+
}
78+
},
79+
80+
updateMatrices: function (rootNode) {
81+
82+
_invMatrix.copy(rootNode.getWorldTransform()).invert();
83+
for (var i = this.bones.length - 1; i >= 0; i--) {
84+
this.matrices[i].mulAffine2(_invMatrix, this.bones[i].getWorldTransform()); // world space -> rootNode space
85+
this.matrices[i].mulAffine2(this.matrices[i], this.skin.inverseBindPose[i]); // rootNode space -> bind space
86+
}
87+
},
88+
89+
updateMatrixPalette: function () {
90+
var pe;
91+
var mp = this.matrixPalette;
92+
var base;
93+
94+
var count = this.bones.length;
95+
for (var i = 0; i < count; i++) {
96+
pe = this.matrices[i].data;
97+
98+
// Copy the matrix into the palette, ready to be sent to the vertex shader, transpose matrix from 4x4 to 4x3 format as well
99+
base = i * 12;
100+
mp[base] = pe[0];
101+
mp[base + 1] = pe[4];
102+
mp[base + 2] = pe[8];
103+
mp[base + 3] = pe[12];
104+
mp[base + 4] = pe[1];
105+
mp[base + 5] = pe[5];
106+
mp[base + 6] = pe[9];
107+
mp[base + 7] = pe[13];
108+
mp[base + 8] = pe[2];
109+
mp[base + 9] = pe[6];
110+
mp[base + 10] = pe[10];
111+
mp[base + 11] = pe[14];
112+
}
113+
114+
this.uploadBones(this.skin.device);
115+
}
116+
});
117+
118+
export { SkinInstance };

src/scene/skin.js

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import { math } from '../math/math.js';
2-
import { Mat4 } from '../math/mat4.js';
3-
4-
import { FILTER_NEAREST, PIXELFORMAT_RGBA32F } from '../graphics/graphics.js';
5-
import { Texture } from '../graphics/texture.js';
6-
71
/**
82
* @class
93
* @name pc.Skin
@@ -15,122 +9,11 @@ import { Texture } from '../graphics/texture.js';
159
* @param {string[]} boneNames - The array of bone names for the bones referenced by this skin.
1610
*/
1711

18-
var _invMatrix = new Mat4();
19-
2012
function Skin(graphicsDevice, ibp, boneNames) {
2113
// Constant between clones
2214
this.device = graphicsDevice;
2315
this.inverseBindPose = ibp;
2416
this.boneNames = boneNames;
2517
}
2618

27-
/**
28-
* @class
29-
* @name pc.SkinInstance
30-
* @classdesc A skin instance is responsible for generating the matrix palette that is used to
31-
* skin vertices from object space to world space.
32-
* @param {pc.Skin} skin - The skin that will provide the inverse bind pose matrices to
33-
* generate the final matrix palette.
34-
* @property {pc.GraphNode[]} bones An array of nodes representing each bone in this skin instance.
35-
*/
36-
function SkinInstance(skin) {
37-
this._dirty = true;
38-
39-
if (skin) {
40-
this.initSkin(skin);
41-
}
42-
}
43-
44-
Object.assign(SkinInstance.prototype, {
45-
46-
init: function (device, numBones) {
47-
48-
if (device.supportsBoneTextures) {
49-
50-
// texture size - roughly square that fits all bones, width is multiply of 3 to simplify shader math
51-
var numPixels = numBones * 3;
52-
var width = Math.ceil(Math.sqrt(numPixels));
53-
width = math.roundUp(width, 3);
54-
var height = Math.ceil(numPixels / width);
55-
56-
this.boneTexture = new Texture(device, {
57-
width: width,
58-
height: height,
59-
format: PIXELFORMAT_RGBA32F,
60-
mipmaps: false,
61-
minFilter: FILTER_NEAREST,
62-
magFilter: FILTER_NEAREST
63-
});
64-
65-
this.boneTexture.name = 'skin';
66-
this.matrixPalette = this.boneTexture.lock();
67-
68-
} else {
69-
this.matrixPalette = new Float32Array(numBones * 12);
70-
}
71-
},
72-
73-
initSkin: function (skin) {
74-
75-
this.skin = skin;
76-
77-
// Unique per clone
78-
this.bones = [];
79-
80-
var numBones = skin.inverseBindPose.length;
81-
this.init(skin.device, numBones);
82-
83-
this.matrices = [];
84-
for (var i = 0; i < numBones; i++) {
85-
this.matrices[i] = new Mat4();
86-
}
87-
},
88-
89-
uploadBones: function (device) {
90-
91-
// TODO: this is a bit strange looking. Change the Texture API to do a reupload
92-
if (device.supportsBoneTextures) {
93-
this.boneTexture.lock();
94-
this.boneTexture.unlock();
95-
}
96-
},
97-
98-
updateMatrices: function (rootNode) {
99-
100-
_invMatrix.copy(rootNode.getWorldTransform()).invert();
101-
for (var i = this.bones.length - 1; i >= 0; i--) {
102-
this.matrices[i].mulAffine2(_invMatrix, this.bones[i].getWorldTransform()); // world space -> rootNode space
103-
this.matrices[i].mulAffine2(this.matrices[i], this.skin.inverseBindPose[i]); // rootNode space -> bind space
104-
}
105-
},
106-
107-
updateMatrixPalette: function () {
108-
var pe;
109-
var mp = this.matrixPalette;
110-
var base;
111-
112-
var count = this.bones.length;
113-
for (var i = 0; i < count; i++) {
114-
pe = this.matrices[i].data;
115-
116-
// Copy the matrix into the palette, ready to be sent to the vertex shader, transpose matrix from 4x4 to 4x3 format as well
117-
base = i * 12;
118-
mp[base] = pe[0];
119-
mp[base + 1] = pe[4];
120-
mp[base + 2] = pe[8];
121-
mp[base + 3] = pe[12];
122-
mp[base + 4] = pe[1];
123-
mp[base + 5] = pe[5];
124-
mp[base + 6] = pe[9];
125-
mp[base + 7] = pe[13];
126-
mp[base + 8] = pe[2];
127-
mp[base + 9] = pe[6];
128-
mp[base + 10] = pe[10];
129-
mp[base + 11] = pe[14];
130-
}
131-
132-
this.uploadBones(this.skin.device);
133-
}
134-
});
135-
136-
export { Skin, SkinInstance };
19+
export { Skin };

0 commit comments

Comments
 (0)