Skip to content

Commit 41fb322

Browse files
authored
[ShaderGraph][2021.2] Update rectangle node to better handle detail at a distance (#3405)
* Fix for 1156801 - erroneous edge pixels generated by rectangle node - Existing method doesn't handle large screen space derivatives well - Add a new control to the rectangle node to switch between methods - Existing method is "fastest" and the new method is "nicest" - Test project uses default of "fastest", but users can pick "nicest" for best results. * Add documentation and changelog entry for the new Rectangle Node control * Add updated thumbnail image for Rectangle node
1 parent 9030004 commit 41fb322

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

com.unity.shadergraph/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1919
- Node included HLSL files are now tracked more robustly, so they work after file moves and renames [1301915] (https://issuetracker.unity3d.com/product/unity/issues/guid/1301915/)
2020
- Clean up console error reporting from node shader compilation so errors are reported in the graph rather than the Editor console [1296291] (https://issuetracker.unity3d.com/product/unity/issues/guid/1296291/)
2121
- Fixed treatment of node precision in subgraphs, now allows subgraphs to switch precisions based on the subgraph node [1304050] (https://issuetracker.unity3d.com/issues/precision-errors-when-theres-a-precision-discrepancy-between-subgraphs-and-parent-graphs)
22+
- Fixed an issue where the Rectangle Node could lose detail at a distance. New control offers additional method that preserves detail better [1156801]
2223

2324
## [11.0.0] - 2020-10-21
2425

com.unity.shadergraph/Documentation~/Rectangle-Node.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ NOTE: This [Node](Node.md) can only be used in the **Fragment** [Shader Stage](S
1515
| Height | Input | Float | None | Rectangle height |
1616
| Out | Output | Float | None | Output value |
1717

18+
## Controls
19+
20+
| Name | Type | Options | Description |
21+
|:------------ |:-------------|:-----|:---|
22+
| | Dropdown | Fastest, Nicest | Robustness of computation |
23+
1824
## Generated Code Example
1925

2026
The following example code represents one possible outcome of this node.
Lines changed: 2 additions & 2 deletions
Loading

com.unity.shadergraph/Editor/Data/Nodes/Procedural/Shape/RectangleNode.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
using System.Reflection;
22
using UnityEngine;
3+
using UnityEditor.Graphing;
4+
using UnityEditor.ShaderGraph.Drawing.Controls;
35

46
namespace UnityEditor.ShaderGraph
57
{
8+
enum ClampType
9+
{
10+
Fastest,
11+
Nicest
12+
};
13+
614
[Title("Procedural", "Shape", "Rectangle")]
715
class RectangleNode : CodeFunctionNode
816
{
@@ -11,12 +19,36 @@ public RectangleNode()
1119
name = "Rectangle";
1220
}
1321

22+
[SerializeField]
23+
private ClampType m_ClampType = ClampType.Fastest;
24+
25+
[EnumControl("")]
26+
public ClampType clampType
27+
{
28+
get { return m_ClampType; }
29+
set
30+
{
31+
if (m_ClampType == value)
32+
return;
33+
34+
m_ClampType = value;
35+
Dirty(ModificationScope.Graph);
36+
}
37+
}
38+
1439
protected override MethodInfo GetFunctionToConvert()
1540
{
16-
return GetType().GetMethod("Unity_Rectangle", BindingFlags.Static | BindingFlags.NonPublic);
41+
switch (clampType)
42+
{
43+
case ClampType.Nicest:
44+
return GetType().GetMethod("Unity_Rectangle_Nicest", BindingFlags.Static | BindingFlags.NonPublic);
45+
case ClampType.Fastest:
46+
default:
47+
return GetType().GetMethod("Unity_Rectangle_Fastest", BindingFlags.Static | BindingFlags.NonPublic);
48+
}
1749
}
1850

19-
static string Unity_Rectangle(
51+
static string Unity_Rectangle_Fastest(
2052
[Slot(0, Binding.MeshUV0)] Vector2 UV,
2153
[Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
2254
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
@@ -28,6 +60,25 @@ static string Unity_Rectangle(
2860
$precision2 d = abs(UV * 2 - 1) - $precision2(Width, Height);
2961
d = 1 - d / fwidth(d);
3062
Out = saturate(min(d.x, d.y));
63+
}";
64+
}
65+
66+
static string Unity_Rectangle_Nicest(
67+
[Slot(0, Binding.MeshUV0)] Vector2 UV,
68+
[Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
69+
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
70+
[Slot(3, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
71+
{
72+
return
73+
@"
74+
{
75+
UV = UV * 2.0 - 1.0;
76+
$precision2 w = $precision2(Width, Height); // rectangle width/height
77+
$precision2 f = min(fwidth(UV), 0.5f);
78+
$precision2 k = 1.0f / f;
79+
$precision2 o = saturate(0.5f + k * (w - abs(UV)));
80+
o = min(o, k * w * 2.0f);
81+
Out = o.x * o.y;
3182
}";
3283
}
3384
}

0 commit comments

Comments
 (0)