Assignment for RMIT Mixed Reality in 2020
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.

128 lines
3.4 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public sealed class TemplateAlphaToMaskModule : TemplateModuleParent
  10. {
  11. private const string AlphaToMaskFormatStr = "AlphaToMask ";
  12. private const string AlphaToMaskFormat = "AlphaToMask {0}";
  13. public TemplateAlphaToMaskModule() : base( "Alpha To Coverage" ) { }
  14. private static readonly string AlphaToMaskStr = "Alpha To Coverage";
  15. [SerializeField]
  16. private bool m_alphaToMask = false;
  17. [SerializeField]
  18. private InlineProperty m_inlineAlphaToMask = new InlineProperty();
  19. public void CopyFrom( TemplateAlphaToMaskModule other , bool allData )
  20. {
  21. if( allData )
  22. m_independentModule = other.IndependentModule;
  23. m_alphaToMask = other.AlphaToMask;
  24. m_inlineAlphaToMask.CopyFrom( other.AlphaToMaskInlineProperty );
  25. }
  26. public void ConfigureFromTemplateData( TemplateAlphaToMaskData data )
  27. {
  28. bool newValidData = ( data.DataCheck == TemplateDataCheck.Valid );
  29. if( newValidData && m_validData != newValidData )
  30. {
  31. m_independentModule = data.IndependentModule;
  32. if( string.IsNullOrEmpty( data.InlineData ) )
  33. {
  34. m_alphaToMask = data.AlphaToMaskData;
  35. m_inlineAlphaToMask.IntValue = m_alphaToMask ? 1 : 0;
  36. m_inlineAlphaToMask.ResetProperty();
  37. }
  38. else
  39. {
  40. m_inlineAlphaToMask.SetInlineByName( data.InlineData );
  41. }
  42. }
  43. m_validData = newValidData;
  44. }
  45. public override void Draw( UndoParentNode owner, bool style = true )
  46. {
  47. EditorGUI.BeginChangeCheck();
  48. m_inlineAlphaToMask.CustomDrawer( ref owner, ( x ) => { m_alphaToMask = owner.EditorGUILayoutToggle( AlphaToMaskStr, m_alphaToMask ); }, AlphaToMaskStr );
  49. if( EditorGUI.EndChangeCheck() )
  50. {
  51. m_inlineAlphaToMask.IntValue = m_alphaToMask ? 1 : 0;
  52. m_isDirty = true;
  53. }
  54. }
  55. public override void ReadFromString( ref uint index, ref string[] nodeParams )
  56. {
  57. bool validDataOnMeta = m_validData;
  58. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  59. {
  60. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  61. }
  62. if( validDataOnMeta )
  63. {
  64. if( UIUtils.CurrentShaderVersion() < 15304 )
  65. {
  66. m_alphaToMask = Convert.ToBoolean( nodeParams[ index++ ] );
  67. m_inlineAlphaToMask.IntValue = m_alphaToMask ? 1 : 0;
  68. }
  69. else
  70. {
  71. m_inlineAlphaToMask.ReadFromString( ref index, ref nodeParams );
  72. m_alphaToMask = m_inlineAlphaToMask.IntValue == 1;
  73. }
  74. }
  75. }
  76. public override void WriteToString( ref string nodeInfo )
  77. {
  78. IOUtils.AddFieldValueToString( ref nodeInfo, m_validData );
  79. if( m_validData )
  80. {
  81. m_inlineAlphaToMask.WriteToString( ref nodeInfo );
  82. }
  83. }
  84. public override string GenerateShaderData( bool isSubShader )
  85. {
  86. return AlphaToMaskFormatStr + m_inlineAlphaToMask.GetValueOrProperty( m_alphaToMask ? "On" : "Off");
  87. }
  88. public override void Destroy()
  89. {
  90. base.Destroy();
  91. m_inlineAlphaToMask = null;
  92. }
  93. //public string CurrentAlphaToMask
  94. //{
  95. // get { return string.Format( AlphaToMaskFormat, m_alphaToMask ? "On" : "Off" ); }
  96. //}
  97. public bool AlphaToMask
  98. {
  99. get { return m_alphaToMask; }
  100. set
  101. {
  102. m_alphaToMask = value;
  103. m_inlineAlphaToMask.IntValue = value ? 1 : 0;
  104. m_inlineAlphaToMask.Active = false;
  105. }
  106. }
  107. public InlineProperty AlphaToMaskInlineProperty { get { return m_inlineAlphaToMask; } }
  108. }
  109. }