chore: dump more information about gltf materials

This commit is contained in:
Jan 2024-05-04 21:58:18 +02:00
parent ebccd67676
commit 1a35152098
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
2 changed files with 90 additions and 2 deletions

View File

@ -194,13 +194,40 @@ namespace gltf
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAnimation, channels, samplers, name); NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAnimation, channels, samplers, name);
class JsonTextureInfo
{
public:
unsigned index;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonTextureInfo, index);
class JsonPbrMetallicRoughness
{
public:
std::optional<JsonTextureInfo> baseColorTexture;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonPbrMetallicRoughness, baseColorTexture);
class JsonNormalTextureInfo
{
public:
unsigned index;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonNormalTextureInfo, index);
class JsonMaterial class JsonMaterial
{ {
public: public:
std::optional<std::string> name; std::optional<std::string> name;
std::optional<JsonPbrMetallicRoughness> pbrMetallicRoughness;
std::optional<JsonNormalTextureInfo> normalTexture;
std::optional<bool> doubleSided;
}; };
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonMaterial, name); NLOHMANN_DEFINE_TYPE_EXTENSION(JsonMaterial, name, pbrMetallicRoughness, normalTexture, doubleSided);
enum class JsonMeshPrimitivesMode enum class JsonMeshPrimitivesMode
{ {
@ -276,6 +303,22 @@ namespace gltf
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonScene, nodes, name); NLOHMANN_DEFINE_TYPE_EXTENSION(JsonScene, nodes, name);
class JsonTexture
{
public:
unsigned source;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonTexture, source);
class JsonImage
{
public:
std::optional<std::string> uri;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonImage, uri);
class JsonRoot class JsonRoot
{ {
public: public:
@ -284,13 +327,16 @@ namespace gltf
JsonAsset asset; JsonAsset asset;
std::optional<std::vector<JsonBuffer>> buffers; std::optional<std::vector<JsonBuffer>> buffers;
std::optional<std::vector<JsonBufferView>> bufferViews; std::optional<std::vector<JsonBufferView>> bufferViews;
std::optional<std::vector<JsonImage>> images;
std::optional<std::vector<JsonMaterial>> materials; std::optional<std::vector<JsonMaterial>> materials;
std::optional<std::vector<JsonMesh>> meshes; std::optional<std::vector<JsonMesh>> meshes;
std::optional<std::vector<JsonNode>> nodes; std::optional<std::vector<JsonNode>> nodes;
std::optional<std::vector<JsonSkin>> skins; std::optional<std::vector<JsonSkin>> skins;
std::optional<unsigned> scene; std::optional<unsigned> scene;
std::optional<std::vector<JsonScene>> scenes; std::optional<std::vector<JsonScene>> scenes;
std::optional<std::vector<JsonTexture>> textures;
}; };
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonRoot, accessors, animations, asset, buffers, bufferViews, materials, meshes, nodes, skins, scene, scenes); NLOHMANN_DEFINE_TYPE_EXTENSION(
JsonRoot, accessors, animations, asset, buffers, bufferViews, images, materials, meshes, nodes, skins, scene, scenes, textures);
} // namespace gltf } // namespace gltf

View File

@ -4,6 +4,8 @@
#include "XModel/Gltf/GltfConstants.h" #include "XModel/Gltf/GltfConstants.h"
#include "XModel/Gltf/JsonGltf.h" #include "XModel/Gltf/JsonGltf.h"
#include <format>
using namespace gltf; using namespace gltf;
using namespace nlohmann; using namespace nlohmann;
@ -124,10 +126,50 @@ namespace
material.name = modelMaterial.name; material.name = modelMaterial.name;
if (!modelMaterial.colorMapName.empty())
material.pbrMetallicRoughness.emplace().baseColorTexture.emplace().index = CreateTexture(gltf, modelMaterial.colorMapName);
if (!modelMaterial.normalMapName.empty())
material.normalTexture.emplace().index = CreateTexture(gltf, modelMaterial.colorMapName);
material.doubleSided = true;
gltf.materials->emplace_back(material); gltf.materials->emplace_back(material);
} }
} }
static unsigned CreateTexture(JsonRoot& gltf, const std::string& textureName)
{
if (!gltf.textures.has_value())
gltf.textures.emplace();
if (!gltf.images.has_value())
gltf.images.emplace();
auto uri = std::format("../images/{}.dds", textureName);
auto existingTexIndex = 0u;
for (const auto& existingTex : gltf.textures.value())
{
const auto& existingImage = gltf.images.value()[existingTex.source];
if (existingImage.uri == uri)
return existingTexIndex;
existingTexIndex++;
}
JsonImage image;
image.uri = std::move(uri);
const auto imageIndex = gltf.images->size();
gltf.images->emplace_back(std::move(image));
JsonTexture texture;
texture.source = imageIndex;
const auto textureIndex = gltf.textures->size();
gltf.textures->emplace_back(texture);
return textureIndex;
}
static void CreateSkeletonNodes(JsonRoot& gltf, const XModelCommon& xmodel) static void CreateSkeletonNodes(JsonRoot& gltf, const XModelCommon& xmodel)
{ {
if (xmodel.m_bones.empty()) if (xmodel.m_bones.empty())