BillboardNode.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Billboard", "Miscellaneous", "Calculates new Vertex positions and normals to achieve a billboard effect." )]
  10. public sealed class BillboardNode : ParentNode
  11. {
  12. private const string ErrorMessage = "Billboard node should only be connected to vertex ports.";
  13. private const string WarningMessage = "This node is a bit different from all others as it injects the necessary code into the vertex body and writes directly on the vertex position and normal.\nIt outputs a value of 0 so it can be connected directly to a vertex port.\n[Only if that port is a relative vertex offset].";
  14. [SerializeField]
  15. private BillboardType m_billboardType = BillboardType.Cylindrical;
  16. [SerializeField]
  17. private bool m_rotationIndependent = false;
  18. [SerializeField]
  19. private bool m_affectNormalTangent = true;
  20. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  21. protected override void CommonInit( int uniqueId )
  22. {
  23. base.CommonInit( uniqueId );
  24. AddOutputPort( WirePortDataType.FLOAT3, "Out" );
  25. m_textLabelWidth = 115;
  26. m_hasLeftDropdown = true;
  27. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_billboardType ) );
  28. }
  29. public override void Destroy()
  30. {
  31. base.Destroy();
  32. m_upperLeftWidget = null;
  33. }
  34. public override void AfterCommonInit()
  35. {
  36. base.AfterCommonInit();
  37. if( PaddingTitleLeft == 0 )
  38. {
  39. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  40. if( PaddingTitleRight == 0 )
  41. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  42. }
  43. }
  44. public override void Draw( DrawInfo drawInfo )
  45. {
  46. base.Draw( drawInfo );
  47. m_upperLeftWidget.DrawWidget<BillboardType>( ref m_billboardType, this, OnWidgetUpdate );
  48. }
  49. private readonly Action<ParentNode> OnWidgetUpdate = ( x ) =>
  50. {
  51. x.SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, ( x as BillboardNode ).Type ) );
  52. };
  53. public override void DrawProperties()
  54. {
  55. base.DrawProperties();
  56. NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, () =>
  57. {
  58. EditorGUI.BeginChangeCheck();
  59. m_billboardType = (BillboardType)EditorGUILayoutEnumPopup( BillboardOpHelper.BillboardTypeStr, m_billboardType );
  60. if( EditorGUI.EndChangeCheck() )
  61. {
  62. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_billboardType ) );
  63. }
  64. m_rotationIndependent = EditorGUILayoutToggle( BillboardOpHelper.BillboardRotIndStr, m_rotationIndependent );
  65. m_affectNormalTangent = EditorGUILayoutToggle( BillboardOpHelper.BillboardAffectNormalTangentStr , m_affectNormalTangent );
  66. } );
  67. EditorGUILayout.HelpBox( WarningMessage, MessageType.Warning );
  68. }
  69. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  70. {
  71. if( dataCollector.IsFragmentCategory )
  72. {
  73. UIUtils.ShowMessage( UniqueId, ErrorMessage,MessageSeverity.Error );
  74. return m_outputPorts[0].ErrorValue;
  75. }
  76. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  77. return m_outputPorts[ 0 ].ErrorValue;
  78. m_outputPorts[ 0 ].SetLocalValue( "0", dataCollector.PortCategory );
  79. string vertexPosValue = dataCollector.IsTemplate ? dataCollector.TemplateDataCollectorInstance.GetVertexPosition( WirePortDataType.OBJECT, CurrentPrecisionType ) : "v.vertex";
  80. string vertexNormalValue = dataCollector.IsTemplate ? dataCollector.TemplateDataCollectorInstance.GetVertexNormal( CurrentPrecisionType ) : "v.normal";
  81. string vertexTangentValue = dataCollector.IsTemplate ? dataCollector.TemplateDataCollectorInstance.GetVertexTangent( WirePortDataType.FLOAT4, CurrentPrecisionType ) : "v.tangent";
  82. bool vertexIsFloat3 = false;
  83. if( dataCollector.IsTemplate )
  84. {
  85. InterpDataHelper info = dataCollector.TemplateDataCollectorInstance.GetInfo( TemplateInfoOnSematics.POSITION );
  86. if( info != null )
  87. {
  88. vertexIsFloat3 = info.VarType == WirePortDataType.FLOAT3;
  89. }
  90. }
  91. BillboardOpHelper.FillDataCollector( ref dataCollector, m_billboardType, m_rotationIndependent, vertexPosValue, vertexNormalValue, vertexTangentValue, vertexIsFloat3, m_affectNormalTangent );
  92. return "0";
  93. }
  94. public override void ReadFromString( ref string[] nodeParams )
  95. {
  96. base.ReadFromString( ref nodeParams );
  97. m_billboardType = (BillboardType)Enum.Parse( typeof( BillboardType ), GetCurrentParam( ref nodeParams ) );
  98. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr , m_billboardType ) );
  99. m_rotationIndependent = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  100. if( UIUtils.CurrentShaderVersion() > 18918 )
  101. {
  102. m_affectNormalTangent = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  103. }
  104. }
  105. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  106. {
  107. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  108. IOUtils.AddFieldValueToString( ref nodeInfo, m_billboardType );
  109. IOUtils.AddFieldValueToString( ref nodeInfo, m_rotationIndependent );
  110. IOUtils.AddFieldValueToString( ref nodeInfo , m_affectNormalTangent );
  111. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_billboardType ) );
  112. }
  113. public BillboardType Type { get { return m_billboardType; } }
  114. }
  115. }