Skip to content

Commit

Permalink
gltfpack: Rework -kn and -km to explicitly assume naming
Browse files Browse the repository at this point in the history
The behavior of -kn is still the same as it used to be - it keeps named
nodes and maintains the transform effects of these nodes on the rest of
the scene, including prohibiting mesh merging for attached meshes. The
setting was renamed to keep_nodes to reflect that better.

The behavior of -km is now matching that for materials - it keeps named
materials, prevents merging them with other materials and keeps them in
the file.
  • Loading branch information
zeux committed May 14, 2020
1 parent 17d9093 commit fc4bfb1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
26 changes: 8 additions & 18 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static void process(cgltf_data* data, const char* input_path, const char* output
continue;

// note: when -kn is specified, we keep mesh-node attachment so that named nodes can be transformed
if (settings.keep_named)
if (settings.keep_nodes)
continue;

// we keep skinned meshes or meshes with morph targets as is
Expand Down Expand Up @@ -230,17 +230,7 @@ static void process(cgltf_data* data, const char* input_path, const char* output

std::vector<MaterialInfo> materials(data->materials_count);

if (settings.keep_materials)
{
for (size_t i = 0; i < materials.size(); ++i)
{
materials[i].keep = true;
}
}
else
{
markNeededMaterials(data, materials, meshes);
}
markNeededMaterials(data, materials, meshes, settings);

#ifndef NDEBUG
std::vector<Mesh> debug_meshes;
Expand Down Expand Up @@ -886,16 +876,16 @@ int main(int argc, char** argv)
}
else if (strcmp(arg, "-kn") == 0)
{
settings.keep_named = true;
}
else if (strcmp(arg, "-ke") == 0)
{
settings.keep_extras = true;
settings.keep_nodes = true;
}
else if (strcmp(arg, "-km") == 0)
{
settings.keep_materials = true;
}
else if (strcmp(arg, "-ke") == 0)
{
settings.keep_extras = true;
}
else if (strcmp(arg, "-mm") == 0)
{
settings.mesh_merge = true;
Expand Down Expand Up @@ -1054,8 +1044,8 @@ int main(int argc, char** argv)
fprintf(stderr, "\t-ac: keep constant animation tracks even if they don't modify the node transform\n");
fprintf(stderr, "\nScene:\n");
fprintf(stderr, "\t-kn: keep named nodes and meshes attached to named nodes so that named nodes can be transformed externally\n");
fprintf(stderr, "\t-km: keep named materials and disable named material merging\n");
fprintf(stderr, "\t-ke: keep extras data\n");
fprintf(stderr, "\t-km: keep unused materials\n");
fprintf(stderr, "\t-mm: merge instances of the same mesh together when possible\n");
fprintf(stderr, "\t-mi: use EXT_mesh_gpu_instancing when serializing multiple mesh instances\n");
fprintf(stderr, "\nMiscellaneous:\n");
Expand Down
6 changes: 3 additions & 3 deletions gltf/gltfpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ struct Settings
int anim_freq;
bool anim_const;

bool keep_named;
bool keep_extras;
bool keep_nodes;
bool keep_materials;
bool keep_extras;

bool mesh_merge;
bool mesh_instancing;
Expand Down Expand Up @@ -249,7 +249,7 @@ void filterEmptyMeshes(std::vector<Mesh>& meshes);

bool usesTextureSet(const cgltf_material& material, int set);
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);
void markNeededMaterials(cgltf_data* data, std::vector<MaterialInfo>& materials, const std::vector<Mesh>& meshes, const Settings& settings);

void analyzeImages(cgltf_data* data, std::vector<ImageInfo>& images);
const char* inferMimeType(const char* path);
Expand Down
22 changes: 21 additions & 1 deletion gltf/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ void mergeMeshMaterials(cgltf_data* data, std::vector<Mesh>& meshes, const Setti
if (!mesh.material)
continue;

if (settings.keep_materials && mesh.material->name && *mesh.material->name)
continue;

for (int j = 0; j < mesh.material - data->materials; ++j)
{
if (settings.keep_materials && data->materials[j].name && *data->materials[j].name)
continue;

if (areMaterialsEqual(data, *mesh.material, data->materials[j], settings))
{
mesh.material = &data->materials[j];
Expand All @@ -179,7 +185,7 @@ void mergeMeshMaterials(cgltf_data* data, std::vector<Mesh>& meshes, const Setti
}
}

void markNeededMaterials(cgltf_data* data, std::vector<MaterialInfo>& materials, const std::vector<Mesh>& meshes)
void markNeededMaterials(cgltf_data* data, std::vector<MaterialInfo>& materials, const std::vector<Mesh>& meshes, const Settings& settings)
{
// mark all used materials as kept
for (size_t i = 0; i < meshes.size(); ++i)
Expand All @@ -193,6 +199,20 @@ void markNeededMaterials(cgltf_data* data, std::vector<MaterialInfo>& materials,
mi.keep = true;
}
}

// mark all named materials as kept if requested
if (settings.keep_materials)
{
for (size_t i = 0; i < data->materials_count; ++i)
{
cgltf_material& material = data->materials[i];

if (material.name && *material.name)
{
materials[i].keep = true;
}
}
}
}

bool usesTextureSet(const cgltf_material& material, int set)
Expand Down
2 changes: 1 addition & 1 deletion gltf/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static bool canMergeMeshNodes(cgltf_node* lhs, cgltf_node* rhs, const Settings&
if (lhs_transform || rhs_transform)
return false;

if (settings.keep_named)
if (settings.keep_nodes)
{
if (lhs->name && *lhs->name)
return false;
Expand Down
2 changes: 1 addition & 1 deletion gltf/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void markNeededNodes(cgltf_data* data, std::vector<NodeInfo>& nodes, const std::
}

// mark all named nodes as needed (if -kn is specified)
if (settings.keep_named)
if (settings.keep_nodes)
{
for (size_t i = 0; i < data->nodes_count; ++i)
{
Expand Down

0 comments on commit fc4bfb1

Please sign in to comment.