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.

1177 lines
39 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. // THIS FILE IS DEPRECATED AND SHOULD NOT BE USED
  4. using System;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System.Collections.Generic;
  8. using System.Text.RegularExpressions;
  9. namespace AmplifyShaderEditor
  10. {
  11. [Serializable]
  12. public class TemplateDataContainer
  13. {
  14. public int UNITY_VERSION = -1;
  15. public TemplateData TemplateDataRef;
  16. }
  17. [Serializable]
  18. public class VertexDataContainer
  19. {
  20. [SerializeField]
  21. private List<TemplateVertexData> m_vertexData;
  22. [SerializeField]
  23. private string m_vertexDataId = string.Empty;
  24. [SerializeField]
  25. private int m_vertexDataStartIdx = -1;
  26. public void Reload()
  27. {
  28. if( m_vertexData != null )
  29. {
  30. m_vertexData.Clear();
  31. }
  32. }
  33. public void Destroy()
  34. {
  35. if( m_vertexData != null )
  36. {
  37. m_vertexData.Clear();
  38. m_vertexData = null;
  39. }
  40. }
  41. public List<TemplateVertexData> VertexData { get { return m_vertexData; } set { m_vertexData = value; } }
  42. public string VertexDataId { get { return m_vertexDataId; } set { m_vertexDataId = value; } }
  43. public int VertexDataStartIdx { get { return m_vertexDataStartIdx; } set { m_vertexDataStartIdx = value; } }
  44. }
  45. [Serializable]
  46. public sealed class TemplateData : TemplateDataParent
  47. {
  48. [SerializeField]
  49. private string m_templateBody = string.Empty;
  50. [SerializeField]
  51. private string m_shaderNameId = string.Empty;
  52. [SerializeField]
  53. private List<TemplateProperty> m_propertyList = new List<TemplateProperty>();
  54. private Dictionary<string, TemplateProperty> m_propertyDict = new Dictionary<string, TemplateProperty>();
  55. [SerializeField]
  56. private List<TemplateInputData> m_inputDataList = new List<TemplateInputData>();
  57. private Dictionary<int, TemplateInputData> m_inputDataDict = new Dictionary<int, TemplateInputData>();
  58. //[SerializeField]
  59. //private List<TemplateCodeSnippetBase> m_snippetElementsList = new List<TemplateCodeSnippetBase>();
  60. //private Dictionary<string, TemplateCodeSnippetBase> m_snippetElementsDict = new Dictionary<string, TemplateCodeSnippetBase>();
  61. [SerializeField]
  62. private List<TemplateLocalVarData> m_localVarsList = new List<TemplateLocalVarData>();
  63. [SerializeField]
  64. private VertexDataContainer m_vertexDataContainer = new VertexDataContainer();
  65. [SerializeField]
  66. private TemplateInterpData m_interpolatorDataContainer;
  67. [SerializeField]
  68. private List<TemplateShaderPropertyData> m_availableShaderProperties = new List<TemplateShaderPropertyData>();
  69. [SerializeField]
  70. private TemplateFunctionData m_vertexFunctionData;
  71. [SerializeField]
  72. private TemplateFunctionData m_fragmentFunctionData;
  73. [SerializeField]
  74. private TemplateBlendData m_blendData = new TemplateBlendData();
  75. [SerializeField]
  76. private TemplateCullModeData m_cullModeData = new TemplateCullModeData();
  77. [SerializeField]
  78. private TemplateColorMaskData m_colorMaskData = new TemplateColorMaskData();
  79. [SerializeField]
  80. private TemplateStencilData m_stencilData = new TemplateStencilData();
  81. [SerializeField]
  82. private TemplateDepthData m_depthData = new TemplateDepthData();
  83. [SerializeField]
  84. private TemplateTagsModuleData m_tagData = new TemplateTagsModuleData();
  85. public TemplateData()
  86. {
  87. m_templateType = TemplateDataType.LegacySinglePass;
  88. }
  89. public TemplateData( string name )
  90. {
  91. m_templateType = TemplateDataType.LegacySinglePass;
  92. Name = name;
  93. }
  94. public TemplateData( string name, string guid )
  95. {
  96. m_templateType = TemplateDataType.LegacySinglePass;
  97. m_communityTemplate = false;
  98. if( !string.IsNullOrEmpty( guid ) )
  99. {
  100. string datapath = AssetDatabase.GUIDToAssetPath( guid );
  101. if( string.IsNullOrEmpty( datapath ) )
  102. {
  103. m_isValid = false;
  104. return;
  105. }
  106. string body = string.Empty;
  107. try
  108. {
  109. body = IOUtils.LoadTextFileFromDisk( datapath );
  110. }
  111. catch( Exception e )
  112. {
  113. Debug.LogException( e );
  114. m_isValid = false;
  115. return;
  116. }
  117. if( !string.IsNullOrEmpty( body ) )
  118. {
  119. LoadTemplateBody( body );
  120. Name = string.IsNullOrEmpty( name ) ? m_defaultShaderName : name;
  121. m_guid = guid;
  122. }
  123. }
  124. }
  125. public TemplateData( string name, string guid, string body )
  126. {
  127. m_templateType = TemplateDataType.LegacySinglePass;
  128. m_communityTemplate = true;
  129. if( !string.IsNullOrEmpty( body ) )
  130. {
  131. LoadTemplateBody( body );
  132. Name = string.IsNullOrEmpty( name ) ? m_defaultShaderName : name;
  133. m_guid = guid;
  134. }
  135. }
  136. public override bool Reload()
  137. {
  138. if( m_vertexDataContainer != null )
  139. {
  140. m_vertexDataContainer.Reload();
  141. }
  142. if( m_interpolatorDataContainer != null )
  143. {
  144. m_interpolatorDataContainer.Destroy();
  145. }
  146. if( m_availableShaderProperties != null )
  147. {
  148. m_availableShaderProperties.Clear();
  149. }
  150. if( m_propertyDict != null )
  151. {
  152. m_propertyDict.Clear();
  153. }
  154. if( m_propertyList != null )
  155. {
  156. m_propertyList.Clear();
  157. }
  158. if( m_inputDataDict != null )
  159. {
  160. m_inputDataDict.Clear();
  161. }
  162. if( m_inputDataList != null )
  163. {
  164. m_inputDataList.Clear();
  165. }
  166. if( m_localVarsList != null )
  167. {
  168. m_localVarsList.Clear();
  169. }
  170. //if( m_snippetElementsDict != null )
  171. //{
  172. // m_snippetElementsDict.Clear();
  173. //}
  174. //if( m_snippetElementsList != null )
  175. //{
  176. // for( int i = 0; i < m_snippetElementsList.Count; i++ )
  177. // {
  178. // GameObject.DestroyImmediate( m_snippetElementsList[ i ] );
  179. // m_snippetElementsList[ i ] = null;
  180. // }
  181. // m_snippetElementsList.Clear();
  182. //}
  183. string datapath = AssetDatabase.GUIDToAssetPath( m_guid );
  184. string body = string.Empty;
  185. try
  186. {
  187. body = IOUtils.LoadTextFileFromDisk( datapath );
  188. body = body.Replace( "\r\n", "\n" );
  189. }
  190. catch( Exception e )
  191. {
  192. Debug.LogException( e );
  193. m_isValid = false;
  194. }
  195. LoadTemplateBody( body );
  196. if( m_communityTemplate )
  197. {
  198. Name = m_defaultShaderName;
  199. }
  200. return true;
  201. }
  202. void LoadTemplateBody( string body )
  203. {
  204. m_templateBody = body.Replace( "\r\n", "\n" ); ;
  205. if( m_templateBody.IndexOf( TemplatesManager.TemplateShaderNameBeginTag ) < 0 )
  206. {
  207. m_isValid = false;
  208. return;
  209. }
  210. //Fetching common tags
  211. FetchCommonTags();
  212. //Fetch function code areas
  213. FetchCodeAreas( TemplatesManager.TemplateVertexCodeBeginArea, MasterNodePortCategory.Vertex );
  214. FetchCodeAreas( TemplatesManager.TemplateFragmentCodeBeginArea, MasterNodePortCategory.Fragment );
  215. //Fetching inputs
  216. FetchInputs( MasterNodePortCategory.Fragment );
  217. FetchInputs( MasterNodePortCategory.Vertex );
  218. //Fetch local variables must be done after fetching code areas as it needs them to see is variable is on vertex or fragment
  219. TemplateHelperFunctions.FetchLocalVars( m_templateBody, ref m_localVarsList, m_vertexFunctionData, m_fragmentFunctionData );
  220. //Fetch snippets
  221. }
  222. void FetchSubShaderProperties()
  223. {
  224. Match match = Regex.Match( m_templateBody, @"Pass\s*{" );
  225. if( match.Groups.Count == 0 )
  226. {
  227. return;
  228. }
  229. int beginSubShader = m_templateBody.IndexOf( "SubShader" );
  230. int endSubShader = match.Groups[ 0 ].Index;
  231. if( beginSubShader > 0 && endSubShader > 0 && endSubShader > beginSubShader )
  232. {
  233. // ADD A PLACE TO INSERT GRAB PASSES
  234. int passIndex = m_templateBody.IndexOf( TemplatesManager.TemplatePassTag );
  235. if( passIndex < 0 )
  236. {
  237. int currIdx = endSubShader - 1;
  238. string identation = string.Empty;
  239. for( ; currIdx > 0; currIdx-- )
  240. {
  241. if( m_templateBody[ currIdx ] != '\n' )
  242. {
  243. identation = m_templateBody[ currIdx ] + identation;
  244. }
  245. else
  246. {
  247. identation = m_templateBody[ currIdx ] + identation;
  248. break;
  249. }
  250. }
  251. if( currIdx > 0 )
  252. {
  253. m_templateBody = m_templateBody.Insert( currIdx, identation + TemplatesManager.TemplatePassTag );
  254. }
  255. }
  256. // GET ALL THE MODULES
  257. string subBody = m_templateBody.Substring( beginSubShader, endSubShader - beginSubShader );
  258. //CULL MODE
  259. {
  260. int cullIdx = subBody.IndexOf( "Cull" );
  261. if( cullIdx > 0 )
  262. {
  263. int end = subBody.IndexOf( TemplatesManager.TemplateNewLine, cullIdx );
  264. string cullParams = subBody.Substring( cullIdx, end - cullIdx );
  265. m_cullModeData.CullModeId = cullParams;
  266. TemplateHelperFunctions.CreateCullMode( cullParams, ref m_cullModeData );
  267. if( m_cullModeData.DataCheck == TemplateDataCheck.Valid )
  268. AddId( cullParams, false, string.Empty );
  269. }
  270. }
  271. //COLOR MASK
  272. {
  273. int colorMaskIdx = subBody.IndexOf( "ColorMask" );
  274. if( colorMaskIdx > 0 )
  275. {
  276. int end = subBody.IndexOf( TemplatesManager.TemplateNewLine, colorMaskIdx );
  277. string colorMaskParams = subBody.Substring( colorMaskIdx, end - colorMaskIdx );
  278. m_colorMaskData.ColorMaskId = colorMaskParams;
  279. TemplateHelperFunctions.CreateColorMask( colorMaskParams, ref m_colorMaskData );
  280. if( m_colorMaskData.DataCheck == TemplateDataCheck.Valid )
  281. AddId( colorMaskParams, false );
  282. }
  283. }
  284. //BlEND MODE
  285. {
  286. int blendModeIdx = subBody.IndexOf( "Blend" );
  287. if( blendModeIdx > 0 )
  288. {
  289. int end = subBody.IndexOf( TemplatesManager.TemplateNewLine, blendModeIdx );
  290. string blendParams = subBody.Substring( blendModeIdx, end - blendModeIdx );
  291. m_blendData.BlendModeId = blendParams;
  292. TemplateHelperFunctions.CreateBlendMode( blendParams, ref m_blendData );
  293. if( m_blendData.ValidBlendMode )
  294. {
  295. AddId( blendParams, false );
  296. }
  297. }
  298. }
  299. //BLEND OP
  300. {
  301. int blendOpIdx = subBody.IndexOf( "BlendOp" );
  302. if( blendOpIdx > 0 )
  303. {
  304. int end = subBody.IndexOf( TemplatesManager.TemplateNewLine, blendOpIdx );
  305. string blendOpParams = subBody.Substring( blendOpIdx, end - blendOpIdx );
  306. BlendData.BlendOpId = blendOpParams;
  307. TemplateHelperFunctions.CreateBlendOp( blendOpParams, ref m_blendData );
  308. if( m_blendData.ValidBlendOp )
  309. {
  310. AddId( blendOpParams, false );
  311. }
  312. }
  313. m_blendData.DataCheck = ( m_blendData.ValidBlendMode || m_blendData.ValidBlendOp ) ? TemplateDataCheck.Valid : TemplateDataCheck.Invalid;
  314. }
  315. //STENCIL
  316. {
  317. int stencilIdx = subBody.IndexOf( "Stencil" );
  318. if( stencilIdx > -1 )
  319. {
  320. int stencilEndIdx = subBody.IndexOf( "}", stencilIdx );
  321. if( stencilEndIdx > 0 )
  322. {
  323. string stencilParams = subBody.Substring( stencilIdx, stencilEndIdx + 1 - stencilIdx );
  324. m_stencilData.StencilBufferId = stencilParams;
  325. TemplateHelperFunctions.CreateStencilOps( stencilParams, ref m_stencilData );
  326. if( m_stencilData.DataCheck == TemplateDataCheck.Valid )
  327. {
  328. AddId( stencilParams, true );
  329. }
  330. }
  331. }
  332. }
  333. //ZWRITE
  334. {
  335. int zWriteOpIdx = subBody.IndexOf( "ZWrite" );
  336. if( zWriteOpIdx > -1 )
  337. {
  338. int zWriteEndIdx = subBody.IndexOf( TemplatesManager.TemplateNewLine, zWriteOpIdx );
  339. if( zWriteEndIdx > 0 )
  340. {
  341. m_depthData.ZWriteModeId = subBody.Substring( zWriteOpIdx, zWriteEndIdx + 1 - zWriteOpIdx );
  342. TemplateHelperFunctions.CreateZWriteMode( m_depthData.ZWriteModeId, ref m_depthData );
  343. if( m_depthData.DataCheck == TemplateDataCheck.Valid )
  344. {
  345. AddId( m_depthData.ZWriteModeId, true );
  346. }
  347. }
  348. }
  349. }
  350. //ZTEST
  351. {
  352. int zTestOpIdx = subBody.IndexOf( "ZTest" );
  353. if( zTestOpIdx > -1 )
  354. {
  355. int zTestEndIdx = subBody.IndexOf( TemplatesManager.TemplateNewLine, zTestOpIdx );
  356. if( zTestEndIdx > 0 )
  357. {
  358. m_depthData.ZTestModeId = subBody.Substring( zTestOpIdx, zTestEndIdx + 1 - zTestOpIdx );
  359. TemplateHelperFunctions.CreateZTestMode( m_depthData.ZTestModeId, ref m_depthData );
  360. if( m_depthData.DataCheck == TemplateDataCheck.Valid )
  361. {
  362. AddId( m_depthData.ZTestModeId, true );
  363. }
  364. }
  365. }
  366. }
  367. //ZOFFSET
  368. {
  369. int zOffsetIdx = subBody.IndexOf( "Offset" );
  370. if( zOffsetIdx > -1 )
  371. {
  372. int zOffsetEndIdx = subBody.IndexOf( TemplatesManager.TemplateNewLine, zOffsetIdx );
  373. if( zOffsetEndIdx > 0 )
  374. {
  375. m_depthData.OffsetId = subBody.Substring( zOffsetIdx, zOffsetEndIdx + 1 - zOffsetIdx );
  376. TemplateHelperFunctions.CreateZOffsetMode( m_depthData.OffsetId, ref m_depthData );
  377. if( m_depthData.DataCheck == TemplateDataCheck.Valid )
  378. {
  379. AddId( m_depthData.OffsetId, true );
  380. }
  381. }
  382. }
  383. }
  384. //TAGS
  385. {
  386. int tagsIdx = subBody.IndexOf( "Tags" );
  387. if( tagsIdx > -1 )
  388. {
  389. int tagsEndIdx = subBody.IndexOf( "}", tagsIdx );
  390. if( tagsEndIdx > -1 )
  391. {
  392. m_tagData.Reset();
  393. m_tagData.TagsId = subBody.Substring( tagsIdx, tagsEndIdx + 1 - tagsIdx );
  394. TemplateHelperFunctions.CreateTags( ref m_tagData, true );
  395. m_tagData.DataCheck = TemplateDataCheck.Valid;
  396. AddId( m_tagData.TagsId, false );
  397. }
  398. else
  399. {
  400. m_tagData.DataCheck = TemplateDataCheck.Invalid;
  401. }
  402. }
  403. else
  404. {
  405. m_tagData.DataCheck = TemplateDataCheck.Invalid;
  406. }
  407. }
  408. }
  409. }
  410. void FetchCommonTags()
  411. {
  412. // Name
  413. try
  414. {
  415. int nameBegin = m_templateBody.IndexOf( TemplatesManager.TemplateShaderNameBeginTag );
  416. if( nameBegin < 0 )
  417. {
  418. // Not a template
  419. return;
  420. }
  421. int nameEnd = m_templateBody.IndexOf( TemplatesManager.TemplateFullEndTag, nameBegin );
  422. int defaultBegin = nameBegin + TemplatesManager.TemplateShaderNameBeginTag.Length;
  423. int defaultLength = nameEnd - defaultBegin;
  424. m_defaultShaderName = m_templateBody.Substring( defaultBegin, defaultLength );
  425. int[] nameIdx = m_defaultShaderName.AllIndexesOf( "\"" );
  426. nameIdx[ 0 ] += 1; // Ignore the " character from the string
  427. m_defaultShaderName = m_defaultShaderName.Substring( nameIdx[ 0 ], nameIdx[ 1 ] - nameIdx[ 0 ] );
  428. m_shaderNameId = m_templateBody.Substring( nameBegin, nameEnd + TemplatesManager.TemplateFullEndTag.Length - nameBegin );
  429. AddId( m_shaderNameId, false );
  430. }
  431. catch( Exception e )
  432. {
  433. Debug.LogException( e );
  434. m_isValid = false;
  435. }
  436. FetchSubShaderProperties();
  437. // Vertex Data
  438. {
  439. int vertexDataTagBegin = m_templateBody.IndexOf( TemplatesManager.TemplateVertexDataTag );
  440. if( vertexDataTagBegin > -1 )
  441. {
  442. m_vertexDataContainer.VertexDataStartIdx = vertexDataTagBegin;
  443. int vertexDataTagEnd = m_templateBody.IndexOf( TemplatesManager.TemplateEndOfLine, vertexDataTagBegin );
  444. m_vertexDataContainer.VertexDataId = m_templateBody.Substring( vertexDataTagBegin, vertexDataTagEnd + TemplatesManager.TemplateEndOfLine.Length - vertexDataTagBegin );
  445. int dataBeginIdx = m_templateBody.LastIndexOf( '{', vertexDataTagBegin, vertexDataTagBegin );
  446. string vertexData = m_templateBody.Substring( dataBeginIdx + 1, vertexDataTagBegin - dataBeginIdx );
  447. int parametersBegin = vertexDataTagBegin + TemplatesManager.TemplateVertexDataTag.Length;
  448. string parameters = m_templateBody.Substring( parametersBegin, vertexDataTagEnd - parametersBegin );
  449. m_vertexDataContainer.VertexData = TemplateHelperFunctions.CreateVertexDataList( vertexData, parameters );
  450. AddId( m_vertexDataContainer.VertexDataId );
  451. }
  452. }
  453. // Available interpolators
  454. try
  455. {
  456. int interpDataBegin = m_templateBody.IndexOf( TemplatesManager.TemplateInterpolatorBeginTag );
  457. if( interpDataBegin > -1 )
  458. {
  459. int interpDataEnd = m_templateBody.IndexOf( TemplatesManager.TemplateEndOfLine, interpDataBegin );
  460. string interpDataId = m_templateBody.Substring( interpDataBegin, interpDataEnd + TemplatesManager.TemplateEndOfLine.Length - interpDataBegin );
  461. int dataBeginIdx = m_templateBody.LastIndexOf( '{', interpDataBegin, interpDataBegin );
  462. string interpData = m_templateBody.Substring( dataBeginIdx + 1, interpDataBegin - dataBeginIdx );
  463. m_interpolatorDataContainer = TemplateHelperFunctions.CreateInterpDataList( interpData, interpDataId, 8 );
  464. m_interpolatorDataContainer.InterpDataId = interpDataId;
  465. m_interpolatorDataContainer.InterpDataStartIdx = interpDataBegin;
  466. AddId( interpDataId );
  467. }
  468. }
  469. catch( Exception e )
  470. {
  471. Debug.LogException( e );
  472. m_isValid = false;
  473. }
  474. try
  475. {
  476. Dictionary<string, TemplateShaderPropertyData> duplicatesHelper = new Dictionary<string, TemplateShaderPropertyData>();
  477. m_availableShaderProperties = new List<TemplateShaderPropertyData>();
  478. // Common Tags
  479. for( int i = 0; i < TemplatesManager.CommonTags.Length; i++ )
  480. {
  481. int idx = m_templateBody.IndexOf( TemplatesManager.CommonTags[ i ].Id );
  482. if( idx > -1 )
  483. {
  484. string currentId = TemplatesManager.CommonTags[ i ].Id;
  485. TemplateCommonTagId commonTagId = (TemplateCommonTagId)i;
  486. switch( commonTagId )
  487. {
  488. // Properties
  489. case TemplateCommonTagId.Property:
  490. {
  491. TemplateHelperFunctions.CreateShaderPropertiesList( m_templateBody.Substring( 0, idx + TemplatesManager.CommonTags[ i ].Id.Length ), ref m_availableShaderProperties, ref duplicatesHelper );
  492. }
  493. break;
  494. // Globals
  495. case TemplateCommonTagId.Global:
  496. {
  497. TemplateHelperFunctions.CreateShaderGlobalsList( m_templateBody.Substring( 0, idx + TemplatesManager.CommonTags[ i ].Id.Length ), ref m_availableShaderProperties, ref duplicatesHelper );
  498. }
  499. break;
  500. //Tags
  501. //case TemplateCommonTagId.Tag:
  502. //{
  503. // m_propertyList[ m_propertyList.Count - 1 ].Indentation = " ";
  504. //}
  505. //break;
  506. //case TemplateCommonTagId.CullMode:
  507. //{
  508. // int newId = idx + TemplatesManager.CommonTags[ i ].Id.Length;
  509. // int end = m_templateBody.IndexOf( TemplatesManager.TemplateNewLine, newId );
  510. // string cullParams = m_templateBody.Substring( newId, end - newId );
  511. // currentId = m_templateBody.Substring( idx, end - idx );
  512. // m_cullModeData.CullModeId = currentId;
  513. // TemplateHelperFunctions.CreateCullMode( cullParams, ref m_cullModeData );
  514. //}
  515. //break;
  516. //Blend Mode
  517. //case TemplateCommonTagId.BlendMode:
  518. //{
  519. // int newId = idx + TemplatesManager.CommonTags[ i ].Id.Length;
  520. // int end = m_templateBody.IndexOf( TemplatesManager.TemplateNewLine, newId );
  521. // string blendParams = m_templateBody.Substring( newId, end - newId );
  522. // currentId = m_templateBody.Substring( idx, end - idx );
  523. // m_blendData.BlendModeId = currentId;
  524. // TemplateHelperFunctions.CreateBlendMode( blendParams, ref m_blendData );
  525. //}break;
  526. //case TemplateCommonTagId.BlendOp:
  527. //{
  528. // int newId = idx + TemplatesManager.CommonTags[ i ].Id.Length;
  529. // int end = m_templateBody.IndexOf( TemplatesManager.TemplateNewLine, newId );
  530. // currentId = m_templateBody.Substring( idx, end - idx );
  531. // BlendData.BlendOpId = currentId;
  532. // TemplateHelperFunctions.CreateBlendOp( m_templateBody.Substring( newId, end - newId ), ref m_blendData );
  533. //}break;
  534. //case TemplateCommonTagId.ColorMask:
  535. //{
  536. // int newId = idx + TemplatesManager.CommonTags[ i ].Id.Length;
  537. // int end = m_templateBody.IndexOf( TemplatesManager.TemplateNewLine, newId );
  538. // string colorMaskParams = m_templateBody.Substring( newId, end - newId );
  539. // currentId = m_templateBody.Substring( idx, end - idx );
  540. // m_colorMaskData.ColorMaskId = currentId;
  541. // TemplateHelperFunctions.CreateColorMask( colorMaskParams, ref m_colorMaskData );
  542. //}
  543. //break;
  544. //case TemplateCommonTagId.StencilOp:
  545. //{
  546. // int id = m_templateBody.LastIndexOf( "Stencil" );
  547. // if( id > -1 )
  548. // {
  549. // string stencilParams = m_templateBody.Substring( id, idx - id );
  550. // currentId = stencilParams + TemplatesManager.TemplateStencilOpTag;
  551. // m_stencilData.StencilBufferId = currentId;
  552. // TemplateHelperFunctions.CreateStencilOps( stencilParams, ref m_stencilData );
  553. // }
  554. //}
  555. //break;
  556. default:
  557. break;
  558. }
  559. //AddId( TemplatesManager.CommonTags[ i ] );
  560. AddId( currentId, TemplatesManager.CommonTags[ i ].SearchIndentation, TemplatesManager.CommonTags[ i ].CustomIndentation );
  561. }
  562. }
  563. duplicatesHelper.Clear();
  564. duplicatesHelper = null;
  565. }
  566. catch( Exception e )
  567. {
  568. Debug.LogException( e );
  569. m_isValid = false;
  570. }
  571. }
  572. void FetchCodeAreas( string begin, MasterNodePortCategory category )
  573. {
  574. int areaBeginIndexes = m_templateBody.IndexOf( begin );
  575. if( areaBeginIndexes > -1 )
  576. {
  577. int beginIdx = areaBeginIndexes + begin.Length;
  578. int endIdx = m_templateBody.IndexOf( TemplatesManager.TemplateEndOfLine, beginIdx );
  579. int length = endIdx - beginIdx;
  580. string parameters = m_templateBody.Substring( beginIdx, length );
  581. string[] parametersArr = parameters.Split( IOUtils.FIELD_SEPARATOR );
  582. string id = m_templateBody.Substring( areaBeginIndexes, endIdx + TemplatesManager.TemplateEndOfLine.Length - areaBeginIndexes );
  583. string inParameters = parametersArr[ 0 ];
  584. string outParameters = ( parametersArr.Length > 1 ) ? parametersArr[ 1 ] : string.Empty;
  585. if( category == MasterNodePortCategory.Fragment )
  586. {
  587. m_fragmentFunctionData = new TemplateFunctionData(-1, string.Empty, id, areaBeginIndexes, inParameters, outParameters, category );
  588. }
  589. else
  590. {
  591. m_vertexFunctionData = new TemplateFunctionData( -1, string.Empty,id, areaBeginIndexes, inParameters, outParameters, category );
  592. }
  593. AddId( id, true );
  594. }
  595. }
  596. void FetchInputs( MasterNodePortCategory portCategory )
  597. {
  598. string beginTag = ( portCategory == MasterNodePortCategory.Fragment ) ? TemplatesManager.TemplateInputsFragBeginTag : TemplatesManager.TemplateInputsVertBeginTag;
  599. int[] inputBeginIndexes = m_templateBody.AllIndexesOf( beginTag );
  600. if( inputBeginIndexes != null && inputBeginIndexes.Length > 0 )
  601. {
  602. for( int i = 0; i < inputBeginIndexes.Length; i++ )
  603. {
  604. int inputEndIdx = m_templateBody.IndexOf( TemplatesManager.TemplateEndSectionTag, inputBeginIndexes[ i ] );
  605. int defaultValueBeginIdx = inputEndIdx + TemplatesManager.TemplateEndSectionTag.Length;
  606. int endLineIdx = m_templateBody.IndexOf( TemplatesManager.TemplateFullEndTag, defaultValueBeginIdx );
  607. string defaultValue = m_templateBody.Substring( defaultValueBeginIdx, endLineIdx - defaultValueBeginIdx );
  608. string tagId = m_templateBody.Substring( inputBeginIndexes[ i ], endLineIdx + TemplatesManager.TemplateFullEndTag.Length - inputBeginIndexes[ i ] );
  609. int beginIndex = inputBeginIndexes[ i ] + beginTag.Length;
  610. int length = inputEndIdx - beginIndex;
  611. string inputData = m_templateBody.Substring( beginIndex, length );
  612. string[] inputDataArray = inputData.Split( IOUtils.FIELD_SEPARATOR );
  613. if( inputDataArray != null && inputDataArray.Length > 0 )
  614. {
  615. try
  616. {
  617. string portName = inputDataArray[ (int)TemplatePortIds.Name ];
  618. WirePortDataType dataType = (WirePortDataType)Enum.Parse( typeof( WirePortDataType ), inputDataArray[ (int)TemplatePortIds.DataType ].ToUpper() );
  619. int portUniqueIDArrIdx = (int)TemplatePortIds.UniqueId;
  620. int portUniqueId = ( portUniqueIDArrIdx < inputDataArray.Length ) ? Convert.ToInt32( inputDataArray[ portUniqueIDArrIdx ] ) : -1;
  621. if( portUniqueId < 0 )
  622. portUniqueId = m_inputDataList.Count;
  623. int portOrderArrayIdx = (int)TemplatePortIds.OrderId;
  624. int portOrderId = ( portOrderArrayIdx < inputDataArray.Length ) ? Convert.ToInt32( inputDataArray[ portOrderArrayIdx ] ) : -1;
  625. if( portOrderId < 0 )
  626. portOrderId = m_inputDataList.Count;
  627. AddInput( inputBeginIndexes[ i ], tagId, portName, defaultValue, dataType, portCategory, portUniqueId, portOrderId );
  628. }
  629. catch( Exception e )
  630. {
  631. Debug.LogException( e );
  632. }
  633. }
  634. }
  635. }
  636. }
  637. //void FetchSnippets()
  638. //{
  639. // int[] codeSnippetAttribBeginIndexes = m_templateBody.AllIndexesOf( TemplatesManager.TemplateCodeSnippetAttribBegin );
  640. // int[] codeSnippetAttribEndIndexes = m_templateBody.AllIndexesOf( TemplatesManager.TemplateCodeSnippetAttribEnd );
  641. // int[] codeSnippetEndIndexes = m_templateBody.AllIndexesOf( TemplatesManager.TemplateCodeSnippetEnd );
  642. // if( codeSnippetAttribBeginIndexes != null && codeSnippetAttribBeginIndexes.Length > 0 &&
  643. // codeSnippetAttribEndIndexes != null && codeSnippetAttribEndIndexes.Length > 0 &&
  644. // codeSnippetEndIndexes != null && codeSnippetEndIndexes.Length > 0 &&
  645. // codeSnippetEndIndexes.Length == codeSnippetAttribBeginIndexes.Length &&
  646. // codeSnippetAttribBeginIndexes.Length == codeSnippetAttribEndIndexes.Length )
  647. // {
  648. // for( int i = 0; i < codeSnippetAttribBeginIndexes.Length; i++ )
  649. // {
  650. // // get attributes
  651. // int startAttribIndex = codeSnippetAttribBeginIndexes[ i ] + TemplatesManager.TemplateCodeSnippetAttribBegin.Length;
  652. // int lengthAttrib = codeSnippetAttribEndIndexes[ i ] - startAttribIndex;
  653. // string snippetAttribs = m_templateBody.Substring( startAttribIndex, lengthAttrib );
  654. // string[] snippetAttribsArr = snippetAttribs.Split( IOUtils.FIELD_SEPARATOR );
  655. // if( snippetAttribsArr != null && snippetAttribsArr.Length > 0 )
  656. // {
  657. // string attribName = snippetAttribsArr[ (int)TemplateCodeSnippetInfoIdx.Name ];
  658. // TemplateCodeSnippetType attribType = (TemplateCodeSnippetType)Enum.Parse( typeof( TemplateCodeSnippetType ), snippetAttribsArr[ (int)TemplateCodeSnippetInfoIdx.Type ] );
  659. // if( m_snippetElementsDict.ContainsKey( attribName ) )
  660. // {
  661. // if( m_snippetElementsDict[ attribName ].Type != attribType )
  662. // {
  663. // if( DebugConsoleWindow.DeveloperMode )
  664. // Debug.LogWarning( "Found incompatible types for snippet " + attribName );
  665. // }
  666. // }
  667. // else
  668. // {
  669. // switch( attribType )
  670. // {
  671. // case TemplateCodeSnippetType.Toggle:
  672. // {
  673. // //Register must be done by first instantiang the correct type and register it on both containers
  674. // //Overrides don't work if we use the container reference into the other
  675. // TemplateCodeSnippetToggle newSnippet = ScriptableObject.CreateInstance<TemplateCodeSnippetToggle>();
  676. // newSnippet.Init( attribName, attribType );
  677. // m_snippetElementsDict.Add( attribName, newSnippet );
  678. // m_snippetElementsList.Add( newSnippet );
  679. // }
  680. // break;
  681. // }
  682. // }
  683. // // Add initial tag indentation
  684. // int indentationIndex = codeSnippetAttribBeginIndexes[ i ];
  685. // int lengthAdjust = 0;
  686. // for( ; indentationIndex > 0; indentationIndex--, lengthAdjust++ )
  687. // {
  688. // if( m_templateBody[ indentationIndex ] == TemplatesManager.TemplateNewLine )
  689. // {
  690. // indentationIndex += 1;
  691. // lengthAdjust -= 1;
  692. // break;
  693. // }
  694. // }
  695. // if( indentationIndex > 0 )
  696. // {
  697. // string snippetId = m_templateBody.Substring( indentationIndex,
  698. // codeSnippetEndIndexes[ i ] + TemplatesManager.TemplateCodeSnippetEnd.Length - codeSnippetAttribBeginIndexes[ i ] + lengthAdjust );
  699. // int snippetCodeStart = codeSnippetAttribEndIndexes[ i ] + TemplatesManager.TemplateCodeSnippetAttribEnd.Length;
  700. // int snippetCodeLength = codeSnippetEndIndexes[ i ] - snippetCodeStart;
  701. // //Remove possible identation characters present between tag and last instruction
  702. // if( m_templateBody[ snippetCodeStart + snippetCodeLength - 1 ] != TemplatesManager.TemplateNewLine )
  703. // {
  704. // for( ; snippetCodeLength > 0; snippetCodeLength-- )
  705. // {
  706. // if( m_templateBody[ snippetCodeStart + snippetCodeLength - 1 ] == TemplatesManager.TemplateNewLine )
  707. // break;
  708. // }
  709. // }
  710. // if( snippetCodeLength > 0 )
  711. // {
  712. // string snippetCode = m_templateBody.Substring( snippetCodeStart, snippetCodeLength );
  713. // TemplateCodeSnippetElement element = new TemplateCodeSnippetElement( snippetId, snippetCode );
  714. // m_snippetElementsDict[ attribName ].AddSnippet( element );
  715. // }
  716. // }
  717. // }
  718. // }
  719. // }
  720. //}
  721. //void RefreshSnippetInfo()
  722. //{
  723. // if( m_snippetElementsDict == null )
  724. // {
  725. // m_snippetElementsDict = new Dictionary<string, TemplateCodeSnippetBase>();
  726. // }
  727. // if( m_snippetElementsDict.Count != m_snippetElementsList.Count )
  728. // {
  729. // m_snippetElementsDict.Clear();
  730. // for( int i = 0; i < m_snippetElementsList.Count; i++ )
  731. // {
  732. // m_snippetElementsDict.Add( m_snippetElementsList[ i ].NameId, m_snippetElementsList[ i ] );
  733. // }
  734. // }
  735. //}
  736. //public void DrawSnippetProperties( ParentNode owner )
  737. //{
  738. // for( int i = 0; i < m_snippetElementsList.Count; i++ )
  739. // {
  740. // m_snippetElementsList[ i ].DrawProperties( owner );
  741. // }
  742. //}
  743. //public void InsertSnippets( ref string shaderBody )
  744. //{
  745. // for( int i = 0; i < m_snippetElementsList.Count; i++ )
  746. // {
  747. // m_snippetElementsList[ i ].InsertSnippet( ref shaderBody );
  748. // }
  749. //}
  750. public void AddId( string ID, bool searchIndentation = true )
  751. {
  752. AddId( ID, searchIndentation, string.Empty );
  753. }
  754. public void AddId( string ID, bool searchIndentation, string customIndentation )
  755. {
  756. int propertyIndex = m_templateBody.IndexOf( ID );
  757. if( propertyIndex > -1 )
  758. {
  759. if( searchIndentation )
  760. {
  761. int indentationIndex = -1;
  762. for( int i = propertyIndex; i > 0; i-- )
  763. {
  764. if( m_templateBody[ i ] == TemplatesManager.TemplateNewLine )
  765. {
  766. indentationIndex = i + 1;
  767. break;
  768. }
  769. }
  770. if( indentationIndex > -1 )
  771. {
  772. int length = propertyIndex - indentationIndex;
  773. string indentation = ( length > 0 ) ? m_templateBody.Substring( indentationIndex, length ) : string.Empty;
  774. m_propertyList.Add( new TemplateProperty( ID, indentation, false ) );
  775. }
  776. }
  777. else
  778. {
  779. m_propertyList.Add( new TemplateProperty( ID, customIndentation, true ) );
  780. }
  781. }
  782. }
  783. void BuildInfo()
  784. {
  785. if( m_propertyDict == null )
  786. {
  787. m_propertyDict = new Dictionary<string, TemplateProperty>();
  788. }
  789. if( m_propertyList.Count != m_propertyDict.Count )
  790. {
  791. m_propertyDict.Clear();
  792. for( int i = 0; i < m_propertyList.Count; i++ )
  793. {
  794. m_propertyDict.Add( m_propertyList[ i ].Id, m_propertyList[ i ] );
  795. }
  796. }
  797. }
  798. public void ResetTemplateUsageData()
  799. {
  800. BuildInfo();
  801. for( int i = 0; i < m_propertyList.Count; i++ )
  802. {
  803. m_propertyList[ i ].Used = false;
  804. }
  805. }
  806. public void AddInput( int tagStartIdx, string tagId, string portName, string defaultValue, WirePortDataType dataType, MasterNodePortCategory portCategory, int portUniqueId, int portOrderId )
  807. {
  808. TemplateInputData inputData = new TemplateInputData( tagStartIdx, tagStartIdx, tagId, portName, defaultValue, dataType, portCategory, portUniqueId, portOrderId, string.Empty );
  809. m_inputDataList.Add( inputData );
  810. m_inputDataDict.Add( inputData.PortUniqueId, inputData );
  811. AddId( tagId, false );
  812. }
  813. public override void Destroy()
  814. {
  815. if( m_vertexDataContainer != null )
  816. {
  817. m_vertexDataContainer.Destroy();
  818. m_vertexDataContainer = null;
  819. }
  820. if( m_interpolatorDataContainer != null )
  821. {
  822. m_interpolatorDataContainer.Destroy();
  823. m_interpolatorDataContainer = null;
  824. }
  825. if( m_availableShaderProperties != null )
  826. {
  827. m_availableShaderProperties.Clear();
  828. m_availableShaderProperties = null;
  829. }
  830. if( m_propertyDict != null )
  831. {
  832. m_propertyDict.Clear();
  833. m_propertyDict = null;
  834. }
  835. if( m_propertyList != null )
  836. {
  837. m_propertyList.Clear();
  838. m_propertyList = null;
  839. }
  840. if( m_inputDataDict != null )
  841. {
  842. m_inputDataDict.Clear();
  843. m_inputDataDict = null;
  844. }
  845. if( m_inputDataList != null )
  846. {
  847. m_inputDataList.Clear();
  848. m_inputDataList = null;
  849. }
  850. if( m_localVarsList != null )
  851. {
  852. m_localVarsList.Clear();
  853. m_localVarsList = null;
  854. }
  855. //if( m_snippetElementsDict != null )
  856. //{
  857. // m_snippetElementsDict.Clear();
  858. // m_snippetElementsDict = null;
  859. //}
  860. //if( m_snippetElementsList != null )
  861. //{
  862. // for( int i = 0; i < m_snippetElementsList.Count; i++ )
  863. // {
  864. // GameObject.DestroyImmediate( m_snippetElementsList[ i ] );
  865. // m_snippetElementsList[ i ] = null;
  866. // }
  867. // m_snippetElementsList.Clear();
  868. // m_snippetElementsList = null;
  869. //}
  870. m_cullModeData = null;
  871. m_blendData = null;
  872. m_colorMaskData = null;
  873. m_stencilData = null;
  874. if( m_tagData != null )
  875. {
  876. m_tagData.Destroy();
  877. m_tagData = null;
  878. }
  879. }
  880. public void FillEmptyTags( ref string body )
  881. {
  882. body = body.Replace( TemplatesManager.TemplateLocalVarTag, string.Empty );
  883. for( int i = 0; i < m_propertyList.Count; i++ )
  884. {
  885. if( !m_propertyList[ i ].Used )
  886. {
  887. if( m_propertyList[ i ].UseCustomIndentation )
  888. {
  889. body = body.Replace( m_propertyList[ i ].Id, string.Empty );
  890. }
  891. else
  892. {
  893. body = body.Replace( m_propertyList[ i ].Indentation + m_propertyList[ i ].Id, string.Empty );
  894. }
  895. }
  896. }
  897. }
  898. public bool FillVertexInstructions( ref string body, params string[] values )
  899. {
  900. if( m_vertexFunctionData != null && !string.IsNullOrEmpty( m_vertexFunctionData.Id ) )
  901. {
  902. return FillTemplateBody( m_vertexFunctionData.Id, ref body, values );
  903. }
  904. if( values.Length > 0 )
  905. {
  906. UIUtils.ShowMessage( "Attemping to add vertex instructions on a template with no assigned vertex code area", MessageSeverity.Error );
  907. return false;
  908. }
  909. return true;
  910. }
  911. public bool FillFragmentInstructions( ref string body, params string[] values )
  912. {
  913. if( m_fragmentFunctionData != null && !string.IsNullOrEmpty( m_fragmentFunctionData.Id ) )
  914. {
  915. return FillTemplateBody( m_fragmentFunctionData.Id, ref body, values );
  916. }
  917. if( values.Length > 0 )
  918. {
  919. UIUtils.ShowMessage( "Attemping to add fragment instructions on a template with no assigned vertex code area", MessageSeverity.Error );
  920. return false;
  921. }
  922. return true;
  923. }
  924. // values must be unindented an without line feed
  925. public bool FillTemplateBody( string id, ref string body, params string[] values )
  926. {
  927. if( values.Length == 0 )
  928. {
  929. return true;
  930. }
  931. BuildInfo();
  932. if( m_propertyDict.ContainsKey( id ) )
  933. {
  934. string finalValue = string.Empty;
  935. for( int i = 0; i < values.Length; i++ )
  936. {
  937. if( m_propertyDict[ id ].AutoLineFeed )
  938. {
  939. string[] valuesArr = values[ i ].Split( '\n' );
  940. for( int j = 0; j < valuesArr.Length; j++ )
  941. {
  942. //first value will be automatically indented by the string replace
  943. finalValue += ( ( i == 0 && j == 0 ) ? string.Empty : m_propertyDict[ id ].Indentation ) + valuesArr[ j ];
  944. finalValue += TemplatesManager.TemplateNewLine;
  945. }
  946. }
  947. else
  948. {
  949. //first value will be automatically indented by the string replace
  950. finalValue += ( i == 0 ? string.Empty : m_propertyDict[ id ].Indentation ) + values[ i ];
  951. }
  952. }
  953. body = body.Replace( id, finalValue );
  954. m_propertyDict[ id ].Used = true;
  955. return true;
  956. }
  957. if( values.Length > 1 || !string.IsNullOrEmpty( values[ 0 ] ) )
  958. {
  959. UIUtils.ShowMessage( string.Format( "Attempting to write data into inexistant tag {0}. Please review the template {1} body and consider adding the missing tag.", id, m_name ), MessageSeverity.Error );
  960. return false;
  961. }
  962. return true;
  963. }
  964. public bool FillTemplateBody( string id, ref string body, List<PropertyDataCollector> values )
  965. {
  966. if( values.Count == 0 )
  967. {
  968. return true;
  969. }
  970. string[] array = new string[ values.Count ];
  971. for( int i = 0; i < values.Count; i++ )
  972. {
  973. array[ i ] = values[ i ].PropertyName;
  974. }
  975. return FillTemplateBody( id, ref body, array );
  976. }
  977. public TemplateInputData InputDataFromId( int id )
  978. {
  979. if( m_inputDataDict == null )
  980. m_inputDataDict = new Dictionary<int, TemplateInputData>();
  981. if( m_inputDataDict.Count != m_inputDataList.Count )
  982. {
  983. m_inputDataDict.Clear();
  984. for( int i = 0; i < m_inputDataList.Count; i++ )
  985. {
  986. m_inputDataDict.Add( m_inputDataList[ i ].PortUniqueId, m_inputDataList[ i ] );
  987. }
  988. }
  989. if( m_inputDataDict.ContainsKey( id ) )
  990. return m_inputDataDict[ id ];
  991. return null;
  992. }
  993. public string GetVertexData( TemplateInfoOnSematics info )
  994. {
  995. int count = m_vertexDataContainer.VertexData.Count;
  996. for( int i = 0; i < count; i++ )
  997. {
  998. if( m_vertexDataContainer.VertexData[ i ].DataInfo == info )
  999. {
  1000. return string.Format( TemplateHelperFunctions.TemplateVarFormat, m_vertexFunctionData.InVarName, m_vertexDataContainer.VertexData[ i ].VarName );
  1001. }
  1002. }
  1003. return string.Empty;
  1004. }
  1005. public string GetInterpolatedData( TemplateInfoOnSematics info )
  1006. {
  1007. int count = m_interpolatorDataContainer.Interpolators.Count;
  1008. for( int i = 0; i < count; i++ )
  1009. {
  1010. if( m_interpolatorDataContainer.Interpolators[ i ].DataInfo == info )
  1011. {
  1012. return string.Format( TemplateHelperFunctions.TemplateVarFormat, m_fragmentFunctionData.InVarName, m_interpolatorDataContainer.Interpolators[ i ].VarName );
  1013. }
  1014. }
  1015. return string.Empty;
  1016. }
  1017. public string InterpDataId { get { return m_interpolatorDataContainer.InterpDataId; } }
  1018. public string VertexDataId { get { return m_vertexDataContainer.VertexDataId; } }
  1019. public string ShaderNameId { get { return m_shaderNameId; } set { m_shaderNameId = value; } }
  1020. public string TemplateBody { get { return m_templateBody; } set { m_templateBody = value; } }
  1021. public List<TemplateInputData> InputDataList { get { return m_inputDataList; } set { m_inputDataList = value; } }
  1022. public List<TemplateLocalVarData> LocalVarsList { get { return m_localVarsList; } }
  1023. public List<TemplateVertexData> VertexDataList { get { return m_vertexDataContainer.VertexData; } }
  1024. public TemplateInterpData InterpolatorData { get { return m_interpolatorDataContainer; } }
  1025. public TemplateFunctionData VertexFunctionData { get { return m_vertexFunctionData; } set { m_vertexFunctionData = value; } }
  1026. public TemplateFunctionData FragmentFunctionData { get { return m_fragmentFunctionData; } set { m_fragmentFunctionData = value; } }
  1027. public TemplateFunctionData FragFunctionData { get { return m_fragmentFunctionData; } set { m_fragmentFunctionData = value; } }
  1028. public List<TemplateShaderPropertyData> AvailableShaderProperties { get { return m_availableShaderProperties; } set { m_availableShaderProperties = value; } }
  1029. public TemplateBlendData BlendData { get { return m_blendData; } set { m_blendData = value; } }
  1030. public TemplateCullModeData CullModeData { get { return m_cullModeData; } set { m_cullModeData = value; } }
  1031. public TemplateColorMaskData ColorMaskData { get { return m_colorMaskData; } set { m_colorMaskData = value; } }
  1032. public TemplateStencilData StencilData { get { return m_stencilData; } set { m_stencilData = value; } }
  1033. public TemplateDepthData DepthData { get { return m_depthData; } set { m_depthData = value; } }
  1034. public TemplateTagsModuleData TagData { get { return m_tagData; } set { m_tagData = value; } }
  1035. private List<TemplateProperty> PropertyList { get { return m_propertyList; } set { m_propertyList = value; } }
  1036. public VertexDataContainer VertexDataContainer { get { return m_vertexDataContainer; } set { m_vertexDataContainer = value; } }
  1037. public TemplateInterpData InterpolatorDataContainer { get { return m_interpolatorDataContainer; } set { m_interpolatorDataContainer = value; } }
  1038. }
  1039. }