From 09c1aafac9ddb59813ef3eb44973e19dec823e29 Mon Sep 17 00:00:00 2001 From: JinhoBa <127280695+JinhoBa@users.noreply.github.com> Date: Sun, 9 Nov 2025 12:37:06 +0900 Subject: [PATCH] [add] Material --- Jusin154_Team3D/Engine/Private/Material.cpp | 220 ++++++++++++++++++ Jusin154_Team3D/Engine/Private/Model.cpp | 80 ++++++- .../Engine/Private/VIBuffer_Terrain.cpp | 2 +- Jusin154_Team3D/Engine/Public/Engine_Enum.h | 2 +- Jusin154_Team3D/Engine/Public/Material.h | 4 + Jusin154_Team3D/Engine/Public/Model.h | 1 + 6 files changed, 304 insertions(+), 5 deletions(-) diff --git a/Jusin154_Team3D/Engine/Private/Material.cpp b/Jusin154_Team3D/Engine/Private/Material.cpp index 5103414fa..d699236ee 100644 --- a/Jusin154_Team3D/Engine/Private/Material.cpp +++ b/Jusin154_Team3D/Engine/Private/Material.cpp @@ -90,6 +90,222 @@ HRESULT CMaterial::Initialize(const _char* pModelFilePath, const aiMaterial* pAI return S_OK; } +HRESULT CMaterial::Initialize(const _char* pMaterialFilePath) +{ + _char szTextureFilePath[MAX_PATH] = {}; + _char szMaterialFilePath[MAX_PATH] = {}; + + strcpy_s(szTextureFilePath, "../Bin/Resources/Models/"); + + strcpy_s(szMaterialFilePath, pMaterialFilePath); + strcat_s(szMaterialFilePath, ".props.txt"); + + if (FAILED(Read_MaterialFile(szMaterialFilePath, szTextureFilePath))) + return E_FAIL; + + return S_OK; +} +HRESULT CMaterial::Read_MaterialFile(const _char* pMaterialFilePath, const _char* pTextureFolderPath) +{ + /* Read MaterialFile to Read TextureFilePath */ + + ifstream file(pMaterialFilePath); + + if (!file.is_open()) + { + MSG_BOX("Failed to Open Materail File"); + return E_FAIL; + } + + _char szTextureFilePath[MAX_PATH] = {}; + + string strText = {}; + + string Value = {}; + string Type = {}; + + _uint iDataIndex = {}; + _uint iNumParameter = {}; + + _uint iBeginIndex = {}; + _uint iEndIndex = {}; + + _bool bParent = { false }; + + getline(file, strText); + + if (!strcmp(strText.c_str(), "none")) + return S_OK; + + while (file) + { + if (2 < iDataIndex) + break; + + if (false == bParent) + { + getline(file, strText); + auto index = strText.find_first_of("["); + auto iBackindex = strText.find_first_of("]"); + + if (index == string::npos || iBackindex == string::npos) + { + return S_OK; + } + + if (index + 1 != iBackindex - 1) + iNumParameter = (strText[iBackindex - 1] - '0') + 10; + else + iNumParameter = strText[index + 1] - '0'; + + getline(file, strText); + bParent = true; + } + else + { + if (0 == iNumParameter) + { + ++iDataIndex; + auto index = strText.find_first_of("["); + + iNumParameter = strText[index + 1] - '0'; + getline(file, strText); + } + for (_uint i = 0; i < iNumParameter; ++i) + { + for (_uint j = 0; j < 3; ++j) + getline(file, strText); + + getline(file, strText); + + switch (iDataIndex) + { + /* Scala */ + case 0: + // value + Value = strText.substr(iBeginIndex + 2); + + getline(file, strText); + // name + iBeginIndex = (_uint)strText.find_first_of('='); + + Type = strText.substr(iBeginIndex + 2); + break; + + /* Texture */ + case 1: + // value + iBeginIndex = (_uint)strText.find("Environment"); + iEndIndex = (_uint)strText.find('.'); + + if (256 < iBeginIndex || 256 < iEndIndex) + Value = "none"; + else + Value = strText.substr(iBeginIndex, iEndIndex - iBeginIndex); + + strcpy_s(szTextureFilePath, pTextureFolderPath); + strcat_s(szTextureFilePath, Value.c_str()); + + // name + iBeginIndex = (_uint)strText.find_last_of('_'); + iEndIndex = (_uint)strText.rfind('\''); + + Type = strText.substr(iBeginIndex + 1, iEndIndex - iBeginIndex - 1); + + + getline(file, strText); + + // Add Texture + if (FAILED(Add_Texture(szTextureFilePath, Type))) + return E_FAIL; + + break; + + case 2: + + break; + default: + break; + } + getline(file, strText); + } + ++iDataIndex; + bParent = false; + getline(file, strText); + } + } + + file.close(); + + return S_OK; +} +HRESULT CMaterial::Add_Texture(const _char* pTextureFolderPath, string& FileType) +{ + ID3D11ShaderResourceView* pSRV = {}; + + aiTextureType eTexture = {}; + + /* Find type */ + if (!strcmp(FileType.c_str(), "D")) + eTexture = aiTextureType::aiTextureType_DIFFUSE; + else if (!strcmp(FileType.c_str(), "N")) + eTexture = aiTextureType::aiTextureType_NORMALS; + else if (!strcmp(FileType.c_str(), "MRO")) + eTexture = aiTextureType::aiTextureType_METALNESS; + else if (!strcmp(FileType.c_str(), "MROH")) + eTexture = aiTextureType::aiTextureType_METALNESS; + else if (!strcmp(FileType.c_str(), "MROA'")) + eTexture = aiTextureType::aiTextureType_DIFFUSE_ROUGHNESS; + else if (!strcmp(FileType.c_str(), "SRO")) + eTexture = aiTextureType::aiTextureType_AMBIENT_OCCLUSION; + else if (!strcmp(FileType.c_str(), "SROH")) + eTexture = aiTextureType::aiTextureType_AMBIENT_OCCLUSION; + else if (!strcmp(FileType.c_str(), "HDR'")) + eTexture = aiTextureType::aiTextureType_DISPLACEMENT; + else + { + MSG_BOX("Failed to Path Material Texture Type"); + return S_OK; + } + _char TexturePath[MAX_PATH] = {}; + + strcpy_s(TexturePath, pTextureFolderPath); + strcat_s(TexturePath, ".dds"); + + _tchar szTextureFilePath[MAX_PATH] = {}; + + string strTexturePath; + + MultiByteToWideChar(CP_ACP, 0, TexturePath, (_int)strlen(TexturePath), szTextureFilePath, MAX_PATH); + + if (FAILED(CreateDDSTextureFromFile(m_pDevice, szTextureFilePath, nullptr, &pSRV))) + { + _char TexturePath_Png[MAX_PATH] = {}; + + strcpy_s(TexturePath_Png, pTextureFolderPath); + strcat_s(TexturePath_Png, ".png"); + + _tchar szTextureFilePath_Png[MAX_PATH] = {}; + + MultiByteToWideChar(CP_ACP, 0, TexturePath_Png, (_int)strlen(TexturePath_Png), szTextureFilePath_Png, MAX_PATH); + + if (FAILED(CreateWICTextureFromFile(m_pDevice, szTextureFilePath_Png, nullptr, &pSRV))) + { + MSG_BOX("Failed to Load TextureFile"); + return E_FAIL; + } + strTexturePath = CMyTools::ToString(szTextureFilePath_Png); + } + else + { + strTexturePath = CMyTools::ToString(szTextureFilePath); + } + + m_SaveMaterial.Path[ENUM_CLASS(eTexture)].push_back(strTexturePath); + m_SRVs[ENUM_CLASS(eTexture)].push_back(pSRV); + + return S_OK; +} HRESULT CMaterial::SaveAsBinary(HANDLE hFile, DWORD& dwByte) { // 실제 값을 저장하지 않고 파일이름.확장자명의 문자열을 저장해서 수정하기 편하게 만들기 @@ -121,6 +337,10 @@ CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContex return pInstance; } +CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pModelFilePath, const _char* pMaterialName) +{ + return nullptr; +} #endif HRESULT CMaterial::Bind_SRV(CShader* pShader, const _char* pConstantName, _uint iType, _uint iTextureIndex) diff --git a/Jusin154_Team3D/Engine/Private/Model.cpp b/Jusin154_Team3D/Engine/Private/Model.cpp index d1b5950ae..4846838cc 100644 --- a/Jusin154_Team3D/Engine/Private/Model.cpp +++ b/Jusin154_Team3D/Engine/Private/Model.cpp @@ -328,6 +328,73 @@ HRESULT CModel::Ready_Materials(const aiScene* pAIScene, const _char* pModelFile return S_OK; } +HRESULT CModel::Ready_Materials_FromFile(const aiScene* pAIScene, const _char* pModelFilePath) +{ + m_iNumMaterials = pAIScene->mNumMaterials; + m_Materials.reserve(m_iNumMaterials); + + _char szDrive[MAX_PATH] = {}; + _char szDir[MAX_PATH] = {}; + _char szFileName[MAX_PATH] = {}; + _char szMeshFilePath[MAX_PATH] = {}; + vector<_string> MaterialFilePathes; + + _splitpath_s(pModelFilePath, szDrive, MAX_PATH, szDir, MAX_PATH, szFileName, MAX_PATH, nullptr, 0); + + string FileName = szFileName; + + auto iPos = FileName.find_last_of('.'); + + FileName = FileName.substr(0, iPos); + + strcpy_s(szMeshFilePath, szDrive); + strcat_s(szMeshFilePath, szDir); + strcat_s(szMeshFilePath, FileName.c_str()); + strcat_s(szMeshFilePath, ".props.txt"); + + ifstream file(szMeshFilePath); + + if (!file.is_open()) + { + MSG_BOX("Failed to Open Mesh File"); + return E_FAIL; + } + + string strText = {}; + _uint iNumParameter = {}; + + getline(file, strText); + getline(file, strText); + + for (_uint i = 0; i < m_iNumMaterials; ++i) + { + getline(file, strText); + + // value + _uint iBeginIndex = (_uint)strText.find("Environment"); + _uint iEndIndex = (_uint)strText.find('.'); + + if ((_uint)strText.size() < iEndIndex - iBeginIndex) + { + MSG_BOX("Fail Path"); + return E_FAIL; + } + + string strPath = "../Bin/Resources/Models/" + strText.substr(iBeginIndex, iEndIndex - iBeginIndex); + MaterialFilePathes.push_back(strPath); + } + + for (size_t i = 0; i < m_iNumMaterials; ++i) { + CMaterial* pMaterial = CMaterial::Create(m_pDevice, m_pContext, MaterialFilePathes[i].c_str(), pAIScene->mMaterials[i]->GetName().C_Str()); + if (nullptr == pMaterial) { + return E_FAIL; + } + m_Materials.push_back(pMaterial); + } + m_Materials.shrink_to_fit(); + return S_OK; +} + HRESULT CModel::Ready_Animations(const aiScene* pAIScene) { m_iNumAnimations = pAIScene->mNumAnimations; @@ -866,7 +933,7 @@ HRESULT CModel::Assimp_Model_Load(const _char* pModelFilePath, MODEL eType, _fma _uint iFlag = {}; iFlag = aiProcess_ConvertToLeftHanded | aiProcessPreset_TargetRealtime_Fast; - if (MODEL::NONANIM == eType) { + if (MODEL::NONANIM == eType || MODEL::ENVIROMENT == eType) { iFlag |= aiProcess_PreTransformVertices; } m_iRootBoneIndex = iRootBoneIndex; @@ -887,8 +954,15 @@ HRESULT CModel::Assimp_Model_Load(const _char* pModelFilePath, MODEL eType, _fma } #pragma endregion #pragma region Material - if (FAILED(Ready_Materials(m_pAIScene, pModelFilePath))) { - return E_FAIL; + if (MODEL::ENVIROMENT == eType) + { + + } + else + { + if (FAILED(Ready_Materials(m_pAIScene, pModelFilePath))) { + return E_FAIL; + } } #pragma endregion #pragma region Animation diff --git a/Jusin154_Team3D/Engine/Private/VIBuffer_Terrain.cpp b/Jusin154_Team3D/Engine/Private/VIBuffer_Terrain.cpp index e084986df..ac794b025 100644 --- a/Jusin154_Team3D/Engine/Private/VIBuffer_Terrain.cpp +++ b/Jusin154_Team3D/Engine/Private/VIBuffer_Terrain.cpp @@ -192,7 +192,7 @@ _bool CVIBuffer_Terrain::Picking(CTransform* pTransform, _float3& pOut) return false; } -/ + CVIBuffer_Terrain * CVIBuffer_Terrain::Create(ID3D11Device * pDevice, ID3D11DeviceContext * pContext, const _char * pFilePath, _uint iSizeX, _uint iSizeZ) { diff --git a/Jusin154_Team3D/Engine/Public/Engine_Enum.h b/Jusin154_Team3D/Engine/Public/Engine_Enum.h index 2a70cd4fd..6b64ab67a 100644 --- a/Jusin154_Team3D/Engine/Public/Engine_Enum.h +++ b/Jusin154_Team3D/Engine/Public/Engine_Enum.h @@ -21,7 +21,7 @@ enum class TEXTURE_LOAD_TYPE { enum class MOUSEKEYSTATE { LBUTTON, RBUTTON, WBUTTON, XBUTTON }; enum class MOUSEMOVESTATE { X, Y, W }; enum class D3DTS { VIEW, PROJ, VIEW_INV, PROJ_INV, END }; -enum class MODEL { ANIM, NONANIM }; +enum class MODEL { ANIM, NONANIM, ENVIROMENT }; enum class NAVI_POINT { A, B, C, END }; enum class NAVI_LINE { AB, BC, CA, END }; diff --git a/Jusin154_Team3D/Engine/Public/Material.h b/Jusin154_Team3D/Engine/Public/Material.h index 4c8764da7..77b2c57f6 100644 --- a/Jusin154_Team3D/Engine/Public/Material.h +++ b/Jusin154_Team3D/Engine/Public/Material.h @@ -24,10 +24,14 @@ class ENGINE_DLL CMaterial final : public CBase #ifdef EDITOR_PROJECT public: static CMaterial* Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pModelFilePath, const aiMaterial* pAIMaterial); + static CMaterial* Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pModelFilePath, const _char* pMaterialName); HRESULT SaveAsBinary(HANDLE hFile, DWORD& dwByte); private: vector m_strPath[AI_TEXTURE_TYPE_MAX]; HRESULT Initialize(const _char* pModelFilePath, const aiMaterial* pAIMaterial); + HRESULT Initialize(const _char* pMaterialFilePath); + HRESULT Read_MaterialFile(const _char* pMaterialFilePath, const _char* pTextureFolderPath); + HRESULT Add_Texture(const _char* pTextureFolderPath, string& FileType); #endif // EDITOR_PROJECT virtual HRESULT Initialize(const _char* pModelFilePath, HANDLE hFile, DWORD& dwByte); diff --git a/Jusin154_Team3D/Engine/Public/Model.h b/Jusin154_Team3D/Engine/Public/Model.h index b248ee612..c3f0ea4f5 100644 --- a/Jusin154_Team3D/Engine/Public/Model.h +++ b/Jusin154_Team3D/Engine/Public/Model.h @@ -51,6 +51,7 @@ class ENGINE_DLL CModel final : public CComponent HRESULT Assimp_Model_Load(const _char* pModelFilePath, MODEL eType, _fmatrix& PreTransformMatrix, _uint iRootBoneIndex); HRESULT Ready_Meshes(MODEL eType, const aiScene* pAIScene, _fmatrix& PreTransformMatrix); HRESULT Ready_Materials(const aiScene* pAIScene, const _char* pModelFilePath); + HRESULT Ready_Materials_FromFile(const aiScene* pAIScene, const _char* pModelFilePath); HRESULT Ready_Animations(const aiScene* pAIScene); HRESULT Ready_Bones(const aiNode* pAINode, _int iParentIndex); // 바이너리