TangentVertexDataNode.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Vertex Tangent", "Vertex Data", "Vertex tangent vector in object space, can be used in both local vertex offset and fragment outputs" )]
  10. public sealed class TangentVertexDataNode : VertexDataNode
  11. {
  12. private const string PropertyLabel = "Size";
  13. private readonly string[] SizeLabels = { "XYZ", "XYZW" };
  14. [SerializeField]
  15. private int m_sizeOption = 0;
  16. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  17. protected override void CommonInit( int uniqueId )
  18. {
  19. base.CommonInit( uniqueId );
  20. m_currentVertexData = "tangent";
  21. ChangeOutputProperties( 0, "XYZ", WirePortDataType.FLOAT3 );
  22. m_outputPorts[ 4 ].Visible = false;
  23. m_drawPreviewAsSphere = true;
  24. m_hasLeftDropdown = true;
  25. m_previewShaderGUID = "0a44bb521d06d6143a4acbc3602037f8";
  26. }
  27. public override void Destroy()
  28. {
  29. base.Destroy();
  30. m_upperLeftWidget = null;
  31. }
  32. public override void Draw( DrawInfo drawInfo )
  33. {
  34. base.Draw( drawInfo );
  35. EditorGUI.BeginChangeCheck();
  36. m_sizeOption = m_upperLeftWidget.DrawWidget( this, m_sizeOption, SizeLabels );
  37. if( EditorGUI.EndChangeCheck() )
  38. {
  39. UpdatePorts();
  40. }
  41. }
  42. public override void DrawProperties()
  43. {
  44. EditorGUI.BeginChangeCheck();
  45. m_sizeOption = EditorGUILayoutPopup( PropertyLabel, m_sizeOption, SizeLabels );
  46. if( EditorGUI.EndChangeCheck() )
  47. {
  48. UpdatePorts();
  49. }
  50. }
  51. void UpdatePorts()
  52. {
  53. if( m_sizeOption == 0 )
  54. {
  55. ChangeOutputProperties( 0, SizeLabels[ 0 ], WirePortDataType.FLOAT3, false );
  56. m_outputPorts[ 4 ].Visible = false;
  57. }
  58. else
  59. {
  60. ChangeOutputProperties( 0, SizeLabels[ 1 ], WirePortDataType.FLOAT4, false );
  61. m_outputPorts[ 4 ].Visible = true;
  62. }
  63. }
  64. public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  65. {
  66. base.PropagateNodeData( nodeData, ref dataCollector );
  67. dataCollector.DirtyNormal = true;
  68. }
  69. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  70. {
  71. string vertexTangent = string.Empty;
  72. if ( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  73. {
  74. vertexTangent = dataCollector.TemplateDataCollectorInstance.GetVertexTangent( WirePortDataType.FLOAT4, CurrentPrecisionType );
  75. if( m_sizeOption == 0 )
  76. vertexTangent += ".xyz";
  77. return GetOutputVectorItem( 0, outputId, vertexTangent );
  78. }
  79. if ( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  80. {
  81. dataCollector.ForceNormal = true;
  82. dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_NORMAL, UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  83. dataCollector.AddToInput( UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false );
  84. }
  85. WirePortDataType sizeType = m_sizeOption == 0 ? WirePortDataType.FLOAT3 : WirePortDataType.FLOAT4;
  86. vertexTangent = GeneratorUtils.GenerateVertexTangent( ref dataCollector, UniqueId, CurrentPrecisionType, sizeType );
  87. return GetOutputVectorItem( 0, outputId, vertexTangent );
  88. }
  89. public override void ReadFromString( ref string[] nodeParams )
  90. {
  91. base.ReadFromString( ref nodeParams );
  92. if( UIUtils.CurrentShaderVersion() > 16100 )
  93. {
  94. m_sizeOption = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  95. UpdatePorts();
  96. }
  97. }
  98. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  99. {
  100. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  101. IOUtils.AddFieldValueToString( ref nodeInfo, m_sizeOption );
  102. }
  103. }
  104. }