diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs index c7096b747..83aaa9bbf 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs @@ -200,23 +200,6 @@ namespace UniGLTF if (!primitives.HasNormal()) HasNormal = false; } - private static (float x, float y, float z, float w) NormalizeBoneWeight( - (float x, float y, float z, float w) src) - { - var sum = src.x + src.y + src.z + src.w; - if (sum == 0) - { - return src; - } - - var f = 1.0f / sum; - src.x *= f; - src.y *= f; - src.z *= f; - src.w *= f; - return src; - } - private BlendShape GetOrCreateBlendShape(int i) { if (i < _blendShapes.Count && _blendShapes[i] != null) @@ -330,8 +313,7 @@ namespace UniGLTF var texCoords0 = primitives.GetTexCoords0(data, positions.Length); var texCoords1 = primitives.GetTexCoords1(data, positions.Length); var colors = primitives.GetColors(data, positions.Length); - var jointsGetter = primitives.GetJoints(data, positions.Length); - var weightsGetter = primitives.GetWeights(data, positions.Length); + var skinning = SkinningInfo.Create(data, gltfMesh, primitives); CheckAttributeUsages(primitives); @@ -357,8 +339,6 @@ namespace UniGLTF } var texCoord1 = texCoords1 != null ? texCoords1.Value[i].ReverseUV() : Vector2.zero; - var joints = jointsGetter?.Invoke(i) ?? (0, 0, 0, 0); - var weights = weightsGetter != null ? NormalizeBoneWeight(weightsGetter(i)) : (0, 0, 0, 0); var color = colors != null ? colors.Value[i] : Color.white; AddVertex( @@ -369,15 +349,7 @@ namespace UniGLTF texCoord1, color ), - (jointsGetter != null) ? new SkinnedMeshVertex( - joints.x, - joints.y, - joints.z, - joints.w, - weights.x, - weights.y, - weights.z, - weights.w) : default); + skinning.GetSkinnedVertex(i)); } // blendshape @@ -468,8 +440,7 @@ namespace UniGLTF var texCoords0 = primitives.GetTexCoords0(data, positions.Length); var texCoords1 = primitives.GetTexCoords1(data, positions.Length); var colors = primitives.GetColors(data, positions.Length); - var jointsGetter = primitives.GetJoints(data, positions.Length); - var weightsGetter = primitives.GetWeights(data, positions.Length); + var skinning = SkinningInfo.Create(data, gltfMesh, primitives); CheckAttributeUsages(primitives); @@ -494,26 +465,15 @@ namespace UniGLTF var texCoord1 = texCoords1 != null ? texCoords1.Value[i].ReverseUV() : Vector2.zero; var color = colors != null ? colors.Value[i] : Color.white; - var joints = jointsGetter?.Invoke(i) ?? (0, 0, 0, 0); - var weights = weightsGetter != null ? NormalizeBoneWeight(weightsGetter(i)) : (0, 0, 0, 0); - - AddVertex(new MeshVertex( - position, - normal, - texCoord0, - texCoord1, - color - ), - (jointsGetter != null) ? new SkinnedMeshVertex( - joints.x, - joints.y, - joints.z, - joints.w, - weights.x, - weights.y, - weights.z, - weights.w) : default); + AddVertex( + new MeshVertex( + position, + normal, + texCoord0, + texCoord1, + color), + skinning.GetSkinnedVertex(i)); } // blendshape diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs new file mode 100644 index 000000000..33fae084a --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs @@ -0,0 +1,54 @@ +namespace UniGLTF +{ + class SkinningInfo + { + public JointsAccessor.Getter Joints; + public WeightsAccessor.Getter Weights; + + public static SkinningInfo Create(GltfData data, glTFMesh mesh, glTFPrimitives primitives) + { + var positions = data.GLTF.accessors[primitives.attributes.POSITION]; + return new SkinningInfo + { + Joints = primitives.GetJoints(data, positions.count), + Weights = primitives.GetWeights(data, positions.count), + }; + } + + private static (float x, float y, float z, float w) NormalizeBoneWeight( + (float x, float y, float z, float w) src) + { + var sum = src.x + src.y + src.z + src.w; + if (sum == 0) + { + return src; + } + + var f = 1.0f / sum; + src.x *= f; + src.y *= f; + src.z *= f; + src.w *= f; + return src; + } + + public SkinnedMeshVertex GetSkinnedVertex(int i) + { + if (Joints == null) + { + return default; + } + var joints = Joints?.Invoke(i) ?? (0, 0, 0, 0); + var weights = Weights != null ? NormalizeBoneWeight(Weights(i)) : (0, 0, 0, 0); + return new SkinnedMeshVertex( + joints.x, + joints.y, + joints.z, + joints.w, + weights.x, + weights.y, + weights.z, + weights.w); + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs.meta new file mode 100644 index 000000000..2dc28d998 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 06927c8487024f541b525aa3bd3e572b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: