Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ParallaxMappingNode.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. using UnityEditor.Graphing;
  3. using System;
  4. using System.Linq;
  5. using UnityEditor.ShaderGraph.Internal;
  6. using UnityEditor.ShaderGraph.Drawing.Controls;
  7. namespace UnityEditor.ShaderGraph
  8. {
  9. enum Channel
  10. {
  11. Red = 0,
  12. Green = 1,
  13. Blue = 2,
  14. Alpha = 3,
  15. }
  16. [Title("UV", "Parallax Mapping")]
  17. class ParallaxMappingNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IMayRequireViewDirection, IMayRequireMeshUV
  18. {
  19. public ParallaxMappingNode()
  20. {
  21. name = "Parallax Mapping";
  22. synonyms = new string[] { "offset mapping" };
  23. UpdateNodeAfterDeserialization();
  24. }
  25. // Input slots
  26. private const int kHeightmapSlotId = 1;
  27. private const string kHeightmapSlotName = "Heightmap";
  28. private const int kHeightmapSamplerSlotId = 2;
  29. private const string kHeightmapSamplerSlotName = "HeightmapSampler";
  30. private const int kAmplitudeSlotId = 3;
  31. private const string kAmplitudeSlotName = "Amplitude";
  32. private const int kUVsSlotId = 4;
  33. private const string kUVsSlotName = "UVs";
  34. // Output slots
  35. private const int kParallaxUVsOutputSlotId = 0;
  36. private const string kParallaxUVsOutputSlotName = "ParallaxUVs";
  37. public override bool hasPreview { get { return false; } }
  38. [SerializeField]
  39. private Channel m_Channel = Channel.Green;
  40. [EnumControl("Heightmap Sample Channel")]
  41. public Channel channel
  42. {
  43. get { return m_Channel; }
  44. set
  45. {
  46. if (m_Channel == value)
  47. return;
  48. m_Channel = value;
  49. Dirty(ModificationScope.Graph);
  50. }
  51. }
  52. public sealed override void UpdateNodeAfterDeserialization()
  53. {
  54. AddSlot(new Texture2DInputMaterialSlot(kHeightmapSlotId, kHeightmapSlotName, kHeightmapSlotName, ShaderStageCapability.Fragment));
  55. AddSlot(new SamplerStateMaterialSlot(kHeightmapSamplerSlotId, kHeightmapSamplerSlotName, kHeightmapSamplerSlotName, SlotType.Input));
  56. AddSlot(new Vector1MaterialSlot(kAmplitudeSlotId, kAmplitudeSlotName, kAmplitudeSlotName, SlotType.Input, 1, ShaderStageCapability.Fragment));
  57. AddSlot(new UVMaterialSlot(kUVsSlotId, kUVsSlotName, kUVsSlotName, UVChannel.UV0, ShaderStageCapability.Fragment));
  58. AddSlot(new Vector2MaterialSlot(kParallaxUVsOutputSlotId, kParallaxUVsOutputSlotName, kParallaxUVsOutputSlotName, SlotType.Output, Vector2.zero, ShaderStageCapability.Fragment));
  59. RemoveSlotsNameNotMatching(new[]
  60. {
  61. kParallaxUVsOutputSlotId,
  62. kHeightmapSlotId,
  63. kHeightmapSamplerSlotId,
  64. kAmplitudeSlotId,
  65. kUVsSlotId,
  66. });
  67. }
  68. public override void Setup()
  69. {
  70. base.Setup();
  71. var textureSlot = FindInputSlot<Texture2DInputMaterialSlot>(kHeightmapSlotId);
  72. textureSlot.defaultType = Texture2DShaderProperty.DefaultType.Black;
  73. }
  74. public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
  75. {
  76. registry.RequiresIncludePath("Packages/com.unity.render-pipelines.core/ShaderLibrary/ParallaxMapping.hlsl");
  77. }
  78. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  79. {
  80. var heightmap = GetSlotValue(kHeightmapSlotId, generationMode);
  81. var samplerSlot = FindInputSlot<MaterialSlot>(kHeightmapSamplerSlotId);
  82. var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
  83. var amplitude = GetSlotValue(kAmplitudeSlotId, generationMode);
  84. var uvs = GetSlotValue(kUVsSlotId, generationMode);
  85. sb.AppendLines(String.Format(@"$precision2 {5} = {0}.GetTransformedUV({4}) + ParallaxMappingChannel(TEXTURE2D_ARGS({0}.tex, {1}.samplerstate), IN.{2}, {3} * 0.01, {0}.GetTransformedUV({4}), {6});",
  86. heightmap,
  87. edgesSampler.Any() ? GetSlotValue(kHeightmapSamplerSlotId, generationMode) : heightmap,
  88. CoordinateSpace.Tangent.ToVariableName(InterpolatorType.ViewDirection),
  89. amplitude, // cm in the interface so we multiply by 0.01 in the shader to convert in meter
  90. uvs,
  91. GetSlotValue(kParallaxUVsOutputSlotId, generationMode),
  92. (int)channel
  93. ));
  94. }
  95. public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability = ShaderStageCapability.All)
  96. {
  97. return NeededCoordinateSpace.Tangent;
  98. }
  99. public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
  100. {
  101. using (var tempSlots = PooledList<MaterialSlot>.Get())
  102. {
  103. GetInputSlots(tempSlots);
  104. var result = false;
  105. foreach (var slot in tempSlots)
  106. {
  107. if (slot.RequiresMeshUV(channel))
  108. {
  109. result = true;
  110. break;
  111. }
  112. }
  113. tempSlots.Clear();
  114. return result;
  115. }
  116. }
  117. }
  118. }