Skip to content

Commit a679a98

Browse files
Shader Graph Updates and Fixes (#240)
1 parent 4291a73 commit a679a98

10 files changed

Lines changed: 226 additions & 20 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#ifndef GT_SCALABLE_COMMON
5+
#define GT_SCALABLE_COMMON
6+
7+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
8+
9+
half3 GTAlphaModulate(half3 albedo, half alpha)
10+
{
11+
#if UNITY_VERSION >= 202200
12+
return AlphaModulate(albedo, alpha);
13+
#else
14+
// Fake alpha for multiply blend by lerping albedo towards 1 (white) using alpha.
15+
// Manual adjustment for "lighter" multiply effect (similar to "premultiplied alpha")
16+
// would be painting whiter pixels in the texture.
17+
// This emulates that procedure in shader, so it should be applied to the base/source color.
18+
#if defined(_ALPHAMODULATE_ON)
19+
return lerp(half3(1.0, 1.0, 1.0), albedo, alpha);
20+
#else
21+
return albedo;
22+
#endif
23+
#endif
24+
}
25+
26+
#endif // GT_SCALABLE_COMMON

com.microsoft.mrtk.graphicstools.shadergraph.unity/Editor/Includes/ScalableCommon.hlsl.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.microsoft.mrtk.graphicstools.shadergraph.unity/Editor/Includes/ScalableForwardPass.hlsl

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,108 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
PackedVaryings vert(Attributes input)
5+
{
6+
Varyings output = (Varyings)0;
7+
output = BuildVaryings(input);
8+
PackedVaryings packedOutput = (PackedVaryings)0;
9+
packedOutput = PackVaryings(output);
10+
return packedOutput;
11+
}
12+
413
#if defined(MATERIAL_QUALITY_LOW)
5-
#include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/UnlitPass.hlsl"
14+
15+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
16+
#if defined(LOD_FADE_CROSSFADE)
17+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
18+
#endif
19+
20+
#include "ScalableCommon.hlsl"
21+
22+
void InitializeInputData(Varyings input, SurfaceDescription surfaceDescription, out InputData inputData)
23+
{
24+
inputData = (InputData)0;
25+
26+
inputData.positionWS = input.positionWS;
27+
inputData.normalWS = input.normalWS;
28+
inputData.viewDirectionWS = half3(0, 0, 1);
29+
inputData.shadowCoord = 0;
30+
inputData.fogCoord = 0;
31+
inputData.vertexLighting = half3(0, 0, 0);
32+
#if defined(LIGHTMAP_ON)
33+
inputData.bakedGI = SampleLightmap(input.staticLightmapUV, input.positionWS, inputData.normalWS);
34+
#else
35+
inputData.bakedGI = half3(1, 1, 1);
36+
#endif
37+
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
38+
inputData.shadowMask = half4(1, 1, 1, 1);
39+
40+
#if defined(DEBUG_DISPLAY)
41+
#if defined(LIGHTMAP_ON)
42+
inputData.staticLightmapUV = input.staticLightmapUV;
43+
#else
44+
inputData.vertexSH = input.sh;
45+
#endif
46+
#endif
47+
}
48+
49+
void frag(
50+
PackedVaryings packedInput,
51+
out half4 outColor : SV_Target0
52+
#ifdef _WRITE_RENDERING_LAYERS
53+
, out float4 outRenderingLayers : SV_Target1
54+
#endif
55+
)
56+
{
57+
Varyings unpacked = UnpackVaryings(packedInput);
58+
UNITY_SETUP_INSTANCE_ID(unpacked);
59+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
60+
SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);
61+
62+
#if defined(_SURFACE_TYPE_TRANSPARENT)
63+
bool isTransparent = true;
64+
#else
65+
bool isTransparent = false;
66+
#endif
67+
68+
#if defined(_ALPHATEST_ON)
69+
half alpha = AlphaDiscard(surfaceDescription.Alpha, surfaceDescription.AlphaClipThreshold);
70+
#elif defined(_SURFACE_TYPE_TRANSPARENT)
71+
half alpha = surfaceDescription.Alpha;
72+
#else
73+
half alpha = half(1.0);
74+
#endif
75+
76+
#if defined(LOD_FADE_CROSSFADE) && USE_UNITY_CROSSFADE
77+
LODFadeCrossFade(unpacked.positionCS);
78+
#endif
79+
80+
#if defined(_ALPHAMODULATE_ON)
81+
surfaceDescription.BaseColor = GTAlphaModulate(surfaceDescription.BaseColor, alpha);
82+
#endif
83+
84+
#if defined(_DBUFFER)
85+
ApplyDecalToBaseColor(unpacked.positionCS, surfaceDescription.BaseColor);
86+
#endif
87+
88+
InputData inputData;
89+
InitializeInputData(unpacked, surfaceDescription, inputData);
90+
91+
half4 albedo = float4(surfaceDescription.BaseColor, saturate(alpha));
92+
half3 normalTS = half3(0, 0, 1);
93+
half4 finalColor = UniversalFragmentBakedLit(inputData, albedo.rgb, alpha, normalTS);
94+
finalColor.a = OutputAlpha(finalColor.a, isTransparent);
95+
outColor = finalColor;
96+
97+
#ifdef _WRITE_RENDERING_LAYERS
98+
uint renderingLayers = GetMeshRenderingLayer();
99+
outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
100+
#endif
101+
}
102+
6103
#else
7104

8-
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
105+
#include "ScalableCommon.hlsl"
9106

10107
void InitializeInputData(Varyings input, SurfaceDescription surfaceDescription, out InputData inputData)
11108
{
@@ -62,15 +159,6 @@ void InitializeInputData(Varyings input, SurfaceDescription surfaceDescription,
62159
#endif
63160
}
64161

65-
PackedVaryings vert(Attributes input)
66-
{
67-
Varyings output = (Varyings)0;
68-
output = BuildVaryings(input);
69-
PackedVaryings packedOutput = (PackedVaryings)0;
70-
packedOutput = PackVaryings(output);
71-
return packedOutput;
72-
}
73-
74162
void frag(
75163
PackedVaryings packedInput
76164
, out half4 outColor : SV_Target0
@@ -151,7 +239,7 @@ void frag(
151239
surface.clearCoatSmoothness = saturate(surfaceDescription.CoatSmoothness);
152240
#endif
153241

154-
surface.albedo = AlphaModulate(surface.albedo, surface.alpha);
242+
surface.albedo = GTAlphaModulate(surface.albedo, surface.alpha);
155243

156244
#ifdef _DBUFFER
157245
ApplyDecalToSurfaceData(unpacked.positionCS, surface, inputData);

com.microsoft.mrtk.graphicstools.shadergraph.unity/Editor/Includes/ScalableGBufferPass.hlsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#if defined(MATERIAL_QUALITY_LOW)
55
#include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/UnlitGBufferPass.hlsl"
66
#else
7+
8+
#include "ScalableCommon.hlsl"
9+
710
void InitializeInputData(Varyings input, SurfaceDescription surfaceDescription, out InputData inputData)
811
{
912
inputData = (InputData)0;
@@ -150,7 +153,7 @@ FragmentOutput frag(PackedVaryings packedInput)
150153
surface.clearCoatMask = 0;
151154
surface.clearCoatSmoothness = 1;
152155

153-
surface.albedo = AlphaModulate(surface.albedo, surface.alpha);
156+
surface.albedo = GTAlphaModulate(surface.albedo, surface.alpha);
154157

155158
Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
156159
MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);

com.microsoft.mrtk.graphicstools.shadergraph.unity/Editor/ShaderGUI/GraphicsToolsShaderGraphLitGUI.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,47 @@ public override void DrawSurfaceOptions(Material material)
4747
DoPopup(SrcBlendAlphaLabel, SrcBlendAlpha, names);
4848
DoPopup(DstBlendAlphaLabel, DstBlendAlpha, names);
4949
}
50+
51+
/// <summary>
52+
/// Ensure that the alpha blending properties are maintained.
53+
/// </summary>
54+
public override void ValidateMaterial(Material material)
55+
{
56+
// We must cache the values of SrcBlendAlpha and DstBlendAlpha because base.ValidateMaterial() will overwrite them with it's own default values.
57+
float prevSrcBlendAlpha;
58+
59+
if (SrcBlendAlpha != null)
60+
{
61+
prevSrcBlendAlpha = SrcBlendAlpha.floatValue;
62+
}
63+
else if (material.HasProperty(GraphicsToolsCoreRenderStates.Property.SrcBlendAlpha))
64+
{
65+
prevSrcBlendAlpha = material.GetFloat(GraphicsToolsCoreRenderStates.Property.SrcBlendAlpha);
66+
}
67+
else
68+
{
69+
prevSrcBlendAlpha = 1.0f;
70+
}
71+
72+
float prevDstBlendAlpha;
73+
74+
if (DstBlendAlpha != null)
75+
{
76+
prevDstBlendAlpha = DstBlendAlpha.floatValue;
77+
}
78+
else if (material.HasProperty(GraphicsToolsCoreRenderStates.Property.DstBlendAlpha))
79+
{
80+
prevDstBlendAlpha = material.GetFloat(GraphicsToolsCoreRenderStates.Property.DstBlendAlpha);
81+
}
82+
else
83+
{
84+
prevDstBlendAlpha = 1.0f;
85+
}
86+
87+
base.ValidateMaterial(material);
88+
89+
material.SetFloat(GraphicsToolsCoreRenderStates.Property.SrcBlendAlpha, prevSrcBlendAlpha);
90+
material.SetFloat(GraphicsToolsCoreRenderStates.Property.DstBlendAlpha, prevDstBlendAlpha);
91+
}
5092
}
5193
}

com.microsoft.mrtk.graphicstools.shadergraph.unity/Editor/ShaderGUI/GraphicsToolsShaderGraphUnlitGUI.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,47 @@ public override void DrawSurfaceOptions(Material material)
4747
DoPopup(SrcBlendAlphaLabel, SrcBlendAlpha, names);
4848
DoPopup(DstBlendAlphaLabel, DstBlendAlpha, names);
4949
}
50+
51+
/// <summary>
52+
/// Ensure that the alpha blending properties are maintained.
53+
/// </summary>
54+
public override void ValidateMaterial(Material material)
55+
{
56+
// We must cache the values of SrcBlendAlpha and DstBlendAlpha because base.ValidateMaterial() will overwrite them with it's own default values.
57+
float prevSrcBlendAlpha;
58+
59+
if (SrcBlendAlpha != null)
60+
{
61+
prevSrcBlendAlpha = SrcBlendAlpha.floatValue;
62+
}
63+
else if (material.HasProperty(GraphicsToolsCoreRenderStates.Property.SrcBlendAlpha))
64+
{
65+
prevSrcBlendAlpha = material.GetFloat(GraphicsToolsCoreRenderStates.Property.SrcBlendAlpha);
66+
}
67+
else
68+
{
69+
prevSrcBlendAlpha = 1.0f;
70+
}
71+
72+
float prevDstBlendAlpha;
73+
74+
if (DstBlendAlpha != null)
75+
{
76+
prevDstBlendAlpha = DstBlendAlpha.floatValue;
77+
}
78+
else if (material.HasProperty(GraphicsToolsCoreRenderStates.Property.DstBlendAlpha))
79+
{
80+
prevDstBlendAlpha = material.GetFloat(GraphicsToolsCoreRenderStates.Property.DstBlendAlpha);
81+
}
82+
else
83+
{
84+
prevDstBlendAlpha = 1.0f;
85+
}
86+
87+
base.ValidateMaterial(material);
88+
89+
material.SetFloat(GraphicsToolsCoreRenderStates.Property.SrcBlendAlpha, prevSrcBlendAlpha);
90+
material.SetFloat(GraphicsToolsCoreRenderStates.Property.DstBlendAlpha, prevDstBlendAlpha);
91+
}
5092
}
5193
}

com.microsoft.mrtk.graphicstools.shadergraph.unity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.microsoft.mrtk.graphicstools.shadergraph.unity",
3-
"version": "0.8.1",
3+
"version": "0.8.7",
44
"displayName": "MRTK Graphics Tools Shader Graph",
55
"description": "Graphics tools and components for developing Mixed Reality applications in Unity Shader Graph.",
66
"documentationUrl": "https://aka.ms/mrtk3graphics",

com.microsoft.mrtk.graphicstools.unity/Editor/Experimental/AreaLight/AreaLightInspector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Microsoft Corporation.
2-
// Licensed under the MIT License.
2+
// Licensed under the MIT License.
33

44
using UnityEditor;
55
using UnityEngine;

com.microsoft.mrtk.graphicstools.unity/Runtime/Shaders/GraphicsToolsStandardProgram.hlsl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,8 @@ Varyings VertexStage(Attributes input)
273273
#if defined(_UV_SCREEN)
274274
output.uvScreen = ComputeScreenPos(output.position);
275275
// Flip vertical UV for orthographic projections (if not already flipped) to ensure the image is not upside down.
276-
#if defined(UNITY_UV_STARTS_AT_TOP)
277-
output.uvScreen.y = unity_OrthoParams.w ? (1.0 - output.uvScreen.y) : output.uvScreen.y;
278-
#else
279-
output.uvScreen.y = unity_OrthoParams.w ? output.uvScreen.y : (1.0 - output.uvScreen.y);
276+
#if !UNITY_UV_STARTS_AT_TOP
277+
output.uvScreen.y = 1.0 - output.uvScreen.y;
280278
#endif
281279
#elif (_BLUR_TEXTURE_PREBAKED_BACKGROUND)
282280
output.uvBackgroundRect = float2((vertexPosition.x - _BlurBackgroundRect.x) / (_BlurBackgroundRect.z - _BlurBackgroundRect.x),

com.microsoft.mrtk.graphicstools.unity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.microsoft.mrtk.graphicstools.unity",
3-
"version": "0.8.6",
3+
"version": "0.8.7",
44
"displayName": "MRTK Graphics Tools",
55
"description": "Graphics tools and components for developing Mixed Reality applications in Unity.",
66
"documentationUrl": "https://aka.ms/mrtk3graphics",

0 commit comments

Comments
 (0)