Skip to content

Commit

Permalink
added support for legacy detail albedo mixing, closes #73
Browse files Browse the repository at this point in the history
  • Loading branch information
orels1 committed Jul 3, 2024
1 parent a6ea3a5 commit c83ee0c
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
[KeywordEnum(Packed, Separated)]DETAILS_MODE("Detail Map Mode %ShowIf(DETAILS_OVERLAY)", Int) = 0
_DDetailsMap("Details Map %ShowIf(DETAILS_OVERLAY)", 2D) = "gray" { }
UI_DetailsMapNote("> R: Albedo, G: Normal G, B: Smooth, A: Normal R. Uncheck sRGB! %ShowIf(DETAILS_OVERLAY && DETAILS_MODE_PACKED)", Int) = 0
UI_DetailsMapNoteSeparate("> RGB: Albedo, A: Smooth. Uncheck sRGB! %ShowIf(DETAILS_OVERLAY && DETAILS_MODE_SEPARATED)", Int) = 0
UI_DetailsMapNoteSeparate("> RGB: Albedo, A: Smooth %ShowIf(DETAILS_OVERLAY && DETAILS_MODE_SEPARATED)", Int) = 0
[NoScaleOffset]_DDetailsNormal("Details Normal Map > %ShowIf(DETAILS_OVERLAY && DETAILS_MODE_SEPARATED)", 2D) = "bump" { }
[Enum(UV, 0, Local Space, 1, World Space, 2)]_DMappingSpace("Mapping Space %ShowIf(DETAILS_OVERLAY)", Int) = 0
[Enum(UV1, 0, UV2, 1, UV3, 2, UV4, 3)]_DUVChannel("UV Set %ShowIf(_DMappingSpace == 0 && DETAILS_OVERLAY)", Int) = 0
[Enum(X, 0, Y, 1, Z, 2)]_DPlanarAxisX("X Axis %ShowIf(_DMappingSpace > 0 && DETAILS_OVERLAY) %CombineWith(_DPlanarAxisY)", Int) = 0
[HideInInspector][Enum(X, 0, Y, 1, Z, 2)]_DPlanarAxisY("Y Axis", Int) = 2
_DAlbedoScale("Albedo Scale %ShowIf(DETAILS_OVERLAY)", Range(0.0, 2.0)) = 1
UI_DetailAlbedoNote("> Values < 0.5 - darken, > 0.5 - lighten %ShowIf(DETAILS_OVERLAY)", Int) = 0
UI_DetailAlbedoNote("> Values < 0.5 - darken, > 0.5 - lighten %ShowIf(DETAILS_OVERLAY && !_DAlbedoMixingLegacy)", Int) = 0
UI_DAlbedoMixingLegacyNote("> Multiplies albedo by 2x the detail Albedo. Same as BIRP Standard Shader %ShowIf(DETAILS_OVERLAY && _DAlbedoMixingLegacy && DETAILS_MODE_SEPARATED)", Int) = 0
[ToggleUI]_DAlbedoMixingLegacy("Legacy Albedo Mixing %ShowIf(DETAILS_OVERLAY && DETAILS_MODE_SEPARATED)", Int) = 0
_DNormalScale("Normal Scale %ShowIf(DETAILS_OVERLAY)", Range(0.0, 2.0)) = 1
[ToggleUI]_DNormalFlipY("Flip Y (UE Mode) %ShowIf(DETAILS_OVERLAY)", Int) = 0
_DSmoothScale("Smooth Scale %ShowIf(DETAILS_OVERLAY)", Range(0.0, 2.0)) = 1
Expand Down Expand Up @@ -47,6 +49,7 @@
half _DNormalScale;
int _DNormalFlipY;
half _DSmoothScale;
int _DAlbedoMixingLegacy;
}

%Textures()
Expand Down Expand Up @@ -100,7 +103,7 @@
#endif

half mask = lerp(masks, 1, _DIgnoreMask);
half2 uv = d.uv0.xy;
float2 uv = d.uv0.xy;
switch(_DUVChannel)
{
case 1: uv = d.uv1.xy; break;
Expand All @@ -118,6 +121,7 @@
half4 detailsMap = SAMPLE_TEXTURE2D(_DDetailsMap, sampler_DDetailsMap, uv);

#if defined(DETAILS_MODE_PACKED)

// 0.5 is the neutral value
half detailAlbedo = detailsMap.r * 2.0 - 1.0;
half detailSmooth = detailsMap.b * 2.0 - 1.0;
Expand All @@ -128,8 +132,13 @@
}
detailNormal = UnpackNormalAG(detailsMap, _DNormalScale);
half detailAlbedoSpeed = saturate(abs(detailAlbedo) * _DAlbedoScale);
#elif defined(DETAILS_MODE_SEPARATED)
half3 detailAlbedo = detailsMap.rgb * 2.0 - 1.0;
half3 albedoOverlay = lerp(sqrt(o.Albedo), (detailAlbedo < 0.0) ? 0.0.xxx : 1.0.xxx, detailAlbedoSpeed * detailAlbedoSpeed);
albedoOverlay *= albedoOverlay;

// Packed mode only supports HDRP-style albedo mixing
o.Albedo = lerp(o.Albedo, saturate(albedoOverlay), mask);

#elif defined(DETAILS_MODE_SEPARATED)
half detailSmooth = detailsMap.a * 2.0 - 1.0;

half4 packedNormal = SAMPLE_TEXTURE2D(_DDetailsNormal, sampler_DDetailsNormal, uv);
Expand All @@ -139,14 +148,19 @@
}
half3 detailNormal = UnpackScaleNormal(packedNormal, _DNormalScale);

half3 detailAlbedoSpeed = saturate(abs(detailAlbedo) * _DAlbedoScale);
// Separated mdoe supports BIRP-style 2x albedo mixing
if (_DAlbedoMixingLegacy) {
// o.Albedo = lerp(o.Albedo, o.Albedo * 2.0 * detailsMap.rgb, saturate(mask * _DAlbedoScale));
o.Albedo *= LerpWhiteTo(detailsMap.rgb * unity_ColorSpaceDouble.rgb, mask * _DAlbedoScale);
} else {
half3 detailAlbedo = detailsMap.rgb * 2.0 - 1.0;
half3 detailAlbedoSpeed = saturate(abs(detailAlbedo) * _DAlbedoScale);
half3 albedoOverlay = lerp(sqrt(o.Albedo), (detailAlbedo < 0.0) ? 0.0.xxx : 1.0.xxx, detailAlbedoSpeed * detailAlbedoSpeed);
albedoOverlay *= albedoOverlay;
o.Albedo = lerp(o.Albedo, saturate(albedoOverlay), mask);
}
#endif

// do the albedo details
half3 albedoOverlay = lerp(sqrt(o.Albedo), (detailAlbedo < 0.0) ? 0.0.xxx : 1.0.xxx, detailAlbedoSpeed * detailAlbedoSpeed);
albedoOverlay *= albedoOverlay;
o.Albedo = lerp(o.Albedo, saturate(albedoOverlay), mask);

// do the smooth details
half detailSmoothSpeed = saturate(abs(detailSmooth) * _DSmoothScale);
half smoothOverlay = lerp(o.Smoothness, (detailSmooth < 0.0) ? 0.0 : 1.0, detailSmoothSpeed * detailSmoothSpeed);
Expand Down

0 comments on commit c83ee0c

Please sign in to comment.