Skip to content

Commit

Permalink
Handle the situation where the recorded component type does not match
Browse files Browse the repository at this point in the history
the required type for the actual number of stored points

This situation arises when decoding certain malformed files, most
notably it's seen in glb tiles from Google Earth's 3d tileset.

It's a port of the workaround used by Cesium native here:

https://github.com/CesiumGS/cesium-native/blob/d9172461e2d53f19d050ebc39f24a4f4ef5b9224/CesiumGltfReader/src/decodeDraco.cpp#L101
  • Loading branch information
nyalldawson committed Aug 24, 2023
1 parent 0eaa23f commit 84bcf50
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4890,6 +4890,25 @@ static bool ParseDracoExtension(Primitive *primitive, Model *model,

// create new bufferView for indices
if (primitive->indices >= 0) {
const draco::PointIndex::ValueType numPoint = mesh->num_points();
// handle the situation where the stored component type does not match the
// required type for the actual number of stored points
int supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
if (numPoint < static_cast<draco::PointIndex::ValueType>(
std::numeric_limits<uint8_t>::max())) {
supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
} else if (
numPoint < static_cast<draco::PointIndex::ValueType>(
std::numeric_limits<uint16_t>::max())) {
supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT;
} else {
supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT;
}

if (supposedComponentType > model->accessors[primitive->indices].componentType) {
model->accessors[primitive->indices].componentType = supposedComponentType;
}

int32_t componentSize = GetComponentSizeInBytes(
model->accessors[primitive->indices].componentType);
Buffer decodedIndexBuffer;
Expand Down

0 comments on commit 84bcf50

Please sign in to comment.