Skip to content

Commit c0be1c9

Browse files
alexandret-unityEvergreen
authored andcommitted
[Backport 6000.3] Fixed generated code for preview nodes for UI Toolkit shaders
1 parent 920df9d commit c0be1c9

13 files changed

Lines changed: 122 additions & 11 deletions

Packages/com.unity.shadergraph/Editor/Data/Nodes/Input/UI/ElementLayoutUVNode.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using UnityEditor.Graphing;
22
using UnityEditor.Rendering.UITK.ShaderGraph;
3+
using UnityEditor.ShaderGraph.Internal;
34
using UnityEngine;
45

56
namespace UnityEditor.ShaderGraph
67
{
78
[Title("Input", "UI", "Element Layout UV")]
89
[SubTargetFilter(typeof(IUISubTarget))]
9-
class ElementLayoutUV : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireUITK
10+
class ElementLayoutUV : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireUITK, IMayRequireMeshUV
1011
{
1112
public const int LayoutUVSlotId = 0;
1213

@@ -29,12 +30,25 @@ public override void UpdateNodeAfterDeserialization()
2930

3031
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
3132
{
32-
if (GetInputNodeFromSlot(LayoutUVSlotId) != null) sb.AppendLine(string.Format("$precision2 {0} = IN.layoutUV.xy;", GetVariableNameForSlot(LayoutUVSlotId)));
33+
if (generationMode == GenerationMode.Preview)
34+
{
35+
// In preview mode, use standard mesh UV0 (will visualize as color: red = u, green = v)
36+
sb.AppendLine("$precision2 {0} = IN.uv0.xy;", GetVariableNameForSlot(LayoutUVSlotId));
37+
return;
38+
}
39+
40+
if (GetInputNodeFromSlot(LayoutUVSlotId) != null) sb.AppendLine("$precision2 {0} = IN.layoutUV.xy;", GetVariableNameForSlot(LayoutUVSlotId));
3341
}
3442

3543
public bool RequiresUITK(ShaderStageCapability stageCapability)
3644
{
3745
return true;
3846
}
47+
48+
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
49+
{
50+
// Require UV0 in preview mode for visualization
51+
return channel == UVChannel.UV0;
52+
}
3953
}
4054
}

Packages/com.unity.shadergraph/Editor/Data/Nodes/Input/UI/ElementTextureSizeNode.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public override void UpdateNodeAfterDeserialization()
3939

4040
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
4141
{
42+
if (generationMode == GenerationMode.Preview)
43+
{
44+
// In preview mode, return default values (1.0)
45+
if (GetInputNodeFromSlot(TextureWidthSlotId) != null) sb.AppendLine("$precision1 {0} = 1.0;", GetVariableNameForSlot(TextureWidthSlotId));
46+
if (GetInputNodeFromSlot(TextureHeightSlotId) != null) sb.AppendLine("$precision1 {0} = 1.0;", GetVariableNameForSlot(TextureHeightSlotId));
47+
if (GetInputNodeFromSlot(TexelWidthSlotId) != null) sb.AppendLine("$precision1 {0} = 1.0;", GetVariableNameForSlot(TexelWidthSlotId));
48+
if (GetInputNodeFromSlot(TexelHeightSlotId) != null) sb.AppendLine("$precision1 {0} = 1.0;", GetVariableNameForSlot(TexelHeightSlotId));
49+
return;
50+
}
51+
4252
if ((GetInputNodeFromSlot(TextureWidthSlotId) != null) ||
4353
(GetInputNodeFromSlot(TextureHeightSlotId) != null) ||
4454
(GetInputNodeFromSlot(TexelWidthSlotId) != null) ||

Packages/com.unity.shadergraph/Editor/Data/Nodes/Input/UI/ElementTextureUVNode.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using UnityEditor.Graphing;
22
using UnityEditor.Rendering.UITK.ShaderGraph;
3+
using UnityEditor.ShaderGraph.Internal;
34
using UnityEngine;
45

56
namespace UnityEditor.ShaderGraph
67
{
78
[Title("Input", "UI", "Element Texture UV")]
89
[SubTargetFilter(typeof(IUISubTarget))]
9-
class ElementTextureUVNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireUITK
10+
class ElementTextureUVNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireUITK, IMayRequireMeshUV
1011
{
1112
public const int TextureUVSlotId = 0;
1213

@@ -29,12 +30,25 @@ public override void UpdateNodeAfterDeserialization()
2930

3031
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
3132
{
32-
if (GetInputNodeFromSlot(TextureUVSlotId) != null) sb.AppendLine(string.Format("$precision2 {0} = IN.uvClip.xy;", GetVariableNameForSlot(TextureUVSlotId)));
33+
if (generationMode == GenerationMode.Preview)
34+
{
35+
// In preview mode, use standard mesh UV0 (will visualize as color: red = u, green = v)
36+
sb.AppendLine("$precision2 {0} = IN.uv0.xy;", GetVariableNameForSlot(TextureUVSlotId));
37+
return;
38+
}
39+
40+
if (GetInputNodeFromSlot(TextureUVSlotId) != null) sb.AppendLine("$precision2 {0} = IN.uvClip.xy;", GetVariableNameForSlot(TextureUVSlotId));
3341
}
3442

3543
public bool RequiresUITK(ShaderStageCapability stageCapability)
3644
{
3745
return true;
3846
}
47+
48+
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
49+
{
50+
// Require UV0 in preview mode for visualization
51+
return channel == UVChannel.UV0;
52+
}
3953
}
4054
}

Packages/com.unity.shadergraph/Editor/Data/Nodes/UI/DefaultBitmapTextNode.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo
3333
{
3434
string outputVarName = GetVariableNameForSlot(k_OutputSlotId);
3535

36-
sb.AppendLine("float4 {0} = float4(1, 1, 0, 1);", outputVarName);
36+
sb.AppendLine("float4 {0} = float4(1, 1, 1, 1);", outputVarName);
37+
38+
if (generationMode == GenerationMode.Preview)
39+
{
40+
bool hasTint = GetInputNodeFromSlot(k_InputSlotIdTint) != null;
41+
if (hasTint)
42+
sb.AppendLine("{0} = {1};", outputVarName, GetSlotValue(k_InputSlotIdTint, generationMode));
43+
return;
44+
}
3745

3846
sb.AppendLine("[branch] if ((_UIE_RENDER_TYPE_TEXT || _UIE_RENDER_TYPE_ANY) && round(IN.typeTexSettings.x) == k_FragTypeText && (!(GetTextureInfo(IN.typeTexSettings.y).sdfScale > 0.0)))");
3947
using (sb.BlockScope())

Packages/com.unity.shadergraph/Editor/Data/Nodes/UI/DefaultGradientNode.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo
2929
{
3030
string outputVarName = GetVariableNameForSlot(k_OutputSlotId);
3131

32-
sb.AppendLine("float4 {0} = float4(1, 1, 0, 1);", outputVarName);
32+
sb.AppendLine("float4 {0} = float4(1, 1, 1, 1);", outputVarName);
33+
34+
if (generationMode == GenerationMode.Preview)
35+
return;
3336

3437
sb.AppendLine("[branch] if (_UIE_RENDER_TYPE_GRADIENT || _UIE_RENDER_TYPE_ANY && round(IN.typeTexSettings.x) == k_FragTypeSvgGradient)");
3538
using (sb.BlockScope())

Packages/com.unity.shadergraph/Editor/Data/Nodes/UI/DefaultSDFTextNode.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo
3333
{
3434
string outputVarName = GetVariableNameForSlot(k_OutputSlotId);
3535

36-
sb.AppendLine("float4 {0} = float4(1, 1, 0, 1);", outputVarName);
36+
sb.AppendLine("float4 {0} = float4(1, 1, 1, 1);", outputVarName);
37+
38+
if (generationMode == GenerationMode.Preview)
39+
{
40+
bool hasTint = GetInputNodeFromSlot(k_InputSlotIdTint) != null;
41+
if (hasTint)
42+
sb.AppendLine("{0} = {1};", outputVarName, GetSlotValue(k_InputSlotIdTint, generationMode));
43+
return;
44+
}
3745

3846
sb.AppendLine("[branch] if ((_UIE_RENDER_TYPE_TEXT || _UIE_RENDER_TYPE_ANY) && round(IN.typeTexSettings.x) == k_FragTypeText && (GetTextureInfo(IN.typeTexSettings.y).sdfScale > 0.0))");
3947
using (sb.BlockScope())

Packages/com.unity.shadergraph/Editor/Data/Nodes/UI/DefaultSolidNode.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo
2929
{
3030
string outputVarName = GetVariableNameForSlot(k_OutputSlotId);
3131

32-
sb.AppendLine("float4 {0} = float4(1, 1, 0, 1);", outputVarName);
32+
sb.AppendLine("float4 {0} = float4(1, 1, 1, 1);", outputVarName);
33+
34+
if (generationMode == GenerationMode.Preview)
35+
return;
3336

3437
sb.AppendLine("[branch] if (_UIE_RENDER_TYPE_SOLID || _UIE_RENDER_TYPE_ANY && round(IN.typeTexSettings.x) == k_FragTypeSolid)");
3538
using (sb.BlockScope())

Packages/com.unity.shadergraph/Editor/Data/Nodes/UI/DefaultTextureNode.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo
3636
{
3737
string outputVarName = GetVariableNameForSlot(k_OutputSlotId);
3838

39-
sb.AppendLine("float4 {0} = float4(1, 1, 0, 1);", outputVarName);
39+
sb.AppendLine("float4 {0} = float4(1, 1, 1, 1);", outputVarName);
40+
41+
if (generationMode == GenerationMode.Preview)
42+
{
43+
bool hasTint = GetInputNodeFromSlot(k_InputSlotIdTint) != null;
44+
if (hasTint)
45+
sb.AppendLine("{0} = {1};", outputVarName, GetSlotValue(k_InputSlotIdTint, generationMode));
46+
return;
47+
}
4048

4149
sb.AppendLine("[branch] if (_UIE_RENDER_TYPE_TEXTURE || _UIE_RENDER_TYPE_ANY && round(IN.typeTexSettings.x) == k_FragTypeTexture)");
4250
using (sb.BlockScope())

Packages/com.unity.shadergraph/Editor/Data/Nodes/UI/IsForcedGammaNode.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public sealed override void UpdateNodeAfterDeserialization()
2626

2727
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
2828
{
29+
if (generationMode == GenerationMode.Preview)
30+
{
31+
// In preview mode, return false by default
32+
sb.AppendLine("bool {0} = false;", GetVariableNameForSlot(k_OutputSlotId));
33+
return;
34+
}
35+
2936
sb.AppendLine("bool {0} = _UIE_FORCE_GAMMA;", GetVariableNameForSlot(k_OutputSlotId));
3037
}
3138

Packages/com.unity.shadergraph/Editor/Data/Nodes/UI/RenderTypeBranchNode.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,21 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo
4949
string outputVarNameColor = GetVariableNameForSlot(k_OutputSlotIdColor);
5050
string outputVarNameAlpha = GetVariableNameForSlot(k_OutputSlotIdAlpha);
5151

52-
sb.AppendLine("float3 {0} = float3(0, 0, 0);", outputVarNameColor);
52+
sb.AppendLine("float3 {0} = float3(1, 1, 1);", outputVarNameColor);
5353
sb.AppendLine("float {0} = 1.0;", outputVarNameAlpha);
5454

55+
if (generationMode == GenerationMode.Preview)
56+
{
57+
// In preview mode the UITK render-type macros don't exist.
58+
// Output the Solid input if connected.
59+
if (GetInputNodeFromSlot(k_InputSlotIdSolid) != null)
60+
{
61+
sb.AppendLine("{0} = {1}.rgb;", outputVarNameColor, GetSlotValue(k_InputSlotIdSolid, generationMode));
62+
sb.AppendLine("{0} = {1}.a;", outputVarNameAlpha, GetSlotValue(k_InputSlotIdSolid, generationMode));
63+
}
64+
return;
65+
}
66+
5567
sb.AppendLine("[branch] if (_UIE_RENDER_TYPE_SOLID || _UIE_RENDER_TYPE_ANY && TestType(IN.typeTexSettings.x, k_FragTypeSolid))");
5668
using (sb.BlockScope())
5769
{

0 commit comments

Comments
 (0)