Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PBR asset matching #448

Merged
merged 5 commits into from
Aug 23, 2024
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
5 changes: 4 additions & 1 deletion package/Shaders/AmbientCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ RWTexture2D<half3> DiffuseAmbientRW : register(u1);
half3 linDirectionalAmbientColor = sRGB2Lin(directionalAmbientColor);
half3 linDiffuseColor = sRGB2Lin(diffuseColor);

half3 linAmbient = lerp(sRGB2Lin(albedo * directionalAmbientColor), linAlbedo * linDirectionalAmbientColor, pbrWeight);

half visibility = 1.0;
#if defined(SKYLIGHTING)
float rawDepth = DepthTexture[dispatchID.xy];
Expand Down Expand Up @@ -87,10 +89,11 @@ RWTexture2D<half3> DiffuseAmbientRW : register(u1);
linDiffuseColor += ssgiDiffuse.rgb;
#endif

linAmbient *= visibility;
diffuseColor = Lin2sRGB(linDiffuseColor);
directionalAmbientColor = Lin2sRGB(linDirectionalAmbientColor * visibility);

diffuseColor = diffuseColor + directionalAmbientColor * albedo;
diffuseColor = lerp(diffuseColor + directionalAmbientColor * albedo, Lin2sRGB(linDiffuseColor + linAmbient), pbrWeight);

MainRW[dispatchID.xy] = diffuseColor;
};
29 changes: 12 additions & 17 deletions package/Shaders/Common/PBR.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,20 @@ namespace PBR

struct LightProperties
{
float3 LightColor;
float3 CoatLightColor;
float3 LinearLightColor;
float3 LinearCoatLightColor;
};

LightProperties InitLightProperties(float3 lightColor, float3 nonParallaxShadow, float3 parallaxShadow)
{
LightProperties result;
result.LightColor = lightColor * nonParallaxShadow * parallaxShadow;
result.LinearLightColor = sRGB2Lin(lightColor) * nonParallaxShadow * parallaxShadow;
[branch] if ((PBRFlags & TruePBR_InterlayerParallax) != 0)
{
result.CoatLightColor = lightColor * nonParallaxShadow;
result.LinearCoatLightColor = sRGB2Lin(lightColor) * nonParallaxShadow;
}
else
{
result.CoatLightColor = result.LightColor;
result.LinearCoatLightColor = result.LinearLightColor;
}
return result;
Expand Down Expand Up @@ -423,12 +418,12 @@ namespace PBR
#if !defined(LANDSCAPE) && !defined(LODLANDSCAPE)
[branch] if ((PBRFlags & TruePBR_HairMarschner) != 0)
{
transmission += PI * lightProperties.LightColor * GetHairColorMarschner(N, V, L, NdotL, NdotV, VdotL, 0, 1, 0, surfaceProperties);
transmission += lightProperties.LinearLightColor * GetHairColorMarschner(N, V, L, NdotL, NdotV, VdotL, 0, 1, 0, surfaceProperties);
}
else
#endif
{
diffuse += lightProperties.LightColor * satNdotL;
diffuse += lightProperties.LinearLightColor * satNdotL * GetDiffuseDirectLightMultiplierLambert();

#if defined(GLINT)
GlintInput glintInput;
Expand All @@ -444,9 +439,9 @@ namespace PBR

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

float2 specularBRDF = 0;
Expand All @@ -459,7 +454,7 @@ namespace PBR
#if !defined(LANDSCAPE) && !defined(LODLANDSCAPE)
[branch] if ((PBRFlags & TruePBR_Fuzz) != 0)
{
float3 fuzzSpecular = PI * GetSpecularDirectLightMultiplierMicroflakes(surfaceProperties.Roughness, surfaceProperties.FuzzColor, satNdotL, satNdotV, satNdotH, satVdotH) * lightProperties.LinearLightColor * satNdotL;
float3 fuzzSpecular = GetSpecularDirectLightMultiplierMicroflakes(surfaceProperties.Roughness, surfaceProperties.FuzzColor, satNdotL, satNdotV, satNdotH, satVdotH) * lightProperties.LinearLightColor * satNdotL;
[branch] if (pbrSettings.UseMultipleScattering)
{
fuzzSpecular *= 1 + surfaceProperties.FuzzColor * (1 / (specularBRDF.x + specularBRDF.y) - 1);
Expand All @@ -474,7 +469,7 @@ namespace PBR
float forwardScatter = exp2(saturate(-VdotL) * subsurfacePower - subsurfacePower);
float backScatter = saturate(satNdotL * surfaceProperties.Thickness + (1.0 - surfaceProperties.Thickness)) * 0.5;
float subsurface = lerp(backScatter, 1, forwardScatter) * (1.0 - surfaceProperties.Thickness);
transmission += surfaceProperties.SubsurfaceColor * subsurface * lightProperties.LightColor;
transmission += surfaceProperties.SubsurfaceColor * subsurface * lightProperties.LinearLightColor;
}
else if ((PBRFlags & TruePBR_TwoLayer) != 0)
{
Expand All @@ -493,13 +488,13 @@ namespace PBR
}

float3 coatF;
float3 coatSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.CoatRoughness, surfaceProperties.CoatF0, coatNdotL, coatNdotV, coatNdotH, coatVdotH, coatF) * lightProperties.LinearCoatLightColor * coatNdotL;
float3 coatSpecular = GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.CoatRoughness, surfaceProperties.CoatF0, coatNdotL, coatNdotV, coatNdotH, coatVdotH, coatF) * lightProperties.LinearCoatLightColor * coatNdotL;

float3 layerAttenuation = 1 - coatF * surfaceProperties.CoatStrength;
diffuse *= layerAttenuation;
specular *= layerAttenuation;

coatDiffuse += lightProperties.CoatLightColor * coatNdotL;
coatDiffuse += lightProperties.LinearCoatLightColor * coatNdotL;
specular += coatSpecular * surfaceProperties.CoatStrength;
}
#endif
Expand All @@ -518,7 +513,7 @@ namespace PBR
float VdotH = saturate(dot(V, H));

float3 wetnessF;
float3 wetnessSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(roughness, wetnessF0, NdotL, NdotV, NdotH, VdotH, wetnessF) * lightColor * NdotL;
float3 wetnessSpecular = GetSpecularDirectLightMultiplierMicrofacet(roughness, wetnessF0, NdotL, NdotV, NdotH, VdotH, wetnessF) * lightColor * NdotL;

return wetnessSpecular * wetnessStrength;
}
Expand Down Expand Up @@ -593,7 +588,7 @@ namespace PBR
// https://marmosetco.tumblr.com/post/81245981087
float3 R = reflect(-V, N);
float horizon = min(1.0 + dot(R, VN), 1.0);
horizon *= horizon * horizon;
horizon = horizon * horizon;
specularLobeWeight *= horizon;

float3 diffuseAO = surfaceProperties.AO;
Expand All @@ -604,7 +599,7 @@ namespace PBR
specularAO = MultiBounceAO(surfaceProperties.F0, specularAO.x).y;
}

diffuseLobeWeight *= diffuseAO;
diffuseLobeWeight *= diffuseAO / PI;
specularLobeWeight *= specularAO;
}

Expand All @@ -621,7 +616,7 @@ namespace PBR
// https://marmosetco.tumblr.com/post/81245981087
float3 R = reflect(-V, N);
float horizon = min(1.0 + dot(R, VN), 1.0);
horizon *= horizon * horizon;
horizon = horizon * horizon;
specularLobeWeight *= horizon;

return specularLobeWeight * wetnessStrength;
Expand Down
35 changes: 16 additions & 19 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if defined(TRUE_PBR) && defined(LANDSCAPE)
[branch] if ((PBRFlags & TruePBR_LandTile0PBR) == 0)
{
rawBaseColor.rgb = pow(rawBaseColor.rgb, pbrSettings.BaseColorGamma);
rawBaseColor.rgb = sRGB2Lin(rawBaseColor.rgb) * PI;
}
# endif
baseColor = rawBaseColor;
Expand Down Expand Up @@ -1382,7 +1382,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
}
else
{
landColor2.rgb = pow(landColor2.rgb, pbrSettings.BaseColorGamma);
landColor2.rgb = sRGB2Lin(landColor2.rgb) * PI;
rawRMAOS += input.LandBlendWeights1.y * float4(1 - landNormal2.w, 0, 1, 0.04);
}
# endif
Expand Down Expand Up @@ -1410,7 +1410,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
}
else
{
landColor3.rgb = pow(landColor3.rgb, pbrSettings.BaseColorGamma);
landColor3.rgb = sRGB2Lin(landColor3.rgb) * PI;
rawRMAOS += input.LandBlendWeights1.z * float4(1 - landNormal3.w, 0, 1, 0.04);
}
# endif
Expand Down Expand Up @@ -1438,7 +1438,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
}
else
{
landColor4.rgb = pow(landColor4.rgb, pbrSettings.BaseColorGamma);
landColor4.rgb = sRGB2Lin(landColor4.rgb) * PI;
rawRMAOS += input.LandBlendWeights1.w * float4(1 - landNormal4.w, 0, 1, 0.04);
}
# endif
Expand Down Expand Up @@ -1466,7 +1466,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
}
else
{
landColor5.rgb = pow(landColor5.rgb, pbrSettings.BaseColorGamma);
landColor5.rgb = sRGB2Lin(landColor5.rgb) * PI;
rawRMAOS += input.LandBlendWeights2.x * float4(1 - landNormal5.w, 0, 1, 0.04);
}
# endif
Expand Down Expand Up @@ -1494,7 +1494,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
}
else
{
landColor6.rgb = pow(landColor6.rgb, pbrSettings.BaseColorGamma);
landColor6.rgb = sRGB2Lin(landColor6.rgb) * PI;
rawRMAOS += input.LandBlendWeights2.y * float4(1 - landNormal6.w, 0, 1, 0.04);
}
# endif
Expand Down Expand Up @@ -1655,8 +1655,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

baseColor.xyz *= 1 - pbrSurfaceProperties.Metallic;

baseColor.xyz = pow(baseColor.xyz, 1 / pbrSettings.BaseColorGamma);

pbrSurfaceProperties.BaseColor = baseColor.xyz;

float3 coatModelNormal = modelNormal.xyz;
Expand All @@ -1673,8 +1671,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
pbrSurfaceProperties.SubsurfaceColor *= sampledSubsurfaceProperties.xyz;
pbrSurfaceProperties.Thickness *= sampledSubsurfaceProperties.w;
}

pbrSurfaceProperties.SubsurfaceColor = pow(pbrSurfaceProperties.SubsurfaceColor, 1 / pbrSettings.BaseColorGamma);
}
else if ((PBRFlags & TruePBR_TwoLayer) != 0)
{
Expand Down Expand Up @@ -1710,8 +1706,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
}
# endif
}

pbrSurfaceProperties.CoatColor = pow(pbrSurfaceProperties.CoatColor, 1 / pbrSettings.BaseColorGamma);
}

[branch] if ((PBRFlags & TruePBR_Fuzz) != 0)
Expand Down Expand Up @@ -2260,17 +2254,21 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

float3 directionalAmbientColor = mul(DirectionalAmbient, modelNormal);
# if defined(TRUE_PBR)
directionalAmbientColor = directionalAmbientColor;
directionalAmbientColor = sRGB2Lin(directionalAmbientColor);
# endif

float3 reflectionDiffuseColor = diffuseColor + directionalAmbientColor;

# if defined(SKYLIGHTING)
float skylightingDiffuse = shFuncProductIntegral(skylightingSH, shEvaluateCosineLobe(skylightingSettings.DirectionalDiffuse ? worldSpaceNormal : float3(0, 0, 1))) / shPI;
skylightingDiffuse = Skylighting::mixDiffuse(skylightingSettings, skylightingDiffuse);
# if !defined(TRUE_PBR)
directionalAmbientColor = sRGB2Lin(directionalAmbientColor);
# endif
directionalAmbientColor *= skylightingDiffuse;
# if !defined(TRUE_PBR)
directionalAmbientColor = Lin2sRGB(directionalAmbientColor);
# endif
# endif

# if defined(TRUE_PBR) && defined(LOD_LAND_BLEND) && !defined(DEFERRED)
Expand Down Expand Up @@ -2387,7 +2385,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# endif

# if !defined(DYNAMIC_CUBEMAPS)
specularColorPBR += indirectSpecularLobeWeight * sRGB2Lin(directionalAmbientColor);
specularColorPBR += indirectSpecularLobeWeight * directionalAmbientColor;
# endif

# if !defined(DEFERRED)
Expand All @@ -2398,7 +2396,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
indirectDiffuseLobeWeight *= vertexColor;
# endif

color.xyz += pow(emitColor.xyz, 1 / pbrSettings.BaseColorGamma);
color.xyz += emitColor.xyz;
color.xyz += transmissionColor;
# else
color.xyz += diffuseColor * baseColor.xyz;
Expand Down Expand Up @@ -2478,9 +2476,8 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if !defined(DEFERRED)
color.xyz += specularColor;
# endif
# endif

color.xyz = sRGB2Lin(color.xyz);
# endif

# if defined(WETNESS_EFFECTS) && !defined(TRUE_PBR)
color.xyz += wetnessSpecular * wetnessGlossinessSpecular;
Expand All @@ -2501,7 +2498,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# if defined(DEFERRED)
specularColorPBR = lerp(specularColorPBR, 0, lodLandBlendFactor);
indirectDiffuseLobeWeight = lerp(indirectDiffuseLobeWeight, input.Color.xyz * lodLandColor * lodLandFadeFactor, lodLandBlendFactor);
indirectDiffuseLobeWeight = lerp(indirectDiffuseLobeWeight, sRGB2Lin(input.Color.xyz * lodLandColor * lodLandFadeFactor), lodLandBlendFactor);
indirectSpecularLobeWeight = lerp(indirectSpecularLobeWeight, 0, lodLandBlendFactor);
pbrGlossiness = lerp(pbrGlossiness, 0, lodLandBlendFactor);
# endif
Expand Down Expand Up @@ -2648,7 +2645,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

float3 outputAlbedo = baseColor.xyz * vertexColor;
# if defined(TRUE_PBR)
outputAlbedo = indirectDiffuseLobeWeight;
outputAlbedo = Lin2sRGB(indirectDiffuseLobeWeight);
# endif
psout.Albedo = float4(outputAlbedo, psout.Diffuse.w);

Expand Down
Loading