HeightMapBlendNode.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //
  4. // Custom Node HeightMap Texture Masking
  5. // Donated by Rea
  6. using UnityEngine;
  7. using UnityEditor;
  8. using System;
  9. namespace AmplifyShaderEditor
  10. {
  11. [Serializable]
  12. [NodeAttributes( "HeightMap Texture Blend", "Textures", "Advanced Texture Blending by using heightMap and splatMask, usefull for texture layering ", null, KeyCode.None, true, false, null, null, "Rea" )]
  13. public sealed class HeightMapBlendNode : ParentNode
  14. {
  15. private const string PreventNaNLabel = "Prevent NaN";
  16. private const string PreventNaNInfo = "Prevent NaN clamps negative base numbers over the internal pow instruction to 0 since these originate NaN.";
  17. [SerializeField]
  18. private bool m_preventNaN = false;
  19. protected override void CommonInit( int uniqueId )
  20. {
  21. base.CommonInit( uniqueId );
  22. AddInputPort( WirePortDataType.FLOAT, false, "HeightMap" );
  23. AddInputPort( WirePortDataType.FLOAT, false, "SplatMask" );
  24. AddInputPort( WirePortDataType.FLOAT, false, "BlendStrength" );
  25. AddOutputVectorPorts( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  26. m_textLabelWidth = 120;
  27. m_useInternalPortData = true;
  28. m_inputPorts[ 2 ].FloatInternalData = 1;
  29. m_autoWrapProperties = true;
  30. m_previewShaderGUID = "b2ac23d6d5dcb334982b6f31c2e7a734";
  31. }
  32. public override void DrawProperties()
  33. {
  34. base.DrawProperties();
  35. m_preventNaN = EditorGUILayoutToggle( PreventNaNLabel , m_preventNaN );
  36. EditorGUILayout.HelpBox( PreventNaNInfo , MessageType.Info );
  37. }
  38. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  39. {
  40. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  41. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  42. string HeightMap = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  43. string SplatMask = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector);
  44. string Blend = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  45. string baseOp = "((" + HeightMap + "*" + SplatMask + ")*4)+(" + SplatMask + "*2)";
  46. if( m_preventNaN )
  47. baseOp = "max( (" + baseOp + "), 0 )";
  48. string HeightMask = "saturate(pow("+baseOp+"," + Blend + "))";
  49. string varName = "HeightMask" + OutputId;
  50. RegisterLocalVariable( 0, HeightMask, ref dataCollector , varName );
  51. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  52. }
  53. /*
  54. A = (heightMap * SplatMask)*4
  55. B = SplatMask*2
  56. C = pow(A+B,Blend)
  57. saturate(C)
  58. saturate(pow(((heightMap * SplatMask)*4)+(SplatMask*2),Blend));
  59. */
  60. public override void ReadFromString( ref string[] nodeParams )
  61. {
  62. base.ReadFromString( ref nodeParams );
  63. if( UIUtils.CurrentShaderVersion() > 18910 )
  64. m_preventNaN = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  65. }
  66. public override void WriteToString( ref string nodeInfo , ref string connectionsInfo )
  67. {
  68. base.WriteToString( ref nodeInfo , ref connectionsInfo );
  69. IOUtils.AddFieldValueToString( ref nodeInfo , m_preventNaN );
  70. }
  71. }
  72. }