Skip to content

Commit e48450e

Browse files
AssetLoader: expose reusable GLTF material loading
Move the material loading context and conversion helper into the public GLTF loader API so clients can build GLTF::Material objects without constructing a GLTF::Model.
1 parent a4a89af commit e48450e

4 files changed

Lines changed: 63 additions & 21 deletions

File tree

AssetLoader/interface/GLTFDocument.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class Document
138138
/// Returns the number of textures in the document.
139139
Uint32 GetTextureCount() const;
140140

141+
/// Returns the number of materials in the document.
142+
Uint32 GetMaterialCount() const;
143+
141144
/// Resolves a GLTF texture to either an external URI or an embedded encoded-data span.
142145
///
143146
/// Embedded buffer-view spans are owned by the document buffers. Embedded data URI

AssetLoader/interface/GLTFLoader.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ static constexpr std::array<TextureAttributeDesc, 17> DefaultTextureAttributes =
149149
};
150150
// clang-format on
151151

152+
/// Material loading context.
153+
struct MaterialLoadContext
154+
{
155+
const TextureAttributeDesc* TextureAttributes = DefaultTextureAttributes.data();
156+
Uint32 NumTextureAttributes = static_cast<Uint32>(DefaultTextureAttributes.size());
157+
158+
const TextureAttributeDesc& GetTextureAttribute(size_t Idx) const;
159+
160+
/// Returns the material texture attribute index in Material.ShaderAttribs for
161+
/// the given texture attribute name, or -1 if the attribute is not defined.
162+
int GetTextureAttributeIndex(const char* Name) const;
163+
};
164+
152165

153166
struct Material
154167
{
@@ -410,6 +423,16 @@ struct Material
410423
}
411424
};
412425

426+
/// Converts a tinygltf material to GLTF::Material.
427+
Material LoadMaterial(const tinygltf::Model& GltfModel,
428+
const tinygltf::Material& GltfMaterial,
429+
const MaterialLoadContext& LoadCtx = {});
430+
431+
/// Loads a material by index from a GLTF document.
432+
Material LoadMaterial(const Document& GltfDoc,
433+
Uint32 MaterialIndex,
434+
const MaterialLoadContext& LoadCtx = {});
435+
413436

414437
struct Primitive
415438
{

AssetLoader/src/GLTFDocument.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ Uint32 Document::GetTextureCount() const
130130
return static_cast<Uint32>(gltf_model.textures.size());
131131
}
132132

133+
Uint32 Document::GetMaterialCount() const
134+
{
135+
const tinygltf::Model& gltf_model = GetModel();
136+
DEV_CHECK_ERR(gltf_model.materials.size() <= (std::numeric_limits<Uint32>::max)(),
137+
"Too many materials in GLTF document");
138+
return static_cast<Uint32>(gltf_model.materials.size());
139+
}
140+
133141
bool Document::GetTextureSourceInfo(Uint32 TextureIndex, TextureSourceInfo& Source) const
134142
{
135143
const tinygltf::Model& gltf_model = GetModel();

AssetLoader/src/GLTFLoader.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,18 @@ static int GetTextureAttributeIndexFromArray(const TextureAttributeDesc* pTextur
318318

319319
} // namespace
320320

321+
const TextureAttributeDesc& MaterialLoadContext::GetTextureAttribute(size_t Idx) const
322+
{
323+
VERIFY_EXPR(TextureAttributes != nullptr);
324+
VERIFY_EXPR(Idx < NumTextureAttributes);
325+
return TextureAttributes[Idx];
326+
}
327+
328+
int MaterialLoadContext::GetTextureAttributeIndex(const char* Name) const
329+
{
330+
return GetTextureAttributeIndexFromArray(TextureAttributes, NumTextureAttributes, Name);
331+
}
332+
321333
Model::Model(const ModelCreateInfo& CI)
322334
{
323335
DEV_CHECK_ERR(CI.IndexType == VT_UINT16 || CI.IndexType == VT_UINT32, "Invalid index type");
@@ -1240,24 +1252,6 @@ static void SetMaterialTextureSamplerProps(const tinygltf::Model& gltf_model, in
12401252
Attribs.SetWrapVMode(GltfWrapModeToAddressMode(gltf_sampler.wrapT));
12411253
}
12421254

1243-
struct MaterialLoadContext
1244-
{
1245-
const TextureAttributeDesc* TextureAttributes = nullptr;
1246-
Uint32 NumTextureAttributes = 0;
1247-
1248-
const TextureAttributeDesc& GetTextureAttribute(size_t Idx) const
1249-
{
1250-
VERIFY_EXPR(TextureAttributes != nullptr);
1251-
VERIFY_EXPR(Idx < NumTextureAttributes);
1252-
return TextureAttributes[Idx];
1253-
}
1254-
1255-
int GetTextureAttributeIndex(const char* Name) const
1256-
{
1257-
return GetTextureAttributeIndexFromArray(TextureAttributes, NumTextureAttributes, Name);
1258-
}
1259-
};
1260-
12611255
static void ReadKhrTextureTransform(const MaterialLoadContext& LoadCtx,
12621256
const tinygltf::ExtensionMap& Extensions,
12631257
MaterialBuilder& Mat,
@@ -1405,9 +1399,9 @@ static void LoadExtensionParameter(const tinygltf::Value& Ext, const char* Name,
14051399
}
14061400
}
14071401

1408-
static Material LoadMaterial(const tinygltf::Model& gltf_model,
1409-
const tinygltf::Material& gltf_mat,
1410-
const MaterialLoadContext& LoadCtx)
1402+
Material LoadMaterial(const tinygltf::Model& gltf_model,
1403+
const tinygltf::Material& gltf_mat,
1404+
const MaterialLoadContext& LoadCtx)
14111405
{
14121406
Material Mat;
14131407
MaterialBuilder MatBuilder{Mat};
@@ -1655,6 +1649,20 @@ static Material LoadMaterial(const tinygltf::Model& gltf_model,
16551649
return Mat;
16561650
}
16571651

1652+
Material LoadMaterial(const Document& GltfDoc,
1653+
Uint32 MaterialIndex,
1654+
const MaterialLoadContext& LoadCtx)
1655+
{
1656+
const tinygltf::Model& gltf_model = GltfDoc.GetModel();
1657+
if (MaterialIndex >= gltf_model.materials.size())
1658+
{
1659+
UNEXPECTED("Material index is out of range");
1660+
return {};
1661+
}
1662+
1663+
return LoadMaterial(gltf_model, gltf_model.materials[MaterialIndex], LoadCtx);
1664+
}
1665+
16581666
void Model::LoadMaterials(const tinygltf::Model& gltf_model, const ModelCreateInfo::MaterialLoadCallbackType& MaterialLoadCallback)
16591667
{
16601668
const MaterialLoadContext LoadCtx{

0 commit comments

Comments
 (0)