RoundedRectangleNode.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Reflection;
  2. using UnityEngine;
  3. namespace UnityEditor.ShaderGraph
  4. {
  5. [Title("Procedural", "Shape", "Rounded Rectangle")]
  6. class RoundedRectangleNode : CodeFunctionNode
  7. {
  8. public RoundedRectangleNode()
  9. {
  10. name = "Rounded Rectangle";
  11. }
  12. protected override MethodInfo GetFunctionToConvert()
  13. {
  14. return GetType().GetMethod("Unity_RoundedRectangle", BindingFlags.Static | BindingFlags.NonPublic);
  15. }
  16. static string Unity_RoundedRectangle(
  17. [Slot(0, Binding.MeshUV0)] Vector2 UV,
  18. [Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
  19. [Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
  20. [Slot(3, Binding.None, 0.1f, 0, 0, 0)] Vector1 Radius,
  21. [Slot(4, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
  22. {
  23. return
  24. @"
  25. {
  26. Radius = max(min(min(abs(Radius * 2), abs(Width)), abs(Height)), 1e-5);
  27. $precision2 uv = abs(UV * 2 - 1) - $precision2(Width, Height) + Radius;
  28. $precision d = length(max(0, uv)) / Radius;
  29. Out = saturate((1 - d) / fwidth(d));
  30. }";
  31. }
  32. }
  33. }