Skip to content

Commit

Permalink
Add Underworld material editor
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Aug 24, 2024
1 parent ce38b44 commit 71c7fef
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "modules/Frontend.h"
#include "modules/Draw.h"
#include "modules/Debug.h"
#include "modules/Materials.h"
#include "modules/camera/FreeCamera.h"

#include "cdc/render/PCDeviceManager.h"
Expand Down Expand Up @@ -144,6 +145,7 @@ void Hook::RegisterModules()
RegisterModule<Debug>();
#else
RegisterModule<ScriptLog>();
RegisterModule<Materials>();
#endif
}

Expand Down
6 changes: 6 additions & 0 deletions src/cdc/render/CommonMaterial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "CommonMaterial.h"

cdc::MaterialData* cdc::CommonMaterial::GetMaterialData() const noexcept
{
return m_pData;
}
21 changes: 21 additions & 0 deletions src/cdc/render/CommonMaterial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "MaterialData.h"

namespace cdc
{
class IMaterial
{
public:
virtual ~IMaterial() = 0;
};

class CommonMaterial : public IMaterial
{
private:
MaterialData* m_pData;

public:
MaterialData* GetMaterialData() const noexcept;
};
}
12 changes: 12 additions & 0 deletions src/cdc/render/MaterialData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

namespace cdc
{
struct MaterialData
{
char pad[88];

__int16 numParameters;
float* parameters;
};
}
14 changes: 14 additions & 0 deletions src/cdc/sys/Array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace cdc
{
template <typename T>
class Array
{
public:
unsigned int m_size;
unsigned int m_cap;

T* m_data;
};
}
23 changes: 23 additions & 0 deletions src/cdc/sys/HashMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <utility>

#include "Array.h"

namespace cdc
{
template <typename K, typename V>
class HashTable
{
public:
class Node
{
public:
Node* m_next;
std::pair<K, V> m_data;
};

cdc::Array<Node*> m_buckets;
unsigned int m_size;
};
}
94 changes: 94 additions & 0 deletions src/modules/Materials.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <imgui.h>
#include <string>

#include "Materials.h"
#include "render/Material.h"

#include "cdc/sys/HashMap.h"

void Materials::OnMenu()
{
if (ImGui::BeginMenu("Materials"))
{
ImGui::MenuItem("Material editor", nullptr, &m_show);

ImGui::EndMenu();
}
}

void Materials::OnDraw()
{
if (m_show)
{
DrawMaterialEditor();
}
}

void Materials::DrawMaterialEditor()
{
ImGui::Begin("Materials", &m_show);
ImGui::Columns(2);

// Filter
ImGui::InputText("Material", m_filter, sizeof(m_filter));

// Material list
ImGui::BeginChild("MaterialsTree");

auto materials = (cdc::HashTable<unsigned int, MaterialEntry>*)0x8A6EBC;

for (int i = 0; i < materials->m_buckets.m_size; i++)
{
auto node = materials->m_buckets.m_data[i];

if (node)
{
auto id = node->m_data.first;
auto entry = &node->m_data.second;

// Check the filter
if (strlen(m_filter) > 0 && std::to_string(id).find(m_filter) == std::string::npos)
{
continue;
}

if (ImGui::TreeNodeEx((void*)entry, ImGuiTreeNodeFlags_Leaf, "%d", id))
{
if (ImGui::IsItemClicked())
{
m_selected = entry->pMaterial;
}

ImGui::TreePop();
}
}
}

ImGui::EndChild();

// Material editor
ImGui::NextColumn();

if (m_selected)
{
ImGui::BeginChild("MaterialEditor");

DrawMaterialEditor(m_selected);

ImGui::EndChild();
}

ImGui::End();
}

void Materials::DrawMaterialEditor(cdc::CommonMaterial* material)
{
auto data = material->GetMaterialData();

for (int i = 0; i < data->numParameters; i++)
{
ImGui::PushID(i);
ImGui::InputFloat4("##Parameters", &data->parameters[i * 4]);
ImGui::PopID();
}
}
19 changes: 19 additions & 0 deletions src/modules/Materials.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "Module.h"
#include "render/Material.h"

class Materials : public Module
{
private:
bool m_show = false;
cdc::CommonMaterial* m_selected = nullptr;
char m_filter[12] = "";

void DrawMaterialEditor();
void DrawMaterialEditor(cdc::CommonMaterial* material);

public:
void OnDraw();
void OnMenu();
};
12 changes: 12 additions & 0 deletions src/render/Material.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "cdc/render/CommonMaterial.h"

struct MaterialEntry
{
cdc::CommonMaterial* pMaterial;
int field_4;
unsigned int refCount;
unsigned int size;
unsigned int versionID;
};

0 comments on commit 71c7fef

Please sign in to comment.