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.

106 lines
3.0 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public sealed class TemplateShaderModelModule : TemplateModuleParent
  10. {
  11. private const string ShaderModelStr = "Shader Model";
  12. private const string ShaderModelFormatStr = "#pragma target ";
  13. private const string ShaderModelEncapsulateFormatStr = "CGINCLUDE\n#pragma target {0}\nENDCG";
  14. [SerializeField]
  15. private int m_shaderModelIdx = 2;
  16. [SerializeField]
  17. private bool m_encapsulateOnCGInlude = false;
  18. public TemplateShaderModelModule() : base("Shader Model"){ }
  19. public override void Draw( UndoParentNode owner, bool style = true )
  20. {
  21. EditorGUI.BeginChangeCheck();
  22. m_shaderModelIdx = owner.EditorGUILayoutPopup( ShaderModelStr, m_shaderModelIdx, TemplateHelperFunctions.AvailableShaderModels );
  23. if( EditorGUI.EndChangeCheck() )
  24. {
  25. m_isDirty = true;
  26. }
  27. }
  28. public void CopyFrom( TemplateShaderModelModule other , bool allData )
  29. {
  30. if( allData )
  31. {
  32. m_independentModule = other.IndependentModule;
  33. m_encapsulateOnCGInlude = other.EncapsulateOnCGInlude;
  34. }
  35. m_shaderModelIdx = other.CurrentShaderModel;
  36. }
  37. public override void ReadFromString( ref uint index, ref string[] nodeParams )
  38. {
  39. bool validDataOnMeta = m_validData;
  40. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  41. {
  42. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  43. }
  44. if( validDataOnMeta )
  45. m_shaderModelIdx = Convert.ToInt32( nodeParams[ index++ ] );
  46. }
  47. public override void WriteToString( ref string nodeInfo )
  48. {
  49. IOUtils.AddFieldValueToString( ref nodeInfo, m_validData );
  50. if( m_validData )
  51. IOUtils.AddFieldValueToString( ref nodeInfo, m_shaderModelIdx );
  52. }
  53. public override string GenerateShaderData( bool isSubShader )
  54. {
  55. if( m_encapsulateOnCGInlude )
  56. {
  57. return string.Format( ShaderModelEncapsulateFormatStr, TemplateHelperFunctions.AvailableShaderModels[ m_shaderModelIdx ] );
  58. }
  59. else
  60. {
  61. return ShaderModelFormatStr + TemplateHelperFunctions.AvailableShaderModels[ m_shaderModelIdx ];
  62. }
  63. }
  64. public void ConfigureFromTemplateData( TemplateShaderModelData data )
  65. {
  66. bool newValidData = ( data.DataCheck == TemplateDataCheck.Valid );
  67. if( newValidData && m_validData != newValidData )
  68. {
  69. m_independentModule = data.IndependentModule;
  70. if( TemplateHelperFunctions.ShaderModelToArrayIdx.ContainsKey( data.Value ) )
  71. {
  72. m_shaderModelIdx = TemplateHelperFunctions.ShaderModelToArrayIdx[ data.Value ];
  73. }
  74. m_encapsulateOnCGInlude = data.Encapsulate;
  75. }
  76. m_validData = newValidData;
  77. }
  78. public int CurrentShaderModel { get { return m_shaderModelIdx; } }
  79. public bool EncapsulateOnCGInlude { get { return m_encapsulateOnCGInlude; } }
  80. public int InterpolatorAmount
  81. {
  82. get
  83. {
  84. return TemplateHelperFunctions.AvailableInterpolators[ TemplateHelperFunctions.AvailableShaderModels[ m_shaderModelIdx ] ];
  85. }
  86. }
  87. }
  88. }