Skip to content

Commit 7b8f715

Browse files
Scalable Shader Graphs + Improved Backplate Shaders (#229)
Adding new scalable shader graph feature and canvas shader updates.
1 parent d18aadb commit 7b8f715

24 files changed

+3617
-31
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Autogenerated VS files
2+
*.vs
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using UnityEditor;
6+
using UnityEditor.Rendering.Universal.ShaderGraph;
7+
using UnityEditor.ShaderGraph;
8+
using UnityEngine.Rendering;
9+
10+
namespace Microsoft.MixedReality.GraphicsTools.Editor
11+
{
12+
/// <summary>
13+
/// GraphicsToolsUniversalScalableSubTarget creation.
14+
/// </summary>
15+
static class CreateGraphicsToolsScalableShaderGraph
16+
{
17+
/// <summary>
18+
/// Menu item to automatically create a shader graph with the correct sub target.
19+
/// </summary>
20+
[MenuItem("Assets/Create/Shader Graph/GraphicsTools/URP/Scalable Shader Graph", priority = CoreUtils.Priorities.assetsCreateShaderMenuPriority)]
21+
public static void CreateGraphicsToolsScalableGraph()
22+
{
23+
var target = (UniversalTarget)Activator.CreateInstance(typeof(UniversalTarget));
24+
target.TrySetActiveSubTarget(typeof(GraphicsToolsUniversalScalableSubTarget));
25+
26+
var blockDescriptors = new[]
27+
{
28+
BlockFields.VertexDescription.Position,
29+
BlockFields.VertexDescription.Normal,
30+
BlockFields.VertexDescription.Tangent,
31+
BlockFields.SurfaceDescription.BaseColor,
32+
BlockFields.SurfaceDescription.NormalTS,
33+
BlockFields.SurfaceDescription.Metallic,
34+
BlockFields.SurfaceDescription.Smoothness,
35+
BlockFields.SurfaceDescription.Emission,
36+
BlockFields.SurfaceDescription.Occlusion,
37+
};
38+
39+
GraphUtil.CreateNewGraphWithOutputs(new[] { target }, blockDescriptors);
40+
}
41+
}
42+
}

com.microsoft.mrtk.graphicstools.shadergraph.unity/Editor/AssetCallbacks/CreateGraphicsToolsScalableShaderGraph.cs.meta

Lines changed: 11 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.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#if defined(MATERIAL_QUALITY_LOW)
5+
#include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/UnlitPass.hlsl"
6+
#else
7+
8+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
9+
10+
void InitializeInputData(Varyings input, SurfaceDescription surfaceDescription, out InputData inputData)
11+
{
12+
inputData = (InputData)0;
13+
14+
inputData.positionWS = input.positionWS;
15+
16+
#ifdef _NORMALMAP
17+
// IMPORTANT! If we ever support Flip on double sided materials ensure bitangent and tangent are NOT flipped.
18+
float crossSign = (input.tangentWS.w > 0.0 ? 1.0 : -1.0) * GetOddNegativeScale();
19+
float3 bitangent = crossSign * cross(input.normalWS.xyz, input.tangentWS.xyz);
20+
21+
inputData.tangentToWorld = half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz);
22+
#if _NORMAL_DROPOFF_TS
23+
inputData.normalWS = TransformTangentToWorld(surfaceDescription.NormalTS, inputData.tangentToWorld);
24+
#elif _NORMAL_DROPOFF_OS
25+
inputData.normalWS = TransformObjectToWorldNormal(surfaceDescription.NormalOS);
26+
#elif _NORMAL_DROPOFF_WS
27+
inputData.normalWS = surfaceDescription.NormalWS;
28+
#endif
29+
#else
30+
inputData.normalWS = input.normalWS;
31+
#endif
32+
inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
33+
inputData.viewDirectionWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
34+
35+
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
36+
inputData.shadowCoord = input.shadowCoord;
37+
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
38+
inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
39+
#else
40+
inputData.shadowCoord = float4(0, 0, 0, 0);
41+
#endif
42+
43+
inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
44+
inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
45+
#if defined(DYNAMICLIGHTMAP_ON)
46+
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV.xy, input.sh, inputData.normalWS);
47+
#else
48+
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.sh, inputData.normalWS);
49+
#endif
50+
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
51+
inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
52+
53+
#if defined(DEBUG_DISPLAY)
54+
#if defined(DYNAMICLIGHTMAP_ON)
55+
inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
56+
#endif
57+
#if defined(LIGHTMAP_ON)
58+
inputData.staticLightmapUV = input.staticLightmapUV;
59+
#else
60+
inputData.vertexSH = input.sh;
61+
#endif
62+
#endif
63+
}
64+
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+
74+
void frag(
75+
PackedVaryings packedInput
76+
, out half4 outColor : SV_Target0
77+
#ifdef _WRITE_RENDERING_LAYERS
78+
, out float4 outRenderingLayers : SV_Target1
79+
#endif
80+
)
81+
{
82+
Varyings unpacked = UnpackVaryings(packedInput);
83+
UNITY_SETUP_INSTANCE_ID(unpacked);
84+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
85+
SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);
86+
87+
#if defined(_SURFACE_TYPE_TRANSPARENT)
88+
bool isTransparent = true;
89+
#else
90+
bool isTransparent = false;
91+
#endif
92+
93+
#if defined(_ALPHATEST_ON)
94+
half alpha = AlphaDiscard(surfaceDescription.Alpha, surfaceDescription.AlphaClipThreshold);
95+
#elif defined(_SURFACE_TYPE_TRANSPARENT)
96+
half alpha = surfaceDescription.Alpha;
97+
#else
98+
half alpha = half(1.0);
99+
#endif
100+
101+
#if defined(LOD_FADE_CROSSFADE) && USE_UNITY_CROSSFADE
102+
LODFadeCrossFade(unpacked.positionCS);
103+
#endif
104+
105+
InputData inputData;
106+
InitializeInputData(unpacked, surfaceDescription, inputData);
107+
// TODO: Mip debug modes would require this, open question how to do this on ShaderGraph.
108+
//SETUP_DEBUG_TEXTURE_DATA(inputData, unpacked.texCoord1.xy, _MainTex);
109+
110+
#ifdef _SPECULAR_SETUP
111+
float3 specular = surfaceDescription.Specular;
112+
#if defined(MATERIAL_QUALITY_MEDIUM)
113+
#else
114+
float metallic = 1;
115+
#endif//MATERIAL_QUALITY_MEDIUM
116+
#else
117+
float3 specular = 0;
118+
#if defined(MATERIAL_QUALITY_MEDIUM)
119+
#else
120+
float metallic = surfaceDescription.Metallic;
121+
#endif//MATERIAL_QUALITY_MEDIUM
122+
#endif
123+
124+
half3 normalTS = half3(0, 0, 0);
125+
#if defined(_NORMALMAP) && defined(_NORMAL_DROPOFF_TS)
126+
normalTS = surfaceDescription.NormalTS;
127+
#endif
128+
129+
SurfaceData surface;
130+
surface.albedo = surfaceDescription.BaseColor;
131+
#if defined(MATERIAL_QUALITY_MEDIUM)
132+
surface.metallic = 0.0;
133+
#else
134+
surface.metallic = saturate(metallic);
135+
#endif//MATERIAL_QUALITY_MEDIUM
136+
surface.specular = specular;
137+
surface.smoothness = saturate(surfaceDescription.Smoothness),
138+
#if defined(MATERIAL_QUALITY_MEDIUM)
139+
surface.occlusion = 1.0;
140+
#else
141+
surface.occlusion = surfaceDescription.Occlusion,
142+
#endif//MATERIAL_QUALITY_MEDIUM
143+
surface.emission = surfaceDescription.Emission,
144+
surface.alpha = saturate(alpha);
145+
surface.normalTS = normalTS;
146+
surface.clearCoatMask = 0;
147+
surface.clearCoatSmoothness = 1;
148+
149+
#ifdef _CLEARCOAT
150+
surface.clearCoatMask = saturate(surfaceDescription.CoatMask);
151+
surface.clearCoatSmoothness = saturate(surfaceDescription.CoatSmoothness);
152+
#endif
153+
154+
surface.albedo = AlphaModulate(surface.albedo, surface.alpha);
155+
156+
#ifdef _DBUFFER
157+
ApplyDecalToSurfaceData(unpacked.positionCS, surface, inputData);
158+
#endif
159+
160+
#if defined(MATERIAL_QUALITY_MEDIUM)
161+
half4 color = UniversalFragmentBlinnPhong(inputData, surface);
162+
#else
163+
half4 color = UniversalFragmentPBR(inputData, surface);
164+
#endif//MATERIAL_QUALITY_MEDIUM
165+
color.rgb = MixFog(color.rgb, inputData.fogCoord);
166+
color.a = OutputAlpha(color.a, isTransparent);
167+
168+
outColor = color;
169+
170+
#ifdef _WRITE_RENDERING_LAYERS
171+
uint renderingLayers = GetMeshRenderingLayer();
172+
outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
173+
#endif
174+
}
175+
#endif

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

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

0 commit comments

Comments
 (0)