Skip to content

Commit

Permalink
Merge pull request #8846 from CesiumGS/gltf-skinning-what-the-
Browse files Browse the repository at this point in the history
Simplify skinning to always expect VEC4 joint/weight accessors.
  • Loading branch information
lilleyse authored May 13, 2020
2 parents 1fba8a9 + 6f1c590 commit d07271e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4388,7 +4388,7 @@ function applySkins(model) {
computedJointMatrices[m]
);
if (defined(bindShapeMatrix)) {
// Optimization for when bind shape matrix is the identity.
// NOTE: bindShapeMatrix is glTF 1.0 only, removed in glTF 2.0.
computedJointMatrices[m] = Matrix4.multiplyTransformation(
computedJointMatrices[m],
bindShapeMatrix,
Expand Down
9 changes: 3 additions & 6 deletions Source/Scene/ModelUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {

var jointAccessorId = primitive.attributes.JOINTS_0;
var componentType;
var type;
var accessorType;
if (defined(jointAccessorId)) {
var jointAccessor = accessors[jointAccessorId];
componentType = jointAccessor.componentType;
type = jointAccessor.type;
accessorType = jointAccessor.type;
}
var isSkinned = defined(jointAccessorId);
var isSkinned = defined(jointAccessorId) && accessorType === "VEC4";
var hasVertexColors = defined(primitive.attributes.COLOR_0);
var hasMorphTargets = defined(primitive.targets);
var hasNormals = defined(primitive.attributes.NORMAL);
Expand All @@ -92,7 +92,6 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {
skinning: {
skinned: isSkinned,
componentType: componentType,
type: type,
},
hasVertexColors: hasVertexColors,
hasMorphTargets: hasMorphTargets,
Expand All @@ -103,7 +102,6 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {
};
} else if (
primitiveInfo.skinning.skinned !== isSkinned ||
primitiveInfo.skinning.type !== type ||
primitiveInfo.hasVertexColors !== hasVertexColors ||
primitiveInfo.hasMorphTargets !== hasMorphTargets ||
primitiveInfo.hasNormals !== hasNormals ||
Expand All @@ -124,7 +122,6 @@ ModelUtility.splitIncompatibleMaterials = function (gltf) {
skinning: {
skinned: isSkinned,
componentType: componentType,
type: type,
},
hasVertexColors: hasVertexColors,
hasMorphTargets: hasMorphTargets,
Expand Down
58 changes: 12 additions & 46 deletions Source/Scene/processModelMaterialsCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import webGLConstantToGlslType from "../Core/webGLConstantToGlslType.js";
import addToArray from "../ThirdParty/GltfPipeline/addToArray.js";
import ForEach from "../ThirdParty/GltfPipeline/ForEach.js";
import hasExtension from "../ThirdParty/GltfPipeline/hasExtension.js";
import numberOfComponentsForType from "../ThirdParty/GltfPipeline/numberOfComponentsForType.js";
import ModelUtility from "./ModelUtility.js";

/**
Expand Down Expand Up @@ -389,44 +388,12 @@ function generateTechnique(
// Add attributes with semantics
var vertexShaderMain = "";
if (hasSkinning) {
var i, j;
var numberOfComponents = numberOfComponentsForType(skinningInfo.type);
var matrix = false;
if (skinningInfo.type.indexOf("MAT") === 0) {
matrix = true;
numberOfComponents = Math.sqrt(numberOfComponents);
}
if (!matrix) {
for (i = 0; i < numberOfComponents; i++) {
if (i === 0) {
vertexShaderMain += " mat4 skinMat = ";
} else {
vertexShaderMain += " skinMat += ";
}
vertexShaderMain +=
"a_weight[" + i + "] * u_jointMatrix[int(a_joint[" + i + "])];\n";
}
} else {
for (i = 0; i < numberOfComponents; i++) {
for (j = 0; j < numberOfComponents; j++) {
if (i === 0 && j === 0) {
vertexShaderMain += " mat4 skinMat = ";
} else {
vertexShaderMain += " skinMat += ";
}
vertexShaderMain +=
"a_weight[" +
i +
"][" +
j +
"] * u_jointMatrix[int(a_joint[" +
i +
"][" +
j +
"])];\n";
}
}
}
vertexShaderMain +=
" mat4 skinMatrix =\n" +
" a_weight.x * u_jointMatrix[int(a_joint.x)] +\n" +
" a_weight.y * u_jointMatrix[int(a_joint.y)] +\n" +
" a_weight.z * u_jointMatrix[int(a_joint.z)] +\n" +
" a_weight.w * u_jointMatrix[int(a_joint.w)];\n";
}

// Add position always
Expand All @@ -439,7 +406,7 @@ function generateTechnique(
vertexShader += "varying vec3 v_positionEC;\n";
if (hasSkinning) {
vertexShaderMain +=
" vec4 pos = u_modelViewMatrix * skinMat * vec4(a_position,1.0);\n";
" vec4 pos = u_modelViewMatrix * skinMatrix * vec4(a_position,1.0);\n";
} else {
vertexShaderMain +=
" vec4 pos = u_modelViewMatrix * vec4(a_position,1.0);\n";
Expand All @@ -457,7 +424,7 @@ function generateTechnique(
vertexShader += "varying vec3 v_normal;\n";
if (hasSkinning) {
vertexShaderMain +=
" v_normal = u_normalMatrix * mat3(skinMat) * a_normal;\n";
" v_normal = u_normalMatrix * mat3(skinMatrix) * a_normal;\n";
} else {
vertexShaderMain += " v_normal = u_normalMatrix * a_normal;\n";
}
Expand All @@ -481,16 +448,15 @@ function generateTechnique(
}

if (hasSkinning) {
var attributeType = ModelUtility.getShaderVariable(skinningInfo.type);
techniqueAttributes.a_joint = {
semantic: "JOINT",
semantic: "JOINTS_0",
};
techniqueAttributes.a_weight = {
semantic: "WEIGHT",
semantic: "WEIGHTS_0",
};

vertexShader += "attribute " + attributeType + " a_joint;\n";
vertexShader += "attribute " + attributeType + " a_weight;\n";
vertexShader += "attribute vec4 a_joint;\n";
vertexShader += "attribute vec4 a_weight;\n";
}

if (hasVertexColors) {
Expand Down
50 changes: 8 additions & 42 deletions Source/Scene/processPbrMaterials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import webGLConstantToGlslType from "../Core/webGLConstantToGlslType.js";
import addToArray from "../ThirdParty/GltfPipeline/addToArray.js";
import ForEach from "../ThirdParty/GltfPipeline/ForEach.js";
import hasExtension from "../ThirdParty/GltfPipeline/hasExtension.js";
import numberOfComponentsForType from "../ThirdParty/GltfPipeline/numberOfComponentsForType.js";
import ModelUtility from "./ModelUtility.js";

/**
Expand Down Expand Up @@ -374,44 +373,12 @@ function generateTechnique(
// Add attributes with semantics
var vertexShaderMain = "";
if (hasSkinning) {
var i, j;
var numberOfComponents = numberOfComponentsForType(skinningInfo.type);
var matrix = false;
if (skinningInfo.type.indexOf("MAT") === 0) {
matrix = true;
numberOfComponents = Math.sqrt(numberOfComponents);
}
if (!matrix) {
for (i = 0; i < numberOfComponents; i++) {
if (i === 0) {
vertexShaderMain += " mat4 skinMatrix = ";
} else {
vertexShaderMain += " skinMatrix += ";
}
vertexShaderMain +=
"a_weight[" + i + "] * u_jointMatrix[int(a_joint[" + i + "])];\n";
}
} else {
for (i = 0; i < numberOfComponents; i++) {
for (j = 0; j < numberOfComponents; j++) {
if (i === 0 && j === 0) {
vertexShaderMain += " mat4 skinMatrix = ";
} else {
vertexShaderMain += " skinMatrix += ";
}
vertexShaderMain +=
"a_weight[" +
i +
"][" +
j +
"] * u_jointMatrix[int(a_joint[" +
i +
"][" +
j +
"])];\n";
}
}
}
vertexShaderMain +=
" mat4 skinMatrix =\n" +
" a_weight.x * u_jointMatrix[int(a_joint.x)] +\n" +
" a_weight.y * u_jointMatrix[int(a_joint.y)] +\n" +
" a_weight.z * u_jointMatrix[int(a_joint.z)] +\n" +
" a_weight.w * u_jointMatrix[int(a_joint.w)];\n";
}

// Add position always
Expand Down Expand Up @@ -619,16 +586,15 @@ function generateTechnique(

// Add skinning information if available
if (hasSkinning) {
var attributeType = ModelUtility.getShaderVariable(skinningInfo.type);
techniqueAttributes.a_joint = {
semantic: "JOINTS_0",
};
techniqueAttributes.a_weight = {
semantic: "WEIGHTS_0",
};

vertexShader += "attribute " + attributeType + " a_joint;\n";
vertexShader += "attribute " + attributeType + " a_weight;\n";
vertexShader += "attribute vec4 a_joint;\n";
vertexShader += "attribute vec4 a_weight;\n";
}

if (hasVertexColors) {
Expand Down

0 comments on commit d07271e

Please sign in to comment.