Editor/ShaderGraphPlus/Nodes/Effects/Fresnel.cs

A Shader Graph editor node that computes a Fresnel term. It defines inputs for normal, view direction, and power (with a default), and emits HLSL code that calculates pow(1 - dot(normalize(normal), normalize(direction)), power).

Reflection

namespace ShaderGraphPlus.Nodes;

/// <summary>
/// Calculates a Fresnel term.
/// </summary>
[Title( "Fresnel" ), Category( "Effects" ), Icon( "blur_circular" )]
public sealed class Fresnel : ShaderNodePlus
{
	[JsonIgnore, Hide, Browsable( false )]
	public override Color NodeTitleColor => ShaderGraphPlusTheme.NodeHeaderColors.FunctionNode;

	/// <summary>
	/// Normal at the point being shaded.
	/// </summary>
	[Input( typeof( Vector3 ) )]
	[Hide]
	public NodeInput Normal { get; set; }

	/// <summary>
	/// Direction of the viewer's eye.
	/// </summary>
	[Input( typeof( Vector3 ) )]
	[Hide]
	public NodeInput Direction { get; set; }

	/// <summary>
	/// Power that controls the strength of the Fresnel effect.
	/// </summary>
	[Input( typeof( float ) )]
	[Hide]
	public NodeInput Power { get; set; }

	/// <summary>
	/// Default value for when Power input is missing.
	/// </summary>
	public float DefaultPower { get; set; } = 10.0f;

	[Output( typeof( Vector3 ) )]
	[Hide]
	public NodeResult.Func Result => ( GraphCompiler compiler ) =>
	{
		var power = compiler.ResultOrDefault( Power, DefaultPower ).Cast( 1 );
		var normal = Normal.IsValid ? compiler.Result( Normal ).Cast( 3 ) : "i.vNormalWs";
		var direction = Direction.IsValid ? compiler.Result( Direction ).Cast( 3 ) :
			$"CalculatePositionToCameraDirWs( {(compiler.IsVs ? "i.vPositionWs.xyz" : "i.vPositionWithOffsetWs.xyz + g_vHighPrecisionLightingOffsetWs.xyz")} )";

		return new( ResultType.Vector3, $"pow( 1.0 - dot( normalize( {normal} ), normalize( {direction} ) ), {power} )" );
	};
}