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.

720 lines
25 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Collections.Generic;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. public sealed class TemplatesBlendModule : TemplateModuleParent
  11. {
  12. private const string AlphaToMaskStr = "Alpha To Coverage";
  13. private const string BlendModeStr = " Blend Mode";
  14. private const string BlendModesRGBStr = "Blend RGB";
  15. private const string BlendModesAlphaStr = "Blend Alpha";
  16. private const string BlendOpsRGBStr = "Blend Op RGB";
  17. private const string BlendOpsAlphaStr = "Blend Op Alpha";
  18. private const string SourceFactorStr = "Src";
  19. private const string DstFactorStr = "Dst";
  20. private const string AlphaToMaskFormat = "AlphaToMask {0}";
  21. private const string BlendFactorOff = "Blend Off";
  22. private const string SingleBlendFactorStr = "Blend {0} {1}";
  23. private const string SeparateBlendFactorStr = "Blend {0} {1} , {2} {3}";
  24. private const string SingleBlendOpStr = "BlendOp {0}";
  25. private const string SeparateBlendOpStr = "BlendOp {0} , {1}";
  26. private const string BlendOpOffStr = "BlendOp Off";
  27. private string[] m_commonBlendTypesArr;
  28. private List<CommonBlendTypes> m_commonBlendTypes = new List<CommonBlendTypes>
  29. {
  30. new CommonBlendTypes("<OFF>", AvailableBlendFactor.Zero, AvailableBlendFactor.Zero ),
  31. new CommonBlendTypes("Custom", AvailableBlendFactor.Zero, AvailableBlendFactor.Zero ) ,
  32. new CommonBlendTypes("Alpha Blend", AvailableBlendFactor.SrcAlpha, AvailableBlendFactor.OneMinusSrcAlpha ) ,
  33. new CommonBlendTypes("Premultiplied", AvailableBlendFactor.One, AvailableBlendFactor.OneMinusSrcAlpha ),
  34. new CommonBlendTypes("Additive", AvailableBlendFactor.One, AvailableBlendFactor.One ),
  35. new CommonBlendTypes("Soft Additive", AvailableBlendFactor.OneMinusDstColor, AvailableBlendFactor.One ),
  36. new CommonBlendTypes("Multiplicative", AvailableBlendFactor.DstColor, AvailableBlendFactor.Zero ),
  37. new CommonBlendTypes("2x Multiplicative", AvailableBlendFactor.DstColor, AvailableBlendFactor.SrcColor ),
  38. new CommonBlendTypes("Particle Additive", AvailableBlendFactor.SrcAlpha, AvailableBlendFactor.One )
  39. };
  40. [SerializeField]
  41. private bool m_validBlendMode = false;
  42. [SerializeField]
  43. private bool m_validBlendOp = false;
  44. [SerializeField]
  45. private bool m_blendModeEnabled = false;
  46. [SerializeField]
  47. private bool m_validAlphaToMask = false;
  48. [SerializeField]
  49. private bool m_alphaToMaskValue = false;
  50. [SerializeField]
  51. private bool m_alphaToMaskIndependent = false;
  52. // Blend Factor
  53. // RGB
  54. [SerializeField]
  55. private int m_currentRGBIndex = 0;
  56. [SerializeField]
  57. private AvailableBlendFactor m_sourceFactorRGB = AvailableBlendFactor.Zero;
  58. [SerializeField]
  59. private InlineProperty m_sourceFactorRGBInline = new InlineProperty();
  60. [SerializeField]
  61. private AvailableBlendFactor m_destFactorRGB = AvailableBlendFactor.Zero;
  62. [SerializeField]
  63. private InlineProperty m_destFactorRGBInline = new InlineProperty();
  64. // Alpha
  65. [SerializeField]
  66. private int m_currentAlphaIndex = 0;
  67. [SerializeField]
  68. private AvailableBlendFactor m_sourceFactorAlpha = AvailableBlendFactor.Zero;
  69. [SerializeField]
  70. private InlineProperty m_sourceFactorAlphaInline = new InlineProperty();
  71. [SerializeField]
  72. private AvailableBlendFactor m_destFactorAlpha = AvailableBlendFactor.Zero;
  73. [SerializeField]
  74. private InlineProperty m_destFactorAlphaInline = new InlineProperty();
  75. //Blend Ops
  76. [SerializeField]
  77. private bool m_blendOpEnabled = false;
  78. [SerializeField]
  79. private AvailableBlendOps m_blendOpRGB = AvailableBlendOps.OFF;
  80. [SerializeField]
  81. private InlineProperty m_blendOpRGBInline = new InlineProperty();
  82. [SerializeField]
  83. private AvailableBlendOps m_blendOpAlpha = AvailableBlendOps.OFF;
  84. [SerializeField]
  85. private InlineProperty m_blendOpAlphaInline = new InlineProperty();
  86. public TemplatesBlendModule() : base( "Blend Mode and Ops" )
  87. {
  88. m_commonBlendTypesArr = new string[ m_commonBlendTypes.Count ];
  89. for( int i = 0; i < m_commonBlendTypesArr.Length; i++ )
  90. {
  91. m_commonBlendTypesArr[ i ] = m_commonBlendTypes[ i ].Name;
  92. }
  93. }
  94. public void CopyFrom( TemplatesBlendModule other, bool allData )
  95. {
  96. if( allData )
  97. {
  98. m_independentModule = other.IndependentModule;
  99. m_alphaToMaskIndependent = other.AlphaToMaskIndependent;
  100. m_validBlendMode = other.ValidBlendMode;
  101. m_validBlendOp = other.ValidBlendOp;
  102. m_validAlphaToMask = other.ValidAlphaToMask;
  103. }
  104. m_alphaToMaskValue = other.AlphaToMaskValue;
  105. m_blendModeEnabled = other.BlendModeEnabled;
  106. m_currentRGBIndex = other.CurrentRGBIndex;
  107. m_sourceFactorRGB = other.SourceFactorRGB;
  108. m_destFactorRGB = other.DestFactorRGB;
  109. m_currentAlphaIndex = other.CurrentAlphaIndex;
  110. m_sourceFactorAlpha = other.SourceFactorAlpha;
  111. m_destFactorAlpha = other.DestFactorAlpha;
  112. m_blendOpEnabled = other.BlendOpEnabled;
  113. m_blendOpRGB = other.BlendOpRGB;
  114. m_blendOpAlpha = other.BlendOpAlpha;
  115. m_sourceFactorRGBInline = other.SourceFactorRGBInline;
  116. m_destFactorRGBInline = other.DestFactorRGBInline;
  117. m_sourceFactorAlphaInline = other.SourceFactorAlphaInline;
  118. m_destFactorAlphaInline = other.DestFactorAlphaInline;
  119. m_blendOpRGBInline = other.BlendOpRGBInline;
  120. m_blendOpAlphaInline = other.BlendOpAlphaInline;
  121. }
  122. public void ConfigureFromTemplateData( TemplateBlendData blendData )
  123. {
  124. if( blendData.ValidAlphaToMask )
  125. {
  126. if( m_validAlphaToMask != blendData.ValidAlphaToMask )
  127. {
  128. m_alphaToMaskValue = blendData.AlphaToMaskValue;
  129. m_validAlphaToMask = blendData.ValidAlphaToMask;
  130. m_alphaToMaskIndependent = blendData.IndependentAlphaToMask;
  131. }
  132. }
  133. if( blendData.ValidBlendMode )
  134. {
  135. if( m_validBlendMode != blendData.ValidBlendMode )
  136. {
  137. m_blendModeEnabled = true;
  138. m_independentModule = blendData.IndependentModule;
  139. if( string.IsNullOrEmpty( blendData.SourceFactorRGBInline ) )
  140. {
  141. m_sourceFactorRGB = blendData.SourceFactorRGB;
  142. m_sourceFactorRGBInline.ResetProperty();
  143. }
  144. else
  145. {
  146. m_sourceFactorRGBInline.SetInlineByName( blendData.SourceFactorRGBInline );
  147. }
  148. if( string.IsNullOrEmpty( blendData.DestFactorRGBInline ) )
  149. {
  150. m_destFactorRGB = blendData.DestFactorRGB;
  151. m_destFactorRGBInline.ResetProperty();
  152. }
  153. else
  154. {
  155. m_destFactorRGBInline.SetInlineByName( blendData.DestFactorRGBInline );
  156. }
  157. if( string.IsNullOrEmpty( blendData.SourceFactorAlphaInline ) )
  158. {
  159. m_sourceFactorAlpha = blendData.SourceFactorAlpha;
  160. m_sourceFactorAlphaInline.ResetProperty();
  161. }
  162. else
  163. {
  164. m_sourceFactorAlphaInline.SetInlineByName( blendData.SourceFactorAlphaInline );
  165. }
  166. if( string.IsNullOrEmpty( blendData.DestFactorAlphaInline ) )
  167. {
  168. m_destFactorAlpha = blendData.DestFactorAlpha;
  169. m_destFactorAlphaInline.ResetProperty();
  170. }
  171. else
  172. {
  173. m_destFactorAlphaInline.SetInlineByName( blendData.DestFactorAlphaInline );
  174. }
  175. if( blendData.SeparateBlendFactors )
  176. {
  177. if( blendData.BlendModeOff )
  178. {
  179. m_currentRGBIndex = 0;
  180. }
  181. else
  182. {
  183. CheckRGBIndex();
  184. }
  185. CheckAlphaIndex();
  186. }
  187. else
  188. {
  189. if( blendData.BlendModeOff )
  190. {
  191. m_currentRGBIndex = 0;
  192. }
  193. else
  194. {
  195. CheckRGBIndex();
  196. }
  197. m_currentAlphaIndex = 0;
  198. }
  199. }
  200. }
  201. else
  202. {
  203. m_blendModeEnabled = false;
  204. }
  205. if( blendData.ValidBlendOp )
  206. {
  207. if( m_validBlendOp != blendData.ValidBlendOp )
  208. {
  209. m_blendOpEnabled = true;
  210. if( string.IsNullOrEmpty( blendData.BlendOpRGBInline ) )
  211. {
  212. m_blendOpRGB = blendData.BlendOpRGB;
  213. m_blendOpRGBInline.ResetProperty();
  214. }
  215. else
  216. {
  217. m_blendOpRGBInline.SetInlineByName( blendData.BlendOpRGBInline );
  218. }
  219. if( string.IsNullOrEmpty( blendData.BlendOpAlphaInline ) )
  220. {
  221. m_blendOpAlpha = blendData.BlendOpAlpha;
  222. m_blendOpAlphaInline.ResetProperty();
  223. }
  224. else
  225. {
  226. m_blendOpAlphaInline.SetInlineByName( blendData.BlendOpAlphaInline );
  227. }
  228. }
  229. }
  230. else
  231. {
  232. m_blendOpEnabled = false;
  233. }
  234. m_validBlendMode = blendData.ValidBlendMode;
  235. m_validBlendOp = blendData.ValidBlendOp;
  236. m_validData = m_validBlendMode || m_validBlendOp;
  237. }
  238. public override void ShowUnreadableDataMessage( ParentNode owner )
  239. {
  240. bool foldout = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedBlendModeModule;
  241. NodeUtils.DrawPropertyGroup( ref foldout, BlendModeStr, base.ShowUnreadableDataMessage );
  242. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedBlendModeModule = foldout;
  243. }
  244. public override void Draw( UndoParentNode owner, bool style = true )
  245. {
  246. bool foldout = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedBlendModeModule;
  247. if( style )
  248. {
  249. NodeUtils.DrawPropertyGroup( ref foldout, BlendModeStr, () =>
  250. {
  251. DrawBlock( owner, style );
  252. } );
  253. }
  254. else
  255. {
  256. NodeUtils.DrawNestedPropertyGroup( ref foldout, BlendModeStr, () =>
  257. {
  258. DrawBlock( owner, style );
  259. } );
  260. }
  261. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedBlendModeModule = foldout;
  262. }
  263. void DrawBlock( UndoParentNode owner, bool style )
  264. {
  265. EditorGUI.BeginChangeCheck();
  266. {
  267. if( m_blendModeEnabled )
  268. {
  269. // RGB
  270. EditorGUI.BeginChangeCheck();
  271. m_currentRGBIndex = owner.EditorGUILayoutPopup( BlendModesRGBStr, m_currentRGBIndex, m_commonBlendTypesArr );
  272. if( EditorGUI.EndChangeCheck() )
  273. {
  274. if( m_currentRGBIndex > 1 )
  275. {
  276. m_sourceFactorRGB = m_commonBlendTypes[ m_currentRGBIndex ].SourceFactor;
  277. m_sourceFactorRGBInline.IntValue = (int)m_sourceFactorRGB;
  278. m_sourceFactorRGBInline.SetInlineNodeValue();
  279. m_destFactorRGB = m_commonBlendTypes[ m_currentRGBIndex ].DestFactor;
  280. m_destFactorRGBInline.IntValue = (int)m_destFactorRGB;
  281. m_destFactorRGBInline.SetInlineNodeValue();
  282. }
  283. }
  284. EditorGUI.BeginDisabledGroup( m_currentRGBIndex == 0 );
  285. EditorGUI.BeginChangeCheck();
  286. float cached = EditorGUIUtility.labelWidth;
  287. if( style )
  288. {
  289. EditorGUIUtility.labelWidth = 40;
  290. }
  291. else
  292. {
  293. EditorGUIUtility.labelWidth = 25;
  294. }
  295. EditorGUILayout.BeginHorizontal();
  296. //m_sourceFactorRGB = (AvailableBlendFactor)owner.EditorGUILayoutEnumPopup( SourceFactorStr, m_sourceFactorRGB );
  297. m_sourceFactorRGBInline.CustomDrawer( ref owner, ( x ) => { m_sourceFactorRGB = (AvailableBlendFactor)x.EditorGUILayoutEnumPopup( SourceFactorStr, m_sourceFactorRGB ); }, SourceFactorStr );
  298. if( style )
  299. {
  300. EditorGUI.indentLevel--;
  301. EditorGUIUtility.labelWidth = 25;
  302. }
  303. //m_destFactorRGB = (AvailableBlendFactor)owner.EditorGUILayoutEnumPopup( DstFactorStr, m_destFactorRGB );
  304. m_destFactorRGBInline.CustomDrawer( ref owner, ( x ) => { m_destFactorRGB = (AvailableBlendFactor)x.EditorGUILayoutEnumPopup( DstFactorStr, m_destFactorRGB ); }, DstFactorStr );
  305. if( style )
  306. EditorGUI.indentLevel++;
  307. EditorGUILayout.EndHorizontal();
  308. EditorGUIUtility.labelWidth = cached;
  309. if( EditorGUI.EndChangeCheck() )
  310. {
  311. CheckRGBIndex();
  312. }
  313. EditorGUI.EndDisabledGroup();
  314. // Alpha
  315. EditorGUILayout.Separator();
  316. EditorGUI.BeginChangeCheck();
  317. m_currentAlphaIndex = owner.EditorGUILayoutPopup( BlendModesAlphaStr, m_currentAlphaIndex, m_commonBlendTypesArr );
  318. if( EditorGUI.EndChangeCheck() )
  319. {
  320. if( m_currentAlphaIndex > 0 )
  321. {
  322. m_sourceFactorAlpha = m_commonBlendTypes[ m_currentAlphaIndex ].SourceFactor;
  323. m_sourceFactorAlphaInline.IntValue = (int)m_sourceFactorAlpha;
  324. m_sourceFactorAlphaInline.SetInlineNodeValue();
  325. m_destFactorAlpha = m_commonBlendTypes[ m_currentAlphaIndex ].DestFactor;
  326. m_destFactorAlphaInline.IntValue = (int)m_destFactorAlpha;
  327. m_destFactorAlphaInline.SetInlineNodeValue();
  328. }
  329. }
  330. EditorGUI.BeginDisabledGroup( m_currentAlphaIndex == 0 );
  331. EditorGUI.BeginChangeCheck();
  332. cached = EditorGUIUtility.labelWidth;
  333. if( style )
  334. {
  335. EditorGUIUtility.labelWidth = 40;
  336. }
  337. else
  338. {
  339. EditorGUIUtility.labelWidth = 25;
  340. }
  341. EditorGUILayout.BeginHorizontal();
  342. //m_sourceFactorAlpha = (AvailableBlendFactor)owner.EditorGUILayoutEnumPopup( SourceFactorStr, m_sourceFactorAlpha );
  343. m_sourceFactorAlphaInline.CustomDrawer( ref owner, ( x ) => { m_sourceFactorAlpha = (AvailableBlendFactor)x.EditorGUILayoutEnumPopup( SourceFactorStr, m_sourceFactorAlpha ); }, SourceFactorStr );
  344. if( style )
  345. {
  346. EditorGUI.indentLevel--;
  347. EditorGUIUtility.labelWidth = 25;
  348. }
  349. //m_destFactorAlpha = (AvailableBlendFactor)owner.EditorGUILayoutEnumPopup( DstFactorStr, m_destFactorAlpha );
  350. m_destFactorAlphaInline.CustomDrawer( ref owner, ( x ) => { m_destFactorAlpha = (AvailableBlendFactor)x.EditorGUILayoutEnumPopup( DstFactorStr, m_destFactorAlpha ); }, DstFactorStr );
  351. if( style )
  352. EditorGUI.indentLevel++;
  353. EditorGUILayout.EndHorizontal();
  354. EditorGUIUtility.labelWidth = cached;
  355. if( EditorGUI.EndChangeCheck() )
  356. {
  357. CheckAlphaIndex();
  358. }
  359. EditorGUI.EndDisabledGroup();
  360. EditorGUILayout.Separator();
  361. }
  362. if( m_blendOpEnabled )
  363. {
  364. // Both these tests should be removed on a later stage
  365. // ASE v154dev004 changed AvailableBlendOps.OFF value from -1 to 0
  366. // If importing the new package into an already opened ASE window makes
  367. // hotcode to preserve the -1 value on these variables
  368. if( (int)m_blendOpRGB == -1 )
  369. m_blendOpRGB = AvailableBlendOps.OFF;
  370. if( (int)m_blendOpAlpha == -1 )
  371. m_blendOpAlpha = AvailableBlendOps.OFF;
  372. //m_blendOpRGB = (AvailableBlendOps)owner.EditorGUILayoutEnumPopup( BlendOpsRGBStr, m_blendOpRGB );
  373. m_blendOpRGBInline.CustomDrawer( ref owner, ( x ) => { m_blendOpRGB = (AvailableBlendOps)x.EditorGUILayoutPopup( BlendOpsRGBStr, (int)m_blendOpRGB, BlendOpsHelper.BlendOpsLabels ); }, BlendOpsRGBStr );
  374. EditorGUILayout.Separator();
  375. //m_blendOpAlpha = (AvailableBlendOps)owner.EditorGUILayoutEnumPopup( BlendOpsAlphaStr, m_blendOpAlpha );
  376. m_blendOpAlphaInline.CustomDrawer( ref owner, ( x ) => { m_blendOpAlpha = (AvailableBlendOps)x.EditorGUILayoutPopup( BlendOpsAlphaStr, (int)m_blendOpAlpha, BlendOpsHelper.BlendOpsLabels ); }, BlendOpsAlphaStr );
  377. }
  378. if( m_validAlphaToMask )
  379. {
  380. EditorGUILayout.Space();
  381. m_alphaToMaskValue = owner.EditorGUILayoutToggle( AlphaToMaskStr, m_alphaToMaskValue );
  382. }
  383. }
  384. if( EditorGUI.EndChangeCheck() )
  385. {
  386. m_isDirty = true;
  387. }
  388. }
  389. void CheckRGBIndex()
  390. {
  391. int count = m_commonBlendTypes.Count;
  392. m_currentRGBIndex = 1;
  393. for( int i = 1; i < count; i++ )
  394. {
  395. if( m_commonBlendTypes[ i ].SourceFactor == m_sourceFactorRGB && m_commonBlendTypes[ i ].DestFactor == m_destFactorRGB )
  396. {
  397. m_currentRGBIndex = i;
  398. return;
  399. }
  400. }
  401. }
  402. void CheckAlphaIndex()
  403. {
  404. int count = m_commonBlendTypes.Count;
  405. m_currentAlphaIndex = 1;
  406. for( int i = 1; i < count; i++ )
  407. {
  408. if( m_commonBlendTypes[ i ].SourceFactor == m_sourceFactorAlpha && m_commonBlendTypes[ i ].DestFactor == m_destFactorAlpha )
  409. {
  410. m_currentAlphaIndex = i;
  411. if( m_currentAlphaIndex > 0 && m_currentRGBIndex == 0 )
  412. m_currentRGBIndex = 1;
  413. return;
  414. }
  415. }
  416. if( m_currentAlphaIndex > 0 && m_currentRGBIndex == 0 )
  417. m_currentRGBIndex = 1;
  418. }
  419. public void ReadAlphaToMaskFromString( ref uint index, ref string[] nodeParams )
  420. {
  421. if( UIUtils.CurrentShaderVersion() > 16102 )
  422. {
  423. m_validAlphaToMask = Convert.ToBoolean( nodeParams[ index++ ] );
  424. if( m_validAlphaToMask )
  425. {
  426. m_alphaToMaskValue = Convert.ToBoolean( nodeParams[ index++ ] );
  427. }
  428. }
  429. }
  430. public void ReadBlendModeFromString( ref uint index, ref string[] nodeParams )
  431. {
  432. bool validDataOnMeta = m_validBlendMode;
  433. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  434. {
  435. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  436. }
  437. if( validDataOnMeta )
  438. {
  439. if( UIUtils.CurrentShaderVersion() < 15304 )
  440. {
  441. m_currentRGBIndex = Convert.ToInt32( nodeParams[ index++ ] );
  442. m_sourceFactorRGB = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), nodeParams[ index++ ] );
  443. m_destFactorRGB = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), nodeParams[ index++ ] );
  444. m_currentAlphaIndex = Convert.ToInt32( nodeParams[ index++ ] );
  445. m_sourceFactorAlpha = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), nodeParams[ index++ ] );
  446. m_destFactorAlpha = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), nodeParams[ index++ ] );
  447. }
  448. else
  449. {
  450. m_currentRGBIndex = Convert.ToInt32( nodeParams[ index++ ] );
  451. m_sourceFactorRGBInline.ReadFromString( ref index, ref nodeParams );
  452. m_sourceFactorRGB = (AvailableBlendFactor)m_sourceFactorRGBInline.IntValue;
  453. m_destFactorRGBInline.ReadFromString( ref index, ref nodeParams );
  454. m_destFactorRGB = (AvailableBlendFactor)m_destFactorRGBInline.IntValue;
  455. m_currentAlphaIndex = Convert.ToInt32( nodeParams[ index++ ] );
  456. m_sourceFactorAlphaInline.ReadFromString( ref index, ref nodeParams );
  457. m_sourceFactorAlpha = (AvailableBlendFactor)m_sourceFactorAlphaInline.IntValue;
  458. m_destFactorAlphaInline.ReadFromString( ref index, ref nodeParams );
  459. m_destFactorAlpha = (AvailableBlendFactor)m_destFactorAlphaInline.IntValue;
  460. }
  461. }
  462. }
  463. public void ReadBlendOpFromString( ref uint index, ref string[] nodeParams )
  464. {
  465. bool validDataOnMeta = m_validBlendOp;
  466. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  467. {
  468. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  469. }
  470. if( validDataOnMeta )
  471. {
  472. if( UIUtils.CurrentShaderVersion() < 15304 )
  473. {
  474. m_blendOpRGB = (AvailableBlendOps)Enum.Parse( typeof( AvailableBlendOps ), nodeParams[ index++ ] );
  475. m_blendOpAlpha = (AvailableBlendOps)Enum.Parse( typeof( AvailableBlendOps ), nodeParams[ index++ ] );
  476. }
  477. else
  478. {
  479. m_blendOpRGBInline.ReadFromString( ref index, ref nodeParams );
  480. m_blendOpAlphaInline.ReadFromString( ref index, ref nodeParams );
  481. if( UIUtils.CurrentShaderVersion() < 15404 )
  482. {
  483. // Now BlendOps enum starts at 0 and not -1
  484. m_blendOpRGBInline.FloatValue += 1;
  485. m_blendOpAlphaInline.FloatValue += 1;
  486. }
  487. m_blendOpRGB = (AvailableBlendOps)m_blendOpRGBInline.IntValue;
  488. m_blendOpAlpha = (AvailableBlendOps)m_blendOpAlphaInline.IntValue;
  489. }
  490. //m_blendOpEnabled = ( m_blendOpRGB != AvailableBlendOps.OFF );
  491. }
  492. }
  493. public void WriteAlphaToMaskToString( ref string nodeInfo )
  494. {
  495. IOUtils.AddFieldValueToString( ref nodeInfo, m_validAlphaToMask );
  496. if( m_validAlphaToMask )
  497. {
  498. IOUtils.AddFieldValueToString( ref nodeInfo, m_alphaToMaskValue );
  499. }
  500. }
  501. public void WriteBlendModeToString( ref string nodeInfo )
  502. {
  503. IOUtils.AddFieldValueToString( ref nodeInfo, m_validBlendMode );
  504. if( m_validBlendMode )
  505. {
  506. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentRGBIndex );
  507. if( !m_sourceFactorRGBInline.IsValid ) m_sourceFactorRGBInline.IntValue = (int)m_sourceFactorRGB;
  508. m_sourceFactorRGBInline.WriteToString( ref nodeInfo );
  509. if( !m_destFactorRGBInline.IsValid ) m_destFactorRGBInline.IntValue = (int)m_destFactorRGB;
  510. m_destFactorRGBInline.WriteToString( ref nodeInfo );
  511. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentAlphaIndex );
  512. if( !m_sourceFactorAlphaInline.IsValid ) m_sourceFactorAlphaInline.IntValue = (int)m_sourceFactorAlpha;
  513. m_sourceFactorAlphaInline.WriteToString( ref nodeInfo );
  514. if( !m_destFactorAlphaInline.IsValid ) m_destFactorAlphaInline.IntValue = (int)m_destFactorAlpha;
  515. m_destFactorAlphaInline.WriteToString( ref nodeInfo );
  516. }
  517. }
  518. public void WriteBlendOpToString( ref string nodeInfo )
  519. {
  520. IOUtils.AddFieldValueToString( ref nodeInfo, m_validBlendOp );
  521. if( m_validBlendOp )
  522. {
  523. if( !m_blendOpRGBInline.IsValid ) m_blendOpRGBInline.IntValue = (int)m_blendOpRGB;
  524. m_blendOpRGBInline.WriteToString( ref nodeInfo );
  525. if( !m_blendOpAlphaInline.IsValid ) m_blendOpAlphaInline.IntValue = (int)m_blendOpAlpha;
  526. m_blendOpAlphaInline.WriteToString( ref nodeInfo );
  527. }
  528. }
  529. public override void ReadFromString( ref uint index, ref string[] nodeParams )
  530. {
  531. ReadBlendModeFromString( ref index, ref nodeParams );
  532. ReadBlendOpFromString( ref index, ref nodeParams );
  533. ReadAlphaToMaskFromString( ref index, ref nodeParams );
  534. }
  535. public override void WriteToString( ref string nodeInfo )
  536. {
  537. WriteBlendModeToString( ref nodeInfo );
  538. WriteBlendOpToString( ref nodeInfo );
  539. WriteAlphaToMaskToString( ref nodeInfo );
  540. }
  541. public override void Destroy()
  542. {
  543. base.Destroy();
  544. m_sourceFactorRGBInline = null;
  545. m_destFactorRGBInline = null;
  546. m_sourceFactorAlphaInline = null;
  547. m_destFactorAlphaInline = null;
  548. m_blendOpRGBInline = null;
  549. m_blendOpAlphaInline = null;
  550. }
  551. public string CurrentAlphaToMask
  552. {
  553. get
  554. {
  555. return string.Format( AlphaToMaskFormat, m_alphaToMaskValue ? "On" : "Off" );
  556. }
  557. }
  558. public string CurrentBlendFactorSingle
  559. {
  560. get
  561. {
  562. return ( m_currentRGBIndex > 0 ) ? string.Format( SingleBlendFactorStr, m_sourceFactorRGBInline.GetValueOrProperty( m_sourceFactorRGB.ToString() ), m_destFactorRGBInline.GetValueOrProperty( m_destFactorRGB.ToString() ) ) : BlendFactorOff;
  563. }
  564. }
  565. public string CurrentBlendFactorSeparate
  566. {
  567. get
  568. {
  569. return string.Format( SeparateBlendFactorStr,
  570. m_sourceFactorRGBInline.GetValueOrProperty( ( m_currentRGBIndex > 0 ? m_sourceFactorRGB.ToString() : AvailableBlendFactor.One.ToString() ) ),
  571. m_destFactorRGBInline.GetValueOrProperty( m_currentRGBIndex > 0 ? m_destFactorRGB.ToString() : AvailableBlendFactor.Zero.ToString() ),
  572. m_sourceFactorAlphaInline.GetValueOrProperty( m_sourceFactorAlpha.ToString() ),
  573. m_destFactorAlphaInline.GetValueOrProperty( m_destFactorAlpha.ToString() ) );
  574. }
  575. }
  576. public string CurrentBlendFactor
  577. {
  578. get
  579. {
  580. return ( ( m_currentAlphaIndex > 0 ) ? CurrentBlendFactorSeparate : CurrentBlendFactorSingle );
  581. }
  582. }
  583. public string CurrentBlendOpSingle
  584. {
  585. get
  586. {
  587. return ( m_blendOpRGB != AvailableBlendOps.OFF || m_blendOpRGBInline.IsValid ) ? string.Format( SingleBlendOpStr, m_blendOpRGBInline.GetValueOrProperty( m_blendOpRGB.ToString() ) ) : string.Empty;
  588. }
  589. }
  590. public string CurrentBlendOpSeparate
  591. {
  592. get
  593. {
  594. return string.Format( SeparateBlendOpStr, m_blendOpRGBInline.GetValueOrProperty( ( m_currentRGBIndex > 0 && m_blendOpRGB != AvailableBlendOps.OFF ) ? m_blendOpRGB.ToString() : AvailableBlendOps.Add.ToString() ), m_blendOpAlphaInline.GetValueOrProperty( m_blendOpAlpha.ToString() ) );
  595. }
  596. }
  597. public string CurrentBlendOp { get { return ( ( m_blendOpAlpha != AvailableBlendOps.OFF || m_blendOpAlphaInline.IsValid ) ? CurrentBlendOpSeparate : CurrentBlendOpSingle ); } }
  598. public bool Active { get { return m_blendModeEnabled && ( m_currentRGBIndex > 0 || m_currentAlphaIndex > 0 ); } }
  599. public bool BlendOpActive
  600. {
  601. get
  602. {
  603. return m_blendOpEnabled &&
  604. (
  605. m_blendOpRGBInline.Active ||
  606. m_blendOpAlphaInline.Active ||
  607. ( !m_blendOpRGBInline.Active && m_blendOpRGB != AvailableBlendOps.OFF ) ||
  608. ( !m_blendOpAlphaInline.Active && m_blendOpAlpha != AvailableBlendOps.OFF ) );
  609. }
  610. }
  611. public bool ValidBlendMode { get { return m_validBlendMode; } }
  612. public bool ValidBlendOp { get { return m_validBlendOp; } }
  613. public int CurrentRGBIndex { get { return m_currentRGBIndex; } }
  614. public AvailableBlendFactor SourceFactorRGB { get { return m_sourceFactorRGB; } }
  615. public AvailableBlendFactor DestFactorRGB { get { return m_destFactorRGB; } }
  616. public int CurrentAlphaIndex { get { return m_currentAlphaIndex; } }
  617. public AvailableBlendFactor SourceFactorAlpha { get { return m_sourceFactorAlpha; } }
  618. public AvailableBlendFactor DestFactorAlpha { get { return m_destFactorAlpha; } }
  619. public bool BlendModeEnabled { get { return m_blendModeEnabled; } }
  620. public bool BlendOpEnabled { get { return m_blendOpEnabled; } }
  621. public AvailableBlendOps BlendOpRGB { get { return m_blendOpRGB; } }
  622. public AvailableBlendOps BlendOpAlpha { get { return m_blendOpAlpha; } }
  623. public InlineProperty SourceFactorRGBInline { get { return m_sourceFactorRGBInline; } }
  624. public InlineProperty DestFactorRGBInline { get { return m_destFactorRGBInline; } }
  625. public InlineProperty SourceFactorAlphaInline { get { return m_sourceFactorAlphaInline; } }
  626. public InlineProperty DestFactorAlphaInline { get { return m_destFactorAlphaInline; } }
  627. public InlineProperty BlendOpRGBInline { get { return m_blendOpRGBInline; } }
  628. public InlineProperty BlendOpAlphaInline { get { return m_blendOpAlphaInline; } }
  629. public bool IsAdditiveRGB { get { return m_validBlendMode && m_blendModeEnabled && ( m_currentRGBIndex > 0 ) && ( m_sourceFactorRGB == AvailableBlendFactor.One ) && ( m_destFactorRGB == AvailableBlendFactor.One ); } }
  630. public bool IsAlphaBlendRGB { get { return m_validBlendMode && m_blendModeEnabled && ( m_currentRGBIndex > 0 ) && ( m_sourceFactorRGB == AvailableBlendFactor.SrcAlpha ) && ( m_destFactorRGB == AvailableBlendFactor.OneMinusSrcAlpha ); } }
  631. public bool ValidAlphaToMask { get { return m_validAlphaToMask; } }
  632. public bool AlphaToMaskValue { get { return m_alphaToMaskValue; } }
  633. public bool AlphaToMaskIndependent { get { return m_alphaToMaskIndependent; } }
  634. }
  635. }