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.

131 lines
3.9 KiB

  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. namespace AmplifyShaderEditor
  5. {
  6. public enum eVectorFromMatrixMode
  7. {
  8. Row,
  9. Column
  10. }
  11. [Serializable]
  12. [NodeAttributes( "Vector From Matrix", "Matrix Operators", "Retrieve vector data from a matrix" )]
  13. public sealed class VectorFromMatrixNode : ParentNode
  14. {
  15. private const string IndexStr = "Index";
  16. private const string ModeStr = "Mode";
  17. [SerializeField]
  18. private eVectorFromMatrixMode m_mode = eVectorFromMatrixMode.Row;
  19. [SerializeField]
  20. private int m_index = 0;
  21. [SerializeField]
  22. private int m_maxIndex = 3;
  23. protected override void CommonInit( int uniqueId )
  24. {
  25. base.CommonInit( uniqueId );
  26. AddInputPort( WirePortDataType.FLOAT4x4, false, Constants.EmptyPortValue );
  27. m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.FLOAT3x3, WirePortDataType.FLOAT4x4 );
  28. AddOutputVectorPorts( WirePortDataType.FLOAT4, "XYZW" );
  29. m_useInternalPortData = true;
  30. m_autoWrapProperties = true;
  31. }
  32. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  33. {
  34. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  35. UpdatePorts();
  36. }
  37. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  38. {
  39. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  40. UpdatePorts();
  41. }
  42. void UpdatePorts()
  43. {
  44. m_inputPorts[ 0 ].MatchPortToConnection();
  45. if ( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT3x3 )
  46. {
  47. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
  48. m_outputPorts[ 0 ].Name = "XYZ";
  49. m_maxIndex = 2;
  50. m_outputPorts[ 4 ].Visible = false;
  51. }
  52. else
  53. {
  54. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
  55. m_outputPorts[ 0 ].Name = "XYZW";
  56. m_maxIndex = 3;
  57. m_outputPorts[ 4 ].Visible = true;
  58. }
  59. m_sizeIsDirty = true;
  60. }
  61. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  62. {
  63. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  64. if ( m_inputPorts[ 0 ].DataType != WirePortDataType.FLOAT4x4 &&
  65. m_inputPorts[ 0 ].DataType != WirePortDataType.FLOAT3x3 )
  66. {
  67. value = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), value, m_inputPorts[ 0 ].DataType, WirePortDataType.FLOAT4x4, value );
  68. }
  69. if ( m_mode == eVectorFromMatrixMode.Row )
  70. {
  71. value += "[" + m_index + "]";
  72. }
  73. else
  74. {
  75. string formatStr = value + "[{0}]" + "[" + m_index + "]";
  76. int count = 4;
  77. if ( m_inputPorts[ 0 ].DataType != WirePortDataType.FLOAT3x3 )
  78. {
  79. value = "float4( ";
  80. }
  81. else
  82. {
  83. count = 3;
  84. value = "float3( ";
  85. }
  86. for ( int i = 0; i < count; i++ )
  87. {
  88. value += string.Format( formatStr, i );
  89. if ( i != ( count - 1 ) )
  90. {
  91. value += ",";
  92. }
  93. }
  94. value += " )";
  95. }
  96. return GetOutputVectorItem( 0, outputId, value );
  97. }
  98. public override void DrawProperties()
  99. {
  100. m_mode = (eVectorFromMatrixMode)EditorGUILayoutEnumPopup( ModeStr, m_mode );
  101. m_index = EditorGUILayoutIntSlider( IndexStr, m_index, 0, m_maxIndex );
  102. base.DrawProperties();
  103. }
  104. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  105. {
  106. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  107. IOUtils.AddFieldValueToString( ref nodeInfo, m_mode );
  108. IOUtils.AddFieldValueToString( ref nodeInfo, m_index );
  109. }
  110. public override void ReadFromString( ref string[] nodeParams )
  111. {
  112. base.ReadFromString( ref nodeParams );
  113. m_mode = ( eVectorFromMatrixMode ) Enum.Parse( typeof( eVectorFromMatrixMode ), GetCurrentParam( ref nodeParams ) );
  114. m_index = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  115. }
  116. }
  117. }