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.

138 lines
4.1 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. // 4x4 Invert by DBN in
  4. // http://answers.unity3d.com/questions/218333/shader-inversefloat4x4-function.html?childToView=641391#answer-641391
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Inverse", "Matrix Operators", "Inverse matrix of a matrix" )]
  10. public sealed class InverseOpNode : SingleInputOp
  11. {
  12. private string Inverse4x4Header = "Inverse4x4( {0} )";
  13. //4x4
  14. private string[] Inverse4x4Function =
  15. {
  16. "{0}4x4 Inverse4x4({0}4x4 input)\n",
  17. "{\n",
  18. "\t#define minor(a,b,c) determinant({0}3x3(input.a, input.b, input.c))\n",
  19. "\t{0}4x4 cofactors = {0}4x4(\n",
  20. "\tminor( _22_23_24, _32_33_34, _42_43_44 ),\n",
  21. "\t-minor( _21_23_24, _31_33_34, _41_43_44 ),\n",
  22. "\tminor( _21_22_24, _31_32_34, _41_42_44 ),\n",
  23. "\t-minor( _21_22_23, _31_32_33, _41_42_43 ),\n",
  24. "\n",
  25. "\t-minor( _12_13_14, _32_33_34, _42_43_44 ),\n",
  26. "\tminor( _11_13_14, _31_33_34, _41_43_44 ),\n",
  27. "\t-minor( _11_12_14, _31_32_34, _41_42_44 ),\n",
  28. "\tminor( _11_12_13, _31_32_33, _41_42_43 ),\n",
  29. "\n",
  30. "\tminor( _12_13_14, _22_23_24, _42_43_44 ),\n",
  31. "\t-minor( _11_13_14, _21_23_24, _41_43_44 ),\n",
  32. "\tminor( _11_12_14, _21_22_24, _41_42_44 ),\n",
  33. "\t-minor( _11_12_13, _21_22_23, _41_42_43 ),\n",
  34. "\n",
  35. "\t-minor( _12_13_14, _22_23_24, _32_33_34 ),\n",
  36. "\tminor( _11_13_14, _21_23_24, _31_33_34 ),\n",
  37. "\t-minor( _11_12_14, _21_22_24, _31_32_34 ),\n",
  38. "\tminor( _11_12_13, _21_22_23, _31_32_33 ));\n",
  39. "\t#undef minor\n",
  40. "\treturn transpose( cofactors ) / determinant( input );\n",
  41. "}\n"
  42. };
  43. private bool[] Inverse4x4FunctionFlags =
  44. {
  45. true,
  46. false,
  47. true,
  48. true,
  49. false,
  50. false,
  51. false,
  52. false,
  53. false,
  54. false,
  55. false,
  56. false,
  57. false,
  58. false,
  59. false,
  60. false,
  61. false,
  62. false,
  63. false,
  64. false,
  65. false,
  66. false,
  67. false,
  68. false,
  69. false,
  70. false,
  71. false
  72. };
  73. protected override void CommonInit( int uniqueId )
  74. {
  75. base.CommonInit( uniqueId );
  76. m_opName = "inverse";
  77. m_drawPreview = false;
  78. m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.FLOAT3x3,
  79. WirePortDataType.FLOAT4x4 );
  80. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4x4, false );
  81. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4x4, false );
  82. }
  83. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  84. {
  85. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  86. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  87. string precisionString = UIUtils.PrecisionWirePortToCgType( m_currentPrecisionType, WirePortDataType.FLOAT );
  88. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  89. if ( m_outputPorts[ 0 ].DataType == WirePortDataType.FLOAT3x3 )
  90. {
  91. GeneratorUtils.Add3x3InverseFunction( ref dataCollector, precisionString );
  92. RegisterLocalVariable( 0, string.Format( GeneratorUtils.Inverse3x3Header, value ), ref dataCollector, "invertVal" + OutputId );
  93. }
  94. else
  95. {
  96. if ( !dataCollector.HasFunction( Inverse4x4Header ) )
  97. {
  98. //Hack to be used util indent is properly used
  99. int currIndent = UIUtils.ShaderIndentLevel;
  100. if ( dataCollector.IsTemplate )
  101. {
  102. UIUtils.ShaderIndentLevel = 0;
  103. }
  104. else
  105. {
  106. UIUtils.ShaderIndentLevel = 1;
  107. UIUtils.ShaderIndentLevel++;
  108. }
  109. string finalFunction = string.Empty;
  110. for ( int i = 0; i < Inverse4x4Function.Length; i++ )
  111. {
  112. finalFunction += UIUtils.ShaderIndentTabs + ( Inverse4x4FunctionFlags[ i ] ? string.Format( Inverse4x4Function[ i ], precisionString ) : Inverse4x4Function[ i ] );
  113. }
  114. UIUtils.ShaderIndentLevel = currIndent;
  115. dataCollector.AddFunction( Inverse4x4Header, finalFunction );
  116. }
  117. RegisterLocalVariable( 0, string.Format( Inverse4x4Header, value ), ref dataCollector, "invertVal" + OutputId );
  118. }
  119. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  120. }
  121. }
  122. }