PosVertexDataNode.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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( "Vertex Position", "Vertex Data", "Vertex position vector in object space, can be used in both local vertex offset and fragment outputs" )]
  10. public sealed class PosVertexDataNode : 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 = "vertex";
  21. ChangeOutputProperties( 0, "XYZ", WirePortDataType.FLOAT3 );
  22. m_drawPreviewAsSphere = true;
  23. m_outputPorts[ 4 ].Visible = false;
  24. m_hasLeftDropdown = true;
  25. m_autoWrapProperties = true;
  26. m_previewShaderGUID = "a5c14f759dd021b4b8d4b6eeb85ac227";
  27. }
  28. public override void Destroy()
  29. {
  30. base.Destroy();
  31. m_upperLeftWidget = null;
  32. }
  33. public override void Draw( DrawInfo drawInfo )
  34. {
  35. base.Draw( drawInfo );
  36. EditorGUI.BeginChangeCheck();
  37. m_sizeOption = m_upperLeftWidget.DrawWidget( this, m_sizeOption, SizeLabels );
  38. if( EditorGUI.EndChangeCheck() )
  39. {
  40. UpdatePorts();
  41. }
  42. }
  43. public override void DrawProperties()
  44. {
  45. EditorGUI.BeginChangeCheck();
  46. m_sizeOption = EditorGUILayoutPopup( PropertyLabel, m_sizeOption, SizeLabels );
  47. if ( EditorGUI.EndChangeCheck() )
  48. {
  49. UpdatePorts();
  50. }
  51. }
  52. void UpdatePorts()
  53. {
  54. if ( m_sizeOption == 0 )
  55. {
  56. ChangeOutputProperties( 0, SizeLabels[ 0 ], WirePortDataType.FLOAT3, false );
  57. m_outputPorts[ 4 ].Visible = false;
  58. }
  59. else
  60. {
  61. ChangeOutputProperties( 0, SizeLabels[ 1 ], WirePortDataType.FLOAT4, false );
  62. m_outputPorts[ 4 ].Visible = true;
  63. }
  64. }
  65. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  66. {
  67. if ( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  68. {
  69. UIUtils.ShowNoVertexModeNodeMessage( this );
  70. return "0";
  71. }
  72. if ( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  73. {
  74. string vertexPos = dataCollector.TemplateDataCollectorInstance.GetVertexPosition( ( m_sizeOption == 0 ) ? WirePortDataType.FLOAT3 : WirePortDataType.FLOAT4, CurrentPrecisionType );
  75. return GetOutputVectorItem( 0, outputId, vertexPos );
  76. }
  77. if ( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  78. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
  79. WirePortDataType sizeType = m_sizeOption == 0 ? WirePortDataType.FLOAT3 : WirePortDataType.FLOAT4;
  80. string vertexPosition = GeneratorUtils.GenerateVertexPosition( ref dataCollector, UniqueId, sizeType );
  81. return GetOutputVectorItem( 0, outputId, vertexPosition );
  82. //if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  83. //{
  84. // string vertexVar = base.GenerateShaderForOutput( 0, ref dataCollector, ignoreLocalVar );
  85. // if ( outputId != 0 )
  86. // {
  87. // return GetOutputVectorItem( 0, outputId, vertexVar );
  88. // }
  89. // else if ( m_sizeOption == 0 )
  90. // {
  91. // vertexVar += ".xyz";
  92. // }
  93. // return vertexVar;
  94. //}
  95. //else
  96. //{
  97. // string vertexVar = GeneratorUtils.GenerateVertexPositionOnFrag( ref dataCollector, UniqueId, m_currentPrecisionType );
  98. // if ( outputId != 0 )
  99. // {
  100. // return GetOutputVectorItem( 0, outputId, vertexVar );
  101. // }
  102. // else if ( m_sizeOption == 0 )
  103. // {
  104. // vertexVar += ".xyz";
  105. // }
  106. // return GetOutputVectorItem( 0, outputId, vertexVar );
  107. //}
  108. }
  109. public override void ReadFromString( ref string[] nodeParams )
  110. {
  111. base.ReadFromString( ref nodeParams );
  112. if ( UIUtils.CurrentShaderVersion() > 7101 )
  113. {
  114. m_sizeOption = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  115. UpdatePorts();
  116. }
  117. }
  118. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  119. {
  120. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  121. IOUtils.AddFieldValueToString( ref nodeInfo, m_sizeOption );
  122. }
  123. }
  124. }