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.

95 lines
4.5 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEditor;
  4. using System;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Rotate About Axis", "Vector Operators", "Rotates a vector around a normalized axis" )]
  9. public class RotateAboutAxisNode : ParentNode
  10. {
  11. private const string FunctionHeader = "float3 RotateAroundAxis( float3 center, float3 original, float3 u, float angle )";
  12. private const string FunctionCall = "RotateAroundAxis( {0}, {1}, {2}, {3} )";
  13. private readonly string[] FunctionBody =
  14. {
  15. "float3 RotateAroundAxis( float3 center, float3 original, float3 u, float angle )\n",
  16. "{\n",
  17. "\toriginal -= center;\n",
  18. "\tfloat C = cos( angle );\n",
  19. "\tfloat S = sin( angle );\n",
  20. "\tfloat t = 1 - C;\n",
  21. "\tfloat m00 = t * u.x * u.x + C;\n",
  22. "\tfloat m01 = t * u.x * u.y - S * u.z;\n",
  23. "\tfloat m02 = t * u.x * u.z + S * u.y;\n",
  24. "\tfloat m10 = t * u.x * u.y + S * u.z;\n",
  25. "\tfloat m11 = t * u.y * u.y + C;\n",
  26. "\tfloat m12 = t * u.y * u.z - S * u.x;\n",
  27. "\tfloat m20 = t * u.x * u.z - S * u.y;\n",
  28. "\tfloat m21 = t * u.y * u.z + S * u.x;\n",
  29. "\tfloat m22 = t * u.z * u.z + C;\n",
  30. "\tfloat3x3 finalMatrix = float3x3( m00, m01, m02, m10, m11, m12, m20, m21, m22 );\n",
  31. "\treturn mul( finalMatrix, original ) + center;\n",
  32. "}\n"
  33. };
  34. private const string NormalizeAxisLabel = "Rotation Axis";
  35. private const string NonNormalizeAxisLabel = "Normalized Rotation Axis";
  36. private const string NormalizeAxisStr = "Normalize Axis";
  37. [UnityEngine.SerializeField]
  38. private bool m_normalizeAxis = false;
  39. protected override void CommonInit( int uniqueId )
  40. {
  41. base.CommonInit( uniqueId );
  42. AddInputPort( WirePortDataType.FLOAT3, false, m_normalizeAxis? NormalizeAxisLabel: NonNormalizeAxisLabel );
  43. AddInputPort( WirePortDataType.FLOAT, false, "Rotation Angle" );
  44. AddInputPort( WirePortDataType.FLOAT3, false, "Pivot Point" );
  45. AddInputPort( WirePortDataType.FLOAT3, false, "Position" );
  46. AddOutputPort( WirePortDataType.FLOAT3, Constants.EmptyPortValue );
  47. m_useInternalPortData = true;
  48. m_autoWrapProperties = true;
  49. }
  50. public override void DrawProperties()
  51. {
  52. base.DrawProperties();
  53. EditorGUI.BeginChangeCheck();
  54. m_normalizeAxis = EditorGUILayoutToggle( NormalizeAxisStr, m_normalizeAxis );
  55. if( EditorGUI.EndChangeCheck() )
  56. {
  57. m_inputPorts[ 0 ].Name = (m_normalizeAxis ? NormalizeAxisLabel : NonNormalizeAxisLabel);
  58. }
  59. }
  60. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  61. {
  62. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  63. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  64. string normalizeRotAxis = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  65. if( m_normalizeAxis )
  66. {
  67. normalizeRotAxis = string.Format( "normalize( {0} )", normalizeRotAxis );
  68. }
  69. string rotationAngle = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  70. string pivotPoint = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  71. string position = m_inputPorts[ 3 ].GeneratePortInstructions( ref dataCollector );
  72. dataCollector.AddFunction( FunctionHeader, FunctionBody, false );
  73. RegisterLocalVariable( 0, string.Format( FunctionCall, pivotPoint, position, normalizeRotAxis, rotationAngle ), ref dataCollector, "rotatedValue" + OutputId );
  74. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  75. }
  76. public override void ReadFromString( ref string[] nodeParams )
  77. {
  78. base.ReadFromString( ref nodeParams );
  79. m_normalizeAxis = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  80. }
  81. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  82. {
  83. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  84. IOUtils.AddFieldValueToString( ref nodeInfo, m_normalizeAxis );
  85. }
  86. }
  87. }