Skip to content

Commit cc8f023

Browse files
fix invalid bone weights (#8316)
# Objective when a mesh uses zero for all bone weights, vertices end up in the middle of the screen. ## Solution we can address this by explicitly setting the first bone weight to 1 when the weights are given as zero. this is the approach taken by [unity](https://forum.unity.com/threads/whats-the-problem-with-this-import-fbx-warning.133736/) (although that also sets the bone index to zero) and [three.js](https://github.com/mrdoob/three.js/blob/94c1a4b86f53c91b61ec4ca3299b4ad02422ddb8/src/objects/SkinnedMesh.js#L98), and likely other engines. ## Alternatives it does add a bit of overhead, and users can always fix this themselves, though it's a bit awkward particularly with gltfs. (note - this is for work so my sme status shouldn't apply) --------- Co-authored-by: ira <[email protected]>
1 parent be0ca7f commit cc8f023

File tree

1 file changed

+12
-1
lines changed
  • crates/bevy_render/src/mesh/mesh

1 file changed

+12
-1
lines changed

crates/bevy_render/src/mesh/mesh/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Mesh {
110110
attribute: MeshVertexAttribute,
111111
values: impl Into<VertexAttributeValues>,
112112
) {
113-
let values = values.into();
113+
let mut values = values.into();
114114
let values_format = VertexFormat::from(&values);
115115
if values_format != attribute.format {
116116
panic!(
@@ -119,6 +119,17 @@ impl Mesh {
119119
);
120120
}
121121

122+
// validate attributes
123+
if attribute.id == Self::ATTRIBUTE_JOINT_WEIGHT.id {
124+
let VertexAttributeValues::Float32x4(ref mut values) = values else {
125+
unreachable!() // we confirmed the format above
126+
};
127+
for value in values.iter_mut().filter(|v| *v == &[0.0, 0.0, 0.0, 0.0]) {
128+
// zero weights are invalid
129+
value[0] = 1.0;
130+
}
131+
}
132+
122133
self.attributes
123134
.insert(attribute.id, MeshAttributeData { attribute, values });
124135
}

0 commit comments

Comments
 (0)