PolygonNode.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Reflection;
  2. using UnityEngine;
  3. namespace UnityEditor.ShaderGraph
  4. {
  5. [Title("Procedural", "Shape", "Polygon")]
  6. class PolygonNode : CodeFunctionNode
  7. {
  8. public PolygonNode()
  9. {
  10. name = "Polygon";
  11. }
  12. protected override MethodInfo GetFunctionToConvert()
  13. {
  14. return GetType().GetMethod("Unity_Polygon", BindingFlags.Static | BindingFlags.NonPublic);
  15. }
  16. static string Unity_Polygon(
  17. [Slot(0, Binding.MeshUV0)] Vector2 UV,
  18. [Slot(1, Binding.None, 6, 0, 0, 0)] Vector1 Sides,
  19. [Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
  20. [Slot(3, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
  21. [Slot(4, Binding.None, ShaderStageCapability.Fragment)] out DynamicDimensionVector Out)
  22. {
  23. return
  24. @"
  25. {
  26. $precision pi = 3.14159265359;
  27. $precision aWidth = Width * cos(pi / Sides);
  28. $precision aHeight = Height * cos(pi / Sides);
  29. $precision2 uv = (UV * 2 - 1) / $precision2(aWidth, aHeight);
  30. uv.y *= -1;
  31. $precision pCoord = atan2(uv.x, uv.y);
  32. $precision r = 2 * pi / Sides;
  33. $precision distance = cos(floor(0.5 + pCoord / r) * r - pCoord) * length(uv);
  34. Out = saturate((1 - distance) / fwidth(distance));
  35. }
  36. ";
  37. }
  38. }
  39. }