SampleTexture3DNode.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Drawing.Controls;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. [Title("Input", "Texture", "Sample Texture 3D")]
  8. class SampleTexture3DNode : AbstractMaterialNode, IGeneratesBodyCode
  9. {
  10. public const int OutputSlotId = 0;
  11. public const int TextureInputId = 1;
  12. public const int UVInput = 2;
  13. public const int SamplerInput = 3;
  14. const string kOutputSlotName = "Out";
  15. const string kTextureInputName = "Texture";
  16. const string kUVInputName = "UV";
  17. const string kSamplerInputName = "Sampler";
  18. public override bool hasPreview { get { return true; } }
  19. public SampleTexture3DNode()
  20. {
  21. name = "Sample Texture 3D";
  22. UpdateNodeAfterDeserialization();
  23. }
  24. public sealed override void UpdateNodeAfterDeserialization()
  25. {
  26. AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero, ShaderStageCapability.Fragment));
  27. AddSlot(new Texture3DInputMaterialSlot(TextureInputId, kTextureInputName, kTextureInputName));
  28. AddSlot(new Vector3MaterialSlot(UVInput, kUVInputName, kUVInputName, SlotType.Input, Vector3.zero));
  29. AddSlot(new SamplerStateMaterialSlot(SamplerInput, kSamplerInputName, kSamplerInputName, SlotType.Input));
  30. RemoveSlotsNameNotMatching(new[] { OutputSlotId, TextureInputId, UVInput, SamplerInput });
  31. }
  32. // Node generations
  33. public virtual void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  34. {
  35. var uvName = GetSlotValue(UVInput, generationMode);
  36. //Sampler input slot
  37. var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInput);
  38. var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
  39. var id = GetSlotValue(TextureInputId, generationMode);
  40. var result = string.Format("$precision4 {0} = SAMPLE_TEXTURE3D({1}, {2}, {3});"
  41. , GetVariableNameForSlot(OutputSlotId)
  42. , id
  43. , edgesSampler.Any() ? GetSlotValue(SamplerInput, generationMode) : "sampler" + id
  44. , uvName);
  45. sb.AppendLine(result);
  46. }
  47. }
  48. }