206 lines
6.9 KiB
Plaintext
206 lines
6.9 KiB
Plaintext
Shader "NLD_URP/NLD_Scene_Opaque_D"
|
|
{
|
|
Properties
|
|
{
|
|
[Header(Main map)]
|
|
_MainTex ("MainTex", 2D) = "white" {}
|
|
_Color("Base Tint", color) = (1,1,1,1)
|
|
|
|
_CutOff("Alpha测试值",Range(0,1)) = 0.5
|
|
_ShadowIntensity("阴影强度",Range(0,1)) = 1
|
|
_AmbientMultiplier("环境光强度",Range(0,2)) = 1
|
|
|
|
[Header(Rendering)]
|
|
[Enum(Off,0,Front,1,Back,2)] _CullMode ("剔除模式", Float) = 0 // 0 = 双面渲染
|
|
[Toggle(_TWOSIDED_LIGHTING)] _TwoSidedLighting ("双面光照", Float) = 1 // 使背面亮度与正面一致
|
|
}
|
|
SubShader
|
|
{
|
|
Tags
|
|
{
|
|
"RenderType" = "Transparent"
|
|
"RenderPipeline" = "UniversalPipeline"
|
|
"IgnoreProjector" = "True"
|
|
}
|
|
pass
|
|
{
|
|
name "Base"
|
|
Tags
|
|
{
|
|
"LightMode" = "UniversalForward"
|
|
}
|
|
Cull [_CullMode] // 应用剔除模式
|
|
ZWrite On
|
|
ZTest Less
|
|
Blend SrcAlpha OneMinusSrcAlpha
|
|
|
|
HLSLPROGRAM
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
|
|
#pragma multi_compile_instancing
|
|
#pragma shader_feature_local _ _TWOSIDED_LIGHTING // 支持双面光照开关(避免多余变体)
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/GlobalIllumination.hlsl"
|
|
|
|
TEXTURE2D(_MainTex);
|
|
SAMPLER(sampler_MainTex);
|
|
|
|
half4 _MainTex_ST;
|
|
half4 _Color;
|
|
|
|
half _CutOff;
|
|
half _ShadowIntensity;
|
|
half _AmbientMultiplier;
|
|
half _TwoSidedLighting; // 在 HLSL 中声明,以便在 frag 中使用
|
|
|
|
struct Attributes
|
|
{
|
|
float3 positionOS : POSITION;
|
|
float3 normalOS : NORMAL;
|
|
float4 texcoord : TEXCOORD0;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float2 uv : TEXCOORD0;
|
|
float3 normalWS : TEXCOORD1;
|
|
float3 positionWS : TEXCOORD2;
|
|
float4 positionCS : SV_POSITION;
|
|
float4 shadowCoord : TEXCOORD3;
|
|
};
|
|
|
|
Varyings vert(Attributes input)
|
|
{
|
|
Varyings output;
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
VertexPositionInputs positionInputs = GetVertexPositionInputs(input.positionOS);
|
|
output.positionWS = positionInputs.positionWS;
|
|
output.positionCS = positionInputs.positionCS;
|
|
|
|
VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS);
|
|
output.normalWS = vertexNormalInput.normalWS;
|
|
|
|
output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
|
|
output.shadowCoord = GetShadowCoord(positionInputs);
|
|
return output;
|
|
}
|
|
|
|
float4 frag(Varyings input) : SV_Target
|
|
{
|
|
#if _MAIN_LIGHT_SHADOWS
|
|
Light mainLight = GetMainLight(input.shadowCoord);
|
|
#else
|
|
Light mainLight = GetMainLight();
|
|
#endif
|
|
|
|
float3 N = normalize(input.normalWS);
|
|
float3 L = normalize(mainLight.direction);
|
|
|
|
half4 baseColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
|
|
clip(baseColor.a - _CutOff);
|
|
|
|
half3 albedo = baseColor * _Color;
|
|
half3 mainLightColor = mainLight.color.rgb;
|
|
|
|
// 漫反射系数:双面光照开启时取绝对值,关闭时使用 clamp
|
|
half NdotL = dot(N, L);
|
|
#if defined(_TWOSIDED_LIGHTING) && _TWOSIDED_LIGHTING
|
|
NdotL = abs(NdotL); // 背面获得相同光照
|
|
#else
|
|
NdotL = saturate(NdotL);
|
|
#endif
|
|
|
|
half3 diffuse = NdotL * albedo * mainLightColor;
|
|
diffuse *= lerp(1, mainLight.shadowAttenuation, _ShadowIntensity);
|
|
|
|
// 全局光照(球谐环境光)
|
|
half3 gi = SampleSH(input.normalWS) * albedo * _AmbientMultiplier;
|
|
|
|
half3 finalColor = diffuse + gi;
|
|
float4 col = float4(finalColor, 1);
|
|
|
|
return col;
|
|
}
|
|
ENDHLSL
|
|
}
|
|
|
|
Pass
|
|
{
|
|
Name "ShadowCaster"
|
|
Tags
|
|
{
|
|
"LightMode" = "ShadowCaster"
|
|
}
|
|
|
|
Cull [_CullMode] // 阴影投射也跟随剔除模式
|
|
ZWrite On
|
|
ColorMask 0
|
|
|
|
HLSLPROGRAM
|
|
#pragma target 3.0
|
|
#pragma vertex ShadowPassVertex
|
|
#pragma fragment ShadowPassFragment
|
|
#pragma multi_compile_instancing
|
|
|
|
// 阴影投射不需要双面光照开关,仅纹理采样和 Alpha 测试
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
|
|
|
float3 _LightDirection;
|
|
float3 _LightPosition;
|
|
TEXTURE2D(_MainTex);
|
|
SAMPLER(sampler_MainTex);
|
|
half4 _MainTex_ST;
|
|
|
|
half _CutOff;
|
|
|
|
struct AttributesLean
|
|
{
|
|
float4 position : POSITION;
|
|
float3 normalOS : NORMAL;
|
|
float2 texcoord : TEXCOORD0;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
struct VaryingsLean
|
|
{
|
|
float4 clipPos : SV_POSITION;
|
|
float2 texcoord : TEXCOORD0;
|
|
};
|
|
|
|
VaryingsLean ShadowPassVertex(AttributesLean v)
|
|
{
|
|
VaryingsLean o = (VaryingsLean)0;
|
|
UNITY_SETUP_INSTANCE_ID(v);
|
|
float3 positionWS = TransformObjectToWorld(v.position.xyz);
|
|
float3 normalWS = TransformObjectToWorldNormal(v.normalOS);
|
|
|
|
float3 lightDirectionWS = _LightDirection;
|
|
|
|
float4 clipPos = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
|
|
|
|
#if UNITY_REVERSED_Z
|
|
clipPos.z = min(clipPos.z, UNITY_NEAR_CLIP_VALUE);
|
|
#else
|
|
clipPos.z = max(clipPos.z, UNITY_NEAR_CLIP_VALUE);
|
|
#endif
|
|
|
|
o.clipPos = clipPos;
|
|
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
|
|
return o;
|
|
}
|
|
|
|
half4 ShadowPassFragment(VaryingsLean IN) : SV_TARGET
|
|
{
|
|
float4 texcol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.texcoord);
|
|
clip(texcol.a - _CutOff);
|
|
return 0;
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
} |