A static template for a shader surface definition used in the Editor's upgraded Shader Graph compiler. It provides a dictionary of feature toggles and a large HLSL template string with placeholders for injecting feature-specific blocks, vertex and pixel shader code, and common includes.
namespace Editor.ShaderGraphExtras;
public static class SurfaceTemplate
{
public static Dictionary<string, bool> Features => new()
{
{ "SupportsAlbedo", true},
{ "SupportsEmission", true },
{ "SupportsOpacity", true},
{ "SupportsNormal", true },
{ "SupportsRoughness", true },
{ "SupportsMetalness", true },
{ "SupportsAmbientOcclusion", true },
{ "SupportsPositionOffset", true },
{ "SupportsPixelDepthOffset", true},
{"SupportsLitShadingModel", true},
{"SupportsUnlitShadingModel", true},
{"SupportsCustomShadingModel", true},
{ "SupportsOpaqueBlendMode", true},
{ "SupportsMaskedBlendMode", true},
{ "SupportsTranslucentBlendMode", true},
{ "SupportsDynamicBlendMode", true }
};
public static string Code => @"
HEADER
{{
Description = ""{0}"";
}}
FEATURES
{{
#include ""common/features.hlsl""
{1}
}}
MODES
{{
Forward();
Depth();
ToolsShadingComplexity( ""tools_shading_complexity.shader"" );
}}
COMMON
{{
{2}
#include ""common/shared.hlsl""
#include ""procedural.hlsl""
#define S_UV2 1
}}
struct VertexInput
{{
#include ""common/vertexinput.hlsl""
float4 vColor : COLOR0 < Semantic( Color ); >;
{3}
}};
struct PixelInput
{{
#include ""common/pixelinput.hlsl""
float3 vPositionOs : TEXCOORD14;
float3 vNormalOs : TEXCOORD15;
float4 vTangentUOs_flTangentVSign : TANGENT < Semantic( TangentU_SignV ); >;
float4 vColor : COLOR0;
float4 vTintColor : COLOR1;
#if ( PROGRAM == VFX_PROGRAM_PS )
bool vFrontFacing : SV_IsFrontFace;
#endif
{4}
}};
VS
{{
#include ""common/vertex.hlsl""
{5}{6}{7}
PixelInput MainVs( VertexInput v )
{{
PixelInput i = ProcessVertex( v );
i.vPositionOs = v.vPositionOs.xyz;
i.vColor = v.vColor;
ExtraShaderData_t extraShaderData = GetExtraPerInstanceShaderData( v.nInstanceTransformID );
i.vTintColor = extraShaderData.vTint;
VS_DecodeObjectSpaceNormalAndTangent( v, i.vNormalOs, i.vTangentUOs_flTangentVSign );
{8}
return FinalizeVertex( i );
}}
}}
PS
{{
#include ""common/pixel.hlsl""
{9}{10}{11}
float4 MainPs( PixelInput i ) : SV_Target0
{{
Material m = Material::Init( i );
m.Albedo = float3( 1, 1, 1 );
m.Normal = float3( 0, 0, 1 );
m.Roughness = 1;
m.Metalness = 0;
m.AmbientOcclusion = 1;
m.TintMask = 1;
m.Opacity = 1;
m.Emission = float3( 0, 0, 0 );
m.Transmission = 0;
{12}
m.AmbientOcclusion = saturate( m.AmbientOcclusion );
m.Roughness = saturate( m.Roughness );
m.Metalness = saturate( m.Metalness );
m.Opacity = saturate( m.Opacity );
// Result node takes normal as tangent space, convert it to world space now
m.Normal = TransformNormal( m.Normal, i.vNormalWs, i.vTangentUWs, i.vTangentVWs );
// Toolvis:
m.WorldTangentU = i.vTangentUWs;
m.WorldTangentV = i.vTangentVWs;
m.TextureCoords = i.vTextureCoords.xy;
{13}
}}
}}
";
}