From 78edfa379ea7a83913bc815751863b6ce5110a1f Mon Sep 17 00:00:00 2001 From: mcdev Date: Mon, 27 Oct 2025 12:15:40 +0100 Subject: [PATCH] Adds Int16/Uint16 -> Int8/Uint8 converters for multiple vertex color channels support. --- AssetLoader/src/GLTFBuilder.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/AssetLoader/src/GLTFBuilder.cpp b/AssetLoader/src/GLTFBuilder.cpp index 7e274206..5aeba179 100644 --- a/AssetLoader/src/GLTFBuilder.cpp +++ b/AssetLoader/src/GLTFBuilder.cpp @@ -118,6 +118,25 @@ inline float ConvertElement(Uint16 Src) } +// ========================== Int16/Uint16 -> Int8/Uint8 =========================== +template <> +inline Uint8 ConvertElement(Uint16 Src) +{ + return static_cast((static_cast(Src) * 255 + 128) / 65535); +} + +template <> +inline Int8 ConvertElement(Int16 Src) +{ + // Scale from [-32768, 32767] ? [-128, 127] with rounding + // Use 32-bit math to avoid overflow + Int32 temp = static_cast(Src) * 127; // multiply first + temp += (Src >= 0 ? 16384 : -16384); // add 0.5 for rounding + temp /= 32767; // scale down + return static_cast(temp); +} + + template inline void WriteGltfData(const void* pSrc, Uint32 NumComponents,