Assignment for RMIT Mixed Reality in 2020
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.

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