Skip to content

Commit

Permalink
add texture select button to inspector ui (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
VTui22 authored Feb 28, 2024
1 parent 6d6d21f commit c6b7231
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 65 deletions.
13 changes: 12 additions & 1 deletion Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,18 @@ void ECWorldConsumer::AddMaterial(engine::Entity entity, const cd::Material* pMa
pTextureResource->SetDDSBuiltTexturePath(path);
pTextureResource->UpdateTextureType(type);
pTextureResource->UpdateUVMapMode(pTexture->GetUMapMode(), pTexture->GetVMapMode());
materialComponent.SetTextureResource(type, pMaterial, pTextureResource);

cd::Vec2f uvOffset;
cd::Vec2f uvScale;
if (auto optUVOffset = pMaterial->GetVec2fProperty(type, cd::MaterialProperty::UVOffset); optUVOffset.has_value())
{
uvOffset = optUVOffset.value();
}
if (auto optUVScale = pMaterial->GetVec2fProperty(type, cd::MaterialProperty::UVScale); optUVScale.has_value())
{
uvScale = optUVScale.value();
}
materialComponent.SetTextureResource(type, uvOffset, uvScale, pTextureResource);

if (auto pPropertyGroup = materialComponent.GetPropertyGroup(type); pPropertyGroup)
{
Expand Down
4 changes: 2 additions & 2 deletions Engine/Source/Editor/UILayers/AssetBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ bool AssetBrowser::RenderFile(int dirIndex, bool folder, int shownIndex, bool gr
}

}
if (0 == strcmp(".dds", extension.c_str()))
/* if (0 == strcmp(".dds", extension.c_str()))
{
ImGui::Button(reinterpret_cast<const char*>(ICON_MDI_DELTA), ImVec2(m_gridSize, m_gridSize));
}
}*/


}
Expand Down
103 changes: 60 additions & 43 deletions Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

#include "Rendering/RenderContext.h"
#include "Rendering/ShaderCollections.h"
#include "Resources/ResourceBuilder.h"
#include "Resources/ResourceLoader.h"
#include "Rendering/Resources/ResourceContext.h"
#include "Rendering/Resources/TextureResource.h"
#include "Graphics/GraphicsBackend.h"
#include "ImGui/ImGuiUtils.hpp"
#include "Path/Path.h"

#include "ImGui/imfilebrowser.h"

namespace details
{

static editor::Inspector* s_pInspector = nullptr;

template<typename Component>
void UpdateComponentWidget(engine::SceneWorld* pSceneWorld, engine::Entity entity) {}

Expand Down Expand Up @@ -154,45 +161,6 @@ void UpdateComponentWidget<engine::MaterialComponent>(engine::SceneWorld* pScene

if (isOpen)
{
/*
std::vector<std::string> fileNames;
std::filesystem::path dirPath{ CDPROJECT_RESOURCES_ROOT_PATH };
dirPath /= "test";
std::filesystem::path frontPath{ "test" };
engine::RenderContext* pRenderContext = engine::ImGuiBaseLayer::GetRenderContext();
std::vector<std::string> texturePaths;
for (const auto& it : std::filesystem::directory_iterator(dirPath))
{
std::string fileName = it.path().filename().string();
std::string fullpath = (frontPath / fileName).string();
pRenderContext->CreateTexture(fullpath.c_str());
fileNames.emplace_back(cd::MoveTemp(fileName));
texturePaths.emplace_back(cd::MoveTemp(fullpath));
}
static int currentItem = 0;
if (ImGui::BeginCombo("Select Texture", fileNames[currentItem].c_str()))
{
for (int i = 0; i < fileNames.size(); ++i)
{
bool isSelected = (currentItem == i);
if (ImGui::Selectable(fileNames[i].c_str(), isSelected))
{
currentItem = i;
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
bgfx::TextureHandle textureHandle = pRenderContext->GetTexture(engine::StringCrc(texturePaths[currentItem].c_str()));
//sparater
ImGui::Separator();
ImGui::Image(ImTextureID(textureHandle.idx), ImVec2(64, 64));
*/
ImGui::Separator();
ImGuiUtils::ImGuiStringProperty("Name", pMaterialComponent->GetName());

Expand Down Expand Up @@ -239,28 +207,37 @@ void UpdateComponentWidget<engine::MaterialComponent>(engine::SceneWorld* pScene
{
ImGui::PushID(textureTypeValue);

constexpr float textureWidth = 64.0f;
constexpr float textureHeight = 64.0f;
auto& textureInfo = pPropertyGroup->textureInfo;
engine::TextureResource* pTextureResource = textureInfo.pTextureResource;
if (pTextureResource &&
(pTextureResource->GetStatus() == engine::ResourceStatus::Ready || pTextureResource->GetStatus() == engine::ResourceStatus::Optimized))
{
ImGui::Image(reinterpret_cast<ImTextureID>(pTextureResource->GetTextureHandle()), ImVec2(64, 64));
ImGui::Image(reinterpret_cast<ImTextureID>(pTextureResource->GetTextureHandle()), ImVec2(textureWidth, textureHeight));
}
else
{
// Draw a black square with text here.
ImVec2 currentPos = ImGui::GetCursorScreenPos();
ImGui::GetWindowDrawList()->AddRectFilled(currentPos, ImVec2{ currentPos.x + 64, currentPos.y + 64 }, IM_COL32(0, 0, 0, 255));
ImGui::GetWindowDrawList()->AddRectFilled(currentPos, ImVec2{ currentPos.x + textureWidth, currentPos.y + textureHeight }, IM_COL32(0, 0, 0, 255));
ImGui::SetCursorScreenPos(ImVec2{ currentPos.x, currentPos.y + 27});
ImGui::SetWindowFontScale(0.55f);
ImGui::Text("No Resources");
ImGui::SetWindowFontScale(1.0f);
ImGui::SetCursorScreenPos(ImVec2{ currentPos.x, currentPos.y + 66});
}

ImGuiUtils::ImGuiBoolProperty("Use texture", pPropertyGroup->useTexture);
ImGui::SameLine(130.0f);
if (ImGui::Button("Select..."))
{
s_pInspector->SetSelectMaterialTextureType(static_cast<cd::MaterialTextureType>(textureTypeValue));
s_pInspector->SetIsOpenFileBrowser(true);
}

ImGuiUtils::ImGuiVectorProperty("UV Offset", textureInfo.GetUVOffset(), cd::Unit::None, cd::Vec2f::Zero(), cd::Vec2f::One(), false, 0.01f);
ImGuiUtils::ImGuiVectorProperty("UV Scale", textureInfo.GetUVScale());
ImGuiUtils::ImGuiBoolProperty("Use texture", pPropertyGroup->useTexture);

if (pPropertyGroup->useTexture)
{
Expand Down Expand Up @@ -656,7 +633,8 @@ Inspector::~Inspector()

void Inspector::Init()
{

details::s_pInspector = this;
m_pSelectFileBrowser = std::make_unique<ImGui::FileBrowser>();
}

void Inspector::Update()
Expand Down Expand Up @@ -695,13 +673,52 @@ void Inspector::Update()
details::UpdateComponentWidget<engine::CollisionMeshComponent>(pSceneWorld, m_lastSelectedEntity);
details::UpdateComponentWidget<engine::BlendShapeComponent>(pSceneWorld, m_lastSelectedEntity);

if (IsOpenFileBrowser())
{
m_pSelectFileBrowser->SetTitle("Select Texture");
m_pSelectFileBrowser->Open();
SetIsOpenFileBrowser(false);
}

#ifdef ENABLE_DDGI
details::UpdateComponentWidget<engine::DDGIComponent>(pSceneWorld, m_lastSelectedEntity);
#endif

ImGui::EndChild();

ImGui::End();

m_pSelectFileBrowser->Display();
if (m_pSelectFileBrowser->HasSelected() && m_optSelectMaterialTextureType.has_value())
{
auto selectTextureType = m_optSelectMaterialTextureType.value();

// Build dds texture.
std::string selectTextureFilePath = m_pSelectFileBrowser->GetSelected().string();
std::filesystem::path outputSelectTexturePath = CDPROJECT_RESOURCES_ROOT_PATH;
outputSelectTexturePath += m_pSelectFileBrowser->GetSelected().filename().replace_extension(".dds").string();
ResourceBuilder::Get().AddTextureBuildTask(selectTextureType, selectTextureFilePath.c_str(), outputSelectTexturePath.string().c_str());
ResourceBuilder::Get().Update();

auto* pMaterialComponent = pSceneWorld->GetMaterialComponent(m_lastSelectedEntity);
engine::TextureResource* pTextureResource = pMaterialComponent->GetTextureResource(selectTextureType);
if (!pTextureResource)
{
pTextureResource = GetRenderContext()->GetResourceContext()->AddTextureResource(engine::StringCrc(selectTextureFilePath));
pTextureResource->UpdateTextureType(selectTextureType);
pTextureResource->UpdateUVMapMode(cd::TextureMapMode::Wrap, cd::TextureMapMode::Wrap);
pMaterialComponent->SetTextureResource(selectTextureType, cd::Vec2f(0.0f, 0.0f), cd::Vec2f(1.0f, 1.0f), pTextureResource);
}
else
{
pTextureResource->Reset();
}
pTextureResource->SetDDSBuiltTexturePath(outputSelectTexturePath.string());
pMaterialComponent->ActivateShaderFeature(engine::MaterialTextureTypeToShaderFeature.at(selectTextureType));

m_pSelectFileBrowser->ClearSelected();
m_optSelectMaterialTextureType.reset();
}
}

}
21 changes: 21 additions & 0 deletions Engine/Source/Editor/UILayers/Inspector.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
#include "ImGui/ImGuiBaseLayer.h"
#include "ImGui/ImGuiUtils.hpp"
#include "Scene/MaterialTextureType.h"

#include <filesystem>
#include <optional>

namespace ImGui
{

class FileBrowser;

}

namespace editor
{
Expand All @@ -13,8 +24,18 @@ class Inspector : public engine::ImGuiBaseLayer
virtual void Init() override;
virtual void Update() override;

void SetSelectMaterialTextureType(cd::MaterialTextureType textureType) { m_optSelectMaterialTextureType = textureType; }

void SetIsOpenFileBrowser(bool flag) { m_isOpenFileBrowser = flag; }
bool IsOpenFileBrowser() const { return m_isOpenFileBrowser; }

private:
engine::Entity m_lastSelectedEntity = engine::INVALID_ENTITY;

// Select file
std::optional<cd::MaterialTextureType> m_optSelectMaterialTextureType;
bool m_isOpenFileBrowser = false;
std::unique_ptr<ImGui::FileBrowser> m_pSelectFileBrowser;
};

}
18 changes: 9 additions & 9 deletions Engine/Source/Runtime/ECWorld/MaterialComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ void MaterialComponent::Reset()
m_propertyGroups.clear();
}

void MaterialComponent::SetTextureResource(cd::MaterialTextureType textureType, const cd::Material* pMaterial, TextureResource* pTextureResource)
TextureResource* MaterialComponent::GetTextureResource(cd::MaterialTextureType textureType) const
{
auto itPropertyGroup = m_propertyGroups.find(textureType);
return itPropertyGroup != m_propertyGroups.end() ? itPropertyGroup->second.textureInfo.pTextureResource : nullptr;
}

void MaterialComponent::SetTextureResource(cd::MaterialTextureType textureType, cd::Vec2f uvOffset, cd::Vec2f uvScale, TextureResource* pTextureResource)
{
std::optional<uint8_t> optTextureSlot = m_pMaterialType->GetTextureSlot(textureType);
if (!optTextureSlot.has_value())
Expand All @@ -141,14 +147,8 @@ void MaterialComponent::SetTextureResource(cd::MaterialTextureType textureType,
TextureInfo& textureInfo = propertyGroup.textureInfo;
textureInfo.slot = optTextureSlot.value();
textureInfo.pTextureResource = pTextureResource;
if (auto optUVScale = pMaterial->GetVec2fProperty(textureType, cd::MaterialProperty::UVScale); optUVScale.has_value())
{
textureInfo.uvScale = optUVScale.value();
}
if (auto optUVOffset = pMaterial->GetVec2fProperty(textureType, cd::MaterialProperty::UVOffset); optUVOffset.has_value())
{
textureInfo.uvOffset = optUVOffset.value();
}
textureInfo.uvScale = uvOffset;
textureInfo.uvOffset = uvScale;
}

}
3 changes: 2 additions & 1 deletion Engine/Source/Runtime/ECWorld/MaterialComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class MaterialComponent final
const std::string& GetFeaturesCombine();

// Texture data.
void SetTextureResource(cd::MaterialTextureType textureType, const cd::Material* pMaterial, TextureResource* pTextureResource);
TextureResource* GetTextureResource(cd::MaterialTextureType textureType) const;
void SetTextureResource(cd::MaterialTextureType textureType, cd::Vec2f uvOffset, cd::Vec2f uvScale, TextureResource* pTextureResource);

const std::map<cd::MaterialPropertyGroup, PropertyGroup>& GetPropertyGroups() const { return m_propertyGroups; }
PropertyGroup* GetPropertyGroup(cd::MaterialPropertyGroup propertyGroup);
Expand Down
1 change: 1 addition & 0 deletions Engine/Source/Runtime/Rendering/Resources/IResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class IResource
virtual ~IResource() = default;

virtual void Update() = 0;
virtual void Reset() = 0;

StringCrc GetName() const { return m_nameCrc; }
void SetName(StringCrc crc) { m_nameCrc = crc; }
Expand Down
17 changes: 15 additions & 2 deletions Engine/Source/Runtime/Rendering/Resources/MeshResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ void MeshResource::Update()
constexpr uint32_t recycleDelayFrames = 30U;
if (m_recycleCount++ >= recycleDelayFrames)
{
m_vertexBuffer.clear();
m_indexBuffers.clear();
FreeMeshData();
SetStatus(ResourceStatus::Optimized);
}
break;
Expand All @@ -165,6 +164,14 @@ void MeshResource::Update()
}
}

void MeshResource::Reset()
{
DestroyVertexBufferHandle();
DestroyIndexBufferHandle();
FreeMeshData();
SetStatus(ResourceStatus::Loading);
}

bool MeshResource::BuildVertexBuffer()
{
assert(m_pMeshAsset && m_vertexCount > 3U);
Expand Down Expand Up @@ -269,6 +276,12 @@ void MeshResource::SubmitIndexBuffer()
}
}

void MeshResource::FreeMeshData()
{
m_vertexBuffer.clear();
m_indexBuffers.clear();
}

void MeshResource::DestroyVertexBufferHandle()
{
if (m_vertexBufferHandle != UINT16_MAX)
Expand Down
2 changes: 2 additions & 0 deletions Engine/Source/Runtime/Rendering/Resources/MeshResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MeshResource : public IResource
virtual ~MeshResource();

virtual void Update() override;
virtual void Reset() override;

const cd::Mesh* GetMeshAsset() const { return m_pMeshAsset; }
void SetMeshAsset(const cd::Mesh* pMeshAsset);
Expand All @@ -48,6 +49,7 @@ class MeshResource : public IResource
bool BuildIndexBuffer();
void SubmitVertexBuffer();
void SubmitIndexBuffer();
void FreeMeshData();
void DestroyVertexBufferHandle();
void DestroyIndexBufferHandle();

Expand Down
Loading

0 comments on commit c6b7231

Please sign in to comment.