Skip to content

Commit 513d609

Browse files
Decal cross fade support (#1986)
* Enable LOD cross fade * Changelog * Make changes only for mesh decals * Update DecalData.hlsl * Update DecalSurfaceOptionsUIBlock.cs * do some cleanup and move ComputeFadeMaskSeed in ShaderVariablesFunctions.hlsl * update documentation * Update ShaderVariablesFunctions.hlsl Co-authored-by: sebastienlagarde <[email protected]>
1 parent b6cd5eb commit 513d609

File tree

11 files changed

+66
-32
lines changed

11 files changed

+66
-32
lines changed

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
131131
- LOD meshes are now properly stripped based on the maximum lod value parameters contained in the HDRP asset.
132132
- Fixed an inconsistency in the LOD group UI where LOD bias was not the right one.
133133
- Fixed outlines in transitions between post-processed and plain regions in the graphics compositor (case 1278775).
134+
- Fix decal being applied twice with LOD Crossfade.
134135

135136
### Changed
136137
- Preparation pass for RTSSShadows to be supported by render graph.

com.unity.render-pipelines.high-definition/Documentation~/Master-Node-Decal.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ To view these properties, click the gear in the top right of the Master Node.
5555
| **Affects Ambient Occlusion** | Enable or disable the effect of the **Ambient Occlusion** property. |
5656
| **Affects Smoothness** | Enable or disable the effect of the **Smoothness** property. |
5757
| **Affects Emission** | Enable or disable the effect of the **Emission** property. |
58+
| **Support LOD CrossFace** | Indicates whether dithering occurs when moving from one LOD level of the receiving Mesh to another when sampling Textures. Only use with Decal Mesh. |
5859
| **Override ShaderGUI** | Lets you override the [ShaderGUI](https://docs.unity3d.com/ScriptReference/ShaderGUI.html) that this Shader Graph uses. If `true`, the **ShaderGUI** property appears, which lets you specify the ShaderGUI to use. |
59-
| **- ShaderGUI** | The full name of the ShaderGUI class to use, including the class path. |
60+
| **- ShaderGUI** | The full name of the ShaderGUI class to use, including the class path. |
6061

6162
<a name="MaterialProperties"></a>
6263

com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalData.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public int drawOrder
6262
set => m_DrawOrder = value;
6363
}
6464

65+
[SerializeField]
66+
bool m_SupportLodCrossFade;
67+
public bool supportLodCrossFade
68+
{
69+
get => m_SupportLodCrossFade;
70+
set => m_SupportLodCrossFade = value;
71+
}
72+
6573
public bool affectsMaskmap => affectsSmoothness || affectsMetal || affectsAO;
6674
}
6775
}

com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalPass.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ Pass
9797
fragInputs.tangentToWorld[2].xyz = TransformObjectToWorldDir(float3(0, 1, 0));
9898
fragInputs.tangentToWorld[1].xyz = TransformObjectToWorldDir(float3(0, 0, 1));
9999
#else
100+
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group
101+
LODDitheringTransition(ComputeFadeMaskSeed(V, posInput.positionSS), unity_LODFade.x);
102+
#endif
103+
100104
float fadeFactor = 1.0;
101105
#endif
102106

com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalPropertyBlock.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected override void CreatePropertyGUI()
2929
AddProperty(affectAmbientOcclusionText, () => decalData.affectsAO, (newValue) => decalData.affectsAO = newValue);
3030
AddProperty(affectSmoothnessText, () => decalData.affectsSmoothness, (newValue) => decalData.affectsSmoothness = newValue);
3131
AddProperty(affectEmissionText, () => decalData.affectsEmission, (newValue) => decalData.affectsEmission = newValue);
32+
AddProperty(supportLodCrossFadeText, () => decalData.supportLodCrossFade, (newValue) => decalData.supportLodCrossFade = newValue);
3233
}
3334
}
3435
}

com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ protected override void CollectPassKeywords(ref PassDescriptor pass)
7070
if (decalData.affectsMaskmap)
7171
pass.keywords.Add(DecalDefines.Maskmap);
7272
}
73+
74+
if (pass.lightMode == DecalSystem.s_MaterialDecalPassNames[(int)DecalSystem.MaterialDecalPass.DecalMeshForwardEmissive] ||
75+
pass.lightMode == DecalSystem.s_MaterialDecalPassNames[(int)DecalSystem.MaterialDecalPass.DBufferMesh])
76+
{
77+
pass.keywords.Add(CoreKeywordDescriptors.LodFadeCrossfade, new FieldCondition(Fields.LodCrossFade, true));
78+
}
7379
}
7480

7581
public static FieldDescriptor AffectsAlbedo = new FieldDescriptor(kMaterial, "AffectsAlbedo", "");
@@ -93,6 +99,7 @@ public override void GetFields(ref TargetFieldContext context)
9399
context.AddField(AffectsMaskMap, decalData.affectsMaskmap);
94100
context.AddField(DecalDefault, decalData.affectsAlbedo || decalData.affectsNormal || decalData.affectsMetal ||
95101
decalData.affectsAO || decalData.affectsSmoothness );
102+
context.AddField(Fields.LodCrossFade, decalData.supportLodCrossFade);
96103
}
97104

98105
public override void GetActiveBlocks(ref TargetActiveBlockContext context)

com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/DecalSurfaceOptionsUIBlock.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Styles
2222
public static GUIContent affectAmbientOcclusionText = new GUIContent("Affect Ambient Occlusion", "When enabled, this decal uses the smoothness channel of its Mask Map. When disabled, the decal has no smoothness effect.");
2323
public static GUIContent affectSmoothnessText = new GUIContent("Affect Smoothness", "When enabled, this decal uses the ambient occlusion channel of its Mask Map. When disabled, the decal has no ambient occlusion effect.");
2424
public static GUIContent affectEmissionText = new GUIContent("Affect Emission", "When enabled, this decal becomes emissive and appears self-illuminated. Affect Emission does not support Affects Transparents option on Decal Projector.");
25+
public static GUIContent supportLodCrossFadeText = new GUIContent("Support LOD CrossFade", "When enabled, this decal material supports LOD Cross fade if use on a Mesh.");
2526
}
2627

2728
Expandable m_ExpandableBit;

com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.shader

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ Shader "HDRP/Decal"
207207
HLSLPROGRAM
208208

209209
#pragma multi_compile DECALS_3RT DECALS_4RT
210+
// enable dithering LOD crossfade
211+
#pragma multi_compile _ LOD_FADE_CROSSFADE
212+
210213
#define SHADERPASS SHADERPASS_DBUFFER_MESH
211214
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
212215
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/DecalProperties.hlsl"
@@ -238,6 +241,8 @@ Shader "HDRP/Decal"
238241
Blend 0 SrcAlpha One
239242

240243
HLSLPROGRAM
244+
// enable dithering LOD crossfade
245+
#pragma multi_compile _ LOD_FADE_CROSSFADE
241246

242247
#define _MATERIAL_AFFECTS_EMISSION
243248
#define SHADERPASS SHADERPASS_FORWARD_EMISSIVE_MESH

com.unity.render-pipelines.high-definition/Runtime/Material/Decal/DecalData.hlsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ void GetSurfaceData(FragInputs input, float3 V, PositionInputs posInput, out Dec
1414
float2 offset = float2(normalToWorld[3][2], normalToWorld[3][3]);
1515
float2 texCoords = input.texCoord0.xy * scale + offset;
1616
#elif (SHADERPASS == SHADERPASS_DBUFFER_MESH) || (SHADERPASS == SHADERPASS_FORWARD_EMISSIVE_MESH)
17+
18+
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group
19+
LODDitheringTransition(ComputeFadeMaskSeed(V, posInput.positionSS), unity_LODFade.x);
20+
#endif
21+
1722
float fadeFactor = _DecalBlend;
1823
float2 texCoords = input.texCoord0.xy;
1924
#endif

com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -125,37 +125,6 @@ void DoAlphaTest(float alpha, float alphaCutoff, out bool alphaTestResult)
125125
{
126126
alphaTestResult = alpha >= alphaCutoff;
127127
}
128-
//-----------------------------------------------------------------------------
129-
// LoD Fade
130-
//-----------------------------------------------------------------------------
131-
132-
// Helper for LODDitheringTransition.
133-
uint2 ComputeFadeMaskSeed(float3 V, uint2 positionSS)
134-
{
135-
uint2 fadeMaskSeed;
136-
137-
if (IsPerspectiveProjection())
138-
{
139-
// Start with the world-space direction V. It is independent from the orientation of the camera,
140-
// and only depends on the position of the camera and the position of the fragment.
141-
// Now, project and transform it into [-1, 1].
142-
float2 pv = PackNormalOctQuadEncode(V);
143-
// Rescale it to account for the resolution of the screen.
144-
pv *= _ScreenSize.xy;
145-
// The camera only sees a small portion of the sphere, limited by hFoV and vFoV.
146-
// Therefore, we must rescale again (before quantization), roughly, by 1/tan(FoV/2).
147-
pv *= UNITY_MATRIX_P._m00_m11;
148-
// Truncate and quantize.
149-
fadeMaskSeed = asuint((int2)pv);
150-
}
151-
else
152-
{
153-
// Can't use the view direction, it is the same across the entire screen.
154-
fadeMaskSeed = positionSS;
155-
}
156-
157-
return fadeMaskSeed;
158-
}
159128

160129
//-----------------------------------------------------------------------------
161130
// Reflection / Refraction hierarchy handling

0 commit comments

Comments
 (0)