SampleCubemapNode.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Internal;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. [FormerName("UnityEditor.ShaderGraph.CubemapNode")]
  8. [Title("Input", "Texture", "Sample Cubemap")]
  9. class SampleCubemapNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireViewDirection, IMayRequireNormal
  10. {
  11. public const int OutputSlotId = 0;
  12. public const int CubemapInputId = 1;
  13. public const int ViewDirInputId = 2;
  14. public const int NormalInputId = 3;
  15. public const int SamplerInputId = 5;
  16. public const int LODInputId = 4;
  17. const string kOutputSlotName = "Out";
  18. const string kCubemapInputName = "Cube";
  19. const string kViewDirInputName = "ViewDir";
  20. const string kNormalInputName = "Normal";
  21. const string kSamplerInputName = "Sampler";
  22. const string kLODInputName = "LOD";
  23. public override bool hasPreview { get { return true; } }
  24. public SampleCubemapNode()
  25. {
  26. name = "Sample Cubemap";
  27. UpdateNodeAfterDeserialization();
  28. }
  29. public sealed override void UpdateNodeAfterDeserialization()
  30. {
  31. AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
  32. AddSlot(new CubemapInputMaterialSlot(CubemapInputId, kCubemapInputName, kCubemapInputName));
  33. AddSlot(new ViewDirectionMaterialSlot(ViewDirInputId, kViewDirInputName, kViewDirInputName, CoordinateSpace.Object));
  34. AddSlot(new NormalMaterialSlot(NormalInputId, kNormalInputName, kNormalInputName, CoordinateSpace.Object));
  35. AddSlot(new SamplerStateMaterialSlot(SamplerInputId, kSamplerInputName, kSamplerInputName, SlotType.Input));
  36. AddSlot(new Vector1MaterialSlot(LODInputId, kLODInputName, kLODInputName, SlotType.Input, 0));
  37. RemoveSlotsNameNotMatching(new[] { OutputSlotId, CubemapInputId, ViewDirInputId, NormalInputId, SamplerInputId, LODInputId });
  38. }
  39. public override PreviewMode previewMode
  40. {
  41. get { return PreviewMode.Preview3D; }
  42. }
  43. // Node generations
  44. public virtual void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  45. {
  46. //Sampler input slot
  47. var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInputId);
  48. var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
  49. var id = GetSlotValue(CubemapInputId, generationMode);
  50. string result = string.Format("$precision4 {0} = SAMPLE_TEXTURECUBE_LOD({1}, {2}, reflect(-{3}, {4}), {5});"
  51. , GetVariableNameForSlot(OutputSlotId)
  52. , id
  53. , edgesSampler.Any() ? GetSlotValue(SamplerInputId, generationMode) : "sampler" + id
  54. , GetSlotValue(ViewDirInputId, generationMode)
  55. , GetSlotValue(NormalInputId, generationMode)
  56. , GetSlotValue(LODInputId, generationMode));
  57. sb.AppendLine(result);
  58. }
  59. public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
  60. {
  61. var viewDirSlot = FindInputSlot<MaterialSlot>(ViewDirInputId);
  62. var edgesViewDir = owner.GetEdges(viewDirSlot.slotReference);
  63. if (!edgesViewDir.Any())
  64. return CoordinateSpace.Object.ToNeededCoordinateSpace();
  65. else
  66. return NeededCoordinateSpace.None;
  67. }
  68. public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
  69. {
  70. var normalSlot = FindInputSlot<MaterialSlot>(NormalInputId);
  71. var edgesNormal = owner.GetEdges(normalSlot.slotReference);
  72. if (!edgesNormal.Any())
  73. return CoordinateSpace.Object.ToNeededCoordinateSpace();
  74. else
  75. return NeededCoordinateSpace.None;
  76. }
  77. }
  78. }