ReciprocalNode.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Reflection;
  2. using UnityEngine;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Drawing.Controls;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. enum ReciprocalMethod
  8. {
  9. Default,
  10. Fast
  11. };
  12. [Title("Math", "Advanced", "Reciprocal")]
  13. class ReciprocalNode : CodeFunctionNode
  14. {
  15. public ReciprocalNode()
  16. {
  17. name = "Reciprocal";
  18. }
  19. [SerializeField]
  20. private ReciprocalMethod m_ReciprocalMethod = ReciprocalMethod.Default;
  21. [EnumControl("Method")]
  22. public ReciprocalMethod reciprocalMethod
  23. {
  24. get { return m_ReciprocalMethod; }
  25. set
  26. {
  27. if (m_ReciprocalMethod == value)
  28. return;
  29. m_ReciprocalMethod = value;
  30. Dirty(ModificationScope.Graph);
  31. }
  32. }
  33. protected override MethodInfo GetFunctionToConvert()
  34. {
  35. switch (m_ReciprocalMethod)
  36. {
  37. case ReciprocalMethod.Fast:
  38. return GetType().GetMethod("Unity_Reciprocal_Fast", BindingFlags.Static | BindingFlags.NonPublic);
  39. default:
  40. return GetType().GetMethod("Unity_Reciprocal", BindingFlags.Static | BindingFlags.NonPublic);
  41. }
  42. }
  43. static string Unity_Reciprocal(
  44. [Slot(0, Binding.None, 1, 1, 1, 1)] DynamicDimensionVector In,
  45. [Slot(1, Binding.None)] out DynamicDimensionVector Out)
  46. {
  47. return
  48. @"
  49. {
  50. Out = 1.0/In;
  51. }
  52. ";
  53. }
  54. static string Unity_Reciprocal_Fast(
  55. [Slot(0, Binding.None, 1, 1, 1, 1)] DynamicDimensionVector In,
  56. [Slot(1, Binding.None)] out DynamicDimensionVector Out)
  57. {
  58. return
  59. @"
  60. {
  61. Out = rcp(In);
  62. }
  63. ";
  64. }
  65. }
  66. }