Skip to content

Commit 1c4599f

Browse files
[10.x.x] HDRP - Update Shader Graph Unlit to use new shared template [Skip CI] (#894)
* Move Unlit to the new sharing template * Remove comment from unlit template * Disable code that break raytracing
1 parent 325612d commit 1c4599f

File tree

10 files changed

+134
-201
lines changed

10 files changed

+134
-201
lines changed

com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/Templates/ShaderPass.template

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ Pass
2424
$splice(PassKeywords)
2525
$splice(GraphKeywords)
2626

27+
// --------------------------------------------------
2728
// Defines
29+
30+
// Lighting Material only (will not affect unlit)
2831
$DisableDecals: #define _DISABLE_DECALS 1
2932
$DisableSSR: #define _DISABLE_SSR 1
3033
$DisableSSRTransparent: #define _DISABLE_SSR_TRANSPARENT 1
34+
$BlendMode.PreserveSpecular: #define _BLENDMODE_PRESERVE_SPECULAR_LIGHTING 1
35+
36+
// Precomputed velocity is used for alembic
3137
$AddPrecomputedVelocity: #define _ADD_PRECOMPUTED_VELOCITY
3238
$TransparentWritesMotionVec: #define _TRANSPARENT_WRITES_MOTION_VEC 1
3339
$DepthOffset: #define _DEPTHOFFSET_ON 1
34-
$BlendMode.PreserveSpecular: #define _BLENDMODE_PRESERVE_SPECULAR_LIGHTING 1
40+
41+
// Attribute
3542
$AttributesMesh.normalOS: #define ATTRIBUTES_NEED_NORMAL
3643
$AttributesMesh.tangentOS: #define ATTRIBUTES_NEED_TANGENT
3744
$AttributesMesh.uv0: #define ATTRIBUTES_NEED_TEXCOORD0
@@ -65,10 +72,12 @@ Pass
6572
#endif
6673
#endif
6774

75+
#ifndef SHADER_UNLIT
6876
// We need isFontFace when using double sided
6977
#if defined(_DOUBLESIDED_ON) && !defined(VARYINGS_NEED_CULLFACE)
7078
#define VARYINGS_NEED_CULLFACE
7179
#endif
80+
#endif
7281

7382
// Translate transparent motion vector define
7483
#if defined(_TRANSPARENT_WRITES_MOTION_VEC) && defined(_SURFACE_TYPE_TRANSPARENT)
@@ -83,7 +92,7 @@ Pass
8392
// Includes
8493
$splice(PreGraphIncludes)
8594

86-
// Used by SceneSelectionPass
95+
// Properties used by SceneSelectionPass
8796
#ifdef SCENESELECTIONPASS
8897
int _ObjectId;
8998
int _PassValue;
@@ -256,6 +265,7 @@ Pass
256265

257266
void GetSurfaceAndBuiltinData(FragInputs fragInputs, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData RAY_TRACING_OPTIONAL_PARAMETERS)
258267
{
268+
#ifndef SHADER_UNLIT
259269
// Don't dither if displaced tessellation (we're fading out the displacement instead to match the next LOD)
260270
#if !defined(SHADER_STAGE_RAY_TRACING) && !defined(_TESSELLATION_DISPLACEMENT)
261271
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group
@@ -270,6 +280,7 @@ Pass
270280
#endif
271281

272282
ApplyDoubleSidedFlipOrMirror(fragInputs, doubleSidedConstants); // Apply double sided flip on the vertex normal
283+
#endif // SHADER_UNLIT
273284

274285
SurfaceDescriptionInputs surfaceDescriptionInputs = FragInputsToSurfaceDescriptionInputs(fragInputs, V);
275286
SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
@@ -287,13 +298,29 @@ Pass
287298
$DepthOffset: ApplyDepthOffsetPositionInput(V, surfaceDescription.DepthOffset, GetViewForwardDir(), GetWorldToHClipMatrix(), posInput);
288299
#endif
289300

301+
#ifndef SHADER_UNLIT
290302
float3 bentNormalWS;
291303
BuildSurfaceData(fragInputs, surfaceDescription, V, posInput, surfaceData, bentNormalWS);
292304

293305
// Builtin Data
294306
// For back lighting we use the oposite vertex normal
295307
InitBuiltinData(posInput, surfaceDescription.Alpha, bentNormalWS, -fragInputs.tangentToWorld[2], fragInputs.texCoord1, fragInputs.texCoord2, builtinData);
296308

309+
#else
310+
BuildSurfaceData(fragInputs, surfaceDescription, V, posInput, surfaceData);
311+
312+
ZERO_INITIALIZE(BuiltinData, builtinData); // No call to InitBuiltinData as we don't have any lighting
313+
builtinData.opacity = surfaceDescription.Alpha;
314+
315+
#if defined(DEBUG_DISPLAY)
316+
// Light Layers are currently not used for the Unlit shader (because it is not lit)
317+
// But Unlit objects do cast shadows according to their rendering layer mask, which is what we want to
318+
// display in the light layers visualization mode, therefore we need the renderingLayers
319+
builtinData.renderingLayers = _EnableLightLayers ? asuint(unity_RenderingLayer.x) : DEFAULT_LIGHT_LAYERS;
320+
#endif
321+
322+
#endif // SHADER_UNLIT
323+
297324
#ifdef _ALPHATEST_ON
298325
// Used for sharpening by alpha to mask
299326
$DoAlphaTest: builtinData.alphaClipTreshold = surfaceDescription.AlphaClipThreshold;
@@ -302,12 +329,28 @@ Pass
302329
$DoAlphaTestShadow: builtinData.alphaClipTreshold = surfaceDescription.AlphaClipThresholdShadow;
303330
#endif
304331

305-
// override sampleBakedGI:
332+
// override sampleBakedGI - not used by Unlit
306333
$LightingGI: builtinData.bakeDiffuseLighting = surfaceDescription.BakedGI;
307334
$BackLightingGI: builtinData.backBakeDiffuseLighting = surfaceDescription.BakedBackGI;
308335

309336
$SurfaceDescription.Emission: builtinData.emissiveColor = surfaceDescription.Emission;
310337

338+
#ifndef SHADER_UNLIT
339+
// TODO: need to implement for shader graph raytracing
340+
/*
341+
// Raytracing specific code to remove Emissive color from the pass to avoid double contribution
342+
// To avoid to have double lighting (once from the light source and once from the light itself)
343+
#if SHADERPASS == SHADERPASS_RAYTRACING_INDIRECT || SHADERPASS == SHADERPASS_RAYTRACING_GBUFFER
344+
builtinData.emissiveColor *= _IncludeIndirectLighting;
345+
#elif SHADERPASS == SHADERPASS_RAYTRACING_FORWARD || SHADERPASS == SHADERPASS_PATH_TRACING
346+
if (rayCone.spreadAngle < 0.0)
347+
{
348+
builtinData.emissiveColor *= _IncludeIndirectLighting;
349+
}
350+
#endif
351+
*/
352+
#endif // SHADER_UNLIT
353+
311354
// Note this will not fully work on transparent surfaces (can check with _SURFACE_TYPE_TRANSPARENT define)
312355
// We will always overwrite vt feeback with the nearest. So behind transparent surfaces vt will not be resolved
313356
// This is a limitation of the current MRT approach.
@@ -321,7 +364,12 @@ Pass
321364
builtinData.distortionBlur = surfaceDescription.DistortionBlur;
322365
#endif
323366

367+
#ifndef SHADER_UNLIT
368+
// PostInitBuiltinData call ApplyDebugToBuiltinData
324369
PostInitBuiltinData(V, posInput, surfaceData, builtinData);
370+
#else
371+
ApplyDebugToBuiltinData(builtinData);
372+
#endif
325373

326374
RAY_TRACING_OPTIONAL_ALPHA_TEST_PASS
327375
}

com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/HDUnlitPass.template

Lines changed: 0 additions & 180 deletions
This file was deleted.

com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/HDUnlitPass.template.meta

Lines changed: 0 additions & 7 deletions
This file was deleted.

com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/HDUnlitSubTarget.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ sealed partial class HDUnlitSubTarget : SurfaceSubTarget, IRequiresData<HDUnlitD
1818
public HDUnlitSubTarget() => displayName = "Unlit";
1919

2020
// Templates
21-
protected override string templatePath => $"{HDUtils.GetHDRenderPipelinePath()}Editor/Material/Unlit/ShaderGraph/HDUnlitPass.template";
22-
protected override string templateMaterialDirectory => $"{HDUtils.GetHDRenderPipelinePath()}Editor/Material/ShaderGraph/Templates";
21+
protected override string templateMaterialDirectory => $"{HDUtils.GetHDRenderPipelinePath()}Editor/Material/Unlit/ShaderGraph/";
2322
protected override ShaderID shaderID => HDShaderUtils.ShaderID.SG_Unlit;
2423
protected override string renderType => HDRenderTypeTags.HDUnlitShader.ToString();
2524
protected override string subTargetAssetGuid => "4516595d40fa52047a77940183dc8e74"; // HDUnlitSubTarget
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
void BuildSurfaceData(FragInputs fragInputs, inout SurfaceDescription surfaceDescription, float3 V, PositionInputs posInput, out SurfaceData surfaceData)
2+
{
3+
// setup defaults -- these are used if the graph doesn't output a value
4+
ZERO_INITIALIZE(SurfaceData, surfaceData);
5+
6+
// copy across graph values, if defined
7+
$SurfaceDescription.BaseColor: surfaceData.color = surfaceDescription.BaseColor;
8+
9+
#if defined(DEBUG_DISPLAY)
10+
if (_DebugMipMapMode != DEBUGMIPMAPMODE_NONE)
11+
{
12+
// TODO
13+
}
14+
#endif
15+
16+
#if defined(_ENABLE_SHADOW_MATTE) && SHADERPASS == SHADERPASS_FORWARD_UNLIT
17+
HDShadowContext shadowContext = InitShadowContext();
18+
float shadow;
19+
float3 shadow3;
20+
// We need to recompute some coordinate not computed by default for shadow matte
21+
posInput = GetPositionInput(fragInputs.positionSS.xy, _ScreenSize.zw, fragInputs.positionSS.z, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
22+
float3 upWS = normalize(fragInputs.tangentToWorld[1]);
23+
uint renderingLayers = _EnableLightLayers ? asuint(unity_RenderingLayer.x) : DEFAULT_LIGHT_LAYERS;
24+
ShadowLoopMin(shadowContext, posInput, upWS, asuint(_ShadowMatteFilter), renderingLayers, shadow3);
25+
shadow = dot(shadow3, float3(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0));
26+
27+
float4 shadowColor = (1.0 - shadow) * surfaceDescription.ShadowTint.rgba;
28+
float localAlpha = saturate(shadowColor.a + surfaceDescription.Alpha);
29+
30+
// Keep the nested lerp
31+
// With no Color (bsdfData.color.rgb, bsdfData.color.a == 0.0f), just use ShadowColor*Color to avoid a ring of "white" around the shadow
32+
// And mix color to consider the Color & ShadowColor alpha (from texture or/and color picker)
33+
#ifdef _SURFACE_TYPE_TRANSPARENT
34+
surfaceData.color = lerp(shadowColor.rgb * surfaceData.color, lerp(lerp(shadowColor.rgb, surfaceData.color, 1.0 - surfaceDescription.ShadowTint.a), surfaceData.color, shadow), surfaceDescription.Alpha);
35+
#else
36+
surfaceData.color = lerp(lerp(shadowColor.rgb, surfaceData.color, 1.0 - surfaceDescription.ShadowTint.a), surfaceData.color, shadow);
37+
#endif
38+
localAlpha = ApplyBlendMode(surfaceData.color, localAlpha).a;
39+
40+
surfaceDescription.Alpha = localAlpha;
41+
#endif
42+
}

com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/ShaderPass.template.hlsl.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Setup a define to say we are an unlit shader
2+
#define SHADER_UNLIT
3+
$EnableShadowMatte: #define _ENABLE_SHADOW_MATTE
4+
5+
// Following Macro are only used by Unlit material
6+
#if defined(_ENABLE_SHADOW_MATTE) && SHADERPASS == SHADERPASS_FORWARD_UNLIT
7+
#define LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
8+
#define HAS_LIGHTLOOP
9+
#endif

0 commit comments

Comments
 (0)