Skip to content

Commit 9697d48

Browse files
mos9527K0lb3
authored andcommitted
MeshHandler: Fix weightsData dequantization error (#338)
fix(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 5734513 commit 9697d48

1 file changed

Lines changed: 2 additions & 3 deletions

File tree

UnityPy/helpers/MeshHelper.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,6 @@ def decompress_compressed_mesh(self):
567567
# Skin
568568
if m_CompressedMesh.m_Weights.m_NumItems > 0:
569569
weightsData = unpack_ints(m_CompressedMesh.m_Weights)
570-
weightsData = [weight / 31 for weight in weightsData]
571570
boneIndicesData = unpack_ints(m_CompressedMesh.m_BoneIndices)
572571

573572
vertexIndex = 0
@@ -580,14 +579,14 @@ def decompress_compressed_mesh(self):
580579
boneIndicesIterator = iter(boneIndicesData)
581580
for weight, boneIndex in zip(weightsData, boneIndicesIterator):
582581
# read bone index and weight
583-
boneWeights[vertexIndex][j] = weight
582+
boneWeights[vertexIndex][j] = weight / 31
584583
boneIndices[vertexIndex][j] = boneIndex
585584

586585
j += 1
587586
sum += weight
588587

589588
# the weights add up to one, continue with the next vertex.
590-
if sum >= 1.0:
589+
if sum >= 31:
591590
j = 4
592591
# set weights and boneIndices to 0,
593592
# already done on init

0 commit comments

Comments
 (0)