WeightedAvgNode.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. WirePortDataType.SAMPLER2DARRAY,
  38. WirePortDataType.SAMPLERSTATE );
  39. }
  40. UpdateConnection( 0 );
  41. m_useInternalPortData = true;
  42. }
  43. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  44. {
  45. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  46. UpdateConnection( inputPortId );
  47. }
  48. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  49. {
  50. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  51. UpdateConnection( portId );
  52. }
  53. void UpdateInputPorts( int activePorts )
  54. {
  55. int idx = 1;
  56. for ( ; idx < m_minimumSize + activePorts; idx++ )
  57. {
  58. m_inputPorts[ idx ].Visible = true;
  59. }
  60. m_activeCount = idx - 1;
  61. for ( ; idx < m_inputPorts.Count; idx++ )
  62. {
  63. m_inputPorts[ idx ].Visible = false;
  64. }
  65. }
  66. protected void UpdateConnection( int portId )
  67. {
  68. if ( portId == 0 )
  69. {
  70. if( m_inputPorts[ portId ].IsConnected )
  71. m_inputPorts[ portId ].MatchPortToConnection();
  72. switch ( m_inputPorts[ 0 ].DataType )
  73. {
  74. case WirePortDataType.INT:
  75. case WirePortDataType.FLOAT:
  76. {
  77. UpdateInputPorts( 1 );
  78. m_previewMaterialPassId = 0;
  79. }
  80. break;
  81. case WirePortDataType.FLOAT2:
  82. {
  83. UpdateInputPorts( 2 );
  84. m_previewMaterialPassId = 1;
  85. }
  86. break;
  87. case WirePortDataType.FLOAT3:
  88. {
  89. UpdateInputPorts( 3 );
  90. m_previewMaterialPassId = 2;
  91. }
  92. break;
  93. case WirePortDataType.COLOR:
  94. case WirePortDataType.FLOAT4:
  95. {
  96. UpdateInputPorts( 4 );
  97. m_previewMaterialPassId = 3;
  98. }
  99. break;
  100. case WirePortDataType.OBJECT:
  101. case WirePortDataType.FLOAT3x3:
  102. case WirePortDataType.FLOAT4x4:
  103. {
  104. for ( int i = 1; i < m_inputPorts.Count; i++ )
  105. {
  106. m_inputPorts[ i ].Visible = false;
  107. }
  108. m_activeCount = 0;
  109. }
  110. break;
  111. }
  112. }
  113. //else
  114. //{
  115. // SetMainOutputType();
  116. //}
  117. SetMainOutputType();
  118. m_sizeIsDirty = true;
  119. }
  120. protected void SetMainOutputType()
  121. {
  122. m_mainDataType = WirePortDataType.OBJECT;
  123. int count = m_inputPorts.Count;
  124. for ( int i = 1; i < count; i++ )
  125. {
  126. if ( m_inputPorts[ i ].Visible )
  127. {
  128. WirePortDataType portType = m_inputPorts[ i ].IsConnected ? m_inputPorts[ i ].ConnectionType() : WirePortDataType.FLOAT;
  129. if ( m_mainDataType != portType &&
  130. UIUtils.GetPriority( portType ) > UIUtils.GetPriority( m_mainDataType ) )
  131. {
  132. m_mainDataType = portType;
  133. }
  134. }
  135. }
  136. for( int i = 1; i < count; i++ )
  137. {
  138. if( m_inputPorts[ i ].Visible )
  139. {
  140. m_inputPorts[ i ].ChangeType( m_mainDataType, false );
  141. }
  142. }
  143. m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  144. }
  145. protected void GetInputData( ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  146. {
  147. m_inputData[ 0 ] = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  148. for ( int i = 1; i < m_inputPorts.Count; i++ )
  149. {
  150. if ( m_inputPorts[ i ].Visible )
  151. {
  152. m_inputData[ i ] = m_inputPorts[ i ].GeneratePortInstructions( ref dataCollector );
  153. }
  154. }
  155. }
  156. public override void ReadInputDataFromString( ref string[] nodeParams )
  157. {
  158. base.ReadInputDataFromString( ref nodeParams );
  159. UpdateConnection( 0 );
  160. }
  161. }
  162. }