diff --git a/goalc/build_level/common/gltf_mesh_extract.cpp b/goalc/build_level/common/gltf_mesh_extract.cpp index a7cbb42db6..812b1a59e5 100644 --- a/goalc/build_level/common/gltf_mesh_extract.cpp +++ b/goalc/build_level/common/gltf_mesh_extract.cpp @@ -74,6 +74,23 @@ std::vector> extract_color_from_vec4_float(const u8* data, return result; } +/*! + * Convert a GLTF color buffer (u16 format) to u8 colors. + */ +std::vector> extract_color_from_vec4_u16(const u8* data, + u32 count, + u32 stride) { + std::vector> result; + result.reserve(count); + for (u32 i = 0; i < count; i++) { + math::Vector temp; + memcpy(&temp, data, sizeof(math::Vector)); + data += stride; + result.emplace_back(temp.x() >> 8, temp.y() >> 8, temp.z() >> 8, temp.w() >> 8); + } + return result; +} + /*! * Convert a GLTF index buffer */ @@ -169,14 +186,19 @@ ExtractedVertices gltf_vertices(const tinygltf::Model& model, const auto count = attrib_accessor.count; ASSERT_MSG(attrib_accessor.type == TINYGLTF_TYPE_VEC4, "COLOR_0 wasn't vec4"); - ASSERT_MSG( - attrib_accessor.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT, - fmt::format("COLOR_0 wasn't float, got {} instead", attrib_accessor.componentType)); - auto colors = extract_color_from_vec4_float(data_ptr, count, byte_stride); + std::vector> colors; + switch (attrib_accessor.componentType) { + case TINYGLTF_COMPONENT_TYPE_FLOAT: + colors = extract_color_from_vec4_float(data_ptr, count, byte_stride); + break; + case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: + colors = extract_color_from_vec4_u16(data_ptr, count, byte_stride); + break; + default: + lg::die("Unknown component type for COLOR_0: {}", attrib_accessor.componentType); + } vtx_colors.insert(vtx_colors.end(), colors.begin(), colors.end()); } - - // ASSERT_MSG(color_attrib != attributes.end(), "Did not find color attribute."); } bool got_texture = false;