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.

180 lines
4.7 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public class WeightedAvgNode : ParentNode
  10. {
  11. protected string[] AmountsStr = { "Layer 1", "Layer 2", "Layer 3", "Layer 4" };
  12. [SerializeField]
  13. protected int m_minimumSize = 1;
  14. [SerializeField]
  15. protected WirePortDataType m_mainDataType = WirePortDataType.FLOAT;
  16. [SerializeField]
  17. protected string[] m_inputData;
  18. [SerializeField]
  19. protected int m_activeCount = 0;
  20. protected override void CommonInit( int uniqueId )
  21. {
  22. base.CommonInit( uniqueId );
  23. AddInputPort( WirePortDataType.FLOAT, false, "Weights" );
  24. AddInputPort( WirePortDataType.FLOAT, false, AmountsStr[ 0 ] );
  25. AddInputPort( WirePortDataType.FLOAT, false, AmountsStr[ 1 ] );
  26. AddInputPort( WirePortDataType.FLOAT, false, AmountsStr[ 2 ] );
  27. AddInputPort( WirePortDataType.FLOAT, false, AmountsStr[ 3 ] );
  28. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  29. for( int i = 0; i < m_inputPorts.Count; i++ )
  30. {
  31. m_inputPorts[ i ].AddPortForbiddenTypes( WirePortDataType.FLOAT3x3,
  32. WirePortDataType.FLOAT4x4,
  33. WirePortDataType.SAMPLER1D,
  34. WirePortDataType.SAMPLER2D,
  35. WirePortDataType.SAMPLER3D,
  36. WirePortDataType.SAMPLERCUBE );
  37. }
  38. UpdateConnection( 0 );
  39. m_useInternalPortData = true;
  40. }
  41. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  42. {
  43. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  44. UpdateConnection( inputPortId );
  45. }
  46. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  47. {
  48. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  49. UpdateConnection( portId );
  50. }
  51. void UpdateInputPorts( int activePorts )
  52. {
  53. int idx = 1;
  54. for ( ; idx < m_minimumSize + activePorts; idx++ )
  55. {
  56. m_inputPorts[ idx ].Visible = true;
  57. }
  58. m_activeCount = idx - 1;
  59. for ( ; idx < m_inputPorts.Count; idx++ )
  60. {
  61. m_inputPorts[ idx ].Visible = false;
  62. }
  63. }
  64. protected void UpdateConnection( int portId )
  65. {
  66. if ( portId == 0 )
  67. {
  68. if( m_inputPorts[ portId ].IsConnected )
  69. m_inputPorts[ portId ].MatchPortToConnection();
  70. switch ( m_inputPorts[ 0 ].DataType )
  71. {
  72. case WirePortDataType.INT:
  73. case WirePortDataType.FLOAT:
  74. {
  75. UpdateInputPorts( 1 );
  76. m_previewMaterialPassId = 0;
  77. }
  78. break;
  79. case WirePortDataType.FLOAT2:
  80. {
  81. UpdateInputPorts( 2 );
  82. m_previewMaterialPassId = 1;
  83. }
  84. break;
  85. case WirePortDataType.FLOAT3:
  86. {
  87. UpdateInputPorts( 3 );
  88. m_previewMaterialPassId = 2;
  89. }
  90. break;
  91. case WirePortDataType.COLOR:
  92. case WirePortDataType.FLOAT4:
  93. {
  94. UpdateInputPorts( 4 );
  95. m_previewMaterialPassId = 3;
  96. }
  97. break;
  98. case WirePortDataType.OBJECT:
  99. case WirePortDataType.FLOAT3x3:
  100. case WirePortDataType.FLOAT4x4:
  101. {
  102. for ( int i = 1; i < m_inputPorts.Count; i++ )
  103. {
  104. m_inputPorts[ i ].Visible = false;
  105. }
  106. m_activeCount = 0;
  107. }
  108. break;
  109. }
  110. }
  111. //else
  112. //{
  113. // SetMainOutputType();
  114. //}
  115. SetMainOutputType();
  116. m_sizeIsDirty = true;
  117. }
  118. protected void SetMainOutputType()
  119. {
  120. m_mainDataType = WirePortDataType.OBJECT;
  121. int count = m_inputPorts.Count;
  122. for ( int i = 1; i < count; i++ )
  123. {
  124. if ( m_inputPorts[ i ].Visible )
  125. {
  126. WirePortDataType portType = m_inputPorts[ i ].IsConnected ? m_inputPorts[ i ].ConnectionType() : WirePortDataType.FLOAT;
  127. if ( m_mainDataType != portType &&
  128. UIUtils.GetPriority( portType ) > UIUtils.GetPriority( m_mainDataType ) )
  129. {
  130. m_mainDataType = portType;
  131. }
  132. }
  133. }
  134. for( int i = 1; i < count; i++ )
  135. {
  136. if( m_inputPorts[ i ].Visible )
  137. {
  138. m_inputPorts[ i ].ChangeType( m_mainDataType, false );
  139. }
  140. }
  141. m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  142. }
  143. protected void GetInputData( ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  144. {
  145. m_inputData[ 0 ] = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  146. for ( int i = 1; i < m_inputPorts.Count; i++ )
  147. {
  148. if ( m_inputPorts[ i ].Visible )
  149. {
  150. m_inputData[ i ] = m_inputPorts[ i ].GeneratePortInstructions( ref dataCollector );
  151. }
  152. }
  153. }
  154. public override void ReadInputDataFromString( ref string[] nodeParams )
  155. {
  156. base.ReadInputDataFromString( ref nodeParams );
  157. UpdateConnection( 0 );
  158. }
  159. }
  160. }