Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. using Random = UnityEngine.Random;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. [Title("UV", "Flipbook")]
  11. class FlipbookNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IMayRequireMeshUV
  12. {
  13. public FlipbookNode()
  14. {
  15. name = "Flipbook";
  16. synonyms = new string[] { "atlas", "animation" };
  17. UpdateNodeAfterDeserialization();
  18. }
  19. const int UVSlotId = 0;
  20. const int WidthSlotId = 1;
  21. const int HeightSlotId = 2;
  22. const int TileSlotId = 3;
  23. const int OutputSlotId = 4;
  24. const string kUVSlotName = "UV";
  25. const string kWidthSlotName = "Width";
  26. const string kHeightSlotName = "Height";
  27. const string kTileSlotName = "Tile";
  28. const string kOutputSlotName = "Out";
  29. public override bool hasPreview
  30. {
  31. get { return true; }
  32. }
  33. string GetFunctionName()
  34. {
  35. string invertText = string.Empty;
  36. if (m_InvertX && m_InvertY)
  37. invertText = "InvertXY_";
  38. else if (m_InvertX)
  39. invertText = "InvertX_";
  40. else if (m_InvertY)
  41. invertText = "InvertY_";
  42. return $"Unity_Flipbook_{invertText}$precision";
  43. }
  44. public sealed override void UpdateNodeAfterDeserialization()
  45. {
  46. AddSlot(new UVMaterialSlot(UVSlotId, kUVSlotName, kUVSlotName, UVChannel.UV0));
  47. AddSlot(new Vector1MaterialSlot(WidthSlotId, kWidthSlotName, kWidthSlotName, SlotType.Input, 1));
  48. AddSlot(new Vector1MaterialSlot(HeightSlotId, kHeightSlotName, kHeightSlotName, SlotType.Input, 1));
  49. AddSlot(new Vector1MaterialSlot(TileSlotId, kTileSlotName, kTileSlotName, SlotType.Input, 0));
  50. AddSlot(new Vector2MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector2.zero));
  51. RemoveSlotsNameNotMatching(new[] { UVSlotId, WidthSlotId, HeightSlotId, TileSlotId, OutputSlotId });
  52. }
  53. [SerializeField]
  54. private bool m_InvertX = false;
  55. [ToggleControl("Invert X")]
  56. public ToggleData invertX
  57. {
  58. get { return new ToggleData(m_InvertX); }
  59. set
  60. {
  61. if (m_InvertX == value.isOn)
  62. return;
  63. m_InvertX = value.isOn;
  64. Dirty(ModificationScope.Graph);
  65. }
  66. }
  67. [SerializeField]
  68. private bool m_InvertY = true;
  69. [ToggleControl("Invert Y")]
  70. public ToggleData invertY
  71. {
  72. get { return new ToggleData(m_InvertY); }
  73. set
  74. {
  75. if (m_InvertY == value.isOn)
  76. return;
  77. m_InvertY = value.isOn;
  78. Dirty(ModificationScope.Graph);
  79. }
  80. }
  81. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  82. {
  83. var uvValue = GetSlotValue(UVSlotId, generationMode);
  84. var widthValue = GetSlotValue(WidthSlotId, generationMode);
  85. var heightValue = GetSlotValue(HeightSlotId, generationMode);
  86. var tileValue = GetSlotValue(TileSlotId, generationMode);
  87. var outputValue = GetSlotValue(OutputSlotId, generationMode);
  88. sb.AppendLine("{0} {1};", FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToShaderString(), GetVariableNameForSlot(OutputSlotId));
  89. if (!generationMode.IsPreview())
  90. {
  91. sb.AppendLine("$precision2 _{0}_Invert = $precision2 ({1}, {2});", GetVariableNameForNode(), invertX.isOn ? 1 : 0, invertY.isOn ? 1 : 0);
  92. }
  93. sb.AppendLine("{0}({1}, {2}, {3}, {4}, _{5}_Invert, {6});", GetFunctionName(), uvValue, widthValue, heightValue, tileValue, GetVariableNameForNode(), outputValue);
  94. }
  95. public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
  96. {
  97. base.CollectPreviewMaterialProperties(properties);
  98. properties.Add(new PreviewProperty(PropertyType.Vector2)
  99. {
  100. name = string.Format("_{0}_Invert", GetVariableNameForNode()),
  101. vector4Value = new Vector2(invertX.isOn ? 1 : 0, invertY.isOn ? 1 : 0)
  102. });
  103. }
  104. public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
  105. {
  106. if (!generationMode.IsPreview())
  107. return;
  108. base.CollectShaderProperties(properties, generationMode);
  109. properties.AddShaderProperty(new Vector2ShaderProperty()
  110. {
  111. overrideReferenceName = string.Format("_{0}_Invert", GetVariableNameForNode()),
  112. generatePropertyBlock = false
  113. });
  114. }
  115. public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
  116. {
  117. registry.ProvideFunction(GetFunctionName(), s =>
  118. {
  119. s.AppendLine("void {0} ({1} UV, {2} Width, {3} Height, {4} Tile, $precision2 Invert, out {5} Out)",
  120. GetFunctionName(),
  121. FindInputSlot<MaterialSlot>(UVSlotId).concreteValueType.ToShaderString(),
  122. FindInputSlot<MaterialSlot>(WidthSlotId).concreteValueType.ToShaderString(),
  123. FindInputSlot<MaterialSlot>(HeightSlotId).concreteValueType.ToShaderString(),
  124. FindInputSlot<MaterialSlot>(TileSlotId).concreteValueType.ToShaderString(),
  125. FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToShaderString());
  126. using (s.BlockScope())
  127. {
  128. s.AppendLine("Tile = floor(fmod(Tile + $precision(0.00001), Width*Height));");
  129. s.AppendLine("$precision2 tileCount = $precision2(1.0, 1.0) / $precision2(Width, Height);");
  130. s.AppendLine("$precision base = floor((Tile + $precision(0.5)) * tileCount.x);");
  131. AppendInvertSpecificLines(s);
  132. s.AppendLine("Out = (UV + $precision2(tileX, tileY)) * tileCount;");
  133. }
  134. });
  135. }
  136. private void AppendInvertSpecificLines(ShaderStringBuilder stringBuilder)
  137. {
  138. if (m_InvertX)
  139. {
  140. stringBuilder.AppendLine("$precision tileX = (Invert.x * Width - (Tile - Width * base + Invert.x * 1));");
  141. }
  142. else
  143. {
  144. stringBuilder.AppendLine("$precision tileX = (Tile - Width * base);");
  145. }
  146. if (m_InvertY)
  147. {
  148. stringBuilder.AppendLine("$precision tileY = (Invert.y * Height - (base + Invert.y * 1));");
  149. }
  150. else
  151. {
  152. stringBuilder.AppendLine("$precision tileY = base;");
  153. }
  154. }
  155. public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
  156. {
  157. using (var tempSlots = PooledList<MaterialSlot>.Get())
  158. {
  159. GetInputSlots(tempSlots);
  160. var result = false;
  161. foreach (var slot in tempSlots)
  162. {
  163. if (slot.RequiresMeshUV(channel))
  164. {
  165. result = true;
  166. break;
  167. }
  168. }
  169. tempSlots.Clear();
  170. return result;
  171. }
  172. }
  173. }
  174. }