Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package/Shaders/Common/Glints/Glints2023.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ void UnpackFloatParallel4(float4 input, out float4 a, out float4 b)

struct GlintInput
{
bool enabled;
float3 H;
float2 uv;
float2 duvdx;
Expand Down
70 changes: 30 additions & 40 deletions package/Shaders/Common/PBR.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,13 @@ namespace PBR
surfaceProperties.FuzzWeight = 0;

surfaceProperties.GlintScreenSpaceScale = 1.5;
surfaceProperties.GlintLogMicrofacetDensity = 18.0;
surfaceProperties.GlintLogMicrofacetDensity = 40.0;
surfaceProperties.GlintMicrofacetRoughness = 0.015;
surfaceProperties.GlintDensityRandomization = 2.0;

return surfaceProperties;
}

#if !defined(GLINT)
struct GlintInput
{};
#endif

float3 AdjustDirectionalLightColor(float3 lightColor)
{
return pbrSettings.DirectionalLightColorMultiplier * sRGB2Lin(lightColor);
Expand Down Expand Up @@ -160,16 +155,22 @@ namespace PBR
return (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);
}

float3 GetSpecularDirectLightMultiplierMicrofacet(float roughness, float3 specularColor, float NdotL, float NdotV, float NdotH, float VdotH, GlintInput glintInput, out float3 F)
#if defined(GLINT)
float3 GetSpecularDirectLightMultiplierMicrofacetWithGlint(float roughness, float3 specularColor, float NdotL, float NdotV, float NdotH, float VdotH, GlintInput glintInput, out float3 F)
{
float D = GetNormalDistributionFunctionGGX(roughness, NdotH);
#if defined(GLINT)
[branch] if (glintInput.enabled)
{
float D_max = GetNormalDistributionFunctionGGX(roughness, 1);
D = SampleGlints2023NDF(glintInput, D, D_max);
}
float D_max = GetNormalDistributionFunctionGGX(roughness, 1);
D = SampleGlints2023NDF(glintInput, D, D_max);
float G = GetVisibilityFunctionSmithJointApprox(roughness, NdotV, NdotL);
F = GetFresnelFactorSchlick(specularColor, VdotH);

return D * G * F;
}
#endif

float3 GetSpecularDirectLightMultiplierMicrofacet(float roughness, float3 specularColor, float NdotL, float NdotV, float NdotH, float VdotH, out float3 F)
{
float D = GetNormalDistributionFunctionGGX(roughness, NdotH);
float G = GetVisibilityFunctionSmithJointApprox(roughness, NdotV, NdotL);
F = GetFresnelFactorSchlick(specularColor, VdotH);

Expand Down Expand Up @@ -401,28 +402,24 @@ namespace PBR
{
diffuse += lightColor * satNdotL;

GlintInput glintInput;
#if defined(GLINT)
# if defined(LANDSCAPE)
glintInput.enabled = PBRFlags & TruePBR_LandGlint;
# else
glintInput.enabled = PBRFlags & TruePBR_Glint;
# endif
[branch] if (glintInput.enabled)
{
glintInput.H = mul(H, tbn);
glintInput.uv = uv;
glintInput.duvdx = ddx(uv);
glintInput.duvdy = ddy(uv);
glintInput.ScreenSpaceScale = surfaceProperties.GlintScreenSpaceScale;
glintInput.LogMicrofacetDensity = surfaceProperties.GlintLogMicrofacetDensity;
glintInput.MicrofacetRoughness = surfaceProperties.GlintMicrofacetRoughness;
glintInput.DensityRandomization = surfaceProperties.GlintDensityRandomization;
}
GlintInput glintInput;
glintInput.H = mul(tbn, H);
glintInput.uv = uv;
glintInput.duvdx = ddx(uv);
glintInput.duvdy = ddy(uv);
glintInput.ScreenSpaceScale = max(1, surfaceProperties.GlintScreenSpaceScale);
glintInput.LogMicrofacetDensity = 40 - clamp(surfaceProperties.GlintLogMicrofacetDensity, 0, 40);
glintInput.MicrofacetRoughness = clamp(surfaceProperties.GlintMicrofacetRoughness, 0.005, 0.3);
glintInput.DensityRandomization = clamp(surfaceProperties.GlintDensityRandomization, 0, 5);
#endif

float3 F;
specular += PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.Roughness, surfaceProperties.F0, satNdotL, satNdotV, satNdotH, satVdotH, glintInput, F) * lightColor * satNdotL;
#if defined(GLINT)
specular += PI * GetSpecularDirectLightMultiplierMicrofacetWithGlint(surfaceProperties.Roughness, surfaceProperties.F0, satNdotL, satNdotV, satNdotH, satVdotH, glintInput, F) * lightColor * satNdotL;
#else
specular += PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.Roughness, surfaceProperties.F0, satNdotL, satNdotV, satNdotH, satVdotH, F) * lightColor * satNdotL;
#endif

float2 specularBRDF = 0;
[branch] if (pbrSettings.UseMultipleScattering)
Expand Down Expand Up @@ -468,10 +465,7 @@ namespace PBR
}

float3 coatF;
# if defined(GLINT)
glintInput.H = mul(coatH, tbn);
# endif
float3 coatSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.CoatRoughness, surfaceProperties.CoatF0, coatNdotL, coatNdotV, coatNdotH, coatVdotH, glintInput, coatF) * coatLightColor * coatNdotL;
float3 coatSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.CoatRoughness, surfaceProperties.CoatF0, coatNdotL, coatNdotV, coatNdotH, coatVdotH, coatF) * coatLightColor * coatNdotL;

float3 layerAttenuation = 1 - coatF * surfaceProperties.CoatStrength;
diffuse *= layerAttenuation;
Expand All @@ -495,12 +489,8 @@ namespace PBR
float NdotH = saturate(dot(N, H));
float VdotH = saturate(dot(V, H));

GlintInput glintInput;
#if defined(GLINT)
glintInput.enabled = false;
#endif
float3 wetnessF;
float3 wetnessSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(roughness, wetnessF0, NdotL, NdotV, NdotH, VdotH, glintInput, wetnessF) * lightColor * NdotL;
float3 wetnessSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(roughness, wetnessF0, NdotL, NdotV, NdotH, VdotH, wetnessF) * lightColor * NdotL;

return wetnessSpecular * wetnessStrength;
}
Expand Down
74 changes: 38 additions & 36 deletions src/TruePBR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,47 +184,14 @@ void TruePBR::SaveSettings(json& o_json)
void TruePBR::PrePass()
{
auto context = State::GetSingleton()->context;
if (context && glintsNoiseTexture) {
if (context) {
if (!glintsNoiseTexture)
SetupGlintsTexture();
ID3D11ShaderResourceView* srv = glintsNoiseTexture->srv.get();
context->PSSetShaderResources(28, 1, &srv);
}
}

void TruePBR::SetupFrame()
{
float newDirectionalLightScale = 1.f;
float newDirectionalAmbientLightScale = 1.f;

if (const auto* player = RE::PlayerCharacter::GetSingleton()) {
if (const auto* currentCell = player->GetParentCell()) {
if (currentCell->IsInteriorCell()) {
if (const auto* lightingTemplate = currentCell->GetRuntimeData().lightingTemplate) {
const auto* editorId = lightingTemplate->GetFormEditorID();
if (auto it = pbrLightingTemplates.find(editorId); it != pbrLightingTemplates.cend()) {
newDirectionalLightScale = it->second.directionalLightColorScale;
newDirectionalAmbientLightScale = it->second.directionalAmbientLightColorScale;
}
}
} else if (RE::Sky* sky = RE::Sky::GetSingleton()) {
if (const auto* weather = sky->currentWeather) {
const auto* editorId = weather->GetFormEditorID();
if (auto it = pbrWeathers.find(editorId); it != pbrWeathers.cend()) {
newDirectionalLightScale = it->second.directionalLightColorScale;
newDirectionalAmbientLightScale = it->second.directionalAmbientLightColorScale;
}
}
}
}
}

weatherPBRDirectionalLightColorMultiplier = newDirectionalLightScale;
weatherPBRDirectionalAmbientLightColorMultiplier = newDirectionalAmbientLightScale;

settings.directionalLightColorMultiplier = globalPBRDirectLightColorMultiplier * weatherPBRDirectionalLightColorMultiplier;
settings.pointLightColorMultiplier = globalPBRDirectLightColorMultiplier;
settings.ambientLightColorMultiplier = globalPBRAmbientLightColorMultiplier * weatherPBRDirectionalAmbientLightColorMultiplier;
}

void TruePBR::SetupGlintsTexture()
{
constexpr uint noiseTexSize = 512;
Expand Down Expand Up @@ -295,6 +262,41 @@ void TruePBR::SetupGlintsTexture()
noiseGenProgram->Release();
}

void TruePBR::SetupFrame()
{
float newDirectionalLightScale = 1.f;
float newDirectionalAmbientLightScale = 1.f;

if (const auto* player = RE::PlayerCharacter::GetSingleton()) {
if (const auto* currentCell = player->GetParentCell()) {
if (currentCell->IsInteriorCell()) {
if (const auto* lightingTemplate = currentCell->GetRuntimeData().lightingTemplate) {
const auto* editorId = lightingTemplate->GetFormEditorID();
if (auto it = pbrLightingTemplates.find(editorId); it != pbrLightingTemplates.cend()) {
newDirectionalLightScale = it->second.directionalLightColorScale;
newDirectionalAmbientLightScale = it->second.directionalAmbientLightColorScale;
}
}
} else if (RE::Sky* sky = RE::Sky::GetSingleton()) {
if (const auto* weather = sky->currentWeather) {
const auto* editorId = weather->GetFormEditorID();
if (auto it = pbrWeathers.find(editorId); it != pbrWeathers.cend()) {
newDirectionalLightScale = it->second.directionalLightColorScale;
newDirectionalAmbientLightScale = it->second.directionalAmbientLightColorScale;
}
}
}
}
}

weatherPBRDirectionalLightColorMultiplier = newDirectionalLightScale;
weatherPBRDirectionalAmbientLightColorMultiplier = newDirectionalAmbientLightScale;

settings.directionalLightColorMultiplier = globalPBRDirectLightColorMultiplier * weatherPBRDirectionalLightColorMultiplier;
settings.pointLightColorMultiplier = globalPBRDirectLightColorMultiplier;
settings.ambientLightColorMultiplier = globalPBRAmbientLightColorMultiplier * weatherPBRDirectionalAmbientLightColorMultiplier;
}

void TruePBR::SetupTextureSetData()
{
logger::info("[TruePBR] loading PBR texture set configs");
Expand Down
2 changes: 1 addition & 1 deletion src/TruePBR.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct GlintParameters
{
bool enabled = false;
float screenSpaceScale = 1.5f;
float logMicrofacetDensity = 18.f;
float logMicrofacetDensity = 40.f;
float microfacetRoughness = .015f;
float densityRandomization = 2.f;

Expand Down