Skip to content

Commit

Permalink
gltfpack: Take account of PR comments and also write the scene extras…
Browse files Browse the repository at this point in the history
… and the mesh name
  • Loading branch information
JulienIcon committed Nov 21, 2024
1 parent d92e18b commit ca2e3c5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
14 changes: 11 additions & 3 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ static void process(cgltf_data* data, const char* input_path, const char* output

mergeMeshMaterials(data, meshes, settings);
if (settings.mesh_dedup)
dedupMeshes(meshes);
dedupMeshes(meshes, settings);

for (size_t i = 0; i < meshes.size(); ++i)
detachMesh(meshes[i], data, nodes, settings);
Expand Down Expand Up @@ -587,7 +587,15 @@ static void process(cgltf_data* data, const char* input_path, const char* output
const Mesh& mesh = meshes[i];

comma(json_meshes);
append(json_meshes, "{\"primitives\":[");
append(json_meshes, "{");
if (mesh.name && *mesh.name)
{
append(json_meshes, "\"name\":\"");
append(json_meshes, mesh.name);
append(json_meshes, "\"");
comma(json_meshes);
}
append(json_meshes, "\"primitives\":[");

size_t pi = i;
for (; pi < meshes.size(); ++pi)
Expand Down Expand Up @@ -896,7 +904,7 @@ static void process(cgltf_data* data, const char* input_path, const char* output
append(json, ",\"scenes\":[");

for (size_t i = 0; i < data->scenes_count; ++i)
writeScene(json, data->scenes[i], json_roots[i]);
writeScene(json, data->scenes[i], json_roots[i], settings);

append(json, "]");
}
Expand Down
7 changes: 5 additions & 2 deletions gltf/gltfpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct Mesh
bool geometry_duplicate;
uint64_t geometry_hash[2];

const char* name;

size_t targets;
std::vector<float> target_weights;
std::vector<const char*> target_names;
Expand Down Expand Up @@ -321,12 +323,13 @@ bool compareMeshVariants(const Mesh& lhs, const Mesh& rhs);
bool compareMeshNodes(const Mesh& lhs, const Mesh& rhs);

void hashMesh(Mesh& mesh);
void dedupMeshes(std::vector<Mesh>& meshes);
void dedupMeshes(std::vector<Mesh>& meshes, const Settings& settings);
void mergeMeshInstances(Mesh& mesh);
void mergeMeshes(std::vector<Mesh>& meshes, const Settings& settings);
void filterEmptyMeshes(std::vector<Mesh>& meshes);
void filterStreams(Mesh& mesh, const MaterialInfo& mi);

bool areExtrasEqual(const cgltf_extras& lhs, const cgltf_extras& rhs);
void mergeMeshMaterials(cgltf_data* data, std::vector<Mesh>& meshes, const Settings& settings);
void markNeededMaterials(cgltf_data* data, std::vector<MaterialInfo>& materials, const std::vector<Mesh>& meshes, const Settings& settings);

Expand Down Expand Up @@ -399,7 +402,7 @@ void writeLight(std::string& json, const cgltf_light& light);
void writeArray(std::string& json, const char* name, const std::string& contents);
void writeExtensions(std::string& json, const ExtensionInfo* extensions, size_t count);
void writeExtras(std::string& json, const cgltf_extras& extras);
void writeScene(std::string& json, const cgltf_scene& scene, const std::string& roots);
void writeScene(std::string& json, const cgltf_scene& scene, const std::string& roots, const Settings& settings);

/**
* Copyright (c) 2016-2024 Arseny Kapoulkine
Expand Down
16 changes: 8 additions & 8 deletions gltf/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ static bool areTextureViewsEqual(const cgltf_texture_view& lhs, const cgltf_text
return true;
}

static bool areExtrasEqual(const cgltf_extras& lhs, const cgltf_extras& rhs)
{
if (lhs.data && rhs.data)
return strcmp(lhs.data, rhs.data) == 0;
else
return lhs.data == rhs.data;
}

static bool areMaterialComponentsEqual(const cgltf_pbr_metallic_roughness& lhs, const cgltf_pbr_metallic_roughness& rhs)
{
if (!areTextureViewsEqual(lhs.base_color_texture, rhs.base_color_texture))
Expand Down Expand Up @@ -576,6 +568,14 @@ static bool shouldKeepAlpha(const cgltf_texture_view& color, float alpha, cgltf_
return image && getChannels(*image, images[image - data->images], input_path) == 4;
}

bool areExtrasEqual(const cgltf_extras& lhs, const cgltf_extras& rhs)
{
if (lhs.data && rhs.data)
return strcmp(lhs.data, rhs.data) == 0;
else
return lhs.data == rhs.data;
}

void optimizeMaterials(cgltf_data* data, const char* input_path, std::vector<ImageInfo>& images)
{
for (size_t i = 0; i < data->materials_count; ++i)
Expand Down
14 changes: 9 additions & 5 deletions gltf/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static bool canMergeMeshes(const Mesh& lhs, const Mesh& rhs, const Settings& set
if (lhs.streams.size() != rhs.streams.size())
return false;

if (lhs.extras.data && rhs.extras.data && strcmp(lhs.extras.data, rhs.extras.data))
if (settings.keep_extras && !areExtrasEqual(lhs.extras, rhs.extras))
return false;

for (size_t i = 0; i < lhs.streams.size(); ++i)
Expand Down Expand Up @@ -340,7 +340,7 @@ void hashMesh(Mesh& mesh)
hashUpdate(mesh.geometry_hash, meta, sizeof(meta));
}

static bool canDedupMesh(const Mesh& mesh)
static bool canDedupMesh(const Mesh& mesh, const Settings& settings)
{
// empty mesh
if (mesh.streams.empty())
Expand All @@ -350,14 +350,18 @@ static bool canDedupMesh(const Mesh& mesh)
if (mesh.nodes.empty() && mesh.instances.empty())
return false;

// has extras
if (settings.keep_extras && mesh.extras.data)
return false;

// to simplify dedup we ignore complex target setups for now
if (!mesh.target_weights.empty() || !mesh.target_names.empty() || !mesh.variants.empty())
return false;

return true;
}

void dedupMeshes(std::vector<Mesh>& meshes)
void dedupMeshes(std::vector<Mesh>& meshes, const Settings& settings)
{
std::unordered_map<uint64_t, int> hashes;

Expand All @@ -373,7 +377,7 @@ void dedupMeshes(std::vector<Mesh>& meshes)
{
Mesh& target = meshes[i];

if (!canDedupMesh(target))
if (!canDedupMesh(target, settings))
continue;

if (hashes[target.geometry_hash[0] ^ target.geometry_hash[1]] <= 1)
Expand All @@ -386,7 +390,7 @@ void dedupMeshes(std::vector<Mesh>& meshes)
if (mesh.geometry_hash[0] != target.geometry_hash[0] || mesh.geometry_hash[1] != target.geometry_hash[1])
continue;

if (!canDedupMesh(mesh))
if (!canDedupMesh(mesh, settings))
continue;

if (mesh.scene != target.scene || mesh.material != target.material || mesh.skin != target.skin)
Expand Down
1 change: 1 addition & 0 deletions gltf/parsegltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ static void parseMeshesGltf(cgltf_data* data, std::vector<Mesh>& meshes, std::ve
result.targets = primitive.targets_count;
result.target_weights.assign(mesh.weights, mesh.weights + mesh.weights_count);
result.target_names.assign(mesh.target_names, mesh.target_names + mesh.target_names_count);
result.name = mesh.name;

result.variants.assign(primitive.mappings, primitive.mappings + primitive.mappings_count);
}
Expand Down
4 changes: 3 additions & 1 deletion gltf/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ void writeExtras(std::string& json, const cgltf_extras& extras)
appendJson(json, extras.data);
}

void writeScene(std::string& json, const cgltf_scene& scene, const std::string& roots)
void writeScene(std::string& json, const cgltf_scene& scene, const std::string& roots, const Settings& settings)
{
comma(json);
append(json, "{");
Expand All @@ -1627,5 +1627,7 @@ void writeScene(std::string& json, const cgltf_scene& scene, const std::string&
append(json, roots);
append(json, "]");
}
if (settings.keep_extras)
writeExtras(json, scene.extras);
append(json, "}");
}

0 comments on commit ca2e3c5

Please sign in to comment.