MatrixParentNode.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. public class MatrixParentNode : PropertyNode
  9. {
  10. private readonly string[] AvailablePropertyTypeLabels = { PropertyType.Constant.ToString(), PropertyType.Global.ToString(), "Instanced" };
  11. private readonly int[] AvailablePropertyTypeValues = { (int)PropertyType.Constant, (int)PropertyType.Global , (int)PropertyType.InstancedProperty };
  12. private readonly string[] AvailablePropertyTypeLabelsSRP = { PropertyType.Constant.ToString() ,"CBuffer", PropertyType.Global.ToString() , "Instanced" };
  13. private readonly int[] AvailablePropertyTypeValuesSRP = { (int)PropertyType.Constant , (int)PropertyType.Property,( int)PropertyType.Global , (int)PropertyType.InstancedProperty };
  14. protected bool m_isEditingFields;
  15. protected bool m_showCBuffer = false;
  16. [SerializeField]
  17. protected Matrix4x4 m_defaultValue = Matrix4x4.identity;
  18. [SerializeField]
  19. protected Matrix4x4 m_materialValue = Matrix4x4.identity;
  20. [NonSerialized]
  21. protected Matrix4x4 m_previousValue;
  22. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  23. public MatrixParentNode() : base() { }
  24. public MatrixParentNode( int uniqueId, float x, float y, float width, float height ) : base( uniqueId, x, y, width, height ) { }
  25. protected override void CommonInit( int uniqueId )
  26. {
  27. base.CommonInit( uniqueId );
  28. m_freeType = false;
  29. m_showVariableMode = true;
  30. m_srpBatcherCompatible = true;
  31. }
  32. public override void AfterCommonInit()
  33. {
  34. base.AfterCommonInit();
  35. m_hasLeftDropdown = true;
  36. m_drawAttributes = false;
  37. m_availableAttribs.Clear();
  38. if( PaddingTitleLeft == 0 )
  39. {
  40. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  41. if( PaddingTitleRight == 0 )
  42. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  43. }
  44. }
  45. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  46. {
  47. base.OnNodeLogicUpdate( drawInfo );
  48. m_showCBuffer = m_containerGraph.IsSRP || m_containerGraph.CurrentShaderFunction != null;
  49. }
  50. protected void DrawParameterType()
  51. {
  52. PropertyType parameterType;
  53. if( m_showCBuffer )
  54. {
  55. parameterType = (PropertyType)EditorGUILayoutIntPopup( ParameterTypeStr , (int)m_currentParameterType , AvailablePropertyTypeLabelsSRP , AvailablePropertyTypeValuesSRP );
  56. }
  57. else
  58. {
  59. parameterType = (PropertyType)EditorGUILayoutIntPopup( ParameterTypeStr , (int)m_currentParameterType , AvailablePropertyTypeLabels , AvailablePropertyTypeValues );
  60. }
  61. if( parameterType != m_currentParameterType )
  62. {
  63. ChangeParameterType( parameterType );
  64. }
  65. }
  66. public override void Draw( DrawInfo drawInfo )
  67. {
  68. base.Draw( drawInfo );
  69. PropertyType parameterType;
  70. if( m_showCBuffer )
  71. {
  72. parameterType = (PropertyType)m_upperLeftWidget.DrawWidget( this , (int)m_currentParameterType , AvailablePropertyTypeLabelsSRP , AvailablePropertyTypeValuesSRP );
  73. }
  74. else
  75. {
  76. parameterType = (PropertyType)m_upperLeftWidget.DrawWidget( this , (int)m_currentParameterType , AvailablePropertyTypeLabels , AvailablePropertyTypeValues );
  77. }
  78. if( parameterType != m_currentParameterType )
  79. {
  80. ChangeParameterType( parameterType );
  81. }
  82. }
  83. public override void DrawMainPropertyBlock()
  84. {
  85. DrawParameterType();
  86. base.DrawMainPropertyBlock();
  87. }
  88. public override void Destroy()
  89. {
  90. base.Destroy();
  91. m_upperLeftWidget = null;
  92. }
  93. public override void OnMasterNodeReplaced( MasterNode newMasterNode )
  94. {
  95. base.OnMasterNodeReplaced( newMasterNode );
  96. if( m_containerGraph.IsStandardSurface || !m_containerGraph.IsSRP )
  97. {
  98. if( m_currentParameterType == PropertyType.Property )
  99. {
  100. m_currentParameterType = PropertyType.Global;
  101. }
  102. }
  103. }
  104. public override void SetGlobalValue() { Shader.SetGlobalMatrix( m_propertyName, m_defaultValue ); }
  105. public override void FetchGlobalValue() { m_materialValue = Shader.GetGlobalMatrix( m_propertyName ); }
  106. }
  107. }