SceneDepthNode.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Reflection;
  2. using UnityEngine;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Drawing.Controls;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. enum DepthSamplingMode
  8. {
  9. Linear01,
  10. Raw,
  11. Eye
  12. };
  13. [Title("Input", "Scene", "Scene Depth")]
  14. sealed class SceneDepthNode : CodeFunctionNode, IMayRequireDepthTexture
  15. {
  16. const string kScreenPositionSlotName = "UV";
  17. const string kOutputSlotName = "Out";
  18. public const int ScreenPositionSlotId = 0;
  19. public const int OutputSlotId = 1;
  20. [SerializeField]
  21. private DepthSamplingMode m_DepthSamplingMode = DepthSamplingMode.Linear01;
  22. [EnumControl("Sampling Mode")]
  23. public DepthSamplingMode depthSamplingMode
  24. {
  25. get { return m_DepthSamplingMode; }
  26. set
  27. {
  28. if (m_DepthSamplingMode == value)
  29. return ;
  30. m_DepthSamplingMode = value;
  31. Dirty(ModificationScope.Graph);
  32. }
  33. }
  34. public SceneDepthNode()
  35. {
  36. name = "Scene Depth";
  37. UpdateNodeAfterDeserialization();
  38. }
  39. public override bool hasPreview { get { return false; } }
  40. protected override MethodInfo GetFunctionToConvert()
  41. {
  42. switch (m_DepthSamplingMode)
  43. {
  44. case DepthSamplingMode.Raw:
  45. return GetType().GetMethod("Unity_SceneDepth_Raw", BindingFlags.Static | BindingFlags.NonPublic);
  46. case DepthSamplingMode.Eye:
  47. return GetType().GetMethod("Unity_SceneDepth_Eye", BindingFlags.Static | BindingFlags.NonPublic);
  48. case DepthSamplingMode.Linear01:
  49. default:
  50. return GetType().GetMethod("Unity_SceneDepth_Linear01", BindingFlags.Static | BindingFlags.NonPublic);
  51. }
  52. }
  53. static string Unity_SceneDepth_Linear01(
  54. [Slot(0, Binding.ScreenPosition)] Vector4 UV,
  55. [Slot(1, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
  56. {
  57. return
  58. @"
  59. {
  60. Out = Linear01Depth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(UV.xy), _ZBufferParams);
  61. }
  62. ";
  63. }
  64. static string Unity_SceneDepth_Raw(
  65. [Slot(0, Binding.ScreenPosition)] Vector4 UV,
  66. [Slot(1, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
  67. {
  68. return
  69. @"
  70. {
  71. Out = SHADERGRAPH_SAMPLE_SCENE_DEPTH(UV.xy);
  72. }
  73. ";
  74. }
  75. static string Unity_SceneDepth_Eye(
  76. [Slot(0, Binding.ScreenPosition)] Vector4 UV,
  77. [Slot(1, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
  78. {
  79. return
  80. @"
  81. {
  82. Out = LinearEyeDepth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(UV.xy), _ZBufferParams);
  83. }
  84. ";
  85. }
  86. public bool RequiresDepthTexture(ShaderStageCapability stageCapability)
  87. {
  88. return true;
  89. }
  90. }
  91. }