From f5248c6f55434ad882024a2f59af5d9b8368e2d0 Mon Sep 17 00:00:00 2001 From: Ilya Perapechka <50655277+Jonahex@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:40:33 +0300 Subject: [PATCH] fix: texture set and material object configs not applying in some cases. (#492) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: fixes for texture set and material object configs not applying in some cases. * style: 🎨 apply clang-format changes --------- Co-authored-by: Ilya Perapechka Co-authored-by: Jonahex --- src/TruePBR.cpp | 22 +++++++++++++++++++ src/TruePBR.h | 2 ++ src/TruePBR/BSLightingShaderMaterialPBR.cpp | 13 ++++++++++- .../BSLightingShaderMaterialPBRLandscape.cpp | 2 ++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/TruePBR.cpp b/src/TruePBR.cpp index 0abee2dab..8bdb95162 100644 --- a/src/TruePBR.cpp +++ b/src/TruePBR.cpp @@ -1551,11 +1551,33 @@ struct TESBoundObject_Clone3D static inline REL::Relocation func; }; +RE::BSShaderTextureSet* hk_BGSTextureSet_ToShaderTextureSet(RE::BGSTextureSet* textureSet); +decltype(&hk_BGSTextureSet_ToShaderTextureSet) ptr_BGSTextureSet_ToShaderTextureSet; +RE::BSShaderTextureSet* hk_BGSTextureSet_ToShaderTextureSet(RE::BGSTextureSet* textureSet) +{ + TruePBR::GetSingleton()->currentTextureSet = textureSet; + + return ptr_BGSTextureSet_ToShaderTextureSet(textureSet); +} + +void hk_BSLightingShaderProperty_OnLoadTextureSet(RE::BSLightingShaderProperty* property, void* a2); +decltype(&hk_BSLightingShaderProperty_OnLoadTextureSet) ptr_BSLightingShaderProperty_OnLoadTextureSet; +void hk_BSLightingShaderProperty_OnLoadTextureSet(RE::BSLightingShaderProperty* property, void* a2) +{ + ptr_BSLightingShaderProperty_OnLoadTextureSet(property, a2); + + TruePBR::GetSingleton()->currentTextureSet = nullptr; +} + void TruePBR::PostPostLoad() { + logger::info("Hooking BGSTextureSet"); + *(uintptr_t*)&ptr_BGSTextureSet_ToShaderTextureSet = Detours::X64::DetourFunction(REL::RelocationID(20905, 21361).address(), (uintptr_t)&hk_BGSTextureSet_ToShaderTextureSet); + logger::info("Hooking BSLightingShaderProperty"); stl::write_vfunc<0x18, BSLightingShaderProperty_LoadBinary>(RE::VTABLE_BSLightingShaderProperty[0]); stl::write_vfunc<0x2A, BSLightingShaderProperty_GetRenderPasses>(RE::VTABLE_BSLightingShaderProperty[0]); + *(uintptr_t*)&ptr_BSLightingShaderProperty_OnLoadTextureSet = Detours::X64::DetourFunction(REL::RelocationID(99865, 106510).address(), (uintptr_t)&hk_BSLightingShaderProperty_OnLoadTextureSet); logger::info("Hooking BSLightingShader"); stl::write_vfunc<0x4, BSLightingShader_SetupMaterial>(RE::VTABLE_BSLightingShader[0]); diff --git a/src/TruePBR.h b/src/TruePBR.h index 3dc9877a7..6298e4465 100644 --- a/src/TruePBR.h +++ b/src/TruePBR.h @@ -110,4 +110,6 @@ struct TruePBR std::unordered_map pbrMaterialObjects; std::string selectedPbrMaterialObjectName; PBRMaterialObjectData* selectedPbrMaterialObject = nullptr; + + RE::BGSTextureSet* currentTextureSet = nullptr; }; diff --git a/src/TruePBR/BSLightingShaderMaterialPBR.cpp b/src/TruePBR/BSLightingShaderMaterialPBR.cpp index 9ee0a35a8..ec3669c4d 100644 --- a/src/TruePBR/BSLightingShaderMaterialPBR.cpp +++ b/src/TruePBR/BSLightingShaderMaterialPBR.cpp @@ -33,12 +33,15 @@ void BSLightingShaderMaterialPBR::CopyMembers(RE::BSShaderMaterial* that) projectedMaterialBaseColorScale = pbrThat->projectedMaterialBaseColorScale; projectedMaterialRoughness = pbrThat->projectedMaterialRoughness; projectedMaterialSpecularLevel = pbrThat->projectedMaterialSpecularLevel; + projectedMaterialGlintParameters = pbrThat->projectedMaterialGlintParameters; rmaosTexture = pbrThat->rmaosTexture; emissiveTexture = pbrThat->emissiveTexture; displacementTexture = pbrThat->displacementTexture; featuresTexture0 = pbrThat->featuresTexture0; featuresTexture1 = pbrThat->featuresTexture1; + + All[this] = All[pbrThat]; } std::uint32_t BSLightingShaderMaterialPBR::ComputeCRC32(uint32_t srcHash) @@ -57,6 +60,10 @@ std::uint32_t BSLightingShaderMaterialPBR::ComputeCRC32(uint32_t srcHash) std::array projectedMaterialBaseColorScale = { 0.f, 0.f, 0.f }; float projectedMaterialRoughness = 0.f; float projectedMaterialSpecularLevel = 0.f; + float projectedMaterialScreenSpaceScale = 0.f; + float projectedMaterialLogMicrofacetDensity = 0.f; + float projectedMaterialMicrofacetRoughness = 0.f; + float projectedMaterialDensityRandomization = 0.f; uint32_t rmaodHash = 0; uint32_t emissiveHash = 0; uint32_t displacementHash = 0; @@ -81,6 +88,10 @@ std::uint32_t BSLightingShaderMaterialPBR::ComputeCRC32(uint32_t srcHash) hashes.projectedMaterialBaseColorScale[2] = projectedMaterialBaseColorScale[2] * 100.f; hashes.projectedMaterialRoughness = projectedMaterialRoughness * 100.f; hashes.projectedMaterialSpecularLevel = projectedMaterialSpecularLevel * 100.f; + hashes.projectedMaterialScreenSpaceScale = projectedMaterialGlintParameters.screenSpaceScale * 100.f; + hashes.projectedMaterialLogMicrofacetDensity = projectedMaterialGlintParameters.logMicrofacetDensity * 100.f; + hashes.projectedMaterialMicrofacetRoughness = projectedMaterialGlintParameters.microfacetRoughness * 100.f; + hashes.projectedMaterialDensityRandomization = projectedMaterialGlintParameters.densityRandomization * 100.f; if (textureSet != nullptr) { hashes.rmaodHash = RE::BSCRC32()(textureSet->GetTexturePath(RmaosTexture)); hashes.emissiveHash = RE::BSCRC32()(textureSet->GetTexturePath(EmissiveTexture)); @@ -157,7 +168,7 @@ void BSLightingShaderMaterialPBR::OnLoadTextureSet(std::uint64_t arg1, RE::BSTex textureSet->SetTexture(FeaturesTexture0, featuresTexture0); textureSet->SetTexture(FeaturesTexture1, featuresTexture1); - if (auto* bgsTextureSet = netimmerse_cast(inTextureSet); bgsTextureSet != nullptr) { + if (auto* bgsTextureSet = TruePBR::GetSingleton()->currentTextureSet) { if (auto* textureSetData = TruePBR::GetSingleton()->GetPBRTextureSetData(bgsTextureSet)) { ApplyTextureSetData(*textureSetData); All[this].textureSetData = textureSetData; diff --git a/src/TruePBR/BSLightingShaderMaterialPBRLandscape.cpp b/src/TruePBR/BSLightingShaderMaterialPBRLandscape.cpp index 2b0cc1576..d6497601d 100644 --- a/src/TruePBR/BSLightingShaderMaterialPBRLandscape.cpp +++ b/src/TruePBR/BSLightingShaderMaterialPBRLandscape.cpp @@ -48,6 +48,8 @@ void BSLightingShaderMaterialPBRLandscape::CopyMembers(RE::BSShaderMaterial* tha pbrThat->terrainTexOffsetY = terrainTexOffsetY; pbrThat->terrainTexFade = terrainTexFade; pbrThat->glintParameters = glintParameters; + + All[this] = All[pbrThat]; } RE::BSShaderMaterial::Feature BSLightingShaderMaterialPBRLandscape::GetFeature() const