Skip to content

Commit

Permalink
fix: texture set and material object configs not applying in some cas…
Browse files Browse the repository at this point in the history
…es. (#492)

* fix: fixes for texture set and material object configs not applying in some cases.

* style: 🎨 apply clang-format changes

---------

Co-authored-by: Ilya Perapechka <i_perapechka@wargaming.net>
Co-authored-by: Jonahex <Jonahex@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 31, 2024
1 parent 149c4d8 commit f5248c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/TruePBR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,11 +1551,33 @@ struct TESBoundObject_Clone3D
static inline REL::Relocation<decltype(thunk)> 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]);
Expand Down
2 changes: 2 additions & 0 deletions src/TruePBR.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ struct TruePBR
std::unordered_map<std::string, PBRMaterialObjectData> pbrMaterialObjects;
std::string selectedPbrMaterialObjectName;
PBRMaterialObjectData* selectedPbrMaterialObject = nullptr;

RE::BGSTextureSet* currentTextureSet = nullptr;
};
13 changes: 12 additions & 1 deletion src/TruePBR/BSLightingShaderMaterialPBR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -57,6 +60,10 @@ std::uint32_t BSLightingShaderMaterialPBR::ComputeCRC32(uint32_t srcHash)
std::array<float, 3> 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;
Expand All @@ -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<const char*>()(textureSet->GetTexturePath(RmaosTexture));
hashes.emissiveHash = RE::BSCRC32<const char*>()(textureSet->GetTexturePath(EmissiveTexture));
Expand Down Expand Up @@ -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<RE::BGSTextureSet*>(inTextureSet); bgsTextureSet != nullptr) {
if (auto* bgsTextureSet = TruePBR::GetSingleton()->currentTextureSet) {
if (auto* textureSetData = TruePBR::GetSingleton()->GetPBRTextureSetData(bgsTextureSet)) {
ApplyTextureSetData(*textureSetData);
All[this].textureSetData = textureSetData;
Expand Down
2 changes: 2 additions & 0 deletions src/TruePBR/BSLightingShaderMaterialPBRLandscape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f5248c6

Please sign in to comment.