Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generated classes for KHR_materials_variants #630

Merged
merged 2 commits into from
May 2, 2023

Conversation

javagl
Copy link
Contributor

@javagl javagl commented May 1, 2023

Add the information for the KHR_materials_variants extension to the tools/generate-classes/glTF.json configuration file, and re-generate the glTF classes.

The extension is located in the main glTF object, and in mesh.primitive objects. We can argue about the naming:

  • The first one is called ExtensionKhrMaterialsVariants
  • The second one is called ExtensionKhrMaterialsVariantsMeshPrimitive (quite a mouthful...)

The resulting structures can be used as follows:

Updated based on review comments

void printMaterialVariantsInfo(const CesiumGltf::Model &model) {

  SPDLOG_INFO("Material variants info");

  bool isPresentOnGltf =
      model.hasExtension<CesiumGltf::ExtensionModelKhrMaterialsVariants>();
  SPDLOG_INFO("  Extension present on glTF? {}", isPresentOnGltf);
  if (!isPresentOnGltf) {
    return;
  }
  const CesiumGltf::ExtensionModelKhrMaterialsVariants *materialVariantsGltf =
      model.getExtension<CesiumGltf::ExtensionModelKhrMaterialsVariants>();
  const std::vector<CesiumGltf::ExtensionModelKhrMaterialsVariantsValue>
      variants = materialVariantsGltf->variants;
  for (const CesiumGltf::ExtensionModelKhrMaterialsVariantsValue &variant :
       variants) {
    SPDLOG_INFO("    Variant name: {}", variant.name);
  }

  const std::vector<CesiumGltf::Mesh> meshes = model.meshes;
  for (size_t m = 0; m < meshes.size(); m++) {
    const CesiumGltf::Mesh &mesh = meshes[m];
    const std::vector<CesiumGltf::MeshPrimitive> meshPrimitives =
        mesh.primitives;
    for (size_t p = 0; p < meshPrimitives.size(); p++) {
      const CesiumGltf::MeshPrimitive &meshPrimitive = meshPrimitives[p];
      bool isPresentOnMeshPrimitive = meshPrimitive.hasExtension<
          CesiumGltf::ExtensionMeshPrimitiveKhrMaterialsVariants>();
      SPDLOG_INFO("  Extension present on mesh primitive {} of mesh {}? {}", p,
                  m, isPresentOnMeshPrimitive);
      if (!isPresentOnMeshPrimitive) {
        continue;
      }
      const CesiumGltf::ExtensionMeshPrimitiveKhrMaterialsVariants
          *materialVariants = meshPrimitive.getExtension<
              CesiumGltf::ExtensionMeshPrimitiveKhrMaterialsVariants>();
      const std::vector<
          CesiumGltf::ExtensionMeshPrimitiveKhrMaterialsVariantsMappingsValue>
          mappings = materialVariants->mappings;
      for (const CesiumGltf::
               ExtensionMeshPrimitiveKhrMaterialsVariantsMappingsValue
                   &mapping : mappings) {
        SPDLOG_INFO("    Mapping:");

        SPDLOG_INFO("      Variant:");
        const std::vector<int32_t> mappingVariants = mapping.variants;
        for (const int32_t mappingVariant : mappingVariants) {
          SPDLOG_INFO("        {}", mappingVariant);
        }
        SPDLOG_INFO("      Material: {}", mapping.material);
        SPDLOG_INFO("      Name: {}", mapping.name.value_or("(none)"));
      }
    }
  }
}

(This code is taken from https://github.com/javagl/cesium-cpp/tree/materials-variants-test )

Comment on lines 165 to 170
"KHR_materials_variants glTF extension": {
"overrideName": "ExtensionKhrMaterialsVariants"
},
"KHR_materials_variants mesh primitive extension": {
"overrideName": "ExtensionKhrMaterialsVariantsMeshPrimitive"
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an extension has multiple attachment points like KHR_materials_variants does, the naming convention is Extension${AttachmentName}${ExtensionName} (see EXT_structural_metadata for example):

  • ExtensionKhrMaterialsVariants should be ExtensionModelKhrMaterialsVariants
  • ExtensionKhrMaterialsVariantsMeshPrimitive should be ExtensionMeshPrimitiveKhrMaterialsVariants.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have noticed that. The code (and the example code) have been updated.

@kring
Copy link
Member

kring commented May 2, 2023

This looks good to me, thanks @javagl! @lilleyse since you did some reviewing, I'll let you hit merge when you're happy.

@lilleyse lilleyse merged commit 15955bc into main May 2, 2023
@lilleyse lilleyse deleted the generate-material-variants branch May 2, 2023 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants