PositionNode.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor.Graphing;
  7. using UnityEditor.ShaderGraph.Drawing.Controls;
  8. using UnityEditor.ShaderGraph.Internal;
  9. namespace UnityEditor.ShaderGraph
  10. {
  11. [FormerName("UnityEngine.MaterialGraph.WorldPosNode")]
  12. [Title("Input", "Geometry", "Position")]
  13. class PositionNode : GeometryNode, IMayRequirePosition
  14. {
  15. private const int kOutputSlotId = 0;
  16. public const string kOutputSlotName = "Out";
  17. public override List<CoordinateSpace> validSpaces => new List<CoordinateSpace> {CoordinateSpace.Object, CoordinateSpace.View, CoordinateSpace.World, CoordinateSpace.Tangent, CoordinateSpace.AbsoluteWorld};
  18. public PositionNode()
  19. {
  20. name = "Position";
  21. precision = Precision.Float;
  22. UpdateNodeAfterDeserialization();
  23. }
  24. public sealed override void UpdateNodeAfterDeserialization()
  25. {
  26. AddSlot(new Vector3MaterialSlot(
  27. kOutputSlotId,
  28. kOutputSlotName,
  29. kOutputSlotName,
  30. SlotType.Output,
  31. Vector3.zero));
  32. RemoveSlotsNameNotMatching(new[] { kOutputSlotId });
  33. }
  34. public override int GetCompiledNodeVersion() => 1;
  35. public override void UpgradeNodeWithVersion(int from, int to)
  36. {
  37. if (from == 0 && to == 1 && space == CoordinateSpace.World)
  38. {
  39. var names = validSpaces.Select(cs => cs.ToString().PascalToLabel()).ToArray();
  40. spacePopup = new PopupList(names, (int)CoordinateSpace.AbsoluteWorld);
  41. }
  42. }
  43. public override string GetVariableNameForSlot(int slotId)
  44. {
  45. return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.Position));
  46. }
  47. public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
  48. {
  49. return space.ToNeededCoordinateSpace();
  50. }
  51. }
  52. }