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

--Set default fallback material handling #2265

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/esp/assets/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,8 @@ Mn::Trade::MaterialData ResourceManager::setMaterialDefaultUserAttributes(
ObjectInstanceShaderType shaderTypeToUse,
bool hasVertObjID,
bool hasTxtrObjID,
int txtrIdx) const {
int txtrIdx,
bool isFallback) const {
// New material's attributes
Cr::Containers::Array<Mn::Trade::MaterialAttributeData> newAttributes;
arrayAppend(newAttributes, Cr::InPlaceInit, "hasPerVertexObjectId",
Expand All @@ -2150,6 +2151,8 @@ Mn::Trade::MaterialData ResourceManager::setMaterialDefaultUserAttributes(
arrayAppend(newAttributes, Cr::InPlaceInit, "shaderTypeToUse",
static_cast<int>(shaderTypeToUse));

arrayAppend(newAttributes, Cr::InPlaceInit, "isFallbackMaterial", isFallback);

Cr::Containers::Optional<Mn::Trade::MaterialData> finalMaterial =
Mn::MaterialTools::merge(
material, Mn::Trade::MaterialData{{}, std::move(newAttributes), {}});
Expand Down Expand Up @@ -2250,9 +2253,20 @@ void ResourceManager::initDefaultMaterials() {

// Build default material for fallback material
Mn::Trade::MaterialData fallbackMaterial = buildDefaultMaterial();
// Set expected user-defined attributes
// Make fallback magenta so visibly obvious
fallbackMaterial.mutableAttribute<Mn::Color4>(
Mn::Trade::MaterialAttribute::AmbientColor) =
Mn::Color4{1.0f, 0.0f, 1.0f, 1.0f};
fallbackMaterial.mutableAttribute<Mn::Color4>(
Mn::Trade::MaterialAttribute::DiffuseColor) =
Mn::Color4{1.0f, 0.0f, 1.0f, 1.0f};
fallbackMaterial.mutableAttribute<Mn::Color4>(
Mn::Trade::MaterialAttribute::SpecularColor) =
Mn::Color4{1.0f, 0.0f, 1.0f, 1.0f};
// Set expected user-defined attributes - specify Flat shader to use since
// this indicates that the asset did not load a material for some reason.
fallbackMaterial = setMaterialDefaultUserAttributes(
fallbackMaterial, ObjectInstanceShaderType::Flat);
fallbackMaterial, ObjectInstanceShaderType::Flat, false, false, -1, true);
// Add to shaderManager as fallback material
shaderManager_.setFallback<Mn::Trade::MaterialData>(
std::move(fallbackMaterial));
Expand Down
5 changes: 4 additions & 1 deletion src/esp/assets/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -751,14 +751,17 @@ class ResourceManager {
* ids for semantics.
* @param txtrIdx The absolute index in the @ref textures_ store for the semantic
* annotation texture.
* @param isFallback This material is default fallback material for missing
* materials.
* @return the updated material
*/
Mn::Trade::MaterialData setMaterialDefaultUserAttributes(
const Mn::Trade::MaterialData& material,
ObjectInstanceShaderType shaderTypeToUse,
bool hasVertObjID = false,
bool hasTxtrObjID = false,
int txtrIdx = -1) const;
int txtrIdx = -1,
bool isFallback = false) const;

/**
* @brief Configure the importerManager_ GL Extensions appropriately based on
Expand Down
23 changes: 23 additions & 0 deletions src/esp/gfx/Drawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ class Drawable : public Magnum::SceneGraph::Drawable3D {
setMaterialValuesInternal(material, true);
}

/**
* @brief Whether this drawable has a specified material or was assigned the
* default fallback material, meaning the material for the object was either
* not loaded or is otherwise missing.
*/
bool getUsesFallbackMaterial() const { return usingFallbackMaterial_; }

private:
/**
* Set or change this drawable's @ref Magnum::Trade::MaterialData values from passed material.
Expand All @@ -171,6 +178,15 @@ class Drawable : public Magnum::SceneGraph::Drawable3D {
CORRADE_UNUSED bool reset) {}

protected:
/**
* @brief Whether this drawable has a specified material or was assigned the
* default fallback material, meaning the material for the object was either
* not loaded or is otherwise missing.
*/
void setUsesFallbackMaterial(bool _usingFallbackMaterial) {
usingFallbackMaterial_ = _usingFallbackMaterial;
}

/**
* @brief resize the jointTransformArray_
*/
Expand Down Expand Up @@ -253,6 +269,13 @@ class Drawable : public Magnum::SceneGraph::Drawable3D {

Corrade::Containers::Array<Magnum::Matrix4> jointTransformations_;

/**
* @brief Whether or not this drawable is being rendered using the default
* fallback material, which implies either that materials were not loaded, or
* else no material existed for the source asset to load.
*/
bool usingFallbackMaterial_ = false;

bool glMeshExists() const { return mesh_ != nullptr; }

private:
Expand Down
6 changes: 6 additions & 0 deletions src/esp/gfx/GenericDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ void GenericDrawable::setMaterialValuesInternal(
if (meshAttributeFlags_ & Drawable::Flag::HasVertexColor) {
flags_ |= Mn::Shaders::PhongGL::Flag::VertexColor;
}

// Set whether this drawable is being rendered using a fallback material; This
// would infer that either materials were not loaded or no materials were
// found for the source asset
setUsesFallbackMaterial(materialData->attribute<bool>("isFallbackMaterial"));

// If not reset then make sure the same shader is used
if (!reset) {
flags_ = oldFlags;
Expand Down
4 changes: 4 additions & 0 deletions src/esp/gfx/PbrDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ void PbrDrawable::setMaterialValuesInternal(
if (meshAttributeFlags_ & Drawable::Flag::HasVertexColor) {
flags_ |= PbrShader::Flag::VertexColor;
}
// Set whether this drawable is being rendered using a fallback material; This
// would infer that either materials were not loaded or no materials were
// found for the source asset
setUsesFallbackMaterial(materialData->attribute<bool>("isFallbackMaterial"));

// Skin support
(skinData_ != nullptr) ? flags_ |= PbrShader::Flag::SkinnedMesh
Expand Down