Skip to content

Commit 189a5ae

Browse files
added entity material component visualization (#188)
* added entity material component visualization * fixed build error
1 parent a8fcd75 commit 189a5ae

File tree

6 files changed

+158
-13
lines changed

6 files changed

+158
-13
lines changed

Tetragrama/src/Components/InspectorViewUIComponent.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,44 @@ namespace Tetragrama::Components {
8181

8282
if (m_scene_entity->HasComponent<MaterialComponent>()) {
8383

84-
if (ImGui::TreeNodeEx(reinterpret_cast<void*>(typeid(MaterialComponent).hash_code()), ImGuiTreeNodeFlags_DefaultOpen, "%s", "Material")) {
84+
if (ImGui::TreeNodeEx(reinterpret_cast<void*>(typeid(MaterialComponent).hash_code()), ImGuiTreeNodeFlags_DefaultOpen, "%s", "Materials")) {
85+
auto& component = m_scene_entity->GetComponent<MaterialComponent>();
86+
auto material = component.GetMaterial();
87+
auto material_shader_type = material->GetShaderBuiltInType();
88+
89+
const char* built_in_shader_type[] = {"Basic", "Standard"};
90+
91+
auto material_name = fmt::format("{0} Material", built_in_shader_type[(int) material_shader_type]);
92+
ImGui::Dummy(ImVec2(0, 3));
93+
Helpers::DrawInputTextControl("Name", material_name, nullptr, true);
94+
95+
if (material_shader_type == ZEngine::Rendering::Shaders::ShaderBuiltInType::STANDARD) {
96+
auto standard_material = reinterpret_cast<ZEngine::Rendering::Materials::StandardMaterial*>(material.get());
97+
98+
ImGui::Dummy(ImVec2(0, 0.5f));
99+
100+
float tile_factor = standard_material->GetTileFactor();
101+
Helpers::DrawDragFloatControl(
102+
"Tile Factor", tile_factor, 0.2f, 0.0f, 0.0f, "%.2f", [standard_material](float value) { standard_material->SetTileFactor(value); });
103+
ImGui::Dummy(ImVec2(0, 0.5f));
104+
105+
float shininess = standard_material->GetShininess();
106+
Helpers::DrawDragFloatControl(
107+
"Shininess", shininess, 0.2f, 0.0f, 0.0f, "%.2f", [standard_material](float value) { standard_material->SetShininess(value); });
108+
ImGui::Dummy(ImVec2(0, 0.5f));
109+
110+
auto tint_color = standard_material->GetTintColor();
111+
auto diffuse_texture = standard_material->GetDiffuseMap();
112+
Helpers::DrawTextureColorControl("Diffuse Map", reinterpret_cast<ImTextureID>(diffuse_texture->GetIdentifier()), tint_color, true, nullptr,
113+
[standard_material](auto& value) { standard_material->SetTintColor(value); });
114+
ImGui::Dummy(ImVec2(0, 0.5f));
115+
116+
auto specular_texture = standard_material->GetSpecularMap();
117+
auto specular_tint_color = ZEngine::Maths::Vector4{1, 1, 1, 1};
118+
Helpers::DrawTextureColorControl("Specular Map", reinterpret_cast<ImTextureID>(specular_texture->GetIdentifier()), specular_tint_color);
119+
ImGui::Dummy(ImVec2(0, 0.5f));
120+
}
121+
85122
ImGui::TreePop();
86123
}
87124
ImGui::Dummy(ImVec2(0, 5));

Tetragrama/src/Helpers/UIComponentDrawerHelper.cpp

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Tetragrama::Helpers {
66

77
void DrawVec4Control(
88
std::string_view label, ZEngine::Maths::Vector4& values, const std::function<void(ZEngine::Maths::Vector4&)>& callback, float default_value, float column_width) {
9-
ImGui::PushID(label.data());
9+
ImGui::PushID(label.data(), (label.data() + label.size()));
1010

1111
ImGui::Columns(2);
1212

@@ -108,7 +108,7 @@ namespace Tetragrama::Helpers {
108108

109109
void DrawVec3Control(
110110
std::string_view label, ZEngine::Maths::Vector3& values, const std::function<void(ZEngine::Maths::Vector3&)>& callback, float default_value, float column_width) {
111-
ImGui::PushID(label.data());
111+
ImGui::PushID(label.data(), (label.data() + label.size()));
112112

113113
ImGui::Columns(2);
114114

@@ -192,7 +192,7 @@ namespace Tetragrama::Helpers {
192192

193193
void DrawVec2Control(
194194
std::string_view label, ZEngine::Maths::Vector2& values, const std::function<void(ZEngine::Maths::Vector2&)>& callback, float default_value, float column_width) {
195-
ImGui::PushID(label.data());
195+
ImGui::PushID(label.data(), (label.data() + label.size()));
196196

197197
ImGui::Columns(2);
198198

@@ -252,7 +252,7 @@ namespace Tetragrama::Helpers {
252252

253253

254254
void DrawInputTextControl(std::string_view label, std::string_view content, const std::function<void(std::string_view)>& callback, bool read_only_mode, float column_width) {
255-
ImGui::PushID(label.data());
255+
ImGui::PushID(label.data(), (label.data() + label.size()));
256256
ImGui::Columns(2);
257257

258258
ImGui::SetColumnWidth(0, column_width);
@@ -262,9 +262,6 @@ namespace Tetragrama::Helpers {
262262
ImGui::PushMultiItemsWidths(1, ImGui::CalcItemWidth() + 60.f);
263263
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{0.5f, 0});
264264

265-
float line_height = GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f;
266-
ImVec2 input_control_size = {line_height + 3.0f, line_height};
267-
268265
char buffer[1024];
269266
memset(buffer, 0, sizeof(buffer));
270267
auto raw_entity_name = content.data();
@@ -288,7 +285,7 @@ namespace Tetragrama::Helpers {
288285

289286
void DrawDragFloatControl(std::string_view label, float value, float increment_speed, float min_value, float max_value, std::string_view fmt,
290287
const std::function<void(float)>& callback, float column_width) {
291-
ImGui::PushID(label.data());
288+
ImGui::PushID(label.data(), (label.data() + label.size()));
292289
ImGui::Columns(2);
293290

294291
ImGui::SetColumnWidth(0, column_width);
@@ -298,9 +295,6 @@ namespace Tetragrama::Helpers {
298295
ImGui::PushMultiItemsWidths(1, ImGui::CalcItemWidth() + 60.f);
299296
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{0.5f, 0});
300297

301-
float line_height = GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f;
302-
ImVec2 input_control_size = {line_height + 3.0f, line_height};
303-
304298
if (ImGui::DragFloat("##DragFloat", &value, increment_speed, min_value, max_value, fmt.data())) {
305299
if (callback) {
306300
callback(value);
@@ -314,7 +308,7 @@ namespace Tetragrama::Helpers {
314308

315309
void DrawCenteredButtonControl(std::string_view label, const std::function<void(void)>& callback) {
316310

317-
ImGui::PushID(label.data());
311+
ImGui::PushID(label.data(), (label.data() + label.size()));
318312

319313
ImGui::BeginTable("##table", 3);
320314

@@ -335,4 +329,72 @@ namespace Tetragrama::Helpers {
335329
ImGui::EndTable();
336330
ImGui::PopID();
337331
}
332+
333+
void DrawColorEdit4Control(
334+
std::string_view label, ZEngine::Maths::Vector4& values, const std::function<void(ZEngine::Maths::Vector4&)>& callback, float default_value, float column_width) {
335+
ImGui::PushID(label.data(), (label.data() + label.size()));
336+
337+
ImGui::Columns(2);
338+
339+
ImGui::SetColumnWidth(0, column_width);
340+
ImGui::Text(label.data());
341+
ImGui::NextColumn();
342+
343+
ImGui::PushMultiItemsWidths(1, ImGui::CalcItemWidth() + 60.f);
344+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{0.5f, 0});
345+
346+
if (ImGui::ColorEdit4("##TintColor", ZEngine::Maths::value_ptr(values))) {
347+
if (callback) {
348+
callback(values);
349+
}
350+
}
351+
352+
ImGui::PopItemWidth();
353+
ImGui::PopStyleVar();
354+
ImGui::Columns(1);
355+
ImGui::PopID();
356+
}
357+
358+
void DrawTextureColorControl(std::string_view label, ImTextureID texture_id, ZEngine::Maths::Vector4& tint_color, bool enable_zoom,
359+
const std::function<void(void)>& image_click_callback, const std::function<void(ZEngine::Maths::Vector4&)>& tint_color_change_callback, float column_width) {
360+
ImGui::PushID(label.data(), (label.data() + label.size()));
361+
ImGui::Columns(2);
362+
363+
ImGui::SetColumnWidth(0, column_width);
364+
ImGui::Text(label.data());
365+
ImGui::NextColumn();
366+
367+
ImGui::PushMultiItemsWidths(1, ImGui::CalcItemWidth() + 60.f);
368+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{0.5f, 0});
369+
370+
float line_height = GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f;
371+
ImVec2 button_size = {line_height + 3.0f, line_height};
372+
373+
if (ImGui::ImageButton(texture_id, button_size, ImVec2(0, 0), ImVec2(1, 1), 1, ImVec4(0, 0, 0, 0), ImVec4{tint_color.x, tint_color.y, tint_color.z, tint_color.w})) {
374+
if (image_click_callback) {
375+
image_click_callback();
376+
}
377+
}
378+
if (enable_zoom) {
379+
if (ImGui::IsItemHovered()) {
380+
ImGui::BeginTooltip();
381+
ImGui::Image(texture_id, ImVec2(200, 200), {0, 0}, {1, 1}, ImVec4{tint_color.x, tint_color.y, tint_color.z, tint_color.w});
382+
ImGui::EndTooltip();
383+
}
384+
}
385+
ImGui::PopItemWidth();
386+
387+
ImGui::SameLine();
388+
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(1, 4));
389+
if (ImGui::ColorEdit4("##TintColor", ZEngine::Maths::value_ptr(tint_color))) {
390+
if (tint_color_change_callback) {
391+
tint_color_change_callback(tint_color);
392+
}
393+
}
394+
ImGui::PopItemWidth();
395+
396+
ImGui::PopStyleVar(2);
397+
ImGui::Columns(1);
398+
ImGui::PopID();
399+
}
338400
} // namespace Tetragrama::Helpers

Tetragrama/src/Helpers/UIComponentDrawerHelper.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ namespace Tetragrama::Helpers {
1616
const std::function<void(float)>& callback = nullptr, float column_width = 100.0f);
1717

1818
void DrawCenteredButtonControl(std::string_view label, const std::function<void(void)>& callback = nullptr);
19+
20+
void DrawColorEdit4Control(std::string_view label, ZEngine::Maths::Vector4& values, const std::function<void(ZEngine::Maths::Vector4&)>& callback = nullptr,
21+
float default_value = 0.0f, float column_width = 100.0f);
22+
23+
void DrawTextureColorControl(std::string_view label, ImTextureID texture_id, ZEngine::Maths::Vector4& texture_tint_color, bool enable_zoom = true,
24+
const std::function<void(void)>& image_click_callback = nullptr, const std::function<void(ZEngine::Maths::Vector4&)>& tint_color_change_callback = nullptr,
25+
float column_width = 100.0f);
1926
} // namespace Tetragrama::Helpers

ZEngine/include/ZEngine/Rendering/Materials/StandardMaterial.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ namespace ZEngine::Rendering::Materials {
3030
void SetTexture(Textures::Texture* const texture) override;
3131
void SetTexture(const Ref<Textures::Texture>& texture) override;
3232

33+
float GetTileFactor() const;
34+
float GetShininess() const;
35+
const Maths::Vector4& GetTintColor() const;
36+
37+
Ref<Textures::Texture> GetSpecularMap() const;
38+
Ref<Textures::Texture> GetDiffuseMap() const;
39+
3340
private:
3441
float m_shininess;
3542
float m_tile_factor;

ZEngine/include/ZEngine/Rendering/Textures/Texture.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ namespace ZEngine::Rendering::Textures {
2929
return m_texture_id;
3030
}
3131

32+
unsigned int GetWidth() const {
33+
return m_width;
34+
}
35+
36+
unsigned int GetHeight() const {
37+
return m_height;
38+
}
39+
40+
std::string_view GetFilePath() {
41+
return m_path;
42+
}
43+
3244
protected:
3345
std::string m_path;
3446
GLuint m_texture_id{0};

ZEngine/src/StandardMaterial.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ namespace ZEngine::Rendering::Materials {
5555
SetDiffuseMap(texture);
5656
}
5757

58+
float StandardMaterial::GetTileFactor() const {
59+
return m_tile_factor;
60+
}
61+
62+
float StandardMaterial::GetShininess() const {
63+
return m_shininess;
64+
}
65+
66+
const Maths::Vector4& StandardMaterial::GetTintColor() const {
67+
return m_tint_color;
68+
}
69+
70+
Ref<Textures::Texture> StandardMaterial::GetSpecularMap() const {
71+
return m_specular_map;
72+
}
73+
74+
Ref<Textures::Texture> StandardMaterial::GetDiffuseMap() const {
75+
return m_texture;
76+
}
77+
5878
void StandardMaterial::SetTexture(Textures::Texture* const texture) {
5979
SetDiffuseMap(texture);
6080
}

0 commit comments

Comments
 (0)