summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Templates/TemplateShaderModelModule.cs
blob: e1de14b25db5e64051952070aa858608d84adfb7 (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
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using System;
using UnityEngine;
using UnityEditor;

namespace AmplifyShaderEditor
{
	[Serializable]
	public sealed class TemplateShaderModelModule : TemplateModuleParent
	{
		private const string ShaderModelStr = "Shader Model";
		private const string ShaderModelFormatStr = "#pragma target ";
		private const string ShaderModelEncapsulateFormatStr = "CGINCLUDE\n#pragma target {0}\nENDCG";

		[SerializeField]
		private int m_shaderModelIdx = 2;

		[SerializeField]
		private bool m_encapsulateOnCGInlude = false;

		public TemplateShaderModelModule() : base("Shader Model"){ }
		
		public override void Draw( UndoParentNode owner, bool style = true )
		{
			EditorGUI.BeginChangeCheck();
			m_shaderModelIdx = owner.EditorGUILayoutPopup( ShaderModelStr, m_shaderModelIdx, TemplateHelperFunctions.AvailableShaderModels );
			if( EditorGUI.EndChangeCheck() )
			{
				m_isDirty = true;
			}
		}

		public void CopyFrom( TemplateShaderModelModule other , bool allData )
		{
			if( allData )
			{
				m_independentModule = other.IndependentModule;
				m_encapsulateOnCGInlude = other.EncapsulateOnCGInlude;
			}

			m_shaderModelIdx = other.CurrentShaderModelIdx;
		}

		public override void ReadFromString( ref uint index, ref string[] nodeParams )
		{
			bool validDataOnMeta = m_validData;
			if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
			{
				validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
			}
			
			if( validDataOnMeta )
				m_shaderModelIdx = Convert.ToInt32( nodeParams[ index++ ] );
		}

		public override void WriteToString( ref string nodeInfo )
		{
			IOUtils.AddFieldValueToString( ref nodeInfo, m_validData );
			if( m_validData )
				IOUtils.AddFieldValueToString( ref nodeInfo, m_shaderModelIdx );
		}

		public override string GenerateShaderData( bool isSubShader )
		{
			if( m_encapsulateOnCGInlude )
			{
				return string.Format( ShaderModelEncapsulateFormatStr, TemplateHelperFunctions.AvailableShaderModels[ m_shaderModelIdx ] );
			}
			else
			{
				return ShaderModelFormatStr + TemplateHelperFunctions.AvailableShaderModels[ m_shaderModelIdx ];
			}
		}

		public void ConfigureFromTemplateData( TemplateShaderModelData data )
		{
			bool newValidData = ( data.DataCheck == TemplateDataCheck.Valid );

			if( newValidData && m_validData != newValidData )
			{
				m_independentModule = data.IndependentModule;

				if( TemplateHelperFunctions.ShaderModelToArrayIdx.ContainsKey( data.Value ) )
				{
					m_shaderModelIdx = TemplateHelperFunctions.ShaderModelToArrayIdx[ data.Value ];
				}
				m_encapsulateOnCGInlude = data.Encapsulate;
			}

			m_validData = newValidData;
		}

		public int CurrentShaderModelIdx { get { return m_shaderModelIdx; } }
		public string CurrentShaderModel { get { return TemplateHelperFunctions.AvailableShaderModels[ m_shaderModelIdx ]; } }
		public bool EncapsulateOnCGInlude { get { return m_encapsulateOnCGInlude; } }
		public int InterpolatorAmount
		{
			get
			{
				return TemplateHelperFunctions.AvailableInterpolators[ TemplateHelperFunctions.AvailableShaderModels[ m_shaderModelIdx ] ];
			}
		}
		
	}
}