Skip to content

Commit d07271e

Browse files
authored
Merge pull request #8846 from CesiumGS/gltf-skinning-what-the-
Simplify skinning to always expect VEC4 joint/weight accessors.
2 parents 1fba8a9 + 6f1c590 commit d07271e

File tree

4 files changed

+24
-95
lines changed

4 files changed

+24
-95
lines changed

Source/Scene/Model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4388,7 +4388,7 @@ function applySkins(model) {
43884388
computedJointMatrices[m]
43894389
);
43904390
if (defined(bindShapeMatrix)) {
4391-
// Optimization for when bind shape matrix is the identity.
4391+
// NOTE: bindShapeMatrix is glTF 1.0 only, removed in glTF 2.0.
43924392
computedJointMatrices[m] = Matrix4.multiplyTransformation(
43934393
computedJointMatrices[m],
43944394
bindShapeMatrix,

Source/Scene/ModelUtility.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {
7070

7171
var jointAccessorId = primitive.attributes.JOINTS_0;
7272
var componentType;
73-
var type;
73+
var accessorType;
7474
if (defined(jointAccessorId)) {
7575
var jointAccessor = accessors[jointAccessorId];
7676
componentType = jointAccessor.componentType;
77-
type = jointAccessor.type;
77+
accessorType = jointAccessor.type;
7878
}
79-
var isSkinned = defined(jointAccessorId);
79+
var isSkinned = defined(jointAccessorId) && accessorType === "VEC4";
8080
var hasVertexColors = defined(primitive.attributes.COLOR_0);
8181
var hasMorphTargets = defined(primitive.targets);
8282
var hasNormals = defined(primitive.attributes.NORMAL);
@@ -92,7 +92,6 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {
9292
skinning: {
9393
skinned: isSkinned,
9494
componentType: componentType,
95-
type: type,
9695
},
9796
hasVertexColors: hasVertexColors,
9897
hasMorphTargets: hasMorphTargets,
@@ -103,7 +102,6 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {
103102
};
104103
} else if (
105104
primitiveInfo.skinning.skinned !== isSkinned ||
106-
primitiveInfo.skinning.type !== type ||
107105
primitiveInfo.hasVertexColors !== hasVertexColors ||
108106
primitiveInfo.hasMorphTargets !== hasMorphTargets ||
109107
primitiveInfo.hasNormals !== hasNormals ||
@@ -124,7 +122,6 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {
124122
skinning: {
125123
skinned: isSkinned,
126124
componentType: componentType,
127-
type: type,
128125
},
129126
hasVertexColors: hasVertexColors,
130127
hasMorphTargets: hasMorphTargets,

Source/Scene/processModelMaterialsCommon.js

+12-46
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import webGLConstantToGlslType from "../Core/webGLConstantToGlslType.js";
55
import addToArray from "../ThirdParty/GltfPipeline/addToArray.js";
66
import ForEach from "../ThirdParty/GltfPipeline/ForEach.js";
77
import hasExtension from "../ThirdParty/GltfPipeline/hasExtension.js";
8-
import numberOfComponentsForType from "../ThirdParty/GltfPipeline/numberOfComponentsForType.js";
98
import ModelUtility from "./ModelUtility.js";
109

1110
/**
@@ -389,44 +388,12 @@ function generateTechnique(
389388
// Add attributes with semantics
390389
var vertexShaderMain = "";
391390
if (hasSkinning) {
392-
var i, j;
393-
var numberOfComponents = numberOfComponentsForType(skinningInfo.type);
394-
var matrix = false;
395-
if (skinningInfo.type.indexOf("MAT") === 0) {
396-
matrix = true;
397-
numberOfComponents = Math.sqrt(numberOfComponents);
398-
}
399-
if (!matrix) {
400-
for (i = 0; i < numberOfComponents; i++) {
401-
if (i === 0) {
402-
vertexShaderMain += " mat4 skinMat = ";
403-
} else {
404-
vertexShaderMain += " skinMat += ";
405-
}
406-
vertexShaderMain +=
407-
"a_weight[" + i + "] * u_jointMatrix[int(a_joint[" + i + "])];\n";
408-
}
409-
} else {
410-
for (i = 0; i < numberOfComponents; i++) {
411-
for (j = 0; j < numberOfComponents; j++) {
412-
if (i === 0 && j === 0) {
413-
vertexShaderMain += " mat4 skinMat = ";
414-
} else {
415-
vertexShaderMain += " skinMat += ";
416-
}
417-
vertexShaderMain +=
418-
"a_weight[" +
419-
i +
420-
"][" +
421-
j +
422-
"] * u_jointMatrix[int(a_joint[" +
423-
i +
424-
"][" +
425-
j +
426-
"])];\n";
427-
}
428-
}
429-
}
391+
vertexShaderMain +=
392+
" mat4 skinMatrix =\n" +
393+
" a_weight.x * u_jointMatrix[int(a_joint.x)] +\n" +
394+
" a_weight.y * u_jointMatrix[int(a_joint.y)] +\n" +
395+
" a_weight.z * u_jointMatrix[int(a_joint.z)] +\n" +
396+
" a_weight.w * u_jointMatrix[int(a_joint.w)];\n";
430397
}
431398

432399
// Add position always
@@ -439,7 +406,7 @@ function generateTechnique(
439406
vertexShader += "varying vec3 v_positionEC;\n";
440407
if (hasSkinning) {
441408
vertexShaderMain +=
442-
" vec4 pos = u_modelViewMatrix * skinMat * vec4(a_position,1.0);\n";
409+
" vec4 pos = u_modelViewMatrix * skinMatrix * vec4(a_position,1.0);\n";
443410
} else {
444411
vertexShaderMain +=
445412
" vec4 pos = u_modelViewMatrix * vec4(a_position,1.0);\n";
@@ -457,7 +424,7 @@ function generateTechnique(
457424
vertexShader += "varying vec3 v_normal;\n";
458425
if (hasSkinning) {
459426
vertexShaderMain +=
460-
" v_normal = u_normalMatrix * mat3(skinMat) * a_normal;\n";
427+
" v_normal = u_normalMatrix * mat3(skinMatrix) * a_normal;\n";
461428
} else {
462429
vertexShaderMain += " v_normal = u_normalMatrix * a_normal;\n";
463430
}
@@ -481,16 +448,15 @@ function generateTechnique(
481448
}
482449

483450
if (hasSkinning) {
484-
var attributeType = ModelUtility.getShaderVariable(skinningInfo.type);
485451
techniqueAttributes.a_joint = {
486-
semantic: "JOINT",
452+
semantic: "JOINTS_0",
487453
};
488454
techniqueAttributes.a_weight = {
489-
semantic: "WEIGHT",
455+
semantic: "WEIGHTS_0",
490456
};
491457

492-
vertexShader += "attribute " + attributeType + " a_joint;\n";
493-
vertexShader += "attribute " + attributeType + " a_weight;\n";
458+
vertexShader += "attribute vec4 a_joint;\n";
459+
vertexShader += "attribute vec4 a_weight;\n";
494460
}
495461

496462
if (hasVertexColors) {

Source/Scene/processPbrMaterials.js

+8-42
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import webGLConstantToGlslType from "../Core/webGLConstantToGlslType.js";
55
import addToArray from "../ThirdParty/GltfPipeline/addToArray.js";
66
import ForEach from "../ThirdParty/GltfPipeline/ForEach.js";
77
import hasExtension from "../ThirdParty/GltfPipeline/hasExtension.js";
8-
import numberOfComponentsForType from "../ThirdParty/GltfPipeline/numberOfComponentsForType.js";
98
import ModelUtility from "./ModelUtility.js";
109

1110
/**
@@ -374,44 +373,12 @@ function generateTechnique(
374373
// Add attributes with semantics
375374
var vertexShaderMain = "";
376375
if (hasSkinning) {
377-
var i, j;
378-
var numberOfComponents = numberOfComponentsForType(skinningInfo.type);
379-
var matrix = false;
380-
if (skinningInfo.type.indexOf("MAT") === 0) {
381-
matrix = true;
382-
numberOfComponents = Math.sqrt(numberOfComponents);
383-
}
384-
if (!matrix) {
385-
for (i = 0; i < numberOfComponents; i++) {
386-
if (i === 0) {
387-
vertexShaderMain += " mat4 skinMatrix = ";
388-
} else {
389-
vertexShaderMain += " skinMatrix += ";
390-
}
391-
vertexShaderMain +=
392-
"a_weight[" + i + "] * u_jointMatrix[int(a_joint[" + i + "])];\n";
393-
}
394-
} else {
395-
for (i = 0; i < numberOfComponents; i++) {
396-
for (j = 0; j < numberOfComponents; j++) {
397-
if (i === 0 && j === 0) {
398-
vertexShaderMain += " mat4 skinMatrix = ";
399-
} else {
400-
vertexShaderMain += " skinMatrix += ";
401-
}
402-
vertexShaderMain +=
403-
"a_weight[" +
404-
i +
405-
"][" +
406-
j +
407-
"] * u_jointMatrix[int(a_joint[" +
408-
i +
409-
"][" +
410-
j +
411-
"])];\n";
412-
}
413-
}
414-
}
376+
vertexShaderMain +=
377+
" mat4 skinMatrix =\n" +
378+
" a_weight.x * u_jointMatrix[int(a_joint.x)] +\n" +
379+
" a_weight.y * u_jointMatrix[int(a_joint.y)] +\n" +
380+
" a_weight.z * u_jointMatrix[int(a_joint.z)] +\n" +
381+
" a_weight.w * u_jointMatrix[int(a_joint.w)];\n";
415382
}
416383

417384
// Add position always
@@ -619,16 +586,15 @@ function generateTechnique(
619586

620587
// Add skinning information if available
621588
if (hasSkinning) {
622-
var attributeType = ModelUtility.getShaderVariable(skinningInfo.type);
623589
techniqueAttributes.a_joint = {
624590
semantic: "JOINTS_0",
625591
};
626592
techniqueAttributes.a_weight = {
627593
semantic: "WEIGHTS_0",
628594
};
629595

630-
vertexShader += "attribute " + attributeType + " a_joint;\n";
631-
vertexShader += "attribute " + attributeType + " a_weight;\n";
596+
vertexShader += "attribute vec4 a_joint;\n";
597+
vertexShader += "attribute vec4 a_weight;\n";
632598
}
633599

634600
if (hasVertexColors) {

0 commit comments

Comments
 (0)