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.

1149 lines
34 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Text.RegularExpressions;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace AmplifyShaderEditor
  8. {
  9. /*ase_pass_options OLDEST
  10. DefineOnConnected:portId:definevalue
  11. DefineOnUnconnected:portId:definevalue
  12. Options:name:defaultOption:opt0:opt1:opt2
  13. SetVisible:PortId:OptionName:OptionValue
  14. */
  15. /*ase_pass_options OLD
  16. Option:Option Name:UI Type:Default:Item0,Item1,Item3...ItemN
  17. Action:Action Type:Action Data:ConditionA && ConditionB || ConditionC:
  18. */
  19. /*ase_pass_options:UniqueId:PropagateDataToHiddenPasses
  20. Option:Color Offset:A,B,C:A
  21. A:ShowPort:My Port Name
  22. B,C:HidePort:My Port Name
  23. B:SetDefine:MY_DEFINE
  24. C:SetDefine:MY_COLOR_DEFINE
  25. Option:My Other Option:True,False
  26. True:ShowOption:Color Offset
  27. False:HideOption:Color Offset
  28. Port:My Port Name
  29. On:SetDefine:MY_COLOR_DEFINE
  30. Off:UnsetDefine:MY_COLOR_DEFINE
  31. */
  32. public enum AseOptionsUIWidget
  33. {
  34. Dropdown,
  35. Toggle,
  36. Float,
  37. FloatRange,
  38. Int,
  39. IntRange
  40. }
  41. public enum AseOptionsType
  42. {
  43. Option,
  44. Port,
  45. Field
  46. }
  47. public enum AseOptionItemSetup
  48. {
  49. None,
  50. InvertActionOnDeselection
  51. }
  52. public enum AseOptionsActionType
  53. {
  54. ShowOption,
  55. HideOption,
  56. SetOption,
  57. HidePort,
  58. ShowPort,
  59. SetPortName,
  60. SetDefine,
  61. RemoveDefine,
  62. SetUndefine,
  63. RemoveUndefine,
  64. ExcludePass,
  65. IncludePass,
  66. SetPropertyOnPass,
  67. SetPropertyOnSubShader,
  68. SetShaderProperty,
  69. SetMaterialProperty
  70. }
  71. public enum PropertyActionsEnum
  72. {
  73. CullMode,
  74. ColorMask,
  75. ColorMask1,
  76. ColorMask2,
  77. ColorMask3,
  78. ZWrite,
  79. ZTest,
  80. ZOffsetFactor,
  81. ZOffsetUnits,
  82. BlendRGB,
  83. BlendAlpha,
  84. BlendOpRGB,
  85. BlendOpAlpha,
  86. BlendRGB1,
  87. BlendAlpha1,
  88. BlendOpRGB1,
  89. BlendOpAlpha1,
  90. BlendRGB2,
  91. BlendAlpha2,
  92. BlendOpRGB2,
  93. BlendOpAlpha2,
  94. BlendRGB3,
  95. BlendAlpha3,
  96. BlendOpRGB3,
  97. BlendOpAlpha3,
  98. StencilReference,
  99. StencilReadMask,
  100. StencilWriteMask,
  101. StencilComparison,
  102. StencilPass,
  103. StencilFail,
  104. StencilZFail,
  105. RenderType,
  106. RenderQueue,
  107. DisableBatching
  108. }
  109. public enum AseOptionsSetup
  110. {
  111. CopyOptionsFromMainPass,
  112. Id,
  113. Name
  114. }
  115. [Serializable]
  116. public class ItemColorMask
  117. {
  118. public bool ValueR = true;
  119. public bool ValueG = true;
  120. public bool ValueB = true;
  121. public bool ValueA = true;
  122. public bool MaskR = false;
  123. public bool MaskG = false;
  124. public bool MaskB = false;
  125. public bool MaskA = false;
  126. public bool[] GetColorMask( bool[] input )
  127. {
  128. bool[] result = { ValueR, ValueG, ValueB, ValueA };
  129. result[ 0 ] = MaskR ? ValueR : input[ 0 ];
  130. result[ 1 ] = MaskG ? ValueG : input[ 1 ];
  131. result[ 2 ] = MaskB ? ValueB : input[ 2 ];
  132. result[ 3 ] = MaskA ? ValueA : input[ 3 ];
  133. return result;
  134. }
  135. public void SetColorMask( int index, string value )
  136. {
  137. switch( index )
  138. {
  139. default:
  140. case 0:
  141. {
  142. MaskR = !string.IsNullOrEmpty( value );
  143. if(MaskR)
  144. ValueR = Convert.ToBoolean( value );
  145. }
  146. break;
  147. case 1:
  148. {
  149. MaskG = !string.IsNullOrEmpty( value );
  150. if( MaskG )
  151. ValueG = Convert.ToBoolean( value );
  152. }
  153. break;
  154. case 2:
  155. {
  156. MaskB = !string.IsNullOrEmpty( value );
  157. if( MaskB )
  158. ValueB = Convert.ToBoolean( value );
  159. }
  160. break;
  161. case 3:
  162. {
  163. MaskA = !string.IsNullOrEmpty( value );
  164. if( MaskA )
  165. ValueA = Convert.ToBoolean( value );
  166. }
  167. break;
  168. }
  169. }
  170. }
  171. [Serializable]
  172. public class TemplateActionItem
  173. {
  174. public AseOptionsActionType ActionType;
  175. public string ActionData = string.Empty;
  176. public int ActionDataIdx = -1;
  177. public string PassName;
  178. public bool AllPasses = false;
  179. public PropertyActionsEnum PropertyAction;
  180. //CULL
  181. public CullMode ActionCullMode;
  182. //COLOR MASK
  183. public ItemColorMask ColorMask = new ItemColorMask();
  184. public ItemColorMask ColorMask1 = new ItemColorMask();
  185. public ItemColorMask ColorMask2 = new ItemColorMask();
  186. public ItemColorMask ColorMask3 = new ItemColorMask();
  187. //DEPTH
  188. public ZWriteMode ActionZWrite;
  189. public ZTestMode ActionZTest;
  190. public float ActionZOffsetFactor;
  191. public float ActionZOffsetUnits;
  192. //BLEND OPS
  193. public AvailableBlendFactor ActionBlendRGBSource;
  194. public AvailableBlendFactor ActionBlendRGBDest;
  195. public AvailableBlendFactor ActionBlendAlphaSource;
  196. public AvailableBlendFactor ActionBlendAlphaDest;
  197. public AvailableBlendOps ActionBlendOpRGB;
  198. public AvailableBlendOps ActionBlendOpAlpha;
  199. public AvailableBlendFactor ActionBlendRGBSource1;
  200. public AvailableBlendFactor ActionBlendRGBDest1;
  201. public AvailableBlendFactor ActionBlendAlphaSource1;
  202. public AvailableBlendFactor ActionBlendAlphaDest1;
  203. public AvailableBlendOps ActionBlendOpRGB1;
  204. public AvailableBlendOps ActionBlendOpAlpha1;
  205. public AvailableBlendFactor ActionBlendRGBSource2;
  206. public AvailableBlendFactor ActionBlendRGBDest2;
  207. public AvailableBlendFactor ActionBlendAlphaSource2;
  208. public AvailableBlendFactor ActionBlendAlphaDest2;
  209. public AvailableBlendOps ActionBlendOpRGB2;
  210. public AvailableBlendOps ActionBlendOpAlpha2;
  211. public AvailableBlendFactor ActionBlendRGBSource3;
  212. public AvailableBlendFactor ActionBlendRGBDest3;
  213. public AvailableBlendFactor ActionBlendAlphaSource3;
  214. public AvailableBlendFactor ActionBlendAlphaDest3;
  215. public AvailableBlendOps ActionBlendOpRGB3;
  216. public AvailableBlendOps ActionBlendOpAlpha3;
  217. //STENCIL
  218. public int ActionStencilReference;
  219. public int ActionStencilReadMask;
  220. public int ActionStencilWriteMask;
  221. public int ActionStencilComparison;
  222. public int ActionStencilPass;
  223. public int ActionStencilFail;
  224. public int ActionStencilZFail;
  225. public bool CopyFromSubShader = false;
  226. public string ActionBuffer;
  227. public override string ToString()
  228. {
  229. return ActionType + " " + ActionData + " " + ActionDataIdx;
  230. }
  231. }
  232. [Serializable]
  233. public class TemplateActionItemGrid
  234. {
  235. [Serializable]
  236. public class TemplateActionItemRow
  237. {
  238. public TemplateActionItem[] Columns;
  239. }
  240. public TemplateActionItemRow[] Rows;
  241. public TemplateActionItemGrid( int rowsCount )
  242. {
  243. Rows = new TemplateActionItemRow[ rowsCount ];
  244. }
  245. public TemplateActionItem this[ int row, int column ]
  246. {
  247. get { return Rows[ row ].Columns[ column ]; }
  248. set { Rows[ row ].Columns[ column ] = value; }
  249. }
  250. public TemplateActionItem[] this[ int row ]
  251. {
  252. get { return Rows[ row ].Columns; }
  253. set
  254. {
  255. if( Rows[ row ] == null )
  256. Rows[ row ] = new TemplateActionItemRow();
  257. Rows[ row ].Columns = value;
  258. }
  259. }
  260. }
  261. [Serializable]
  262. public class TemplateOptionsItem
  263. {
  264. public AseOptionsType Type;
  265. public AseOptionsUIWidget UIWidget;
  266. public AseOptionItemSetup Setup = AseOptionItemSetup.None;
  267. public string Id = string.Empty;
  268. public string Name = string.Empty;
  269. public string DefaultOption = string.Empty;
  270. public string[] Options = null;
  271. public string[] DisplayOptions = null;
  272. public int DisableIdx = -1;
  273. [SerializeField]
  274. private float m_defaultFieldValue;
  275. public float FieldMin;
  276. public float FieldMax;
  277. public bool FieldInline;
  278. public string FieldInlineName;
  279. public string FieldInlineOutput = string.Empty;
  280. [SerializeField]
  281. public InlineProperty FieldValue = new InlineProperty();
  282. public TemplateActionItemGrid ActionsPerOption = null;
  283. public int Count = 0;
  284. [SerializeField]
  285. private int m_defaultOptionIndex = -1;
  286. ~TemplateOptionsItem()
  287. {
  288. Options = null;
  289. }
  290. public int OptionIndexFor( string option )
  291. {
  292. for( int i = 0; i < Options.Length; i++ )
  293. {
  294. if( Options[ i ].Equals( option ) )
  295. {
  296. return i;
  297. }
  298. }
  299. Debug.LogWarning( "Couldn't find index for option: " + option );
  300. return 0;
  301. }
  302. public int DefaultOptionIndex
  303. {
  304. get
  305. {
  306. if( m_defaultOptionIndex > -1 )
  307. return m_defaultOptionIndex;
  308. for( int i = 0; i < Options.Length; i++ )
  309. {
  310. if( Options[ i ].Equals( DefaultOption ) )
  311. {
  312. m_defaultOptionIndex = i;
  313. return i;
  314. }
  315. }
  316. Debug.LogWarning( "Couldn't find index for default option: " + DefaultOption );
  317. return 0;
  318. }
  319. }
  320. public float DefaultFieldValue
  321. {
  322. get
  323. {
  324. return m_defaultFieldValue;
  325. }
  326. set
  327. {
  328. m_defaultFieldValue = value;
  329. }
  330. }
  331. }
  332. [Serializable]
  333. public class TemplateOptionsContainer
  334. {
  335. public bool Enabled = false;
  336. public string Body = string.Empty;
  337. public int Index = -1;
  338. public int Id = -1;
  339. public string Name = string.Empty;
  340. public bool CopyOptionsFromMainPass = false;
  341. public TemplateOptionsItem[] Options = null;
  342. ~TemplateOptionsContainer()
  343. {
  344. Options = null;
  345. }
  346. public void CopyPortOptionsFrom( TemplateOptionsContainer container, string passName )
  347. {
  348. if( container == null || container.Options == null )
  349. return;
  350. List<TemplateOptionsItem> newItems = new List<TemplateOptionsItem>();
  351. for( int i = 0; i < container.Options.Length; i++ )
  352. {
  353. if( container.Options[ i ].Type == AseOptionsType.Port &&
  354. container.Options[ i ].Id.Equals( passName ) )
  355. {
  356. newItems.Add( container.Options[ i ] );
  357. }
  358. }
  359. if( newItems.Count > 0 )
  360. {
  361. Enabled = true;
  362. if( Options == null )
  363. {
  364. Options = newItems.ToArray();
  365. }
  366. else
  367. {
  368. Array.Resize<TemplateOptionsItem>( ref Options, Options.Length + newItems.Count );
  369. Array.Copy( newItems.ToArray(), Options, newItems.Count );
  370. }
  371. }
  372. newItems.Clear();
  373. newItems = null;
  374. }
  375. public int EndIndex { get { return Index + Body.Length; } }
  376. }
  377. public class TemplateOptionsToolsHelper
  378. {
  379. //public const string PassOptionsMainPattern = @"\/\*ase_pass_options:([\w:= ]*)[\n]([\w: \t;\n&|,_\+-]*)\*\/";
  380. //public const string SubShaderOptionsMainPattern = @"\/\*ase_subshader_options:([\w:= ]*)[\n]([\w: \t;\n&|,_\+-]*)\*\/";
  381. public const string PassOptionsMainPattern = "\\/\\*ase_pass_options:([\\w:= ]*)[\n]([\\w: \t;\n&|,_\\+\\-\\(\\)\\[\\]\\\"\\=\\/\\.]*)\\*\\/";
  382. public const string SubShaderOptionsMainPattern = "\\/\\*ase_subshader_options:([\\w:= ]*)[\n]([\\w: \t;\n&|,_\\+\\-\\(\\)\\[\\]\\\"\\=\\/\\.]*)\\*\\/";
  383. public static readonly char OptionsDataSeparator = ',';
  384. public static Dictionary<string, AseOptionsSetup> AseOptionsSetupDict = new Dictionary<string, AseOptionsSetup>()
  385. {
  386. { "CopyOptionsFromMainPass",AseOptionsSetup.CopyOptionsFromMainPass},
  387. { "Id",AseOptionsSetup.Id},
  388. { "Name",AseOptionsSetup.Name},
  389. };
  390. public static Dictionary<string, AseOptionsUIWidget> AseOptionsUITypeDict = new Dictionary<string, AseOptionsUIWidget>()
  391. {
  392. { "Dropdown",AseOptionsUIWidget.Dropdown },
  393. { "Toggle", AseOptionsUIWidget.Toggle }
  394. };
  395. public static Dictionary<string, AseOptionsActionType> AseOptionsActionTypeDict = new Dictionary<string, AseOptionsActionType>()
  396. {
  397. {"ShowOption", AseOptionsActionType.ShowOption },
  398. {"HideOption", AseOptionsActionType.HideOption },
  399. {"SetOption", AseOptionsActionType.SetOption },
  400. {"HidePort", AseOptionsActionType.HidePort },
  401. {"ShowPort", AseOptionsActionType.ShowPort },
  402. {"SetPortName", AseOptionsActionType.SetPortName },
  403. {"SetDefine", AseOptionsActionType.SetDefine },
  404. {"RemoveDefine", AseOptionsActionType.RemoveDefine },
  405. {"SetUndefine", AseOptionsActionType.SetUndefine },
  406. {"RemoveUndefine", AseOptionsActionType.RemoveUndefine },
  407. {"ExcludePass", AseOptionsActionType.ExcludePass },
  408. {"IncludePass", AseOptionsActionType.IncludePass },
  409. {"SetPropertyOnPass", AseOptionsActionType.SetPropertyOnPass },
  410. {"SetPropertyOnSubShader", AseOptionsActionType.SetPropertyOnSubShader },
  411. {"SetShaderProperty", AseOptionsActionType.SetShaderProperty },
  412. {"SetMaterialProperty", AseOptionsActionType.SetMaterialProperty }
  413. };
  414. public static Dictionary<string, AseOptionItemSetup> AseOptionItemSetupDict = new Dictionary<string, AseOptionItemSetup>
  415. {
  416. {"None", AseOptionItemSetup.None },
  417. { "InvertActionOnDeselection", AseOptionItemSetup.InvertActionOnDeselection}
  418. };
  419. public static bool InvertAction( AseOptionsActionType original, ref AseOptionsActionType inverted )
  420. {
  421. bool success = true;
  422. switch( original )
  423. {
  424. case AseOptionsActionType.ShowOption:
  425. inverted = AseOptionsActionType.HideOption;
  426. break;
  427. case AseOptionsActionType.HideOption:
  428. inverted = AseOptionsActionType.ShowOption;
  429. break;
  430. case AseOptionsActionType.HidePort:
  431. inverted = AseOptionsActionType.ShowPort;
  432. break;
  433. case AseOptionsActionType.ShowPort:
  434. inverted = AseOptionsActionType.HidePort;
  435. break;
  436. case AseOptionsActionType.SetDefine:
  437. inverted = AseOptionsActionType.RemoveDefine;
  438. break;
  439. case AseOptionsActionType.RemoveDefine:
  440. inverted = AseOptionsActionType.SetDefine;
  441. break;
  442. case AseOptionsActionType.SetUndefine:
  443. inverted = AseOptionsActionType.RemoveUndefine;
  444. break;
  445. case AseOptionsActionType.RemoveUndefine:
  446. inverted = AseOptionsActionType.SetUndefine;
  447. break;
  448. case AseOptionsActionType.ExcludePass:
  449. inverted = AseOptionsActionType.IncludePass;
  450. break;
  451. case AseOptionsActionType.IncludePass:
  452. inverted = AseOptionsActionType.ExcludePass;
  453. break;
  454. case AseOptionsActionType.SetPortName:
  455. case AseOptionsActionType.SetOption:
  456. case AseOptionsActionType.SetPropertyOnPass:
  457. case AseOptionsActionType.SetPropertyOnSubShader:
  458. success = false;
  459. break;
  460. }
  461. return success;
  462. }
  463. public static TemplateOptionsContainer GenerateOptionsContainer( bool isSubShader, string data )
  464. {
  465. TemplateOptionsContainer optionsContainer = new TemplateOptionsContainer();
  466. Match match = Regex.Match( data, isSubShader ? SubShaderOptionsMainPattern : PassOptionsMainPattern );
  467. optionsContainer.Enabled = match.Success;
  468. if( match.Success )
  469. {
  470. try
  471. {
  472. optionsContainer.Body = match.Value;
  473. optionsContainer.Index = match.Index;
  474. List<TemplateOptionsItem> optionItemsList = new List<TemplateOptionsItem>();
  475. List<List<TemplateActionItem>> actionItemsList = new List<List<TemplateActionItem>>();
  476. Dictionary<string, int> optionItemToIndex = new Dictionary<string, int>();
  477. TemplateOptionsItem currentOption = null;
  478. //OPTIONS OVERALL SETUP
  479. string[] setupLines = match.Groups[ 1 ].Value.Split( ':' );
  480. for( int i = 0; i < setupLines.Length; i++ )
  481. {
  482. if( AseOptionsSetupDict.ContainsKey( setupLines[ i ] ) )
  483. {
  484. AseOptionsSetup setup = AseOptionsSetupDict[ setupLines[ i ] ];
  485. switch( setup )
  486. {
  487. case AseOptionsSetup.CopyOptionsFromMainPass: optionsContainer.CopyOptionsFromMainPass = true; break;
  488. }
  489. }
  490. else
  491. {
  492. string[] args = setupLines[ i ].Split( '=' );
  493. if( args.Length > 1 && AseOptionsSetupDict.ContainsKey( args[ 0 ] ) )
  494. {
  495. AseOptionsSetup setup = AseOptionsSetupDict[ args[ 0 ] ];
  496. switch( setup )
  497. {
  498. case AseOptionsSetup.Id: if( !int.TryParse( args[ 1 ], out optionsContainer.Id ) ) optionsContainer.Id = -1; break;
  499. case AseOptionsSetup.Name: optionsContainer.Name = args[ 1 ]; break;
  500. }
  501. }
  502. }
  503. }
  504. //AVAILABLE OPTIONS
  505. string body = match.Groups[ 2 ].Value.Replace( "\t", string.Empty );
  506. string[] optionLines = body.Split( '\n' );
  507. for( int oL = 0; oL < optionLines.Length; oL++ )
  508. {
  509. string[] optionItems = optionLines[ oL ].Split( ':' );
  510. if( optionItems.Length > 0 )
  511. {
  512. string[] itemIds = optionItems[ 0 ].Split( OptionsDataSeparator );
  513. switch( itemIds[ 0 ] )
  514. {
  515. case "Option":
  516. {
  517. //Fills previous option with its actions
  518. //actionItemsList is cleared over here
  519. FillOptionAction( currentOption, ref actionItemsList );
  520. optionItemToIndex.Clear();
  521. currentOption = new TemplateOptionsItem();
  522. currentOption.Type = AseOptionsType.Option;
  523. string[] optionItemSetup = optionItems[ 1 ].Split( OptionsDataSeparator );
  524. currentOption.Name = optionItemSetup[ 0 ];
  525. if( optionItemSetup.Length > 1 )
  526. {
  527. if( AseOptionItemSetupDict.ContainsKey( optionItemSetup[ 1 ] ) )
  528. currentOption.Setup = AseOptionItemSetupDict[ optionItemSetup[ 1 ] ];
  529. }
  530. currentOption.Id = itemIds.Length > 1 ? itemIds[ 1 ] : optionItems[ 1 ];
  531. currentOption.DisplayOptions = optionItems[ 2 ].Split( OptionsDataSeparator );
  532. currentOption.DisableIdx = currentOption.DisplayOptions.Length;
  533. optionItems[ 2 ] += ",disable";
  534. currentOption.Options = optionItems[ 2 ].Split( OptionsDataSeparator );
  535. currentOption.Count = currentOption.Options.Length;
  536. for( int opIdx = 0; opIdx < currentOption.Options.Length; opIdx++ )
  537. {
  538. optionItemToIndex.Add( currentOption.Options[ opIdx ], opIdx );
  539. actionItemsList.Add( new List<TemplateActionItem>() );
  540. }
  541. if( optionItems.Length > 3 )
  542. {
  543. currentOption.DefaultOption = optionItems[ 3 ];
  544. }
  545. else
  546. {
  547. currentOption.DefaultOption = currentOption.Options[ 0 ];
  548. }
  549. if( currentOption.Options.Length == 2 || ( currentOption.Options.Length == 3 && currentOption.Options[ 2 ].Equals( "disable" ) ) )
  550. {
  551. if( ( currentOption.Options[ 0 ].Equals( "true" ) && currentOption.Options[ 1 ].Equals( "false" ) ) ||
  552. ( currentOption.Options[ 0 ].Equals( "false" ) && currentOption.Options[ 1 ].Equals( "true" ) ) )
  553. {
  554. // Toggle 0 is false and 1 is true
  555. currentOption.Options[ 0 ] = "false";
  556. currentOption.Options[ 1 ] = "true";
  557. currentOption.UIWidget = AseOptionsUIWidget.Toggle;
  558. }
  559. }
  560. else if( currentOption.Options.Length > 2 )
  561. {
  562. currentOption.UIWidget = AseOptionsUIWidget.Dropdown;
  563. }
  564. else
  565. {
  566. Debug.LogWarning( "Detected an option with less than two items:" + optionItems[ 1 ] );
  567. }
  568. optionItemsList.Add( currentOption );
  569. }
  570. break;
  571. case "Port":
  572. {
  573. //Fills previous option with its actions
  574. //actionItemsList is cleared over here
  575. FillOptionAction( currentOption, ref actionItemsList );
  576. optionItemToIndex.Clear();
  577. currentOption = new TemplateOptionsItem();
  578. currentOption.Type = AseOptionsType.Port;
  579. if( isSubShader && optionItems.Length > 2 )
  580. {
  581. currentOption.Id = optionItems[ 1 ];
  582. currentOption.Name = optionItems[ 2 ];
  583. }
  584. else
  585. {
  586. currentOption.Name = optionItems[ 1 ];
  587. }
  588. currentOption.Options = new string[] { "On", "Off" };
  589. optionItemToIndex.Add( currentOption.Options[ 0 ], 0 );
  590. optionItemToIndex.Add( currentOption.Options[ 1 ], 1 );
  591. actionItemsList.Add( new List<TemplateActionItem>() );
  592. actionItemsList.Add( new List<TemplateActionItem>() );
  593. optionItemsList.Add( currentOption );
  594. }
  595. break;
  596. case "Field":
  597. {
  598. //Fills previous option with its actions
  599. //actionItemsList is cleared over here
  600. FillOptionAction( currentOption, ref actionItemsList );
  601. optionItemToIndex.Clear();
  602. currentOption = new TemplateOptionsItem();
  603. currentOption.Type = AseOptionsType.Field;
  604. currentOption.Id = optionItems[ 1 ];
  605. currentOption.Name = optionItems[ 1 ];
  606. currentOption.UIWidget = AseOptionsUIWidget.Float;
  607. if( optionItems[ 2 ].Equals( "Int" ) )
  608. currentOption.UIWidget = AseOptionsUIWidget.Int;
  609. if( optionItems.Length >= 3 )
  610. {
  611. currentOption.DefaultFieldValue = Convert.ToSingle( optionItems[ 3 ], System.Globalization.CultureInfo.InvariantCulture );
  612. }
  613. if( optionItems.Length >= 6 )
  614. {
  615. if( currentOption.UIWidget == AseOptionsUIWidget.Int )
  616. currentOption.UIWidget = AseOptionsUIWidget.Int;
  617. else
  618. currentOption.UIWidget = AseOptionsUIWidget.FloatRange;
  619. currentOption.FieldMin = Convert.ToSingle( optionItems[ 4 ], System.Globalization.CultureInfo.InvariantCulture );
  620. currentOption.FieldMax = Convert.ToSingle( optionItems[ 5 ], System.Globalization.CultureInfo.InvariantCulture );
  621. }
  622. if( optionItems.Length == 5 || optionItems.Length == 7 )
  623. {
  624. currentOption.FieldInline = true;
  625. currentOption.FieldInlineName = optionItems[ optionItems.Length - 1 ];
  626. }
  627. currentOption.Options = new string[] { "Change", "Inline", "disable" };
  628. optionItemToIndex.Add( currentOption.Options[ 0 ], 0 );
  629. optionItemToIndex.Add( currentOption.Options[ 1 ], 1 );
  630. optionItemToIndex.Add( currentOption.Options[ 2 ], 2 );
  631. currentOption.DisableIdx = 2;
  632. actionItemsList.Add( new List<TemplateActionItem>() );
  633. actionItemsList.Add( new List<TemplateActionItem>() );
  634. actionItemsList.Add( new List<TemplateActionItem>() );
  635. optionItemsList.Add( currentOption );
  636. }
  637. break;
  638. default:
  639. {
  640. if( optionItemToIndex.ContainsKey( optionItems[ 0 ] ) )
  641. {
  642. int idx = 0;
  643. if( currentOption != null && currentOption.UIWidget == AseOptionsUIWidget.Toggle )
  644. {
  645. idx = ( optionItems[ 0 ].Equals( "true" ) ) ? 1 : 0;
  646. if( optionItems[ 0 ].Equals( "disable" ) )
  647. idx = 2;
  648. }
  649. else
  650. {
  651. idx = optionItemToIndex[ optionItems[ 0 ] ];
  652. }
  653. actionItemsList[ idx ].Add( CreateActionItem( isSubShader, optionItems ) );
  654. }
  655. else
  656. {
  657. //string[] ids = optionItems[ 0 ].Split( ',' );
  658. if( itemIds.Length > 1 )
  659. {
  660. for( int i = 0; i < itemIds.Length; i++ )
  661. {
  662. if( optionItemToIndex.ContainsKey( itemIds[ i ] ) )
  663. {
  664. int idx = optionItemToIndex[ itemIds[ i ] ];
  665. actionItemsList[ idx ].Add( CreateActionItem( isSubShader, optionItems ) );
  666. }
  667. }
  668. }
  669. }
  670. }
  671. break;
  672. }
  673. }
  674. }
  675. //Fills last option with its actions
  676. FillOptionAction( currentOption, ref actionItemsList );
  677. actionItemsList.Clear();
  678. actionItemsList = null;
  679. optionsContainer.Options = optionItemsList.ToArray();
  680. optionItemsList.Clear();
  681. optionItemsList = null;
  682. optionItemToIndex.Clear();
  683. optionItemToIndex = null;
  684. }
  685. catch( Exception e )
  686. {
  687. Debug.LogException( e );
  688. }
  689. }
  690. return optionsContainer;
  691. }
  692. static void FillOptionAction( TemplateOptionsItem currentOption, ref List<List<TemplateActionItem>> actionItemsList )
  693. {
  694. if( currentOption != null )
  695. {
  696. int count = actionItemsList.Count;
  697. currentOption.ActionsPerOption = new TemplateActionItemGrid( count );
  698. for( int i = 0; i < count; i++ )
  699. {
  700. currentOption.ActionsPerOption[ i ] = actionItemsList[ i ].ToArray();
  701. actionItemsList[ i ].Clear();
  702. }
  703. actionItemsList.Clear();
  704. }
  705. }
  706. static TemplateActionItem CreateActionItem( bool isSubshader, string[] optionItems )
  707. {
  708. TemplateActionItem actionItem = new TemplateActionItem();
  709. try
  710. {
  711. actionItem.ActionType = AseOptionsActionTypeDict[ optionItems[ 1 ] ];
  712. int optionsIdx = 2;
  713. if( optionItems.Length > 3 )
  714. {
  715. optionsIdx = 3;
  716. actionItem.PassName = optionItems[ 2 ];
  717. }
  718. else
  719. {
  720. actionItem.AllPasses = isSubshader;
  721. }
  722. actionItem.ActionData = optionItems[ optionsIdx ];
  723. switch( actionItem.ActionType )
  724. {
  725. case AseOptionsActionType.ShowOption:
  726. case AseOptionsActionType.HideOption:
  727. {
  728. string[] arr = optionItems[ optionsIdx ].Split( OptionsDataSeparator );
  729. if( arr.Length > 1 )
  730. {
  731. actionItem.ActionData = arr[ 0 ];
  732. if( !int.TryParse( arr[ 1 ], out actionItem.ActionDataIdx ) )
  733. {
  734. actionItem.ActionDataIdx = -1;
  735. }
  736. }
  737. }
  738. break;
  739. case AseOptionsActionType.SetOption:
  740. {
  741. string[] arr = optionItems[ optionsIdx ].Split( OptionsDataSeparator );
  742. if( arr.Length > 1 )
  743. {
  744. actionItem.ActionData = arr[ 0 ];
  745. if( !int.TryParse( arr[ 1 ], out actionItem.ActionDataIdx ) )
  746. {
  747. Debug.LogWarning( "SetOption value must be a the selection index" );
  748. }
  749. }
  750. }
  751. break;
  752. case AseOptionsActionType.HidePort:
  753. case AseOptionsActionType.ShowPort:
  754. {
  755. if( !int.TryParse( actionItem.ActionData, out actionItem.ActionDataIdx ) )
  756. actionItem.ActionDataIdx = -1;
  757. }
  758. break;
  759. case AseOptionsActionType.SetPortName:
  760. {
  761. string[] arr = optionItems[ optionsIdx ].Split( OptionsDataSeparator );
  762. if( arr.Length > 1 )
  763. {
  764. int.TryParse( arr[ 0 ], out actionItem.ActionDataIdx );
  765. actionItem.ActionData = arr[ 1 ];
  766. }
  767. }
  768. break;
  769. case AseOptionsActionType.SetDefine:
  770. case AseOptionsActionType.RemoveDefine:
  771. case AseOptionsActionType.SetUndefine:
  772. case AseOptionsActionType.RemoveUndefine:
  773. case AseOptionsActionType.ExcludePass:
  774. case AseOptionsActionType.IncludePass:
  775. break;
  776. case AseOptionsActionType.SetShaderProperty:
  777. {
  778. int optIndex = optionItems[ optionsIdx ].IndexOf( OptionsDataSeparator );
  779. if( optIndex > -1 )
  780. {
  781. actionItem.ActionData = optionItems[ optionsIdx ].Substring( 0, optIndex );
  782. actionItem.ActionBuffer = optionItems[ optionsIdx ].Substring( optIndex + 1, optionItems[ optionsIdx ].Length - optIndex - 1);
  783. }
  784. }break;
  785. case AseOptionsActionType.SetMaterialProperty:
  786. {
  787. int optIndex = optionItems[ optionsIdx ].IndexOf( OptionsDataSeparator );
  788. if( optIndex > -1 )
  789. {
  790. actionItem.ActionData = optionItems[ optionsIdx ].Substring( 0, optIndex );
  791. }
  792. }
  793. break;
  794. case AseOptionsActionType.SetPropertyOnPass:
  795. case AseOptionsActionType.SetPropertyOnSubShader:
  796. {
  797. string[] arr = optionItems[ optionsIdx ].Split( OptionsDataSeparator );
  798. actionItem.PropertyAction = (PropertyActionsEnum)Enum.Parse( typeof( PropertyActionsEnum ), arr[ 0 ] );
  799. if( arr.Length == 1 && actionItem.ActionType == AseOptionsActionType.SetPropertyOnPass )
  800. {
  801. actionItem.CopyFromSubShader = true;
  802. }
  803. else
  804. {
  805. switch( actionItem.PropertyAction )
  806. {
  807. case PropertyActionsEnum.CullMode:
  808. {
  809. if( arr.Length > 1 )
  810. actionItem.ActionCullMode = (CullMode)Enum.Parse( typeof( CullMode ), arr[ 1 ] );
  811. }
  812. break;
  813. case PropertyActionsEnum.ColorMask:
  814. {
  815. if( arr.Length > 4 )
  816. {
  817. actionItem.ColorMask.SetColorMask( 0, arr[ 1 ] );
  818. actionItem.ColorMask.SetColorMask( 1, arr[ 2 ] );
  819. actionItem.ColorMask.SetColorMask( 2, arr[ 3 ] );
  820. actionItem.ColorMask.SetColorMask( 3, arr[ 4 ] );
  821. }
  822. }
  823. break;
  824. case PropertyActionsEnum.ColorMask1:
  825. {
  826. if( arr.Length > 4 )
  827. {
  828. actionItem.ColorMask1.SetColorMask( 0, arr[ 1 ] );
  829. actionItem.ColorMask1.SetColorMask( 1, arr[ 2 ] );
  830. actionItem.ColorMask1.SetColorMask( 2, arr[ 3 ] );
  831. actionItem.ColorMask1.SetColorMask( 3, arr[ 4 ] );
  832. }
  833. }
  834. break;
  835. case PropertyActionsEnum.ColorMask2:
  836. {
  837. if( arr.Length > 4 )
  838. {
  839. actionItem.ColorMask2.SetColorMask( 0, arr[ 1 ] );
  840. actionItem.ColorMask2.SetColorMask( 1, arr[ 2 ] );
  841. actionItem.ColorMask2.SetColorMask( 2, arr[ 3 ] );
  842. actionItem.ColorMask2.SetColorMask( 3, arr[ 4 ] );
  843. }
  844. }
  845. break;
  846. case PropertyActionsEnum.ColorMask3:
  847. {
  848. if( arr.Length > 4 )
  849. {
  850. actionItem.ColorMask3.SetColorMask( 0, arr[ 1 ] );
  851. actionItem.ColorMask3.SetColorMask( 1, arr[ 2 ] );
  852. actionItem.ColorMask3.SetColorMask( 2, arr[ 3 ] );
  853. actionItem.ColorMask3.SetColorMask( 3, arr[ 4 ] );
  854. }
  855. }
  856. break;
  857. case PropertyActionsEnum.ZWrite:
  858. {
  859. if( arr.Length > 1 )
  860. actionItem.ActionZWrite = (ZWriteMode)Enum.Parse( typeof( ZWriteMode ), arr[ 1 ] );
  861. }
  862. break;
  863. case PropertyActionsEnum.ZTest:
  864. {
  865. if( arr.Length > 1 )
  866. actionItem.ActionZTest = (ZTestMode)Enum.Parse( typeof( ZTestMode ), arr[ 1 ] );
  867. }
  868. break;
  869. case PropertyActionsEnum.ZOffsetFactor:
  870. {
  871. if( arr.Length > 1 )
  872. actionItem.ActionZOffsetFactor = Convert.ToSingle( arr[ 1 ] );
  873. }
  874. break;
  875. case PropertyActionsEnum.ZOffsetUnits:
  876. {
  877. if( arr.Length > 1 )
  878. actionItem.ActionZOffsetUnits = Convert.ToSingle( arr[ 1 ] );
  879. }
  880. break;
  881. case PropertyActionsEnum.BlendRGB:
  882. {
  883. if( arr.Length > 2 )
  884. {
  885. actionItem.ActionBlendRGBSource = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  886. actionItem.ActionBlendRGBDest = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  887. }
  888. }
  889. break;
  890. case PropertyActionsEnum.BlendRGB1:
  891. {
  892. if( arr.Length > 2 )
  893. {
  894. actionItem.ActionBlendRGBSource1 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  895. actionItem.ActionBlendRGBDest1 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  896. }
  897. }
  898. break;
  899. case PropertyActionsEnum.BlendRGB2:
  900. {
  901. if( arr.Length > 2 )
  902. {
  903. actionItem.ActionBlendRGBSource2 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  904. actionItem.ActionBlendRGBDest2 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  905. }
  906. }
  907. break;
  908. case PropertyActionsEnum.BlendRGB3:
  909. {
  910. if( arr.Length > 2 )
  911. {
  912. actionItem.ActionBlendRGBSource3 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  913. actionItem.ActionBlendRGBDest3 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  914. }
  915. }
  916. break;
  917. case PropertyActionsEnum.BlendAlpha:
  918. {
  919. if( arr.Length > 2 )
  920. {
  921. actionItem.ActionBlendAlphaSource = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  922. actionItem.ActionBlendAlphaDest = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  923. }
  924. }
  925. break;
  926. case PropertyActionsEnum.BlendAlpha1:
  927. {
  928. if( arr.Length > 2 )
  929. {
  930. actionItem.ActionBlendAlphaSource1 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  931. actionItem.ActionBlendAlphaDest1 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  932. }
  933. }
  934. break;
  935. case PropertyActionsEnum.BlendAlpha2:
  936. {
  937. if( arr.Length > 2 )
  938. {
  939. actionItem.ActionBlendAlphaSource2 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  940. actionItem.ActionBlendAlphaDest2 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  941. }
  942. }
  943. break;
  944. case PropertyActionsEnum.BlendAlpha3:
  945. {
  946. if( arr.Length > 2 )
  947. {
  948. actionItem.ActionBlendAlphaSource3 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 1 ] );
  949. actionItem.ActionBlendAlphaDest3 = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), arr[ 2 ] );
  950. }
  951. }
  952. break;
  953. case PropertyActionsEnum.BlendOpRGB:
  954. {
  955. if( arr.Length > 1 )
  956. {
  957. actionItem.ActionBlendOpRGB = (AvailableBlendOps)Enum.Parse( typeof( AvailableBlendOps ), arr[ 1 ] );
  958. }
  959. }
  960. break;
  961. case PropertyActionsEnum.BlendOpAlpha:
  962. {
  963. if( arr.Length > 1 )
  964. {
  965. actionItem.ActionBlendOpAlpha = (AvailableBlendOps)Enum.Parse( typeof( AvailableBlendOps ), arr[ 1 ] );
  966. }
  967. }
  968. break;
  969. case PropertyActionsEnum.StencilReference:
  970. {
  971. if( arr.Length > 1 )
  972. {
  973. int.TryParse( arr[ 1 ], out actionItem.ActionStencilReference );
  974. }
  975. }
  976. break;
  977. case PropertyActionsEnum.StencilReadMask:
  978. {
  979. if( arr.Length > 1 )
  980. {
  981. int.TryParse( arr[ 1 ], out actionItem.ActionStencilReadMask );
  982. }
  983. }
  984. break;
  985. case PropertyActionsEnum.StencilWriteMask:
  986. {
  987. if( arr.Length > 1 )
  988. {
  989. int.TryParse( arr[ 1 ], out actionItem.ActionStencilWriteMask );
  990. }
  991. }
  992. break;
  993. case PropertyActionsEnum.StencilComparison:
  994. {
  995. if( arr.Length > 1 )
  996. actionItem.ActionStencilComparison = StencilBufferOpHelper.StencilComparisonValuesDict[ arr[ 1 ] ];
  997. }
  998. break;
  999. case PropertyActionsEnum.StencilPass:
  1000. {
  1001. if( arr.Length > 1 )
  1002. actionItem.ActionStencilPass = StencilBufferOpHelper.StencilOpsValuesDict[ arr[ 1 ] ];
  1003. }
  1004. break;
  1005. case PropertyActionsEnum.StencilFail:
  1006. {
  1007. if( arr.Length > 1 )
  1008. actionItem.ActionStencilFail = StencilBufferOpHelper.StencilOpsValuesDict[ arr[ 1 ] ];
  1009. }
  1010. break;
  1011. case PropertyActionsEnum.StencilZFail:
  1012. {
  1013. if( arr.Length > 1 )
  1014. actionItem.ActionStencilZFail = StencilBufferOpHelper.StencilOpsValuesDict[ arr[ 1 ] ];
  1015. }
  1016. break;
  1017. case PropertyActionsEnum.RenderType:
  1018. {
  1019. if( arr.Length > 1 )
  1020. actionItem.ActionData = arr[ 1 ];
  1021. }
  1022. break;
  1023. case PropertyActionsEnum.RenderQueue:
  1024. {
  1025. if( arr.Length > 1 )
  1026. actionItem.ActionData = arr[ 1 ];
  1027. if( arr.Length > 2 )
  1028. {
  1029. int.TryParse( arr[ 2 ], out actionItem.ActionDataIdx );
  1030. }
  1031. else
  1032. {
  1033. actionItem.ActionDataIdx = 0;
  1034. }
  1035. }
  1036. break;
  1037. case PropertyActionsEnum.DisableBatching:
  1038. {
  1039. if( arr.Length > 1 )
  1040. actionItem.ActionData = arr[ 1 ];
  1041. }
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. break;
  1047. }
  1048. }
  1049. catch( Exception e )
  1050. {
  1051. Debug.LogException( e );
  1052. }
  1053. return actionItem;
  1054. }
  1055. }
  1056. }