Skip to content

Commit 9a4b8e7

Browse files
authored
Improved material editor (#587)
* Improved material editor * Added tooltips to buttons * Updated button labels * Added "compile" and "edit shader" buttons (disabled if no shader)
1 parent cfe86cd commit 9a4b8e7

File tree

6 files changed

+86
-23
lines changed

6 files changed

+86
-23
lines changed

Sources/Overload/OvEditor/include/OvEditor/Core/EditorActions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ namespace OvEditor::Core
247247
*/
248248
void CompileShaders();
249249

250+
/**
251+
* Compile the given shader
252+
*/
253+
void CompileShader(OvRendering::Resources::Shader& p_shader);
254+
250255
/**
251256
* Save every materials to their respective files
252257
*/

Sources/Overload/OvEditor/include/OvEditor/Panels/MaterialEditor.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
#pragma once
88

9-
#include <OvUI/Widgets/Texts/Text.h>
9+
#include <OvRendering/Resources/Shader.h>
1010
#include <OvUI/Panels/PanelWindow.h>
11+
#include <OvUI/Widgets/Texts/Text.h>
12+
#include <OvUI/Widgets/Buttons/AButton.h>
1113
#include <OvUI/Widgets/Layout/Group.h>
1214
#include <OvUI/Widgets/Layout/Columns.h>
13-
#include <OvRendering/Resources/Shader.h>
1415

1516
namespace OvCore::Resources { class Material; }
1617

@@ -85,6 +86,9 @@ namespace OvEditor::Panels
8586
OvUI::Widgets::Texts::Text* m_targetMaterialText = nullptr;
8687
OvUI::Widgets::Texts::Text* m_shaderText = nullptr;
8788

89+
OvUI::Widgets::Buttons::AButton* m_editShaderButton = nullptr;
90+
OvUI::Widgets::Buttons::AButton* m_compileShaderButton = nullptr;
91+
8892
OvTools::Eventing::Event<> m_materialDroppedEvent;
8993
OvTools::Eventing::Event<> m_shaderDroppedEvent;
9094

Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,14 @@ void OvEditor::Core::EditorActions::MoveToTarget(OvCore::ECS::Actor& p_target)
709709
}
710710

711711
void OvEditor::Core::EditorActions::CompileShaders()
712+
{
713+
for (const auto shader : m_context.shaderManager.GetResources() | std::views::values)
714+
{
715+
CompileShader(*shader);
716+
}
717+
}
718+
719+
void OvEditor::Core::EditorActions::CompileShader(OvRendering::Resources::Shader& p_shader)
712720
{
713721
using namespace OvRendering::Resources::Loaders;
714722

@@ -717,10 +725,7 @@ void OvEditor::Core::EditorActions::CompileShaders()
717725
newLoggingSettings.summary = true; // Force enable summary logging
718726
ShaderLoader::SetLoggingSettings(newLoggingSettings);
719727

720-
for (const auto shader : m_context.shaderManager.GetResources() | std::views::values)
721-
{
722-
m_context.shaderManager.ReloadResource(shader, GetRealPath(shader->path));
723-
}
728+
m_context.shaderManager.ReloadResource(&p_shader, GetRealPath(p_shader.path));
724729

725730
ShaderLoader::SetLoggingSettings(previousLoggingSettings);
726731
}

Sources/Overload/OvEditor/src/OvEditor/Panels/MaterialEditor.cpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <OvEditor/Panels/AssetView.h>
1212
#include <OvEditor/Panels/MaterialEditor.h>
1313

14+
#include <OvTools/Utils/SystemCalls.h>
15+
1416
#include <OvUI/Widgets/Buttons/Button.h>
1517
#include <OvUI/Widgets/Buttons/ButtonSmall.h>
1618
#include <OvUI/Widgets/Layout/Columns.h>
@@ -232,6 +234,8 @@ void OvEditor::Panels::MaterialEditor::OnMaterialDropped()
232234
void OvEditor::Panels::MaterialEditor::OnShaderDropped()
233235
{
234236
m_materialProperties->enabled = m_shader; // Enable m_shaderSettings group if the shader of the target material is non-null
237+
m_editShaderButton->disabled = m_shader == nullptr;
238+
m_compileShaderButton->disabled = m_shader == nullptr;
235239

236240
if (m_shader != m_target->GetShader())
237241
{
@@ -246,24 +250,26 @@ void OvEditor::Panels::MaterialEditor::OnShaderDropped()
246250
else
247251
{
248252
m_materialPropertiesColumns->RemoveAllWidgets();
253+
m_materialFeaturesColumns->RemoveAllWidgets();
249254
}
250255
}
251256

252257
void OvEditor::Panels::MaterialEditor::CreateHeaderButtons()
253258
{
254-
auto& saveButton = CreateWidget<Buttons::Button>("Save to file");
259+
auto& saveButton = CreateWidget<Buttons::Button>("Save");
255260
saveButton.idleBackgroundColor = { 0.0f, 0.5f, 0.0f };
261+
saveButton.tooltip = "Save the current material to file";
262+
saveButton.lineBreak = false;
256263
saveButton.ClickedEvent += [this] {
257264
if (m_target)
258265
{
259266
OvCore::Resources::Loaders::MaterialLoader::Save(*m_target, EDITOR_EXEC(GetRealPath(m_target->path)));
260267
}
261268
};
262269

263-
saveButton.lineBreak = false;
264-
265-
auto& reloadButton = CreateWidget<Buttons::Button>("Reload from file");
266-
reloadButton.idleBackgroundColor = { 0.7f, 0.5f, 0.0f };
270+
auto& reloadButton = CreateWidget<Buttons::Button>("Reload");
271+
reloadButton.tooltip = "Reload the current material from file";
272+
reloadButton.lineBreak = false;
267273
reloadButton.ClickedEvent += [this] {
268274
if (m_target)
269275
{
@@ -273,15 +279,45 @@ void OvEditor::Panels::MaterialEditor::CreateHeaderButtons()
273279
OnMaterialDropped();
274280
};
275281

276-
reloadButton.lineBreak = false;
282+
auto& compileButton = CreateWidget<Buttons::Button>("Compile");
283+
m_compileShaderButton = &compileButton;
284+
compileButton.tooltip = "Compile the shader of the current material";
285+
compileButton.lineBreak = false;
286+
compileButton.ClickedEvent += [this] {
287+
if (m_target)
288+
{
289+
if (const auto shader = m_target->GetShader())
290+
{
291+
EDITOR_EXEC(CompileShader(*shader));
292+
m_target->UpdateProperties();
293+
OnShaderDropped();
294+
}
295+
}
296+
};
297+
298+
auto& editShaderButton = CreateWidget<Buttons::Button>("Edit Shader");
299+
m_editShaderButton = &editShaderButton;
300+
editShaderButton.tooltip = "Edit the shader of the current material";
301+
editShaderButton.lineBreak = false;
302+
editShaderButton.ClickedEvent += [this] {
303+
if (m_target)
304+
{
305+
if (const auto shader = m_target->GetShader())
306+
{
307+
const auto shaderFilePath = EDITOR_EXEC(GetRealPath(shader->path));
308+
OvTools::Utils::SystemCalls::OpenFile(shaderFilePath);
309+
}
310+
}
311+
};
277312

278313
auto& previewButton = CreateWidget<Buttons::Button>("Preview");
279-
previewButton.idleBackgroundColor = { 0.7f, 0.5f, 0.0f };
280-
previewButton.ClickedEvent += std::bind(&MaterialEditor::Preview, this);
314+
previewButton.tooltip = "Preview the current material in the Asset View";
281315
previewButton.lineBreak = false;
316+
previewButton.ClickedEvent += std::bind(&MaterialEditor::Preview, this);
282317

283-
auto& resetButton = CreateWidget<Buttons::Button>("Reset to default");
318+
auto& resetButton = CreateWidget<Buttons::Button>("Reset");
284319
resetButton.idleBackgroundColor = { 0.5f, 0.0f, 0.0f };
320+
resetButton.tooltip = "Reset the current material to its default state";
285321
resetButton.ClickedEvent += std::bind(&MaterialEditor::Reset, this);
286322
}
287323

Sources/Overload/OvRendering/include/OvRendering/Data/Material.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ namespace OvRendering::Data
7575
) const;
7676

7777
/**
78-
* Fill uniform with default uniform values
78+
* Add missing properties to the material based on the shader, and remove properties that are not used in the shader.
7979
*/
80-
void FillUniform();
80+
void UpdateProperties();
8181

8282
/**
8383
* Bind the material and send its uniform data to the GPU

Sources/Overload/OvRendering/src/OvRendering/Data/Material.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ void OvRendering::Data::Material::SetShader(OvRendering::Resources::Shader* p_sh
7474

7575
if (m_shader)
7676
{
77-
FillUniform();
77+
m_properties.clear();
78+
UpdateProperties();
7879
}
7980
else
8081
{
@@ -98,15 +99,23 @@ OvTools::Utils::OptRef<OvRendering::HAL::ShaderProgram> OvRendering::Data::Mater
9899
return std::nullopt;
99100
}
100101

101-
void OvRendering::Data::Material::FillUniform()
102+
void OvRendering::Data::Material::UpdateProperties()
102103
{
103-
m_properties.clear();
104+
// Collect all uniform names currently used by the shader
105+
std::unordered_set<std::string> usedUniforms;
106+
107+
auto variants_view = m_shader->GetVariants()
108+
| std::views::values
109+
| std::views::join
110+
| std::views::values;
104111

105-
for (const auto& featureVariants : m_shader->GetVariants() | std::views::values)
112+
for (const auto& variant : variants_view)
106113
{
107-
for (const auto& variant : featureVariants | std::views::values)
114+
for (const auto& [name, uniformInfo] : variant->GetUniforms())
108115
{
109-
for (const auto& [name, uniformInfo] : variant->GetUniforms())
116+
usedUniforms.insert(name);
117+
118+
if (!m_properties.contains(name))
110119
{
111120
m_properties.emplace(name, MaterialProperty{
112121
.value = UniformToPropertyValue(uniformInfo.defaultValue),
@@ -115,6 +124,10 @@ void OvRendering::Data::Material::FillUniform()
115124
}
116125
}
117126
}
127+
128+
std::erase_if(m_properties, [&usedUniforms](const auto& property) {
129+
return !usedUniforms.contains(property.first);
130+
});
118131
}
119132

120133
// Note: this function is critical for performance, as it may be called many times during a frame.

0 commit comments

Comments
 (0)