MetalReflectanceNode.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using UnityEngine;
  4. using UnityEditor.Graphing;
  5. using UnityEditor.ShaderGraph.Drawing.Controls;
  6. namespace UnityEditor.ShaderGraph
  7. {
  8. enum MetalMaterialType
  9. {
  10. Iron,
  11. Silver,
  12. Aluminium,
  13. Gold,
  14. Copper,
  15. Chromium,
  16. Nickel,
  17. Titanium,
  18. Cobalt,
  19. Platinum
  20. };
  21. [Title("Input", "PBR", "Metal Reflectance")]
  22. class MetalReflectanceNode : AbstractMaterialNode, IGeneratesBodyCode
  23. {
  24. public MetalReflectanceNode()
  25. {
  26. name = "Metal Reflectance";
  27. UpdateNodeAfterDeserialization();
  28. }
  29. [SerializeField]
  30. private MetalMaterialType m_Material = MetalMaterialType.Iron;
  31. [EnumControl("Material")]
  32. public MetalMaterialType material
  33. {
  34. get { return m_Material; }
  35. set
  36. {
  37. if (m_Material == value)
  38. return;
  39. m_Material = value;
  40. Dirty(ModificationScope.Graph);
  41. }
  42. }
  43. static Dictionary<MetalMaterialType, string> m_MaterialList = new Dictionary<MetalMaterialType, string>
  44. {
  45. {MetalMaterialType.Iron, "(0.560, 0.570, 0.580)"},
  46. {MetalMaterialType.Silver, "(0.972, 0.960, 0.915)"},
  47. {MetalMaterialType.Aluminium, "(0.913, 0.921, 0.925)"},
  48. {MetalMaterialType.Gold, "(1.000, 0.766, 0.336)"},
  49. {MetalMaterialType.Copper, "(0.955, 0.637, 0.538)"},
  50. {MetalMaterialType.Chromium, "(0.550, 0.556, 0.554)"},
  51. {MetalMaterialType.Nickel, "(0.660, 0.609, 0.526)"},
  52. {MetalMaterialType.Titanium, "(0.542, 0.497, 0.449)"},
  53. {MetalMaterialType.Cobalt, "(0.662, 0.655, 0.634)"},
  54. {MetalMaterialType.Platinum, "(0.672, 0.637, 0.585)"}
  55. };
  56. private const int kOutputSlotId = 0;
  57. private const string kOutputSlotName = "Out";
  58. public override bool hasPreview { get { return true; } }
  59. public override PreviewMode previewMode
  60. {
  61. get { return PreviewMode.Preview2D; }
  62. }
  63. public sealed override void UpdateNodeAfterDeserialization()
  64. {
  65. AddSlot(new Vector3MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector3.zero));
  66. RemoveSlotsNameNotMatching(new[] { kOutputSlotId });
  67. }
  68. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  69. {
  70. sb.AppendLine(string.Format("$precision3 {0} = $precision3{1};", GetVariableNameForSlot(kOutputSlotId), m_MaterialList[material].ToString(CultureInfo.InvariantCulture)));
  71. }
  72. }
  73. }