ParallaxMappingNode.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. namespace AmplifyShaderEditor
  5. {
  6. [Serializable]
  7. [NodeAttributes( "Parallax Mapping", "UV Coordinates", "Calculates offseted UVs for parallax mapping" )]
  8. public sealed class ParallaxMappingNode : ParentNode
  9. {
  10. private enum ParallaxType { Normal, Planar }
  11. [SerializeField]
  12. private int m_selectedParallaxTypeInt = 0;
  13. [SerializeField]
  14. private ParallaxType m_selectedParallaxType = ParallaxType.Normal;
  15. private readonly string[] m_parallaxTypeStr = { "Normal", "Planar" };
  16. private int m_cachedPropertyId = -1;
  17. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  18. protected override void CommonInit( int uniqueId )
  19. {
  20. base.CommonInit( uniqueId );
  21. AddInputPort( WirePortDataType.FLOAT2, false, "UV" );
  22. AddInputPort( WirePortDataType.FLOAT, false, "Height" );
  23. AddInputPort( WirePortDataType.FLOAT, false, "Scale" );
  24. AddInputPort( WirePortDataType.FLOAT3, false, "ViewDir (tan)" );
  25. AddOutputPort( WirePortDataType.FLOAT2, "Out" );
  26. m_useInternalPortData = true;
  27. m_autoDrawInternalPortData = true;
  28. m_autoWrapProperties = true;
  29. m_textLabelWidth = 105;
  30. UpdateTitle();
  31. m_forceDrawPreviewAsPlane = true;
  32. m_hasLeftDropdown = true;
  33. m_previewShaderGUID = "589f12f68e00ac74286815aa56053fcc";
  34. }
  35. public override void Destroy()
  36. {
  37. base.Destroy();
  38. m_upperLeftWidget = null;
  39. }
  40. public override void SetPreviewInputs()
  41. {
  42. base.SetPreviewInputs();
  43. if( m_cachedPropertyId == -1 )
  44. m_cachedPropertyId = Shader.PropertyToID( "_ParallaxType" );
  45. PreviewMaterial.SetFloat( m_cachedPropertyId, ( m_selectedParallaxType == ParallaxType.Normal ? 0 : 1 ) );
  46. }
  47. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  48. {
  49. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  50. string textcoords = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  51. string height = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  52. string scale = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  53. string viewDirTan = m_inputPorts[ 3 ].GeneratePortInstructions( ref dataCollector );
  54. string localVarName = "Offset" + OutputId;
  55. string calculation = "";
  56. switch( m_selectedParallaxType )
  57. {
  58. default:
  59. case ParallaxType.Normal:
  60. calculation = "( ( " + height + " - 1 ) * " + viewDirTan + ".xy * " + scale + " ) + " + textcoords;
  61. break;
  62. case ParallaxType.Planar:
  63. calculation = "( ( " + height + " - 1 ) * ( " + viewDirTan + ".xy / " + viewDirTan + ".z ) * " + scale + " ) + " + textcoords;
  64. break;
  65. }
  66. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, m_outputPorts[ 0 ].DataType, localVarName, calculation );
  67. //dataCollector.AddToLocalVariables( UniqueId, m_currentPrecisionType, m_outputPorts[ 0 ].DataType, localVarName, calculation );
  68. return GetOutputVectorItem( 0, outputId, localVarName );
  69. }
  70. public override void Draw( DrawInfo drawInfo )
  71. {
  72. base.Draw( drawInfo );
  73. EditorGUI.BeginChangeCheck();
  74. m_selectedParallaxTypeInt = m_upperLeftWidget.DrawWidget( this, m_selectedParallaxTypeInt, m_parallaxTypeStr );
  75. if( EditorGUI.EndChangeCheck() )
  76. {
  77. switch( m_selectedParallaxTypeInt )
  78. {
  79. default:
  80. case 0: m_selectedParallaxType = ParallaxType.Normal; break;
  81. case 1: m_selectedParallaxType = ParallaxType.Planar; break;
  82. }
  83. UpdateTitle();
  84. }
  85. }
  86. public override void DrawProperties()
  87. {
  88. base.DrawProperties();
  89. EditorGUI.BeginChangeCheck();
  90. m_selectedParallaxTypeInt = EditorGUILayoutPopup( "Parallax Type", m_selectedParallaxTypeInt, m_parallaxTypeStr );
  91. if( EditorGUI.EndChangeCheck() )
  92. {
  93. switch( m_selectedParallaxTypeInt )
  94. {
  95. default:
  96. case 0: m_selectedParallaxType = ParallaxType.Normal; break;
  97. case 1: m_selectedParallaxType = ParallaxType.Planar; break;
  98. }
  99. UpdateTitle();
  100. }
  101. EditorGUILayout.HelpBox( "Normal type does a cheaper approximation thats view dependent while Planar is more accurate but generates higher aliasing artifacts at steep angles.", MessageType.None );
  102. }
  103. void UpdateTitle()
  104. {
  105. m_additionalContent.text = string.Format( Constants.SubTitleTypeFormatStr, m_parallaxTypeStr[ m_selectedParallaxTypeInt ] );
  106. }
  107. public override void ReadFromString( ref string[] nodeParams )
  108. {
  109. base.ReadFromString( ref nodeParams );
  110. m_selectedParallaxType = (ParallaxType)Enum.Parse( typeof( ParallaxType ), GetCurrentParam( ref nodeParams ) );
  111. switch( m_selectedParallaxType )
  112. {
  113. default:
  114. case ParallaxType.Normal: m_selectedParallaxTypeInt = 0; break;
  115. case ParallaxType.Planar: m_selectedParallaxTypeInt = 1; break;
  116. }
  117. UpdateTitle();
  118. }
  119. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  120. {
  121. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  122. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedParallaxType );
  123. }
  124. }
  125. }