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.

172 lines
5.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. using UnityEngine;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. public class TemplateSubShader
  11. {
  12. [SerializeField]
  13. private int m_idx = -1;
  14. [SerializeField]
  15. private List<TemplatePass> m_passes = new List<TemplatePass>();
  16. [SerializeField]
  17. private TemplateModulesData m_modules;
  18. [SerializeField]
  19. private string m_uniquePrefix;
  20. [SerializeField]
  21. private TemplatePropertyContainer m_templateProperties = new TemplatePropertyContainer();
  22. [SerializeField]
  23. private List<TemplateShaderPropertyData> m_availableShaderGlobals = new List<TemplateShaderPropertyData>();
  24. [SerializeField]
  25. private TemplateInfoContainer m_LODContainer = new TemplateInfoContainer();
  26. [SerializeField]
  27. private int m_passAmount = 0;
  28. [SerializeField]
  29. private int m_mainPass = -1;
  30. [SerializeField]
  31. private bool m_foundMainPassTag = false;
  32. [SerializeField]
  33. TemplateOptionsContainer m_customOptionsContainer = new TemplateOptionsContainer();
  34. public TemplateSubShader(TemplateMultiPass template, int subShaderIx, TemplateIdManager idManager, string uniquePrefix, TemplateSubShaderInfo subShaderInfo, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
  35. {
  36. m_idx = subShaderIx;
  37. m_uniquePrefix = uniquePrefix;
  38. FetchLOD( subShaderInfo.StartIdx, subShaderInfo.Modules );
  39. if( m_LODContainer.Index > -1 )
  40. {
  41. idManager.RegisterId( m_LODContainer.Index, uniquePrefix + "Module" + m_LODContainer.Id, m_LODContainer.Id );
  42. }
  43. m_customOptionsContainer = TemplateOptionsToolsHelper.GenerateOptionsContainer( true, subShaderInfo.Data );
  44. if( m_customOptionsContainer.Enabled )
  45. {
  46. idManager.RegisterId( m_customOptionsContainer.Index, uniquePrefix + m_customOptionsContainer.Body, m_customOptionsContainer.Body, true );
  47. }
  48. m_modules = new TemplateModulesData( m_customOptionsContainer, idManager, m_templateProperties, uniquePrefix + "Module", subShaderInfo.StartIdx, subShaderInfo.Modules, true );
  49. if( m_modules.SRPType == TemplateSRPType.HD )
  50. {
  51. m_modules.SRPIsPBR = subShaderInfo.Data.Contains( TemplateHelperFunctions.HDPBRTag );
  52. }
  53. Dictionary<string, TemplateShaderPropertyData> ownDuplicatesDict = new Dictionary<string, TemplateShaderPropertyData>( duplicatesHelper );
  54. TemplateHelperFunctions.CreateShaderGlobalsList( subShaderInfo.Modules, ref m_availableShaderGlobals, ref ownDuplicatesDict );
  55. if( m_modules.SRPType == TemplateSRPType.BuiltIn )
  56. {
  57. TemplateHelperFunctions.CheckUnityBuiltinGlobalMacros( subShaderInfo.Modules, ref m_availableShaderGlobals, ref ownDuplicatesDict );
  58. }
  59. m_passAmount = subShaderInfo.Passes.Count;
  60. //if( !m_modules.PassTag.IsValid )
  61. //{
  62. // m_modules.PassTag.StartIdx = subShaderData.Passes[ 0 ].GlobalStartIdx;
  63. // m_templateProperties.AddId( subShaderData.Data, m_modules.PassTag.Id, subShaderData.Passes[ 0 ].LocalStartIdx, m_modules.PassTag.SearchIndentation );
  64. // m_modules.PassTag.StartIdx -= m_templateProperties.PropertyDict[ m_modules.PassTag.Id ].Indentation.Length;
  65. // m_templateProperties.PropertyDict[ m_modules.PassTag.Id ].UseIndentationAtStart = true;
  66. // idManager.RegisterId( m_modules.PassTag.StartIdx, m_modules.UniquePrefix + m_modules.PassTag.Id, string.Empty );
  67. //}
  68. int firstVisible = -1;
  69. int currAddedPassIdx = 0;
  70. for( int passIdx = 0; passIdx < m_passAmount; passIdx++ )
  71. {
  72. TemplatePass newPass = new TemplatePass( template, this,subShaderIx, passIdx, idManager, uniquePrefix + "Pass" + passIdx, subShaderInfo.Passes[ passIdx ].GlobalStartIdx, subShaderInfo.Passes[ passIdx ], ref ownDuplicatesDict );
  73. if( newPass.AddToList )
  74. {
  75. if( newPass.IsMainPass && m_mainPass < 0 )
  76. {
  77. m_mainPass = currAddedPassIdx;
  78. m_foundMainPassTag = true;
  79. }
  80. else if(!newPass.IsInvisible && firstVisible < 0 )
  81. {
  82. firstVisible = currAddedPassIdx;
  83. }
  84. m_passes.Add( newPass );
  85. currAddedPassIdx++;
  86. }
  87. else
  88. {
  89. newPass.Destroy();
  90. newPass = null;
  91. }
  92. }
  93. if( m_mainPass < 0 )
  94. {
  95. // If no main pass was set then choose the first visible one
  96. m_mainPass = ( firstVisible < 0 ) ? 0 : firstVisible;
  97. m_passes[ m_mainPass ].IsMainPass = true;
  98. }
  99. ownDuplicatesDict.Clear();
  100. ownDuplicatesDict = null;
  101. }
  102. public void Destroy()
  103. {
  104. m_LODContainer = null;
  105. m_customOptionsContainer = null;
  106. m_templateProperties.Destroy();
  107. m_templateProperties = null;
  108. m_passes.Clear();
  109. m_passes = null;
  110. m_modules.Destroy();
  111. m_modules = null;
  112. m_availableShaderGlobals.Clear();
  113. m_availableShaderGlobals = null;
  114. }
  115. void FetchLOD( int offsetIdx, string body )
  116. {
  117. Match match = Regex.Match( body, TemplateHelperFunctions.SubShaderLODPattern );
  118. if( match != null && match.Groups.Count > 1 )
  119. {
  120. m_LODContainer.Id = match.Groups[ 0 ].Value;
  121. m_LODContainer.Data = match.Groups[ 1 ].Value;
  122. m_LODContainer.Index = offsetIdx + match.Index;
  123. }
  124. }
  125. public List<TemplatePass> Passes { get { return m_passes; } }
  126. public TemplateModulesData Modules { get { return m_modules; } }
  127. public string UniquePrefix { get { return m_uniquePrefix; } }
  128. public TemplatePropertyContainer TemplateProperties { get { return m_templateProperties; } }
  129. public List<TemplateShaderPropertyData> AvailableShaderGlobals { get { return m_availableShaderGlobals; } }
  130. public TemplateInfoContainer LODContainer { get { return m_LODContainer; } }
  131. public int PassAmount { get { return m_passAmount; } }
  132. public bool FoundMainPass { get { return m_foundMainPassTag; } }
  133. public int MainPass { get { return m_mainPass; } }
  134. public int Idx { get { return m_idx; } }
  135. public TemplateOptionsContainer CustomOptionsContainer { get { return m_customOptionsContainer; } }
  136. }
  137. }