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.

175 lines
6.0 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. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Multiply", "Math Operators", "Multiplication of two or more values ( A * B * .. )\nIt also handles Matrices multiplication", null, KeyCode.M )]
  9. public sealed class SimpleMultiplyOpNode : DynamicTypeNode
  10. {
  11. protected override void CommonInit( int uniqueId )
  12. {
  13. m_dynamicRestrictions = new WirePortDataType[]
  14. {
  15. WirePortDataType.OBJECT,
  16. WirePortDataType.FLOAT,
  17. WirePortDataType.FLOAT2,
  18. WirePortDataType.FLOAT3,
  19. WirePortDataType.FLOAT4,
  20. WirePortDataType.COLOR,
  21. WirePortDataType.FLOAT3x3,
  22. WirePortDataType.FLOAT4x4,
  23. WirePortDataType.INT
  24. };
  25. base.CommonInit( uniqueId );
  26. m_extensibleInputPorts = true;
  27. m_vectorMatrixOps = true;
  28. m_previewShaderGUID = "1ba1e43e86415ff4bbdf4d81dfcf035b";
  29. }
  30. public override void SetPreviewInputs()
  31. {
  32. base.SetPreviewInputs();
  33. int count = 0;
  34. int inputCount = m_inputPorts.Count;
  35. for( int i = 2; i < inputCount; i++ )
  36. {
  37. count++;
  38. if( !m_inputPorts[ i ].IsConnected )
  39. PreviewMaterial.SetTexture( ( "_" + Convert.ToChar( i + 65 ) ), UnityEditor.EditorGUIUtility.whiteTexture );
  40. }
  41. m_previewMaterialPassId = count;
  42. }
  43. public override string BuildResults( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  44. {
  45. if( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT3x3 ||
  46. m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT4x4 ||
  47. m_inputPorts[ 1 ].DataType == WirePortDataType.FLOAT3x3 ||
  48. m_inputPorts[ 1 ].DataType == WirePortDataType.FLOAT4x4 )
  49. {
  50. m_inputA = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  51. m_inputB = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  52. WirePortDataType autoCast = WirePortDataType.OBJECT;
  53. // Check matrix on first input
  54. if( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT3x3 )
  55. {
  56. switch( m_inputPorts[ 1 ].DataType )
  57. {
  58. case WirePortDataType.OBJECT:
  59. case WirePortDataType.FLOAT:
  60. case WirePortDataType.INT:
  61. case WirePortDataType.FLOAT2:
  62. case WirePortDataType.FLOAT4:
  63. case WirePortDataType.COLOR:
  64. {
  65. m_inputB = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), m_inputB, m_inputPorts[ 1 ].DataType, WirePortDataType.FLOAT3, m_inputB );
  66. autoCast = WirePortDataType.FLOAT3;
  67. }
  68. break;
  69. case WirePortDataType.FLOAT4x4:
  70. {
  71. m_inputA = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), m_inputA, m_inputPorts[ 0 ].DataType, WirePortDataType.FLOAT4x4, m_inputA );
  72. }
  73. break;
  74. case WirePortDataType.FLOAT3:
  75. case WirePortDataType.FLOAT3x3: break;
  76. }
  77. }
  78. if( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT4x4 )
  79. {
  80. switch( m_inputPorts[ 1 ].DataType )
  81. {
  82. case WirePortDataType.OBJECT:
  83. case WirePortDataType.FLOAT:
  84. case WirePortDataType.INT:
  85. case WirePortDataType.FLOAT2:
  86. case WirePortDataType.FLOAT3:
  87. {
  88. m_inputB = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), m_inputB, m_inputPorts[ 1 ].DataType, WirePortDataType.FLOAT4, m_inputB );
  89. autoCast = WirePortDataType.FLOAT4;
  90. }
  91. break;
  92. case WirePortDataType.FLOAT3x3:
  93. {
  94. m_inputB = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), m_inputB, m_inputPorts[ 1 ].DataType, WirePortDataType.FLOAT4x4, m_inputB );
  95. }
  96. break;
  97. case WirePortDataType.FLOAT4x4:
  98. case WirePortDataType.FLOAT4:
  99. case WirePortDataType.COLOR: break;
  100. }
  101. }
  102. // Check matrix on second input
  103. if( m_inputPorts[ 1 ].DataType == WirePortDataType.FLOAT3x3 )
  104. {
  105. switch( m_inputPorts[ 0 ].DataType )
  106. {
  107. case WirePortDataType.OBJECT:
  108. case WirePortDataType.FLOAT:
  109. case WirePortDataType.INT:
  110. case WirePortDataType.FLOAT2:
  111. case WirePortDataType.FLOAT4:
  112. case WirePortDataType.COLOR:
  113. {
  114. m_inputA = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), m_inputA, m_inputPorts[ 0 ].DataType, WirePortDataType.FLOAT3, m_inputA );
  115. autoCast = WirePortDataType.FLOAT3;
  116. }
  117. break;
  118. case WirePortDataType.FLOAT4x4:
  119. case WirePortDataType.FLOAT3:
  120. case WirePortDataType.FLOAT3x3: break;
  121. }
  122. }
  123. if( m_inputPorts[ 1 ].DataType == WirePortDataType.FLOAT4x4 )
  124. {
  125. switch( m_inputPorts[ 0 ].DataType )
  126. {
  127. case WirePortDataType.OBJECT:
  128. case WirePortDataType.FLOAT:
  129. case WirePortDataType.INT:
  130. case WirePortDataType.FLOAT2:
  131. case WirePortDataType.FLOAT3:
  132. {
  133. m_inputA = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), m_inputA, m_inputPorts[ 0 ].DataType, WirePortDataType.FLOAT4, m_inputA );
  134. autoCast = WirePortDataType.FLOAT4;
  135. }
  136. break;
  137. case WirePortDataType.FLOAT3x3:
  138. case WirePortDataType.FLOAT4x4:
  139. case WirePortDataType.FLOAT4:
  140. case WirePortDataType.COLOR: break;
  141. }
  142. }
  143. string result = "mul( " + m_inputA + ", " + m_inputB + " )";
  144. if( autoCast != WirePortDataType.OBJECT && autoCast != m_outputPorts[ 0 ].DataType )
  145. {
  146. result = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), result, autoCast, m_outputPorts[ 0 ].DataType, result );
  147. }
  148. return result;
  149. }
  150. else
  151. {
  152. base.BuildResults( outputId, ref dataCollector, ignoreLocalvar );
  153. string result = "( " + m_extensibleInputResults[ 0 ];
  154. for( int i = 1; i < m_extensibleInputResults.Count; i++ )
  155. {
  156. result += " * " + m_extensibleInputResults[ i ];
  157. }
  158. result += " )";
  159. return result;
  160. }
  161. }
  162. }
  163. }