From 290be716dfc4dab41221b973a552f5b0b843c847 Mon Sep 17 00:00:00 2001 From: 1915rising <127124202+1915rising@users.noreply.github.com> Date: Sat, 21 Nov 2026 18:38:06 -0500 Subject: [PATCH 1/2] Normalize UNorm/SNorm vertex channels in MeshHandler --- UnityPy/helpers/MeshHelper.py | 42 ++++++++++++++++++ tests/test_mesh_helper.py | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/UnityPy/helpers/MeshHelper.py b/UnityPy/helpers/MeshHelper.py index edbd6bee..8d0ee7a9 100644 --- a/UnityPy/helpers/MeshHelper.py +++ b/UnityPy/helpers/MeshHelper.py @@ -397,8 +397,50 @@ def read_vertex_data(self, m_Channels: list[ChannelInfo], m_Streams: list[Stream componentBytes[offset : offset + component_byte_size] = item component_data = list(struct.iter_unpack(f">{channel_dimension}{component_dtype}", componentBytes)) + component_data = self.normalize_channel_data(m_Channel, component_data) self.assign_channel_vertex_data(chn, component_data) + def normalize_channel_data(self, m_Channel: ChannelInfo, component_data: list) -> list: + """Convert normalized (UNorm/SNorm) channel values to floats. + + UNorm values are divided by the format maximum (255 / 65535), + SNorm values by 127 / 32767 and clamped to -1, matching how + compressed meshes and copy_from_mesh already treat colours. + All other formats are returned unchanged. + """ + divisor: Optional[float] = None + signed = False + if self.version[0] < 2017: + # legacy channel formats; only the colour format is normalized + if m_Channel.format == 2: # kChannelFormatColor + divisor = 255.0 + elif self.version[0] < 2019: + format = VertexFormat2017(m_Channel.format) + if format in (VertexFormat2017.kVertexFormatColor, VertexFormat2017.kVertexFormatUNorm8): + divisor = 255.0 + elif format == VertexFormat2017.kVertexFormatSNorm8: + divisor, signed = 127.0, True + elif format == VertexFormat2017.kVertexFormatUNorm16: + divisor = 65535.0 + elif format == VertexFormat2017.kVertexFormatSNorm16: + divisor, signed = 32767.0, True + else: + format = VertexFormat(m_Channel.format) + if format == VertexFormat.kVertexFormatUNorm8: + divisor = 255.0 + elif format == VertexFormat.kVertexFormatSNorm8: + divisor, signed = 127.0, True + elif format == VertexFormat.kVertexFormatUNorm16: + divisor = 65535.0 + elif format == VertexFormat.kVertexFormatSNorm16: + divisor, signed = 32767.0, True + + if divisor is None: + return component_data + if signed: + return [tuple(max(value / divisor, -1.0) for value in vertex) for vertex in component_data] + return [tuple(value / divisor for value in vertex) for vertex in component_data] + def assign_channel_vertex_data(self, channel: int, component_data: list): if self.version[0] >= 2018: if channel == 0: # kShaderChannelVertex diff --git a/tests/test_mesh_helper.py b/tests/test_mesh_helper.py index cff91362..62a3e07a 100644 --- a/tests/test_mesh_helper.py +++ b/tests/test_mesh_helper.py @@ -1,8 +1,22 @@ import os +import struct +from types import SimpleNamespace import pytest import UnityPy +from UnityPy.classes.generated import ( + AABB, + ChannelInfo, + CompressedMesh, + Mesh, + PackedBitVector, + SubMesh, + Vector3f, + VertexData, +) +from UnityPy.export.MeshExporter import export_mesh_obj +from UnityPy.helpers import MeshHelper from UnityPy.helpers.MeshHelper import MeshHandler SAMPLES = os.path.join(os.path.dirname(os.path.abspath(__file__)), "samples") @@ -39,6 +53,72 @@ def test_mesh_with_empty_submeshes_and_no_indices(): assert triangles(mesh) == [[] for _ in mesh.m_SubMeshes] +def build_normalized_channel_mesh(): + # one vertex: Float x3 position, UNorm8 x4 color, UNorm16 x2 uv0, SNorm16 x2 uv1 + vertex = struct.pack( + "<3f4B2H2h", 0, 0, 0, 255, 128, 0, 255, 65535, 32768, -32767, 16384 + ) + # VertexFormat since 2019: 0 Float, 2 UNorm8, 4 UNorm16, 5 SNorm16 + channels = [ + ChannelInfo(dimension=3, format=0, offset=0, stream=0), # position + ChannelInfo(dimension=0, format=0, offset=0, stream=0), # normal + ChannelInfo(dimension=0, format=0, offset=0, stream=0), # tangent + ChannelInfo(dimension=4, format=2, offset=12, stream=0), # color + ChannelInfo(dimension=2, format=4, offset=16, stream=0), # uv0 + ChannelInfo(dimension=2, format=5, offset=20, stream=0), # uv1 + ] + empty = PackedBitVector(m_Data=[], m_NumItems=0) + names = ( + "m_BoneIndices", + "m_NormalSigns", + "m_Normals", + "m_TangentSigns", + "m_Tangents", + "m_Triangles", + "m_UV", + "m_Vertices", + "m_Weights", + ) + aabb = AABB(m_Center=Vector3f(0, 0, 0), m_Extent=Vector3f(1, 1, 1)) + mesh = Mesh( + m_BindPose=[], + m_CompressedMesh=CompressedMesh(**{name: empty for name in names}), + m_IndexBuffer=[], + m_IndexFormat=0, + m_LocalAABB=aabb, + m_MeshCompression=0, + m_Name="mesh", + m_SubMeshes=[ + SubMesh( + firstByte=0, + firstVertex=0, + indexCount=0, + localAABB=aabb, + vertexCount=1, + baseVertex=0, + topology=0, + ) + ], + m_VertexData=VertexData(m_DataSize=vertex, m_VertexCount=1, m_Channels=channels), + ) + mesh.object_reader = SimpleNamespace(version=(2021, 3, 0, 0)) + return mesh + + +@pytest.mark.parametrize("use_boost", [True, False]) +def test_normalized_vertex_channels_are_returned_as_floats(monkeypatch, use_boost): + if not use_boost: + monkeypatch.setattr(MeshHelper, "UnityPyBoost", None) + mesh = build_normalized_channel_mesh() + handler = MeshHandler(mesh) + handler.process() + assert handler.m_Colors == [(1.0, 128 / 255, 0.0, 1.0)] + assert handler.m_UV0 == [(1.0, 32768 / 65535)] + assert handler.m_UV1 == [(-1.0, 16384 / 32767)] + obj = export_mesh_obj(mesh) + assert "vt 65535 32768" not in obj + + def test_mesh_with_submesh_indices_but_no_index_buffer(): mesh = load_mesh() assert any(submesh.indexCount for submesh in mesh.m_SubMeshes) From aab7942b16476c01f2208980f5be814f65173648 Mon Sep 17 00:00:00 2001 From: FordProductions <127124202+1915rising@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:44:52 -0500 Subject: [PATCH 2/2] refactor(MeshHelper): move channel normalization into mapping tables --- UnityPy/enums/VertexFormat.py | 20 ++++++++++++++++++++ UnityPy/helpers/MeshHelper.py | 33 ++++++++++----------------------- tests/test_mesh_helper.py | 3 ++- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/UnityPy/enums/VertexFormat.py b/UnityPy/enums/VertexFormat.py index ea3350a1..3d34b3ec 100644 --- a/UnityPy/enums/VertexFormat.py +++ b/UnityPy/enums/VertexFormat.py @@ -78,3 +78,23 @@ class VertexFormat(IntEnum): VertexFormat.kVertexFormatUInt32: "I", VertexFormat.kVertexFormatSInt32: "i", } + +# format -> (divisor, signed) for formats stored as normalized integers +VERTEX_CHANNEL_FORMAT_NORMALIZATION_MAP = { + VertexChannelFormat.kChannelFormatColor: (255.0, False), +} + +VERTEX_FORMAT_2017_NORMALIZATION_MAP = { + VertexFormat2017.kVertexFormatColor: (255.0, False), + VertexFormat2017.kVertexFormatUNorm8: (255.0, False), + VertexFormat2017.kVertexFormatSNorm8: (127.0, True), + VertexFormat2017.kVertexFormatUNorm16: (65535.0, False), + VertexFormat2017.kVertexFormatSNorm16: (32767.0, True), +} + +VERTEX_FORMAT_NORMALIZATION_MAP = { + VertexFormat.kVertexFormatUNorm8: (255.0, False), + VertexFormat.kVertexFormatSNorm8: (127.0, True), + VertexFormat.kVertexFormatUNorm16: (65535.0, False), + VertexFormat.kVertexFormatSNorm16: (32767.0, True), +} diff --git a/UnityPy/helpers/MeshHelper.py b/UnityPy/helpers/MeshHelper.py index 8d0ee7a9..23e59c1e 100644 --- a/UnityPy/helpers/MeshHelper.py +++ b/UnityPy/helpers/MeshHelper.py @@ -15,8 +15,11 @@ ) from ..enums.MeshTopology import MeshTopology from ..enums.VertexFormat import ( + VERTEX_CHANNEL_FORMAT_NORMALIZATION_MAP, VERTEX_CHANNEL_FORMAT_STRUCT_TYPE_MAP, + VERTEX_FORMAT_2017_NORMALIZATION_MAP, VERTEX_FORMAT_2017_STRUCT_TYPE_MAP, + VERTEX_FORMAT_NORMALIZATION_MAP, VERTEX_FORMAT_STRUCT_TYPE_MAP, VertexChannelFormat, VertexFormat, @@ -408,35 +411,19 @@ def normalize_channel_data(self, m_Channel: ChannelInfo, component_data: list) - compressed meshes and copy_from_mesh already treat colours. All other formats are returned unchanged. """ - divisor: Optional[float] = None - signed = False if self.version[0] < 2017: - # legacy channel formats; only the colour format is normalized - if m_Channel.format == 2: # kChannelFormatColor - divisor = 255.0 + format = VertexChannelFormat(m_Channel.format) + normalization = VERTEX_CHANNEL_FORMAT_NORMALIZATION_MAP.get(format) elif self.version[0] < 2019: format = VertexFormat2017(m_Channel.format) - if format in (VertexFormat2017.kVertexFormatColor, VertexFormat2017.kVertexFormatUNorm8): - divisor = 255.0 - elif format == VertexFormat2017.kVertexFormatSNorm8: - divisor, signed = 127.0, True - elif format == VertexFormat2017.kVertexFormatUNorm16: - divisor = 65535.0 - elif format == VertexFormat2017.kVertexFormatSNorm16: - divisor, signed = 32767.0, True + normalization = VERTEX_FORMAT_2017_NORMALIZATION_MAP.get(format) else: format = VertexFormat(m_Channel.format) - if format == VertexFormat.kVertexFormatUNorm8: - divisor = 255.0 - elif format == VertexFormat.kVertexFormatSNorm8: - divisor, signed = 127.0, True - elif format == VertexFormat.kVertexFormatUNorm16: - divisor = 65535.0 - elif format == VertexFormat.kVertexFormatSNorm16: - divisor, signed = 32767.0, True - - if divisor is None: + normalization = VERTEX_FORMAT_NORMALIZATION_MAP.get(format) + + if normalization is None: return component_data + divisor, signed = normalization if signed: return [tuple(max(value / divisor, -1.0) for value in vertex) for vertex in component_data] return [tuple(value / divisor for value in vertex) for vertex in component_data] diff --git a/tests/test_mesh_helper.py b/tests/test_mesh_helper.py index 62a3e07a..b8934f10 100644 --- a/tests/test_mesh_helper.py +++ b/tests/test_mesh_helper.py @@ -116,7 +116,8 @@ def test_normalized_vertex_channels_are_returned_as_floats(monkeypatch, use_boos assert handler.m_UV0 == [(1.0, 32768 / 65535)] assert handler.m_UV1 == [(-1.0, 16384 / 32767)] obj = export_mesh_obj(mesh) - assert "vt 65535 32768" not in obj + vt = next(line for line in obj.splitlines() if line.startswith("vt ")) + assert [float(value) for value in vt.split()[1:]] == pytest.approx([1.000000, 0.500008], abs=1e-6) def test_mesh_with_submesh_indices_but_no_index_buffer():