Skip to content

Commit

Permalink
gltfpack: Combine variant quantization bounds
Browse files Browse the repository at this point in the history
When a mesh uses multiple materials through the use of variants, we need
to make sure that the quantization parameters for UV for a given
material incorporate all meshes that refer to it, including variants.

In addition, when processing a mesh, we need to take all variant
requirements into account, so that e.g. if only one material needs a
second UV set, we still include it even if it's a variant.

This isn't quite correct in cases when materials are used in other
meshes as well, since we need to ensure that the quantization data is
the same across all materials.
  • Loading branch information
zeux committed Feb 3, 2021
1 parent 5898493 commit 6581c34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
10 changes: 9 additions & 1 deletion gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,15 @@ static void process(cgltf_data* data, const char* input_path, const char* output
{
Mesh& mesh = meshes[i];
MaterialInfo mi = mesh.material ? materials[mesh.material - data->materials] : MaterialInfo();
// TODO: variants

// merge material requirements across all variants
for (size_t j = 0; j < mesh.variants.size(); ++j)
{
MaterialInfo vi = materials[mesh.variants[j].material - data->materials];

mi.needsTangents |= vi.needsTangents;
mi.textureSetMask |= vi.textureSetMask;
}

processMesh(mesh, mi, settings);
}
Expand Down
21 changes: 15 additions & 6 deletions gltf/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ struct Bounds
{
return min.f[0] <= max.f[0] && min.f[1] <= max.f[1] && min.f[2] <= max.f[2] && min.f[3] <= max.f[3];
}

void merge(const Bounds& other)
{
for (int k = 0; k < 4; ++k)
{
min.f[k] = min.f[k] < other.min.f[k] ? min.f[k] : other.min.f[k];
max.f[k] = max.f[k] > other.max.f[k] ? max.f[k] : other.max.f[k];
}
}
};

static void updateAttributeBounds(const Mesh& mesh, cgltf_attribute_type type, Bounds& b)
Expand Down Expand Up @@ -107,14 +116,14 @@ void prepareQuantizationTexture(cgltf_data* data, std::vector<QuantizationTextur
{
const Mesh& mesh = meshes[i];

if (!mesh.material)
continue;
Bounds uvb;
updateAttributeBounds(mesh, cgltf_attribute_type_texcoord, uvb);

size_t mi = mesh.material - data->materials;
assert(mi < bounds.size());
if (mesh.material)
bounds[mesh.material - data->materials].merge(uvb);

updateAttributeBounds(mesh, cgltf_attribute_type_texcoord, bounds[mi]);
// TODO: variants
for (size_t j = 0; j < mesh.variants.size(); ++j)
bounds[mesh.variants[j].material - data->materials].merge(uvb);
}

for (size_t i = 0; i < result.size(); ++i)
Expand Down

0 comments on commit 6581c34

Please sign in to comment.