Merge pull request #2752 from greenrazer/ExposeLodControls

Expose lod controls
This commit is contained in:
Guthrie Adams
2021-08-24 10:41:24 -05:00
committed by GitHub
13 changed files with 313 additions and 69 deletions
@@ -71,16 +71,36 @@ namespace AZ
"MeshComponentConfig", "")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentConfig::m_modelAsset, "Mesh Asset", "Mesh asset reference")
->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentConfig::m_sortKey, "Sort Key", "Transparent meshes are drawn by sort key then depth. Used this to force certain transparent meshes to draw before or after others.")
->DataElement(AZ::Edit::UIHandlers::ComboBox, &MeshComponentConfig::m_lodOverride, "Lod Override", "Allows the rendered LOD to be overridden instead of being calculated automatically.")
->Attribute(AZ::Edit::Attributes::EnumValues, &MeshComponentConfig::GetLodOverrideValues)
->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::IsAssetSet)
->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_excludeFromReflectionCubeMaps, "Exclude from reflection cubemaps", "Mesh will not be visible in baked reflection probe cubemaps")
->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_useForwardPassIblSpecular, "Use Forward Pass IBL Specular",
"Renders IBL specular reflections in the forward pass, using only the most influential probe (based on the position of the entity) and the global IBL cubemap. Can reduce rendering costs, but only recommended for static objects that are affected by at most one reflection probe.")
->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentConfig::m_modelAsset, "Mesh Asset", "Mesh asset reference")
->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentConfig::m_sortKey, "Sort Key", "Transparent meshes are drawn by sort key then depth. Used this to force certain transparent meshes to draw before or after others.")
->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::IsAssetSet)
->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_excludeFromReflectionCubeMaps, "Exclude from reflection cubemaps", "Mesh will not be visible in baked reflection probe cubemaps")
->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_useForwardPassIblSpecular, "Use Forward Pass IBL Specular",
"Renders IBL specular reflections in the forward pass, using only the most influential probe (based on the position of the entity) and the global IBL cubemap. Can reduce rendering costs, but only recommended for static objects that are affected by at most one reflection probe.")
->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
->DataElement(AZ::Edit::UIHandlers::ComboBox, &MeshComponentConfig::m_lodType, "Lod Type", "Lod Method.")
->EnumAttribute(RPI::Cullable::LodType::Default, "Default")
->EnumAttribute(RPI::Cullable::LodType::ScreenCoverage, "Screen Coverage")
->EnumAttribute(RPI::Cullable::LodType::SpecificLod, "Specific Lod")
->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::IsAssetSet)
->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::EntireTree)
->ClassElement(AZ::Edit::ClassElements::Group, "Lod Configuration")
->Attribute(AZ::Edit::Attributes::AutoExpand, false)
->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::ShowLodConfig)
->DataElement(AZ::Edit::UIHandlers::ComboBox, &MeshComponentConfig::m_lodOverride, "Lod Override", "Allows the rendered LOD to be overridden instead of being calculated automatically.")
->Attribute(AZ::Edit::Attributes::EnumValues, &MeshComponentConfig::GetLodOverrideValues)
->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::LodTypeIsSpecificLOD)
->DataElement(AZ::Edit::UIHandlers::Slider, &MeshComponentConfig::m_minimumScreenCoverage, "Minimum Screen Coverage", "Minimum proportion of screen area an entitiy takes up, after that the entitiy is culled.")
->Attribute(AZ::Edit::Attributes::Min, 0.f)
->Attribute(AZ::Edit::Attributes::Max, 1.f)
->Attribute(AZ::Edit::Attributes::Suffix, " percent")
->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::LodTypeIsScreenCoverage)
->DataElement(AZ::Edit::UIHandlers::Slider, &MeshComponentConfig::m_qualityDecayRate, "Quality Decay Rate",
"Rate at which mesh quality decays (0 -> always stay highest quality, 1 -> quality falls off to lowest quality immediately).")
->Attribute(AZ::Edit::Attributes::Min, 0.f)
->Attribute(AZ::Edit::Attributes::Max, 1.f)
->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::LodTypeIsScreenCoverage)
;
}
}
@@ -31,18 +31,40 @@ namespace AZ
{
namespace Render
{
namespace MeshComponentControllerVersionUtility
{
bool VersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
{
if (classElement.GetVersion() < 2)
{
RPI::Cullable::LodOverride lodOverride = aznumeric_cast<RPI::Cullable::LodOverride>(classElement.FindElement(AZ_CRC("LodOverride")));
static constexpr uint8_t old_NoLodOverride = AZStd::numeric_limits <RPI::Cullable::LodOverride>::max();
if (lodOverride == old_NoLodOverride)
{
classElement.AddElementWithData(context, "LodType", RPI::Cullable::LodType::SpecificLod);
}
}
return true;
}
} // namespace MeshComponentControllerVersionUtility
void MeshComponentConfig::Reflect(ReflectContext* context)
{
if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
{
serializeContext->Class<MeshComponentConfig>()
->Version(1)
->Version(2, &MeshComponentControllerVersionUtility::VersionConverter)
->Field("ModelAsset", &MeshComponentConfig::m_modelAsset)
->Field("SortKey", &MeshComponentConfig::m_sortKey)
->Field("LodOverride", &MeshComponentConfig::m_lodOverride)
->Field("ExcludeFromReflectionCubeMaps", &MeshComponentConfig::m_excludeFromReflectionCubeMaps)
->Field("UseForwardPassIBLSpecular", &MeshComponentConfig::m_useForwardPassIblSpecular);
->Field("UseForwardPassIBLSpecular", &MeshComponentConfig::m_useForwardPassIblSpecular)
->Field("LodType", &MeshComponentConfig::m_lodType)
->Field("LodOverride", &MeshComponentConfig::m_lodOverride)
->Field("MinimumScreenCoverage", &MeshComponentConfig::m_minimumScreenCoverage)
->Field("QualityDecayRate", &MeshComponentConfig::m_qualityDecayRate);
}
}
bool MeshComponentConfig::IsAssetSet()
@@ -50,6 +72,21 @@ namespace AZ
return m_modelAsset.GetId().IsValid();
}
bool MeshComponentConfig::LodTypeIsScreenCoverage()
{
return m_lodType == RPI::Cullable::LodType::ScreenCoverage;
}
bool MeshComponentConfig::LodTypeIsSpecificLOD()
{
return m_lodType == RPI::Cullable::LodType::SpecificLod;
}
bool MeshComponentConfig::ShowLodConfig()
{
return LodTypeIsScreenCoverage() && LodTypeIsSpecificLOD();
}
AZStd::vector<AZStd::pair<RPI::Cullable::LodOverride, AZStd::string>> MeshComponentConfig::GetLodOverrideValues()
{
AZStd::vector<AZStd::pair<RPI::Cullable::LodOverride, AZStd::string>> values;
@@ -72,9 +109,9 @@ namespace AZ
}
values.reserve(lodCount + 1);
values.push_back({ RPI::Cullable::NoLodOverride, "Not Set" });
values.push_back({ aznumeric_cast<RPI::Cullable::LodOverride>(0), "Default (Highest)" });
for (uint32_t i = 0; i < lodCount; ++i)
for (uint32_t i = 1; i < lodCount; ++i)
{
AZStd::string enumDescription = AZStd::string::format("Lod %i", i);
values.push_back({ aznumeric_cast<RPI::Cullable::LodOverride>(i), enumDescription.c_str() });
@@ -102,7 +139,12 @@ namespace AZ
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->ConstantProperty("NoLodOverride", BehaviorConstant(RPI::Cullable::NoLodOverride))
behaviorContext->ConstantProperty("DefaultLodOverride", BehaviorConstant(0))
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
->Attribute(AZ::Script::Attributes::Category, "render")
->Attribute(AZ::Script::Attributes::Module, "render");
behaviorContext->ConstantProperty("DefaultLodType", BehaviorConstant(RPI::Cullable::LodType::Default))
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
->Attribute(AZ::Script::Attributes::Category, "render")
->Attribute(AZ::Script::Attributes::Module, "render");
@@ -114,12 +156,21 @@ namespace AZ
->Event("SetModelAssetPath", &MeshComponentRequestBus::Events::SetModelAssetPath)
->Event("SetSortKey", &MeshComponentRequestBus::Events::SetSortKey)
->Event("GetSortKey", &MeshComponentRequestBus::Events::GetSortKey)
->Event("SetLodType", &MeshComponentRequestBus::Events::SetLodType)
->Event("GetLodType", &MeshComponentRequestBus::Events::GetLodType)
->Event("SetLodOverride", &MeshComponentRequestBus::Events::SetLodOverride)
->Event("GetLodOverride", &MeshComponentRequestBus::Events::GetLodOverride)
->Event("SetMinimumScreenCoverage", &MeshComponentRequestBus::Events::SetMinimumScreenCoverage)
->Event("GetMinimumScreenCoverage", &MeshComponentRequestBus::Events::GetMinimumScreenCoverage)
->Event("SetQualityDecayRate", &MeshComponentRequestBus::Events::SetQualityDecayRate)
->Event("GetQualityDecayRate", &MeshComponentRequestBus::Events::GetQualityDecayRate)
->VirtualProperty("ModelAssetId", "GetModelAssetId", "SetModelAssetId")
->VirtualProperty("ModelAssetPath", "GetModelAssetPath", "SetModelAssetPath")
->VirtualProperty("SortKey", "GetSortKey", "SetSortKey")
->VirtualProperty("LodType", "GetLodType", "SetLodType")
->VirtualProperty("LodOverride", "GetLodOverride", "SetLodOverride")
->VirtualProperty("MinimumScreenCoverage", "GetMinimumScreenCoverage", "SetMinimumScreenCoverage")
->VirtualProperty("QualityDecayRate", "GetQualityDecayRate", "SetQualityDecayRate")
;
}
}
@@ -343,7 +394,7 @@ namespace AZ
m_meshFeatureProcessor->SetTransform(m_meshHandle, transform, m_cachedNonUniformScale);
m_meshFeatureProcessor->SetSortKey(m_meshHandle, m_configuration.m_sortKey);
m_meshFeatureProcessor->SetLodOverride(m_meshHandle, m_configuration.m_lodOverride);
m_meshFeatureProcessor->SetMeshLodConfiguration(m_meshHandle, GetMeshLodConfiguration());
m_meshFeatureProcessor->SetExcludeFromReflectionCubeMaps(m_meshHandle, m_configuration.m_excludeFromReflectionCubeMaps);
m_meshFeatureProcessor->SetVisible(m_meshHandle, m_isVisible);
@@ -434,15 +485,66 @@ namespace AZ
return m_meshFeatureProcessor->GetSortKey(m_meshHandle);
}
RPI::Cullable::LodConfiguration MeshComponentController::GetMeshLodConfiguration() const
{
return {
m_configuration.m_lodType,
m_configuration.m_lodOverride,
m_configuration.m_minimumScreenCoverage,
m_configuration.m_qualityDecayRate
};
}
// -----------------------
void MeshComponentController::SetLodType(RPI::Cullable::LodType lodType)
{
RPI::Cullable::LodConfiguration lodConfig = GetMeshLodConfiguration();
lodConfig.m_lodType = lodType;
m_meshFeatureProcessor->SetMeshLodConfiguration(m_meshHandle, lodConfig);
}
RPI::Cullable::LodType MeshComponentController::GetLodType() const
{
RPI::Cullable::LodConfiguration lodConfig = m_meshFeatureProcessor->GetMeshLodConfiguration(m_meshHandle);
return lodConfig.m_lodType;
}
void MeshComponentController::SetLodOverride(RPI::Cullable::LodOverride lodOverride)
{
m_configuration.m_lodOverride = lodOverride; // Save for serialization
m_meshFeatureProcessor->SetLodOverride(m_meshHandle, lodOverride);
RPI::Cullable::LodConfiguration lodConfig = GetMeshLodConfiguration();
lodConfig.m_lodOverride = lodOverride;
m_meshFeatureProcessor->SetMeshLodConfiguration(m_meshHandle, lodConfig);
}
RPI::Cullable::LodOverride MeshComponentController::GetLodOverride() const
{
return static_cast<RPI::Cullable::LodOverride>(m_meshFeatureProcessor->GetSortKey(m_meshHandle));
RPI::Cullable::LodConfiguration lodConfig = m_meshFeatureProcessor->GetMeshLodConfiguration(m_meshHandle);
return lodConfig.m_lodOverride;
}
void MeshComponentController::SetMinimumScreenCoverage(float minimumScreenCoverage)
{
RPI::Cullable::LodConfiguration lodConfig = GetMeshLodConfiguration();
lodConfig.m_minimumScreenCoverage = minimumScreenCoverage;
m_meshFeatureProcessor->SetMeshLodConfiguration(m_meshHandle, lodConfig);
}
float MeshComponentController::GetMinimumScreenCoverage() const
{
RPI::Cullable::LodConfiguration lodConfig = m_meshFeatureProcessor->GetMeshLodConfiguration(m_meshHandle);
return lodConfig.m_minimumScreenCoverage;
}
void MeshComponentController::SetQualityDecayRate(float qualityDecayRate)
{
RPI::Cullable::LodConfiguration lodConfig = GetMeshLodConfiguration();
lodConfig.m_qualityDecayRate = qualityDecayRate;
m_meshFeatureProcessor->SetMeshLodConfiguration(m_meshHandle, lodConfig);
}
float MeshComponentController::GetQualityDecayRate() const
{
RPI::Cullable::LodConfiguration lodConfig = m_meshFeatureProcessor->GetMeshLodConfiguration(m_meshHandle);
return lodConfig.m_qualityDecayRate;
}
void MeshComponentController::SetVisibility(bool visible)
@@ -30,6 +30,9 @@ namespace AZ
{
namespace Render
{
//! A configuration structure for the MeshComponentController
class MeshComponentConfig final
: public AZ::ComponentConfig
@@ -41,13 +44,20 @@ namespace AZ
// Editor helper functions
bool IsAssetSet();
bool LodTypeIsScreenCoverage();
bool LodTypeIsSpecificLOD();
bool ShowLodConfig();
AZStd::vector<AZStd::pair<RPI::Cullable::LodOverride, AZStd::string>> GetLodOverrideValues();
Data::Asset<RPI::ModelAsset> m_modelAsset = { AZ::Data::AssetLoadBehavior::QueueLoad };
RHI::DrawItemSortKey m_sortKey = 0;
RPI::Cullable::LodOverride m_lodOverride = RPI::Cullable::NoLodOverride;
bool m_excludeFromReflectionCubeMaps = false;
bool m_useForwardPassIblSpecular = false;
RPI::Cullable::LodType m_lodType = RPI::Cullable::LodType::Default;
RPI::Cullable::LodOverride m_lodOverride = aznumeric_cast<RPI::Cullable::LodOverride>(0);
float m_minimumScreenCoverage = 1.0f / 1080.0f;
float m_qualityDecayRate = 0.5f;
};
class MeshComponentController final
@@ -94,8 +104,17 @@ namespace AZ
void SetSortKey(RHI::DrawItemSortKey sortKey) override;
RHI::DrawItemSortKey GetSortKey() const override;
void SetLodOverride(RPI::Cullable::LodOverride lodOverride) override;
RPI::Cullable::LodOverride GetLodOverride() const override;
void SetLodType(RPI::Cullable::LodType lodType) override;
RPI::Cullable::LodType GetLodType() const override;
virtual void SetLodOverride(RPI::Cullable::LodOverride lodOverride);
virtual RPI::Cullable::LodOverride GetLodOverride() const;
virtual void SetMinimumScreenCoverage(float minimumScreenCoverage);
virtual float GetMinimumScreenCoverage() const;
virtual void SetQualityDecayRate(float qualityDecayRate);
virtual float GetQualityDecayRate() const;
void SetVisibility(bool visible) override;
bool GetVisibility() const override;
@@ -132,6 +151,8 @@ namespace AZ
void UnregisterModel();
void RefreshModelRegistration();
RPI::Cullable::LodConfiguration GetMeshLodConfiguration() const;
void HandleNonUniformScaleChange(const AZ::Vector3& nonUniformScale);
Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;