Skip to content

Commit 638bf1d

Browse files
authored
[gltf] Support either u16 or float for colors (#3322)
It seems like this depends on the version of blender, so we should support both.
1 parent 5ddf1e4 commit 638bf1d

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

goalc/build_level/common/gltf_mesh_extract.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ std::vector<math::Vector<u8, 4>> extract_color_from_vec4_float(const u8* data,
7474
return result;
7575
}
7676

77+
/*!
78+
* Convert a GLTF color buffer (u16 format) to u8 colors.
79+
*/
80+
std::vector<math::Vector<u8, 4>> extract_color_from_vec4_u16(const u8* data,
81+
u32 count,
82+
u32 stride) {
83+
std::vector<math::Vector<u8, 4>> result;
84+
result.reserve(count);
85+
for (u32 i = 0; i < count; i++) {
86+
math::Vector<u16, 4> temp;
87+
memcpy(&temp, data, sizeof(math::Vector<u16, 4>));
88+
data += stride;
89+
result.emplace_back(temp.x() >> 8, temp.y() >> 8, temp.z() >> 8, temp.w() >> 8);
90+
}
91+
return result;
92+
}
93+
7794
/*!
7895
* Convert a GLTF index buffer
7996
*/
@@ -169,14 +186,19 @@ ExtractedVertices gltf_vertices(const tinygltf::Model& model,
169186
const auto count = attrib_accessor.count;
170187

171188
ASSERT_MSG(attrib_accessor.type == TINYGLTF_TYPE_VEC4, "COLOR_0 wasn't vec4");
172-
ASSERT_MSG(
173-
attrib_accessor.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT,
174-
fmt::format("COLOR_0 wasn't float, got {} instead", attrib_accessor.componentType));
175-
auto colors = extract_color_from_vec4_float(data_ptr, count, byte_stride);
189+
std::vector<math::Vector<u8, 4>> colors;
190+
switch (attrib_accessor.componentType) {
191+
case TINYGLTF_COMPONENT_TYPE_FLOAT:
192+
colors = extract_color_from_vec4_float(data_ptr, count, byte_stride);
193+
break;
194+
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
195+
colors = extract_color_from_vec4_u16(data_ptr, count, byte_stride);
196+
break;
197+
default:
198+
lg::die("Unknown component type for COLOR_0: {}", attrib_accessor.componentType);
199+
}
176200
vtx_colors.insert(vtx_colors.end(), colors.begin(), colors.end());
177201
}
178-
179-
// ASSERT_MSG(color_attrib != attributes.end(), "Did not find color attribute.");
180202
}
181203

182204
bool got_texture = false;

0 commit comments

Comments
 (0)