Skip to content

Commit 71c7fef

Browse files
committed
Add Underworld material editor
1 parent ce38b44 commit 71c7fef

File tree

9 files changed

+203
-0
lines changed

9 files changed

+203
-0
lines changed

src/Hook.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "modules/Frontend.h"
2424
#include "modules/Draw.h"
2525
#include "modules/Debug.h"
26+
#include "modules/Materials.h"
2627
#include "modules/camera/FreeCamera.h"
2728

2829
#include "cdc/render/PCDeviceManager.h"
@@ -144,6 +145,7 @@ void Hook::RegisterModules()
144145
RegisterModule<Debug>();
145146
#else
146147
RegisterModule<ScriptLog>();
148+
RegisterModule<Materials>();
147149
#endif
148150
}
149151

src/cdc/render/CommonMaterial.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "CommonMaterial.h"
2+
3+
cdc::MaterialData* cdc::CommonMaterial::GetMaterialData() const noexcept
4+
{
5+
return m_pData;
6+
}

src/cdc/render/CommonMaterial.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "MaterialData.h"
4+
5+
namespace cdc
6+
{
7+
class IMaterial
8+
{
9+
public:
10+
virtual ~IMaterial() = 0;
11+
};
12+
13+
class CommonMaterial : public IMaterial
14+
{
15+
private:
16+
MaterialData* m_pData;
17+
18+
public:
19+
MaterialData* GetMaterialData() const noexcept;
20+
};
21+
}

src/cdc/render/MaterialData.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
namespace cdc
4+
{
5+
struct MaterialData
6+
{
7+
char pad[88];
8+
9+
__int16 numParameters;
10+
float* parameters;
11+
};
12+
}

src/cdc/sys/Array.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
namespace cdc
4+
{
5+
template <typename T>
6+
class Array
7+
{
8+
public:
9+
unsigned int m_size;
10+
unsigned int m_cap;
11+
12+
T* m_data;
13+
};
14+
}

src/cdc/sys/HashMap.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <utility>
4+
5+
#include "Array.h"
6+
7+
namespace cdc
8+
{
9+
template <typename K, typename V>
10+
class HashTable
11+
{
12+
public:
13+
class Node
14+
{
15+
public:
16+
Node* m_next;
17+
std::pair<K, V> m_data;
18+
};
19+
20+
cdc::Array<Node*> m_buckets;
21+
unsigned int m_size;
22+
};
23+
}

src/modules/Materials.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <imgui.h>
2+
#include <string>
3+
4+
#include "Materials.h"
5+
#include "render/Material.h"
6+
7+
#include "cdc/sys/HashMap.h"
8+
9+
void Materials::OnMenu()
10+
{
11+
if (ImGui::BeginMenu("Materials"))
12+
{
13+
ImGui::MenuItem("Material editor", nullptr, &m_show);
14+
15+
ImGui::EndMenu();
16+
}
17+
}
18+
19+
void Materials::OnDraw()
20+
{
21+
if (m_show)
22+
{
23+
DrawMaterialEditor();
24+
}
25+
}
26+
27+
void Materials::DrawMaterialEditor()
28+
{
29+
ImGui::Begin("Materials", &m_show);
30+
ImGui::Columns(2);
31+
32+
// Filter
33+
ImGui::InputText("Material", m_filter, sizeof(m_filter));
34+
35+
// Material list
36+
ImGui::BeginChild("MaterialsTree");
37+
38+
auto materials = (cdc::HashTable<unsigned int, MaterialEntry>*)0x8A6EBC;
39+
40+
for (int i = 0; i < materials->m_buckets.m_size; i++)
41+
{
42+
auto node = materials->m_buckets.m_data[i];
43+
44+
if (node)
45+
{
46+
auto id = node->m_data.first;
47+
auto entry = &node->m_data.second;
48+
49+
// Check the filter
50+
if (strlen(m_filter) > 0 && std::to_string(id).find(m_filter) == std::string::npos)
51+
{
52+
continue;
53+
}
54+
55+
if (ImGui::TreeNodeEx((void*)entry, ImGuiTreeNodeFlags_Leaf, "%d", id))
56+
{
57+
if (ImGui::IsItemClicked())
58+
{
59+
m_selected = entry->pMaterial;
60+
}
61+
62+
ImGui::TreePop();
63+
}
64+
}
65+
}
66+
67+
ImGui::EndChild();
68+
69+
// Material editor
70+
ImGui::NextColumn();
71+
72+
if (m_selected)
73+
{
74+
ImGui::BeginChild("MaterialEditor");
75+
76+
DrawMaterialEditor(m_selected);
77+
78+
ImGui::EndChild();
79+
}
80+
81+
ImGui::End();
82+
}
83+
84+
void Materials::DrawMaterialEditor(cdc::CommonMaterial* material)
85+
{
86+
auto data = material->GetMaterialData();
87+
88+
for (int i = 0; i < data->numParameters; i++)
89+
{
90+
ImGui::PushID(i);
91+
ImGui::InputFloat4("##Parameters", &data->parameters[i * 4]);
92+
ImGui::PopID();
93+
}
94+
}

src/modules/Materials.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "Module.h"
4+
#include "render/Material.h"
5+
6+
class Materials : public Module
7+
{
8+
private:
9+
bool m_show = false;
10+
cdc::CommonMaterial* m_selected = nullptr;
11+
char m_filter[12] = "";
12+
13+
void DrawMaterialEditor();
14+
void DrawMaterialEditor(cdc::CommonMaterial* material);
15+
16+
public:
17+
void OnDraw();
18+
void OnMenu();
19+
};

src/render/Material.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include "cdc/render/CommonMaterial.h"
4+
5+
struct MaterialEntry
6+
{
7+
cdc::CommonMaterial* pMaterial;
8+
int field_4;
9+
unsigned int refCount;
10+
unsigned int size;
11+
unsigned int versionID;
12+
};

0 commit comments

Comments
 (0)