editor: add shader-driven material property inspector
This commit is contained in:
@@ -726,6 +726,194 @@ bool TryGetLoadedShaderHandle(
|
||||
return outShader.IsValid();
|
||||
}
|
||||
|
||||
bool TryResolveShaderHandle(
|
||||
const std::string& shaderPath,
|
||||
::XCEngine::Resources::ResourceHandle<::XCEngine::Resources::Shader>& outShader) {
|
||||
if (TryGetLoadedShaderHandle(shaderPath, outShader)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
outShader.Reset();
|
||||
if (shaderPath.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outShader = ::XCEngine::Resources::ResourceManager::Get().Load<::XCEngine::Resources::Shader>(shaderPath.c_str());
|
||||
return outShader.IsValid();
|
||||
}
|
||||
|
||||
void CopyMaterialPropertyValue(
|
||||
const InspectorPanel::MaterialPropertyState& source,
|
||||
InspectorPanel::MaterialPropertyState& destination) {
|
||||
destination.type = source.type;
|
||||
destination.floatValue = source.floatValue;
|
||||
destination.intValue = source.intValue;
|
||||
destination.boolValue = source.boolValue;
|
||||
destination.texturePath = source.texturePath;
|
||||
}
|
||||
|
||||
std::vector<InspectorPanel::MaterialPropertyState> BuildShaderDefaultPropertyStates(
|
||||
const ::XCEngine::Resources::ResourceHandle<::XCEngine::Resources::Shader>& shaderHandle) {
|
||||
if (!shaderHandle.IsValid()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
::XCEngine::Resources::Material scratchMaterial;
|
||||
scratchMaterial.SetShader(shaderHandle);
|
||||
return CollectMaterialPropertyStates(scratchMaterial);
|
||||
}
|
||||
|
||||
void SyncMaterialAssetStateWithShader(
|
||||
const ::XCEngine::Resources::ResourceHandle<::XCEngine::Resources::Shader>& shaderHandle,
|
||||
InspectorPanel::MaterialAssetState& state) {
|
||||
if (!shaderHandle.IsValid() || shaderHandle.Get() == nullptr) {
|
||||
state.keywords.clear();
|
||||
state.properties.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, InspectorPanel::MaterialPropertyState> previousProperties;
|
||||
previousProperties.reserve(state.properties.size());
|
||||
for (const InspectorPanel::MaterialPropertyState& property : state.properties) {
|
||||
if (!property.name.empty()) {
|
||||
previousProperties.emplace(property.name, property);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<InspectorPanel::MaterialPropertyState> nextProperties =
|
||||
BuildShaderDefaultPropertyStates(shaderHandle);
|
||||
for (InspectorPanel::MaterialPropertyState& property : nextProperties) {
|
||||
const auto previousPropertyIt = previousProperties.find(property.name);
|
||||
if (previousPropertyIt == previousProperties.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (previousPropertyIt->second.type != property.type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
CopyMaterialPropertyValue(previousPropertyIt->second, property);
|
||||
}
|
||||
state.properties = std::move(nextProperties);
|
||||
|
||||
std::vector<InspectorPanel::MaterialKeywordState> nextKeywords;
|
||||
nextKeywords.reserve(state.keywords.size());
|
||||
for (const InspectorPanel::MaterialKeywordState& keyword : state.keywords) {
|
||||
const std::string keywordValue = TrimCopy(keyword.value);
|
||||
if (!keywordValue.empty() && shaderHandle->DeclaresKeyword(keywordValue.c_str())) {
|
||||
nextKeywords.push_back(keyword);
|
||||
}
|
||||
}
|
||||
state.keywords = std::move(nextKeywords);
|
||||
}
|
||||
|
||||
InspectorPanel::MaterialPropertyState* FindMaterialPropertyState(
|
||||
InspectorPanel::MaterialAssetState& state,
|
||||
const Containers::String& propertyName) {
|
||||
const std::string propertyNameText(propertyName.CStr());
|
||||
for (InspectorPanel::MaterialPropertyState& property : state.properties) {
|
||||
if (property.name == propertyNameText) {
|
||||
return &property;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool DrawFloat4Property(const char* label, float values[4]) {
|
||||
return UI::DrawPropertyRow(label, UI::InspectorPropertyLayout(), [&](const UI::PropertyLayoutMetrics& layout) {
|
||||
UI::AlignPropertyControlVertically(layout, ImGui::GetFrameHeight());
|
||||
ImGui::PushID(label);
|
||||
ImGui::SetNextItemWidth(layout.controlWidth);
|
||||
const bool changed = ImGui::InputFloat4("##Value", values, "%.3f");
|
||||
ImGui::PopID();
|
||||
return changed;
|
||||
});
|
||||
}
|
||||
|
||||
bool DrawShaderDrivenMaterialProperty(
|
||||
const ::XCEngine::Resources::ShaderPropertyDesc& shaderProperty,
|
||||
InspectorPanel::MaterialAssetState& state) {
|
||||
InspectorPanel::MaterialPropertyState* propertyState = FindMaterialPropertyState(state, shaderProperty.name);
|
||||
if (propertyState == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* label = shaderProperty.displayName.Empty()
|
||||
? shaderProperty.name.CStr()
|
||||
: shaderProperty.displayName.CStr();
|
||||
|
||||
switch (shaderProperty.type) {
|
||||
case ::XCEngine::Resources::ShaderPropertyType::Float:
|
||||
case ::XCEngine::Resources::ShaderPropertyType::Range:
|
||||
return UI::DrawPropertyFloat(label, propertyState->floatValue[0], 0.01f);
|
||||
|
||||
case ::XCEngine::Resources::ShaderPropertyType::Int: {
|
||||
int value = propertyState->intValue[0];
|
||||
if (!UI::DrawPropertyInt(label, value, 1)) {
|
||||
return false;
|
||||
}
|
||||
propertyState->intValue[0] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
case ::XCEngine::Resources::ShaderPropertyType::Color:
|
||||
return UI::DrawPropertyColor4(label, propertyState->floatValue.data());
|
||||
|
||||
case ::XCEngine::Resources::ShaderPropertyType::Vector: {
|
||||
if (propertyState->type == ::XCEngine::Resources::MaterialPropertyType::Float2) {
|
||||
::XCEngine::Math::Vector2 value(propertyState->floatValue[0], propertyState->floatValue[1]);
|
||||
if (!UI::DrawPropertyVec2(label, value)) {
|
||||
return false;
|
||||
}
|
||||
propertyState->floatValue[0] = value.x;
|
||||
propertyState->floatValue[1] = value.y;
|
||||
return true;
|
||||
}
|
||||
if (propertyState->type == ::XCEngine::Resources::MaterialPropertyType::Float3) {
|
||||
::XCEngine::Math::Vector3 value(
|
||||
propertyState->floatValue[0],
|
||||
propertyState->floatValue[1],
|
||||
propertyState->floatValue[2]);
|
||||
if (!UI::DrawPropertyVec3(label, value)) {
|
||||
return false;
|
||||
}
|
||||
propertyState->floatValue[0] = value.x;
|
||||
propertyState->floatValue[1] = value.y;
|
||||
propertyState->floatValue[2] = value.z;
|
||||
return true;
|
||||
}
|
||||
return DrawFloat4Property(label, propertyState->floatValue.data());
|
||||
}
|
||||
|
||||
case ::XCEngine::Resources::ShaderPropertyType::Texture2D:
|
||||
case ::XCEngine::Resources::ShaderPropertyType::TextureCube: {
|
||||
const InspectorAssetReferenceInteraction textureInteraction =
|
||||
DrawInspectorAssetReferenceProperty(
|
||||
label,
|
||||
propertyState->texturePath,
|
||||
"Select Texture Asset",
|
||||
{ ".png", ".jpg", ".jpeg", ".bmp", ".tga", ".dds", ".hdr", ".ppm" });
|
||||
if (textureInteraction.clearRequested) {
|
||||
if (propertyState->texturePath.empty()) {
|
||||
return false;
|
||||
}
|
||||
propertyState->texturePath.clear();
|
||||
return true;
|
||||
}
|
||||
if (!textureInteraction.assignedPath.empty() &&
|
||||
textureInteraction.assignedPath != propertyState->texturePath) {
|
||||
propertyState->texturePath = textureInteraction.assignedPath;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string BuildMaterialLoadFailureMessage(const ::XCEngine::Resources::LoadResult& result) {
|
||||
if (!result.errorMessage.Empty()) {
|
||||
return std::string("Material file is invalid or unavailable: ") + result.errorMessage.CStr();
|
||||
@@ -1068,6 +1256,7 @@ void InspectorPanel::ApplyMaterialAssetStateToSelectedMaterial() {
|
||||
++m_materialShaderLoadRevision;
|
||||
m_materialShaderLoadInFlight = false;
|
||||
m_pendingMaterialShaderPath.clear();
|
||||
SyncMaterialAssetStateWithShader(shaderHandle, m_materialAssetState);
|
||||
ApplyResolvedMaterialStateToResource(m_materialAssetState, shaderHandle, material);
|
||||
m_materialAssetState.errorMessage.clear();
|
||||
return;
|
||||
@@ -1117,6 +1306,7 @@ void InspectorPanel::ApplyMaterialAssetStateToSelectedMaterial() {
|
||||
return;
|
||||
}
|
||||
|
||||
SyncMaterialAssetStateWithShader(loadedShader, m_materialAssetState);
|
||||
ApplyResolvedMaterialStateToResource(
|
||||
m_materialAssetState,
|
||||
loadedShader,
|
||||
@@ -1325,6 +1515,15 @@ bool InspectorPanel::SaveMaterialAsset() {
|
||||
}
|
||||
|
||||
try {
|
||||
::XCEngine::Resources::ResourceHandle<::XCEngine::Resources::Shader> shaderHandle;
|
||||
const std::string shaderPath = TrimCopy(BufferToString(m_materialAssetState.shaderPath));
|
||||
if (!shaderPath.empty() && TryResolveShaderHandle(shaderPath, shaderHandle)) {
|
||||
SyncMaterialAssetStateWithShader(shaderHandle, m_materialAssetState);
|
||||
} else if (shaderPath.empty()) {
|
||||
m_materialAssetState.keywords.clear();
|
||||
m_materialAssetState.properties.clear();
|
||||
}
|
||||
|
||||
const std::filesystem::path materialPath(Platform::Utf8ToWide(m_materialAssetState.assetFullPath));
|
||||
std::ofstream output(materialPath, std::ios::out | std::ios::trunc);
|
||||
if (!output.is_open()) {
|
||||
@@ -1389,10 +1588,16 @@ void InspectorPanel::RenderMaterialAsset() {
|
||||
{ ".shader", ".xcshader" });
|
||||
if (shaderInteraction.clearRequested) {
|
||||
CopyToCharBuffer(std::string(), m_materialAssetState.shaderPath);
|
||||
m_materialAssetState.keywords.clear();
|
||||
m_materialAssetState.properties.clear();
|
||||
changed = true;
|
||||
} else if (!shaderInteraction.assignedPath.empty() &&
|
||||
shaderInteraction.assignedPath != BufferToString(m_materialAssetState.shaderPath)) {
|
||||
CopyToCharBuffer(shaderInteraction.assignedPath, m_materialAssetState.shaderPath);
|
||||
::XCEngine::Resources::ResourceHandle<::XCEngine::Resources::Shader> shaderHandle;
|
||||
if (TryResolveShaderHandle(shaderInteraction.assignedPath, shaderHandle)) {
|
||||
SyncMaterialAssetStateWithShader(shaderHandle, m_materialAssetState);
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
|
||||
@@ -1421,6 +1626,44 @@ void InspectorPanel::RenderMaterialAsset() {
|
||||
UI::EndComponentSection(materialSection);
|
||||
}
|
||||
|
||||
::XCEngine::Resources::ResourceHandle<::XCEngine::Resources::Shader> materialShaderHandle;
|
||||
const std::string materialShaderPath = TrimCopy(BufferToString(m_materialAssetState.shaderPath));
|
||||
const bool hasMaterialShader =
|
||||
!materialShaderPath.empty() &&
|
||||
TryResolveShaderHandle(materialShaderPath, materialShaderHandle) &&
|
||||
materialShaderHandle.IsValid() &&
|
||||
materialShaderHandle.Get() != nullptr;
|
||||
|
||||
const UI::ComponentSectionResult propertiesSection = UI::BeginComponentSection(
|
||||
"MaterialAssetProperties",
|
||||
"Properties");
|
||||
if (propertiesSection.open) {
|
||||
ImGui::Indent(propertiesSection.contentIndent);
|
||||
|
||||
if (!hasMaterialShader) {
|
||||
UI::DrawHintText("Select a shader to expose material properties.");
|
||||
} else if (materialShaderHandle->GetProperties().Empty()) {
|
||||
UI::DrawHintText("Selected shader does not expose editable properties.");
|
||||
} else {
|
||||
bool changed = false;
|
||||
for (const ::XCEngine::Resources::ShaderPropertyDesc& shaderProperty : materialShaderHandle->GetProperties()) {
|
||||
changed = DrawShaderDrivenMaterialProperty(shaderProperty, m_materialAssetState) || changed;
|
||||
if (!shaderProperty.defaultValue.Empty()) {
|
||||
const std::string defaultText =
|
||||
std::string("Default: ") + shaderProperty.defaultValue.CStr();
|
||||
UI::DrawHintText(defaultText.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
m_materialAssetState.dirty = true;
|
||||
SaveMaterialAsset();
|
||||
}
|
||||
}
|
||||
|
||||
UI::EndComponentSection(propertiesSection);
|
||||
}
|
||||
|
||||
const UI::ComponentSectionResult renderStateSection = UI::BeginComponentSection(
|
||||
"MaterialAssetRenderState",
|
||||
"Render State");
|
||||
|
||||
Reference in New Issue
Block a user