Skip to content

Commit 3aad5c6

Browse files
authored
animations: convert skinning weights from unorm8x4 to float32x4 (#9338)
# Objective - Fixes part of #9021 ## Solution - Joint mesh are in format `Unorm8x4` in some gltf file, but Bevy expects a `Float32x4`. Converts them. Also converts `Unorm16x4` - According to gltf spec: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#skinned-mesh-attributes > WEIGHTS_n: float, or normalized unsigned byte, or normalized unsigned short
1 parent cfa3303 commit 3aad5c6

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

crates/bevy_gltf/src/vertex_attributes.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy_render::{
66
use bevy_utils::HashMap;
77
use gltf::{
88
accessor::{DataType, Dimensions},
9-
mesh::util::{ReadColors, ReadJoints, ReadTexCoords},
9+
mesh::util::{ReadColors, ReadJoints, ReadTexCoords, ReadWeights},
1010
};
1111
use thiserror::Error;
1212

@@ -206,6 +206,19 @@ impl<'a> VertexAttributeIter<'a> {
206206
}
207207
}
208208

209+
/// Materializes joint weight values, converting compatible formats to Float32x4
210+
fn into_joint_weight_values(self) -> Result<Values, AccessFailed> {
211+
match self {
212+
VertexAttributeIter::U8x4(it, Normalization(true)) => {
213+
Ok(Values::Float32x4(ReadWeights::U8(it).into_f32().collect()))
214+
}
215+
VertexAttributeIter::U16x4(it, Normalization(true)) => {
216+
Ok(Values::Float32x4(ReadWeights::U16(it).into_f32().collect()))
217+
}
218+
s => s.into_any_values(),
219+
}
220+
}
221+
209222
/// Materializes texture coordinate values, converting compatible formats to Float32x2
210223
fn into_tex_coord_values(self) -> Result<Values, AccessFailed> {
211224
match self {
@@ -224,6 +237,7 @@ enum ConversionMode {
224237
Any,
225238
Rgba,
226239
JointIndex,
240+
JointWeight,
227241
TexCoord,
228242
}
229243

@@ -252,7 +266,9 @@ pub(crate) fn convert_attribute(
252266
gltf::Semantic::Joints(0) => {
253267
Some((Mesh::ATTRIBUTE_JOINT_INDEX, ConversionMode::JointIndex))
254268
}
255-
gltf::Semantic::Weights(0) => Some((Mesh::ATTRIBUTE_JOINT_WEIGHT, ConversionMode::Any)),
269+
gltf::Semantic::Weights(0) => {
270+
Some((Mesh::ATTRIBUTE_JOINT_WEIGHT, ConversionMode::JointWeight))
271+
}
256272
gltf::Semantic::Extras(name) => custom_vertex_attributes
257273
.get(name)
258274
.map(|attr| (attr.clone(), ConversionMode::Any)),
@@ -264,6 +280,7 @@ pub(crate) fn convert_attribute(
264280
ConversionMode::Rgba => iter.into_rgba_values(),
265281
ConversionMode::TexCoord => iter.into_tex_coord_values(),
266282
ConversionMode::JointIndex => iter.into_joint_index_values(),
283+
ConversionMode::JointWeight => iter.into_joint_weight_values(),
267284
});
268285
match converted_values {
269286
Ok(values) => {

0 commit comments

Comments
 (0)