Add capybara
This commit is contained in:
@@ -10,12 +10,14 @@ struct SkinningDataType {
|
||||
vec4 weights;
|
||||
};
|
||||
|
||||
layout (buffer_reference, std430) readonly buffer SkinningData {
|
||||
SkinningDataType data[];
|
||||
// Keep buffer-reference pointer alignment explicit so the C++ push-constant
|
||||
// struct can safely use 64-bit VkDeviceAddress fields.
|
||||
layout (buffer_reference, std430, buffer_reference_align = 8) readonly buffer SkinningData {
|
||||
SkinningDataType data[];
|
||||
};
|
||||
|
||||
layout (buffer_reference, std430) readonly buffer JointMatrices {
|
||||
mat4 matrices[];
|
||||
layout (buffer_reference, std430, buffer_reference_align = 8) readonly buffer JointMatrices {
|
||||
mat4 matrices[];
|
||||
};
|
||||
|
||||
layout (push_constant) uniform constants
|
||||
@@ -23,16 +25,45 @@ layout (push_constant) uniform constants
|
||||
JointMatrices jointMatrices;
|
||||
uint jointMatricesStartIndex;
|
||||
uint numVertices;
|
||||
VertexBuffer inputBuffer;
|
||||
VertexBuffer inputBuffer;
|
||||
SkinningData skinningData;
|
||||
VertexBuffer outputBuffer;
|
||||
VertexBuffer outputBuffer;
|
||||
} pcs;
|
||||
|
||||
layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
mat4 getJointMatrix(int jointId) {
|
||||
if (jointId < 0) return mat4(1.0);
|
||||
return pcs.jointMatrices.matrices[pcs.jointMatricesStartIndex + jointId];
|
||||
if (jointId < 0) {
|
||||
return mat4(1.0);
|
||||
}
|
||||
|
||||
return pcs.jointMatrices.matrices[pcs.jointMatricesStartIndex + uint(jointId)];
|
||||
}
|
||||
|
||||
vec3 safeNormalize(vec3 v, vec3 fallback) {
|
||||
float len2 = dot(v, v);
|
||||
if (len2 <= 1e-20) {
|
||||
return fallback;
|
||||
}
|
||||
return v * inversesqrt(len2);
|
||||
}
|
||||
|
||||
mat4 buildSkinMatrix(SkinningDataType sd) {
|
||||
// If imported weights are slightly imperfect, normalize them here as a
|
||||
// second line of defense. If a vertex has no weights at all, pass it
|
||||
// through unchanged instead of collapsing it to vec3(0).
|
||||
float weightSum = sd.weights.x + sd.weights.y + sd.weights.z + sd.weights.w;
|
||||
if (weightSum <= 1e-8) {
|
||||
return mat4(1.0);
|
||||
}
|
||||
|
||||
vec4 w = sd.weights / weightSum;
|
||||
|
||||
return
|
||||
w.x * getJointMatrix(sd.jointIds.x) +
|
||||
w.y * getJointMatrix(sd.jointIds.y) +
|
||||
w.z * getJointMatrix(sd.jointIds.z) +
|
||||
w.w * getJointMatrix(sd.jointIds.w);
|
||||
}
|
||||
|
||||
void main()
|
||||
@@ -42,19 +73,18 @@ void main()
|
||||
return;
|
||||
}
|
||||
|
||||
SkinningDataType sd = pcs.skinningData.data[index];
|
||||
mat4 skinMatrix =
|
||||
sd.weights.x * getJointMatrix(sd.jointIds.x) +
|
||||
sd.weights.y * getJointMatrix(sd.jointIds.y) +
|
||||
sd.weights.z * getJointMatrix(sd.jointIds.z) +
|
||||
sd.weights.w * getJointMatrix(sd.jointIds.w);
|
||||
|
||||
Vertex v = pcs.inputBuffer.vertices[index];
|
||||
SkinningDataType sd = pcs.skinningData.data[index];
|
||||
mat4 skinMatrix = buildSkinMatrix(sd);
|
||||
|
||||
v.position = vec3(skinMatrix * vec4(v.position, 1.0));
|
||||
|
||||
// For rigid joint matrices this is correct. If you allow non-uniform scale
|
||||
// in bones, replace this with a proper inverse-transpose normal matrix path.
|
||||
mat3 skinMat3 = mat3(skinMatrix);
|
||||
v.normal = skinMat3 * v.normal;
|
||||
v.tangent.xyz = skinMat3 * v.tangent.xyz; // don't transform tangent.w
|
||||
v.normal = safeNormalize(skinMat3 * v.normal, v.normal);
|
||||
v.tangent.xyz = safeNormalize(skinMat3 * v.tangent.xyz, v.tangent.xyz);
|
||||
// v.tangent.w is handedness; do not transform it.
|
||||
|
||||
pcs.outputBuffer.vertices[index] = v;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user