Skip to content

Commit 2b9f60a

Browse files
committed
MeshHandler: Fix weightsData dequantization error
In the Editor's serialization code, normalized weights are quantized as 5-bit UNORM values then packed as ints. This implies flooring - while the sum of the quantized values are guaranteed to be `31` this can lead to a loss of precision where the _de_quantized sum (div by 31 then sum) of weights is less than 1.0. Such cases can be trivally reproduced by search, for example a tuple of (1,25,5) summed as (1/31+25/31+5/31) in _that exact order_ will yield 0.9999999999999999 instead of 1.0, and ((1/31+25/31+5/31) >= 1.0) will be falsey. This PR implements the sum of weight with ints instead. Alternatively an epsilon value could be chosen, but this is what the player code does.
1 parent e9ed554 commit 2b9f60a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

UnityPy/helpers/MeshHelper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def decompress_compressed_mesh(self):
583583
# Skin
584584
if m_CompressedMesh.m_Weights.m_NumItems > 0:
585585
weightsData = unpack_ints(m_CompressedMesh.m_Weights)
586-
weightsData = [weight / 31 for weight in weightsData]
586+
weightsData = [weight for weight in weightsData]
587587
boneIndicesData = unpack_ints(m_CompressedMesh.m_BoneIndices)
588588

589589
vertexIndex = 0
@@ -596,14 +596,14 @@ def decompress_compressed_mesh(self):
596596
boneIndicesIterator = iter(boneIndicesData)
597597
for weight, boneIndex in zip(weightsData, boneIndicesIterator):
598598
# read bone index and weight
599-
self.m_BoneWeights[vertexIndex][j] = weight
599+
self.m_BoneWeights[vertexIndex][j] = weight / 31
600600
self.m_BoneIndices[vertexIndex][j] = boneIndex
601601

602602
j += 1
603603
sum += weight
604604

605605
# the weights add up to one, continue with the next vertex.
606-
if sum >= 1.0:
606+
if sum >= 31:
607607
j = 4
608608
# set weights and boneIndices to 0,
609609
# already done on init

0 commit comments

Comments
 (0)