A game about forced loneliness, made by TACStudios
at master 95 lines 2.7 kB view raw
1using System.Reflection; 2using UnityEngine; 3using UnityEditor.Graphing; 4using UnityEditor.ShaderGraph.Drawing.Controls; 5 6namespace UnityEditor.ShaderGraph 7{ 8 enum ClampType 9 { 10 Fastest, 11 Nicest 12 }; 13 14 [Title("Procedural", "Shape", "Rectangle")] 15 class RectangleNode : CodeFunctionNode 16 { 17 public RectangleNode() 18 { 19 name = "Rectangle"; 20 synonyms = new string[] { "square" }; 21 } 22 23 [SerializeField] 24 private ClampType m_ClampType = ClampType.Fastest; 25 26 [EnumControl("")] 27 public ClampType clampType 28 { 29 get { return m_ClampType; } 30 set 31 { 32 if (m_ClampType == value) 33 return; 34 35 m_ClampType = value; 36 Dirty(ModificationScope.Graph); 37 } 38 } 39 40 protected override MethodInfo GetFunctionToConvert() 41 { 42 switch (clampType) 43 { 44 case ClampType.Nicest: 45 return GetType().GetMethod("Unity_Rectangle_Nicest", BindingFlags.Static | BindingFlags.NonPublic); 46 case ClampType.Fastest: 47 default: 48 return GetType().GetMethod("Unity_Rectangle_Fastest", BindingFlags.Static | BindingFlags.NonPublic); 49 } 50 } 51 52 static string Unity_Rectangle_Fastest( 53 [Slot(0, Binding.MeshUV0)] Vector2 UV, 54 [Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width, 55 [Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height, 56 [Slot(3, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out) 57 { 58 return 59@" 60{ 61 $precision2 d = abs(UV * 2 - 1) - $precision2(Width, Height); 62#if defined(SHADER_STAGE_RAY_TRACING) 63 d = saturate((1 - saturate(d * 1e7))); 64#else 65 d = saturate(1 - d / fwidth(d)); 66#endif 67 Out = min(d.x, d.y); 68}"; 69 } 70 71 static string Unity_Rectangle_Nicest( 72 [Slot(0, Binding.MeshUV0)] Vector2 UV, 73 [Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width, 74 [Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height, 75 [Slot(3, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out) 76 { 77 return 78@" 79{ 80 UV = UV * 2.0 - 1.0; 81 $precision2 w = $precision2(Width, Height); // rectangle width/height 82#if defined(SHADER_STAGE_RAY_TRACING) 83 $precision2 o = saturate(0.5f + 1e7 * (w - abs(UV))); 84 o = min(o, 1e7 * w * 2.0f); 85#else 86 $precision2 f = min(fwidth(UV), 0.5f); 87 $precision2 k = 1.0f / f; 88 $precision2 o = saturate(0.5f + k * (w - abs(UV))); 89 o = min(o, k * w * 2.0f); 90#endif 91 Out = o.x * o.y; 92}"; 93 } 94 } 95}