CameraDepthFade.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. namespace AmplifyShaderEditor
  5. {
  6. [Serializable]
  7. [NodeAttributes( "Camera Depth Fade", "Camera And Screen", "Outputs a 0 - 1 gradient representing the distance between the surface of this object and camera near plane" )]
  8. public sealed class CameraDepthFade : ParentNode
  9. {
  10. //{0} - Eye Depth
  11. //{1} - Offset
  12. //{2} - Distance
  13. private const string CameraDepthFadeFormat = "(( {0} -_ProjectionParams.y - {1} ) / {2})";
  14. protected override void CommonInit( int uniqueId )
  15. {
  16. base.CommonInit( uniqueId );
  17. AddInputPort( WirePortDataType.FLOAT3, false, "Vertex Position", -1, MasterNodePortCategory.Fragment, 2 );
  18. AddInputPort( WirePortDataType.FLOAT, false, "Length", -1, MasterNodePortCategory.Fragment, 0 );
  19. AddInputPort( WirePortDataType.FLOAT, false, "Offset", -1, MasterNodePortCategory.Fragment, 1 );
  20. GetInputPortByUniqueId( 0 ).FloatInternalData = 1;
  21. AddOutputPort( WirePortDataType.FLOAT, "Out" );
  22. m_useInternalPortData = true;
  23. }
  24. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  25. {
  26. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  27. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  28. InputPort vertexPort = GetInputPortByUniqueId( 2 );
  29. InputPort lengthPort = GetInputPortByUniqueId( 0 );
  30. InputPort offsetPort = GetInputPortByUniqueId( 1 );
  31. string distance = lengthPort.GeneratePortInstructions( ref dataCollector );
  32. string offset = offsetPort.GeneratePortInstructions( ref dataCollector );
  33. string value = string.Empty;
  34. string eyeDepth = string.Empty;
  35. if( dataCollector.IsTemplate )
  36. {
  37. if( vertexPort.IsConnected )
  38. {
  39. string varName = "customSurfaceDepth" + OutputId;
  40. GenerateInputInVertex( ref dataCollector, 2, varName, false );
  41. string formatStr = string.Empty;
  42. if( dataCollector.IsSRP )
  43. formatStr = "-TransformWorldToView(TransformObjectToWorld({0})).z";
  44. else
  45. formatStr = "-UnityObjectToViewPos({0}).z";
  46. string eyeInstruction = string.Format( formatStr, varName );
  47. eyeDepth = "customEye" + OutputId;
  48. dataCollector.TemplateDataCollectorInstance.RegisterCustomInterpolatedData( eyeDepth, WirePortDataType.FLOAT, CurrentPrecisionType, eyeInstruction );
  49. }
  50. else
  51. {
  52. eyeDepth = dataCollector.TemplateDataCollectorInstance.GetEyeDepth( CurrentPrecisionType );
  53. }
  54. value = string.Format( CameraDepthFadeFormat, eyeDepth, offset, distance );
  55. RegisterLocalVariable( 0, value, ref dataCollector, "cameraDepthFade" + OutputId );
  56. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  57. }
  58. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  59. {
  60. string vertexVarName = string.Empty;
  61. if( vertexPort.IsConnected )
  62. {
  63. vertexVarName = vertexPort.GeneratePortInstructions( ref dataCollector );
  64. }
  65. else
  66. {
  67. vertexVarName = Constants.VertexShaderInputStr + ".vertex.xyz";
  68. }
  69. //dataCollector.AddVertexInstruction( "float cameraDepthFade" + UniqueId + " = (( -UnityObjectToViewPos( " + Constants.VertexShaderInputStr + ".vertex.xyz ).z -_ProjectionParams.y - " + offset + " ) / " + distance + ");", UniqueId );
  70. value = string.Format( CameraDepthFadeFormat, "-UnityObjectToViewPos( " + vertexVarName + " ).z", offset, distance );
  71. RegisterLocalVariable( 0, value, ref dataCollector, "cameraDepthFade" + OutputId );
  72. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  73. }
  74. dataCollector.AddToIncludes( UniqueId, Constants.UnityShaderVariables );
  75. if( dataCollector.TesselationActive )
  76. {
  77. if( vertexPort.IsConnected )
  78. {
  79. string vertexValue = vertexPort.GeneratePortInstructions( ref dataCollector );
  80. eyeDepth = "customSurfaceDepth" + OutputId;
  81. RegisterLocalVariable( 0, string.Format( "-UnityObjectToViewPos( {0} ).z", vertexValue ), ref dataCollector, eyeDepth );
  82. }
  83. else
  84. {
  85. eyeDepth = GeneratorUtils.GenerateScreenDepthOnFrag( ref dataCollector, UniqueId, CurrentPrecisionType );
  86. }
  87. }
  88. else
  89. {
  90. if( vertexPort.IsConnected )
  91. {
  92. string varName = "customSurfaceDepth" + OutputId;
  93. GenerateInputInVertex( ref dataCollector, 2, varName, false );
  94. dataCollector.AddToInput( UniqueId, varName, WirePortDataType.FLOAT );
  95. string vertexInstruction = "-UnityObjectToViewPos( " + varName + " ).z";
  96. dataCollector.AddToVertexLocalVariables( UniqueId, Constants.VertexShaderOutputStr + "." + varName + " = " + vertexInstruction + ";" );
  97. eyeDepth = Constants.InputVarStr + "." + varName;
  98. }
  99. else
  100. {
  101. dataCollector.AddToInput( UniqueId, "eyeDepth", WirePortDataType.FLOAT );
  102. string instruction = "-UnityObjectToViewPos( " + Constants.VertexShaderInputStr + ".vertex.xyz ).z";
  103. dataCollector.AddToVertexLocalVariables( UniqueId, Constants.VertexShaderOutputStr + ".eyeDepth = " + instruction + ";" );
  104. eyeDepth = Constants.InputVarStr + ".eyeDepth";
  105. }
  106. }
  107. value = string.Format( CameraDepthFadeFormat, eyeDepth, offset, distance );
  108. RegisterLocalVariable( 0, value, ref dataCollector, "cameraDepthFade" + OutputId );
  109. //dataCollector.AddToLocalVariables( UniqueId, "float cameraDepthFade" + UniqueId + " = (( " + Constants.InputVarStr + ".eyeDepth -_ProjectionParams.y - "+ offset + " ) / " + distance + ");" );
  110. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  111. }
  112. }
  113. }