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.

156 lines
5.0 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. public TemplateSubShader( int subShaderIx, TemplateIdManager idManager, string uniquePrefix, TemplateSubShaderInfo subShaderData, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
  33. {
  34. m_idx = subShaderIx;
  35. m_uniquePrefix = uniquePrefix;
  36. FetchLOD( subShaderData.StartIdx, subShaderData.Modules );
  37. if( m_LODContainer.Index > -1 )
  38. {
  39. idManager.RegisterId( m_LODContainer.Index, uniquePrefix + "Module" + m_LODContainer.Id, m_LODContainer.Id );
  40. }
  41. m_modules = new TemplateModulesData( idManager, m_templateProperties, uniquePrefix + "Module", subShaderData.StartIdx, subShaderData.Modules, true );
  42. if( m_modules.SRPType == TemplateSRPType.HD )
  43. {
  44. m_modules.SRPIsPBR = subShaderData.Data.Contains( TemplateHelperFunctions.HDPBRTag );
  45. }
  46. Dictionary<string, TemplateShaderPropertyData> ownDuplicatesDict = new Dictionary<string, TemplateShaderPropertyData>( duplicatesHelper );
  47. TemplateHelperFunctions.CreateShaderGlobalsList( subShaderData.Modules, ref m_availableShaderGlobals, ref ownDuplicatesDict );
  48. m_passAmount = subShaderData.Passes.Count;
  49. //if( !m_modules.PassTag.IsValid )
  50. //{
  51. // m_modules.PassTag.StartIdx = subShaderData.Passes[ 0 ].GlobalStartIdx;
  52. // m_templateProperties.AddId( subShaderData.Data, m_modules.PassTag.Id, subShaderData.Passes[ 0 ].LocalStartIdx, m_modules.PassTag.SearchIndentation );
  53. // m_modules.PassTag.StartIdx -= m_templateProperties.PropertyDict[ m_modules.PassTag.Id ].Indentation.Length;
  54. // m_templateProperties.PropertyDict[ m_modules.PassTag.Id ].UseIndentationAtStart = true;
  55. // idManager.RegisterId( m_modules.PassTag.StartIdx, m_modules.UniquePrefix + m_modules.PassTag.Id, string.Empty );
  56. //}
  57. int firstVisible = -1;
  58. int currAddedPassIdx = 0;
  59. for( int passIdx = 0; passIdx < m_passAmount; passIdx++ )
  60. {
  61. TemplatePass newPass = new TemplatePass( m_modules,subShaderIx, passIdx, idManager, uniquePrefix + "Pass" + passIdx, subShaderData.Passes[ passIdx ].GlobalStartIdx, subShaderData.Passes[ passIdx ], ref ownDuplicatesDict );
  62. if( newPass.AddToList )
  63. {
  64. if( newPass.IsMainPass && m_mainPass < 0 )
  65. {
  66. m_mainPass = currAddedPassIdx;
  67. m_foundMainPassTag = true;
  68. }
  69. else if(!newPass.IsInvisible && firstVisible < 0 )
  70. {
  71. firstVisible = currAddedPassIdx;
  72. }
  73. m_passes.Add( newPass );
  74. currAddedPassIdx++;
  75. }
  76. else
  77. {
  78. newPass.Destroy();
  79. newPass = null;
  80. }
  81. }
  82. if( m_mainPass < 0 )
  83. {
  84. // If no main pass was set then choose the first visible one
  85. m_mainPass = ( firstVisible < 0 ) ? 0 : firstVisible;
  86. m_passes[ m_mainPass ].IsMainPass = true;
  87. }
  88. ownDuplicatesDict.Clear();
  89. ownDuplicatesDict = null;
  90. }
  91. public void Destroy()
  92. {
  93. m_LODContainer = null;
  94. m_templateProperties.Destroy();
  95. m_templateProperties = null;
  96. m_passes.Clear();
  97. m_passes = null;
  98. m_modules.Destroy();
  99. m_modules = null;
  100. m_availableShaderGlobals.Clear();
  101. m_availableShaderGlobals = null;
  102. }
  103. void FetchLOD( int offsetIdx, string body )
  104. {
  105. Match match = Regex.Match( body, TemplateHelperFunctions.SubShaderLODPattern );
  106. if( match != null && match.Groups.Count > 1 )
  107. {
  108. m_LODContainer.Id = match.Groups[ 0 ].Value;
  109. m_LODContainer.Data = match.Groups[ 1 ].Value;
  110. m_LODContainer.Index = offsetIdx + match.Index;
  111. }
  112. }
  113. public List<TemplatePass> Passes { get { return m_passes; } }
  114. public TemplateModulesData Modules { get { return m_modules; } }
  115. public string UniquePrefix { get { return m_uniquePrefix; } }
  116. public TemplatePropertyContainer TemplateProperties { get { return m_templateProperties; } }
  117. public List<TemplateShaderPropertyData> AvailableShaderGlobals { get { return m_availableShaderGlobals; } }
  118. public TemplateInfoContainer LODContainer { get { return m_LODContainer; } }
  119. public int PassAmount { get { return m_passAmount; } }
  120. public bool FoundMainPass { get { return m_foundMainPassTag; } }
  121. public int MainPass { get { return m_mainPass; } }
  122. public int Idx { get { return m_idx; } }
  123. }
  124. }