Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FlipNode.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Drawing.Controls;
  5. using UnityEditor.ShaderGraph.Internal;
  6. using UnityEngine;
  7. namespace UnityEditor.ShaderGraph
  8. {
  9. [Title("Channel", "Flip")]
  10. class FlipNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction
  11. {
  12. public FlipNode()
  13. {
  14. name = "Flip";
  15. UpdateNodeAfterDeserialization();
  16. }
  17. const int InputSlotId = 0;
  18. const int OutputSlotId = 1;
  19. const string kInputSlotName = "In";
  20. const string kOutputSlotName = "Out";
  21. public override bool hasPreview
  22. {
  23. get { return true; }
  24. }
  25. string GetFunctionName()
  26. {
  27. // NOTE: it's important we use the $precision generic form of the slot type in the name here
  28. return $"Unity_Flip_{FindSlot<MaterialSlot>(InputSlotId).concreteValueType.ToShaderString()}";
  29. }
  30. public sealed override void UpdateNodeAfterDeserialization()
  31. {
  32. AddSlot(new DynamicVectorMaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, SlotType.Input, Vector4.zero));
  33. AddSlot(new DynamicVectorMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
  34. RemoveSlotsNameNotMatching(new[] { InputSlotId, OutputSlotId });
  35. }
  36. int channelCount { get { return SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(InputSlotId).concreteValueType); } }
  37. [SerializeField]
  38. private bool m_RedChannel;
  39. [ToggleControl("Red")]
  40. public ToggleData redChannel
  41. {
  42. get { return new ToggleData(m_RedChannel, channelCount > 0); }
  43. set
  44. {
  45. if (m_RedChannel == value.isOn)
  46. return;
  47. m_RedChannel = value.isOn;
  48. Dirty(ModificationScope.Node);
  49. }
  50. }
  51. [SerializeField]
  52. private bool m_GreenChannel;
  53. [ToggleControl("Green")]
  54. public ToggleData greenChannel
  55. {
  56. get { return new ToggleData(m_GreenChannel, channelCount > 1); }
  57. set
  58. {
  59. if (m_GreenChannel == value.isOn)
  60. return;
  61. m_GreenChannel = value.isOn;
  62. Dirty(ModificationScope.Node);
  63. }
  64. }
  65. [SerializeField]
  66. private bool m_BlueChannel;
  67. [ToggleControl("Blue")]
  68. public ToggleData blueChannel
  69. {
  70. get { return new ToggleData(m_BlueChannel, channelCount > 2); }
  71. set
  72. {
  73. if (m_BlueChannel == value.isOn)
  74. return;
  75. m_BlueChannel = value.isOn;
  76. Dirty(ModificationScope.Node);
  77. }
  78. }
  79. [SerializeField]
  80. private bool m_AlphaChannel;
  81. [ToggleControl("Alpha")]
  82. public ToggleData alphaChannel
  83. {
  84. get { return new ToggleData(m_AlphaChannel, channelCount > 3); }
  85. set
  86. {
  87. if (m_AlphaChannel == value.isOn)
  88. return;
  89. m_AlphaChannel = value.isOn;
  90. Dirty(ModificationScope.Node);
  91. }
  92. }
  93. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  94. {
  95. var inputValue = GetSlotValue(InputSlotId, generationMode);
  96. var outputValue = GetSlotValue(OutputSlotId, generationMode);
  97. sb.AppendLine("{0} {1};", FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToShaderString(), GetVariableNameForSlot(OutputSlotId));
  98. if (!generationMode.IsPreview())
  99. {
  100. sb.TryAppendIndentation();
  101. sb.Append("{0} _{1}_Flip = {0} ({2}",
  102. FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToShaderString(),
  103. GetVariableNameForNode(),
  104. Convert.ToInt32(m_RedChannel));
  105. if (channelCount > 1)
  106. sb.Append(", {0}", Convert.ToInt32(m_GreenChannel));
  107. if (channelCount > 2)
  108. sb.Append(", {0}", Convert.ToInt32(m_BlueChannel));
  109. if (channelCount > 3)
  110. sb.Append(", {0}", Convert.ToInt32(m_AlphaChannel));
  111. sb.Append(");");
  112. sb.AppendNewLine();
  113. }
  114. sb.AppendLine("{0}({1}, _{2}_Flip, {3});", GetFunctionName(), inputValue, GetVariableNameForNode(), outputValue);
  115. }
  116. public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
  117. {
  118. base.CollectPreviewMaterialProperties(properties);
  119. properties.Add(new PreviewProperty(PropertyType.Vector4)
  120. {
  121. name = string.Format("_{0}_Flip", GetVariableNameForNode()),
  122. vector4Value = new Vector4(Convert.ToInt32(m_RedChannel), Convert.ToInt32(m_GreenChannel), Convert.ToInt32(m_BlueChannel), Convert.ToInt32(m_AlphaChannel)),
  123. });
  124. }
  125. public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
  126. {
  127. if (!generationMode.IsPreview())
  128. return;
  129. base.CollectShaderProperties(properties, generationMode);
  130. properties.AddShaderProperty(new Vector4ShaderProperty
  131. {
  132. overrideReferenceName = string.Format("_{0}_Flip", GetVariableNameForNode()),
  133. generatePropertyBlock = false
  134. });
  135. }
  136. public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
  137. {
  138. registry.ProvideFunction(GetFunctionName(), s =>
  139. {
  140. s.AppendLine("void {0}({1} In, {2} Flip, out {3} Out)",
  141. GetFunctionName(),
  142. FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToShaderString(),
  143. FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToShaderString(),
  144. FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToShaderString());
  145. using (s.BlockScope())
  146. {
  147. s.AppendLine("Out = (Flip * -2 + 1) * In;");
  148. }
  149. });
  150. }
  151. }
  152. }