summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Templates/TemplateSubShader.cs
blob: 6dc3f834c621065497c54fb252f761b9bce665b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

namespace AmplifyShaderEditor
{
	[Serializable]
	public class TemplateSubShader
	{
		[SerializeField]
		private int m_idx = -1;

		[SerializeField]
		private List<TemplatePass> m_passes = new List<TemplatePass>();

		[SerializeField]
		private TemplateModulesData m_modules;

		[SerializeField]
		private string m_uniquePrefix;

		[SerializeField]
		private TemplatePropertyContainer m_templateProperties = new TemplatePropertyContainer();

		[SerializeField]
		private List<TemplateShaderPropertyData> m_availableShaderGlobals = new List<TemplateShaderPropertyData>();

		[SerializeField]
		private TemplateInfoContainer m_LODContainer = new TemplateInfoContainer();

		[SerializeField]
		private int m_passAmount = 0;

		[SerializeField]
		private int m_mainPass = -1;

		[SerializeField]
		private bool m_foundMainPassTag = false;

		[SerializeField]
		TemplateOptionsContainer m_customOptionsContainer = new TemplateOptionsContainer();

		public TemplateSubShader(TemplateMultiPass template, int subShaderIx, TemplateIdManager idManager, string uniquePrefix, TemplateSubShaderInfo subShaderInfo, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
		{
			m_idx = subShaderIx;

			m_uniquePrefix = uniquePrefix;

			FetchLOD( subShaderInfo.StartIdx, subShaderInfo.Modules );
			if( m_LODContainer.Index > -1 )
			{
				idManager.RegisterId( m_LODContainer.Index, uniquePrefix + "Module" + m_LODContainer.Id, m_LODContainer.Id );
			}

			m_customOptionsContainer = TemplateOptionsToolsHelper.GenerateOptionsContainer( true, subShaderInfo.Data );
			if( m_customOptionsContainer.Enabled )
			{
				idManager.RegisterId( m_customOptionsContainer.Index, uniquePrefix + m_customOptionsContainer.Body, m_customOptionsContainer.Body, true );
			}

			m_modules = new TemplateModulesData( m_customOptionsContainer, idManager, m_templateProperties, uniquePrefix + "Module", subShaderInfo.StartIdx, subShaderInfo.Modules, true );
			if( m_modules.SRPType == TemplateSRPType.HD )
			{
				m_modules.SRPIsPBR = subShaderInfo.Data.Contains( TemplateHelperFunctions.HDPBRTag );
			}

			Dictionary<string, TemplateShaderPropertyData> ownDuplicatesDict = new Dictionary<string, TemplateShaderPropertyData>( duplicatesHelper );

			TemplateHelperFunctions.CreateShaderGlobalsList( subShaderInfo.Modules, ref m_availableShaderGlobals, ref ownDuplicatesDict );

			m_passAmount = subShaderInfo.Passes.Count;
			
			//if( !m_modules.PassTag.IsValid )
			//{
			//	m_modules.PassTag.StartIdx = subShaderData.Passes[ 0 ].GlobalStartIdx;
			//	m_templateProperties.AddId( subShaderData.Data, m_modules.PassTag.Id, subShaderData.Passes[ 0 ].LocalStartIdx, m_modules.PassTag.SearchIndentation );
			//	m_modules.PassTag.StartIdx -= m_templateProperties.PropertyDict[ m_modules.PassTag.Id ].Indentation.Length;
			//	m_templateProperties.PropertyDict[ m_modules.PassTag.Id ].UseIndentationAtStart = true;
			//	idManager.RegisterId( m_modules.PassTag.StartIdx, m_modules.UniquePrefix + m_modules.PassTag.Id, string.Empty );
			//}
			
			int firstVisible = -1;
			int currAddedPassIdx = 0;
			for( int passIdx = 0; passIdx < m_passAmount; passIdx++ )
			{
				TemplatePass newPass = new TemplatePass( template, this,subShaderIx, passIdx, idManager, uniquePrefix + "Pass" + passIdx, subShaderInfo.Passes[ passIdx ].GlobalStartIdx, subShaderInfo.Passes[ passIdx ], ref ownDuplicatesDict );
				if( newPass.AddToList )
				{
					if( newPass.IsMainPass && m_mainPass < 0  )
					{
						m_mainPass = currAddedPassIdx;
						m_foundMainPassTag = true;
					}
					else if(!newPass.IsInvisible && firstVisible < 0 )
					{
						firstVisible = currAddedPassIdx;
					}

					m_passes.Add( newPass );
					currAddedPassIdx++;
				}
				else
				{
					newPass.Destroy();
					newPass = null;
				}

			}

			if( m_mainPass < 0 )
			{
				// If no main pass was set then choose the first visible one
				m_mainPass = ( firstVisible < 0 ) ? 0 : firstVisible;
				m_passes[ m_mainPass ].IsMainPass = true;
			}

			ownDuplicatesDict.Clear();
			ownDuplicatesDict = null;
		}

		public void Destroy()
		{
			m_LODContainer = null;

			m_customOptionsContainer = null;

			m_templateProperties.Destroy();
			m_templateProperties = null;

			m_passes.Clear();
			m_passes = null;

			m_modules.Destroy();
			m_modules = null;

			m_availableShaderGlobals.Clear();
			m_availableShaderGlobals = null;

		}

		void FetchLOD( int offsetIdx, string body )
		{
			Match match = Regex.Match( body, TemplateHelperFunctions.SubShaderLODPattern );
			if( match != null && match.Groups.Count > 1 )
			{
				m_LODContainer.Id = match.Groups[ 0 ].Value;
				m_LODContainer.Data = match.Groups[ 1 ].Value;
				m_LODContainer.Index = offsetIdx + match.Index;
			}
		}
		
		public List<TemplatePass> Passes { get { return m_passes; } }
		public TemplateModulesData Modules { get { return m_modules; } }
		public string UniquePrefix { get { return m_uniquePrefix; } }
		public TemplatePropertyContainer TemplateProperties { get { return m_templateProperties; } }
		public List<TemplateShaderPropertyData> AvailableShaderGlobals { get { return m_availableShaderGlobals; } }
		public TemplateInfoContainer LODContainer { get { return m_LODContainer; } }
		public int PassAmount { get { return m_passAmount; } }
		public bool FoundMainPass { get { return m_foundMainPassTag; } }
		public int MainPass { get { return m_mainPass; } }
		public int Idx { get { return m_idx; } }
		public TemplateOptionsContainer CustomOptionsContainer { get { return m_customOptionsContainer; } }
	}
}