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.

121 lines
3.8 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public class TemplatePassInfo
  10. {
  11. public string Modules;
  12. public string Data;
  13. public int GlobalStartIdx = -1;
  14. public int LocalStartIdx = -1;
  15. }
  16. [Serializable]
  17. public class TemplateSubShaderInfo
  18. {
  19. public string Data;
  20. public string Modules;
  21. public int StartIdx = -1;
  22. public List<TemplatePassInfo> Passes = new List<TemplatePassInfo>();
  23. public void Destroy()
  24. {
  25. Passes.Clear();
  26. Passes = null;
  27. }
  28. }
  29. [Serializable]
  30. public class TemplateShaderInfo
  31. {
  32. public string Body;
  33. public string Properties;
  34. public int PropertyStartIdx = -1;
  35. public List<TemplateSubShaderInfo> SubShaders = new List<TemplateSubShaderInfo>();
  36. public void Destroy()
  37. {
  38. int count = SubShaders.Count;
  39. for( int i = 0; i < count; i++ )
  40. {
  41. SubShaders[ i ].Destroy();
  42. }
  43. SubShaders.Clear();
  44. SubShaders = null;
  45. }
  46. }
  47. public class TemplateShaderInfoUtil
  48. {
  49. public static TemplateShaderInfo CreateShaderData( string body )
  50. {
  51. int nameBegin = body.IndexOf( TemplatesManager.TemplateShaderNameBeginTag );
  52. if( nameBegin < 0 )
  53. {
  54. // Not a template
  55. return null;
  56. }
  57. TemplateShaderInfo shaderData = null;
  58. //SHADER
  59. MatchCollection shaderMatch = Regex.Matches( body, "\\bShader\\b" );
  60. if( shaderMatch.Count > 0 )
  61. {
  62. //SUBSHADER
  63. MatchCollection subShaderMatch = Regex.Matches( body, TemplatesManager.TemplateMPSubShaderTag );
  64. int subShaderAmount = subShaderMatch.Count;
  65. if( subShaderAmount > 0 )
  66. {
  67. shaderData = new TemplateShaderInfo();
  68. shaderData.Body = body;
  69. int length = subShaderMatch[ 0 ].Index - shaderMatch[ 0 ].Groups[ 0 ].Index;
  70. shaderData.Properties = body.Substring( shaderMatch[ 0 ].Index, length );
  71. shaderData.PropertyStartIdx = body.IndexOf( TemplatesManager.TemplatePropertyTag );
  72. for( int subShaderIdx = 0; subShaderIdx < subShaderAmount; subShaderIdx++ )
  73. {
  74. TemplateSubShaderInfo subShaderData = new TemplateSubShaderInfo();
  75. int subshaderBeginIndex = subShaderMatch[ subShaderIdx ].Index;
  76. int subShaderEndIndex = ( subShaderIdx == ( subShaderAmount - 1 ) ) ? body.Length - 1 : subShaderMatch[ subShaderIdx + 1 ].Index;
  77. subShaderData.Data = body.Substring( subshaderBeginIndex, subShaderEndIndex - subshaderBeginIndex );
  78. subShaderData.StartIdx = subshaderBeginIndex;
  79. //PASS
  80. MatchCollection passMatch = Regex.Matches( subShaderData.Data, TemplatesManager.TemplatePassTagPattern );
  81. if( passMatch.Count == 0 )
  82. {
  83. passMatch = Regex.Matches( subShaderData.Data, TemplatesManager.TemplateMPPassTag );
  84. }
  85. int passCount = passMatch.Count;
  86. if( passCount > 0 )
  87. {
  88. int lastPassIndex = subShaderData.Data.LastIndexOf( TemplatesManager.TemplatePassesEndTag );
  89. if( lastPassIndex < 0 )
  90. {
  91. lastPassIndex = subShaderData.Data.Length - 1;
  92. }
  93. subShaderData.Modules = subShaderData.Data.Substring( 0, passMatch[ 0 ].Index );
  94. for( int passIdx = 0; passIdx < passCount; passIdx++ )
  95. {
  96. int passBeginIndex = passMatch[ passIdx ].Index;
  97. int passEndIdx = ( passIdx == ( passCount - 1 ) ) ? lastPassIndex : passMatch[ passIdx + 1 ].Index;
  98. TemplatePassInfo passData = new TemplatePassInfo();
  99. passData.Data = subShaderData.Data.Substring( passBeginIndex, passEndIdx - passBeginIndex );
  100. passData.GlobalStartIdx = subshaderBeginIndex + passBeginIndex;
  101. passData.LocalStartIdx = passBeginIndex;
  102. subShaderData.Passes.Add( passData );
  103. }
  104. shaderData.SubShaders.Add( subShaderData );
  105. }
  106. }
  107. }
  108. }
  109. return shaderData;
  110. }
  111. }
  112. }