BlendNode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System.Reflection;
  2. using UnityEditor.ShaderGraph.Drawing.Controls;
  3. using UnityEngine;
  4. using UnityEditor.Graphing;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. [Title("Artistic", "Blend", "Blend")]
  8. class BlendNode : CodeFunctionNode
  9. {
  10. public BlendNode()
  11. {
  12. name = "Blend";
  13. }
  14. string GetCurrentBlendName()
  15. {
  16. return System.Enum.GetName(typeof(BlendMode), m_BlendMode);
  17. }
  18. [SerializeField]
  19. BlendMode m_BlendMode = BlendMode.Overlay;
  20. [EnumControl("Mode")]
  21. public BlendMode blendMode
  22. {
  23. get { return m_BlendMode; }
  24. set
  25. {
  26. if (m_BlendMode == value)
  27. return;
  28. m_BlendMode = value;
  29. Dirty(ModificationScope.Graph);
  30. }
  31. }
  32. protected override MethodInfo GetFunctionToConvert()
  33. {
  34. return GetType().GetMethod(string.Format("Unity_Blend_{0}", GetCurrentBlendName()),
  35. BindingFlags.Static | BindingFlags.NonPublic);
  36. }
  37. static string Unity_Blend_Burn(
  38. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  39. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  40. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  41. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  42. {
  43. return
  44. @"
  45. {
  46. Out = 1.0 - (1.0 - Blend)/(Base + 0.000000000001);
  47. Out = lerp(Base, Out, Opacity);
  48. }";
  49. }
  50. static string Unity_Blend_Darken(
  51. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  52. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  53. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  54. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  55. {
  56. return
  57. @"
  58. {
  59. Out = min(Blend, Base);
  60. Out = lerp(Base, Out, Opacity);
  61. }";
  62. }
  63. static string Unity_Blend_Difference(
  64. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  65. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  66. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  67. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  68. {
  69. return
  70. @"
  71. {
  72. Out = abs(Blend - Base);
  73. Out = lerp(Base, Out, Opacity);
  74. }";
  75. }
  76. static string Unity_Blend_Dodge(
  77. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  78. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  79. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  80. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  81. {
  82. return
  83. @"
  84. {
  85. Out = Base / (1.0 - clamp(Blend, 0.000001, 0.999999));
  86. Out = lerp(Base, Out, Opacity);
  87. }";
  88. }
  89. static string Unity_Blend_Divide(
  90. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  91. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  92. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  93. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  94. {
  95. return
  96. @"
  97. {
  98. Out = Base / (Blend + 0.000000000001);
  99. Out = lerp(Base, Out, Opacity);
  100. }";
  101. }
  102. static string Unity_Blend_Exclusion(
  103. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  104. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  105. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  106. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  107. {
  108. return
  109. @"
  110. {
  111. Out = Blend + Base - (2.0 * Blend * Base);
  112. Out = lerp(Base, Out, Opacity);
  113. }";
  114. }
  115. static string Unity_Blend_HardLight(
  116. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  117. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  118. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  119. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  120. {
  121. return
  122. @"
  123. {
  124. $precision{slot2dimension} result1 = 1.0 - 2.0 * (1.0 - Base) * (1.0 - Blend);
  125. $precision{slot2dimension} result2 = 2.0 * Base * Blend;
  126. $precision{slot2dimension} zeroOrOne = step(Blend, 0.5);
  127. Out = result2 * zeroOrOne + (1 - zeroOrOne) * result1;
  128. Out = lerp(Base, Out, Opacity);
  129. }";
  130. }
  131. static string Unity_Blend_HardMix(
  132. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  133. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  134. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  135. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  136. {
  137. return
  138. @"
  139. {
  140. Out = step(1 - Base, Blend);
  141. Out = lerp(Base, Out, Opacity);
  142. }";
  143. }
  144. static string Unity_Blend_Lighten(
  145. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  146. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  147. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  148. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  149. {
  150. return
  151. @"
  152. {
  153. Out = max(Blend, Base);
  154. Out = lerp(Base, Out, Opacity);
  155. }";
  156. }
  157. static string Unity_Blend_LinearBurn(
  158. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  159. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  160. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  161. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  162. {
  163. return
  164. @"
  165. {
  166. Out = Base + Blend - 1.0;
  167. Out = lerp(Base, Out, Opacity);
  168. }";
  169. }
  170. static string Unity_Blend_LinearDodge(
  171. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  172. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  173. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  174. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  175. {
  176. return
  177. @"
  178. {
  179. Out = Base + Blend;
  180. Out = lerp(Base, Out, Opacity);
  181. }";
  182. }
  183. static string Unity_Blend_LinearLight(
  184. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  185. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  186. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  187. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  188. {
  189. return
  190. @"
  191. {
  192. Out = Blend < 0.5 ? max(Base + (2 * Blend) - 1, 0) : min(Base + 2 * (Blend - 0.5), 1);
  193. Out = lerp(Base, Out, Opacity);
  194. }";
  195. }
  196. static string Unity_Blend_LinearLightAddSub(
  197. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  198. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  199. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  200. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  201. {
  202. return
  203. @"
  204. {
  205. Out = Blend + 2.0 * Base - 1.0;
  206. Out = lerp(Base, Out, Opacity);
  207. }";
  208. }
  209. static string Unity_Blend_Multiply(
  210. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  211. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  212. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  213. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  214. {
  215. return
  216. @"
  217. {
  218. Out = Base * Blend;
  219. Out = lerp(Base, Out, Opacity);
  220. }";
  221. }
  222. static string Unity_Blend_Negation(
  223. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  224. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  225. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  226. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  227. {
  228. return
  229. @"
  230. {
  231. Out = 1.0 - abs(1.0 - Blend - Base);
  232. Out = lerp(Base, Out, Opacity);
  233. }";
  234. }
  235. static string Unity_Blend_Screen(
  236. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  237. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  238. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  239. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  240. {
  241. return
  242. @"
  243. {
  244. Out = 1.0 - (1.0 - Blend) * (1.0 - Base);
  245. Out = lerp(Base, Out, Opacity);
  246. }";
  247. }
  248. static string Unity_Blend_Overlay(
  249. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  250. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  251. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  252. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  253. {
  254. return
  255. @"
  256. {
  257. $precision{slot2dimension} result1 = 1.0 - 2.0 * (1.0 - Base) * (1.0 - Blend);
  258. $precision{slot2dimension} result2 = 2.0 * Base * Blend;
  259. $precision{slot2dimension} zeroOrOne = step(Base, 0.5);
  260. Out = result2 * zeroOrOne + (1 - zeroOrOne) * result1;
  261. Out = lerp(Base, Out, Opacity);
  262. }
  263. ";
  264. }
  265. static string Unity_Blend_PinLight(
  266. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  267. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  268. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  269. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  270. {
  271. return
  272. @"
  273. {
  274. $precision{slot2dimension} check = step (0.5, Blend);
  275. $precision{slot2dimension} result1 = check * max(2.0 * (Base - 0.5), Blend);
  276. Out = result1 + (1.0 - check) * min(2.0 * Base, Blend);
  277. Out = lerp(Base, Out, Opacity);
  278. }
  279. ";
  280. }
  281. static string Unity_Blend_SoftLight(
  282. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  283. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  284. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  285. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  286. {
  287. return
  288. @"
  289. {
  290. $precision{slot2dimension} result1 = 2.0 * Base * Blend + Base * Base * (1.0 - 2.0 * Blend);
  291. $precision{slot2dimension} result2 = sqrt(Base) * (2.0 * Blend - 1.0) + 2.0 * Base * (1.0 - Blend);
  292. $precision{slot2dimension} zeroOrOne = step(0.5, Blend);
  293. Out = result2 * zeroOrOne + (1 - zeroOrOne) * result1;
  294. Out = lerp(Base, Out, Opacity);
  295. }
  296. ";
  297. }
  298. static string Unity_Blend_VividLight(
  299. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  300. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  301. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  302. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  303. {
  304. return
  305. @"
  306. {
  307. Base = clamp(Base, 0.000001, 0.999999);
  308. $precision{slot2dimension} result1 = 1.0 - (1.0 - Blend) / (2.0 * Base);
  309. $precision{slot2dimension} result2 = Blend / (2.0 * (1.0 - Base));
  310. $precision{slot2dimension} zeroOrOne = step(0.5, Base);
  311. Out = result2 * zeroOrOne + (1 - zeroOrOne) * result1;
  312. Out = lerp(Base, Out, Opacity);
  313. }
  314. ";
  315. }
  316. static string Unity_Blend_Subtract(
  317. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  318. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  319. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  320. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  321. {
  322. return
  323. @"
  324. {
  325. Out = Base - Blend;
  326. Out = lerp(Base, Out, Opacity);
  327. }
  328. ";
  329. }
  330. static string Unity_Blend_Overwrite(
  331. [Slot(0, Binding.None)] DynamicDimensionVector Base,
  332. [Slot(1, Binding.None)] DynamicDimensionVector Blend,
  333. [Slot(3, Binding.None, 1, 1, 1, 1)] Vector1 Opacity,
  334. [Slot(2, Binding.None)] out DynamicDimensionVector Out)
  335. {
  336. return
  337. @"
  338. {
  339. Out = lerp(Base, Blend, Opacity);
  340. }";
  341. }
  342. }
  343. }