summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Templates/TemplateVertexData.cs
blob: 3b82e0469aef4a2e1c3876e48e274662ce62949a (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using System;
using UnityEngine;

namespace AmplifyShaderEditor
{
	[Serializable]
	public class TemplateVertexData
	{
		[SerializeField]
		private TemplateSemantics m_semantics = TemplateSemantics.NONE;
		[SerializeField]
		private WirePortDataType m_dataType = WirePortDataType.OBJECT;
		[SerializeField]
		private string m_varName = string.Empty;
		[SerializeField]
		private TemplateInfoOnSematics m_dataInfo = TemplateInfoOnSematics.NONE;
		[SerializeField]
		private string m_dataSwizzle = string.Empty;
		[SerializeField]
		private bool m_available = false;
		[SerializeField]
		private string m_varNameWithSwizzle = string.Empty;
		[SerializeField]
		private bool m_isSingleComponent = true;
		[SerializeField]
		private bool m_excludeStructPrefix = false;
		[SerializeField]
		private string[] m_components = { "0", "0", "0", "0" };
		[SerializeField]
		private bool[] m_componentUsage = { false, false,false,false };

		public TemplateVertexData( TemplateSemantics semantics, WirePortDataType dataType, string varName )
		{
			m_semantics = semantics;
			m_dataType = dataType;
			m_varName = varName;
			m_varNameWithSwizzle = varName;
		}

		public TemplateVertexData( TemplateSemantics semantics, WirePortDataType dataType, string varName, string dataSwizzle )
		{
			m_semantics = semantics;
			m_dataType = dataType;
			m_varName = varName;
			m_dataSwizzle = dataSwizzle;
			m_varNameWithSwizzle = varName + dataSwizzle;
		}

		public TemplateVertexData( TemplateVertexData other )
		{
			m_semantics = other.m_semantics;
			m_dataType = other.m_dataType;
			m_varName = other.m_varName;
			m_dataInfo = other.m_dataInfo;
			m_dataSwizzle = other.m_dataSwizzle;
			m_available = other.m_available;
			m_varNameWithSwizzle = other.m_varNameWithSwizzle;
			m_isSingleComponent = other.IsSingleComponent;
			m_excludeStructPrefix = other.ExcludeStructPrefix;
			for( int i = 0; i < 4; i++ )
			{
				m_components[ i ] = other.Components[ i ];
			}
		}

		public void RegisterComponent( char channelId, string value )
		{
			int channelIdInt = -1;
			switch( channelId )
			{
				case 'r':
				case 'x': channelIdInt = 0; break;
				case 'g':
				case 'y': channelIdInt = 1; break;
				case 'b':
				case 'z': channelIdInt = 2; break;
				case 'a':
				case 'w': channelIdInt = 3; break;
			}

			if( channelId < 0 )
			{
				Debug.LogWarning( "Attempting to create interpolated data from invalid channel " + channelId );
				return;
			}

			RegisterComponent( channelIdInt, value );
		}

		public void RegisterComponent( int channelId, string value )
		{
			channelId = Mathf.Clamp( channelId, 0, 3 );
			m_components[ channelId ] = value;
			m_componentUsage[ channelId ] = true;
			m_isSingleComponent = false;
		}

		public void BuildVar( PrecisionType precisionType = PrecisionType.Float )
		{
			if( m_isSingleComponent )
				return;
			WirePortDataType dataType = WirePortDataType.FLOAT;
			if( m_componentUsage[ 3 ] )
			{
				dataType = WirePortDataType.FLOAT4;
			}
			else if( m_componentUsage[ 2 ] )
			{
				dataType = WirePortDataType.FLOAT3;
			}
			else if( m_componentUsage[ 1 ] )
			{
				dataType = WirePortDataType.FLOAT2;
			}
			
			string newVar = UIUtils.PrecisionWirePortToCgType( precisionType, dataType );
			newVar += "( ";
			switch( dataType )
			{
				default: newVar += "0"; break;
				case WirePortDataType.INT:
				case WirePortDataType.FLOAT:
				{
					newVar += "{0}."+Components[ 0 ];
				}
				break;
				case WirePortDataType.FLOAT2:
				{
					newVar +=	"{0}." + Components[ 0 ] + ", " +
								"{0}." + Components[ 1 ];
				}
				break;
				case WirePortDataType.FLOAT3:
				{
					newVar +=	"{0}." + Components[ 0 ] + ", " +
								"{0}." + Components[ 1 ] + ", " +
								"{0}." + Components[ 2 ];
				}
				break;
				case WirePortDataType.FLOAT4:
				case WirePortDataType.COLOR:
				{
					newVar +=	"{0}." + Components[ 0 ] + ", " +
								"{0}." + Components[ 1 ] + ", " +
								"{0}." + Components[ 2 ] + ", " +
								"{0}." + Components[ 3 ];
				}
				break;

			}
			newVar += " )";
			m_varName = newVar;
			m_varNameWithSwizzle = newVar;
		}

		public bool ExcludeStructPrefix { get { return m_excludeStructPrefix; } set { m_excludeStructPrefix = value; } }
		public bool IsSingleComponent { get { return m_isSingleComponent; } }
		public string[] Components { get { return m_components; } }
		public TemplateSemantics Semantics { get { return m_semantics; } }
		public WirePortDataType DataType { get { return m_dataType; } }
		public string VarName { get { return m_varName; } set { m_varName = value; m_varNameWithSwizzle = value + m_dataSwizzle; } }
		public string DataSwizzle { get { return m_dataSwizzle; } set { m_dataSwizzle = value; m_varNameWithSwizzle = m_varName + value; } }
		public TemplateInfoOnSematics DataInfo { get { return m_dataInfo; } set { m_dataInfo = value; } }
		public bool Available { get { return m_available; } set { m_available = value; } }
		public string VarNameWithSwizzle { get { return m_varNameWithSwizzle; } }
		public WirePortDataType SwizzleType
		{
			get
			{
				if ( string.IsNullOrEmpty( m_dataSwizzle ) )
					return m_dataType;

				WirePortDataType newType = m_dataType;
				switch ( m_dataSwizzle.Length )
				{
					case 2: newType = WirePortDataType.FLOAT;break;
					case 3: newType = WirePortDataType.FLOAT2; break;
					case 4: newType = WirePortDataType.FLOAT3; break;
					case 5: newType = WirePortDataType.FLOAT4; break;
				}

				return newType;
			}
		}

	}
}