NLDClient-yudde/ProjectNLD/Assets/Code/Shaders/Scene/NLD_Scene_Opaque.shader

201 lines
6.8 KiB
Plaintext
Raw Normal View History

2023-11-30 17:02:48 +08:00
Shader "NLD_URP/NLD_Scene_Opaque"
2023-11-23 13:34:21 +08:00
{
2023-11-24 19:42:53 +08:00
Properties
2023-11-23 13:34:21 +08:00
{
[Header(Main map)]
2023-11-30 17:02:48 +08:00
_MainTex ("MainTex", 2D) = "white" {}
2023-12-07 16:41:41 +08:00
_Color("Base Tint", color) = (1,1,1,1)
2023-11-24 19:42:53 +08:00
2023-11-30 17:02:48 +08:00
_CutOff("Alpha测试值",Range(0,1)) = 0.5
_ShadowIntensity("阴影强度",Range(0,1)) = 1
_AmbientMultiplier("环境光强度",Range(0,2)) = 1
2023-11-23 13:34:21 +08:00
}
SubShader
{
2023-11-24 19:42:53 +08:00
Tags
{
2024-12-13 17:52:41 +08:00
"RenderType" = "Transparent"
"RenderPipeline" = "UniversalPipeline"
"IgnoreProjector" = "True"
2023-11-24 19:42:53 +08:00
}
2023-11-23 13:34:21 +08:00
pass
{
name "Base"
2023-11-24 19:42:53 +08:00
Tags
{
"LightMode" = "UniversalForward"
}
2023-11-24 16:01:32 +08:00
Cull Back
2023-11-24 19:42:53 +08:00
ZWrite On
2025-11-26 19:31:19 +08:00
ZTest Less
Blend SrcAlpha OneMinusSrcAlpha
2023-11-24 16:01:32 +08:00
//ZWrite On
2023-11-24 19:42:53 +08:00
2023-11-23 13:34:21 +08:00
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
2023-11-30 17:02:48 +08:00
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile_instancing
2023-11-24 19:42:53 +08:00
#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;
2023-11-23 13:34:21 +08:00
struct Attributes
{
float3 positionOS : POSITION;
float3 normalOS : NORMAL;
2023-11-24 16:01:32 +08:00
// float4 tangentOS : TANGENT;
2023-11-23 13:34:21 +08:00
float4 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
2023-11-23 13:34:21 +08:00
};
struct Varyings
{
2023-11-30 17:02:48 +08:00
float2 uv : TEXCOORD0;
float3 normalWS : TEXCOORD1;
float3 positionWS : TEXCOORD2;
2023-11-23 13:34:21 +08:00
float4 positionCS : SV_POSITION;
2023-11-30 17:02:48 +08:00
float4 shadowCoord : TEXCOORD3;
2023-11-23 13:34:21 +08:00
};
2023-11-24 19:42:53 +08:00
Varyings vert(Attributes input)
2023-11-23 13:34:21 +08:00
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
2023-11-23 13:34:21 +08:00
VertexPositionInputs positionInputs = GetVertexPositionInputs(input.positionOS);
output.positionWS = positionInputs.positionWS;
output.positionCS = positionInputs.positionCS;
2023-11-24 19:42:53 +08:00
2023-11-24 16:01:32 +08:00
VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS);
2023-11-23 13:34:21 +08:00
output.normalWS = vertexNormalInput.normalWS;
2023-11-24 19:42:53 +08:00
2023-11-23 13:34:21 +08:00
output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
2023-11-30 17:02:48 +08:00
output.shadowCoord = GetShadowCoord(positionInputs);
2023-11-23 13:34:21 +08:00
return output;
}
2023-11-24 19:42:53 +08:00
float4 frag(Varyings input) : SV_Target
2023-11-23 13:34:21 +08:00
{
#if _MAIN_LIGHT_SHADOWS
2023-11-30 17:02:48 +08:00
Light mainLight = GetMainLight(input.shadowCoord);
#else
2023-11-24 16:01:32 +08:00
Light mainLight = GetMainLight();
2023-11-30 17:02:48 +08:00
#endif
2023-11-24 19:42:53 +08:00
2023-11-23 13:34:21 +08:00
float3 N = normalize(input.normalWS);
float3 L = normalize(mainLight.direction);
2023-11-30 17:02:48 +08:00
half4 baseColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
2024-12-13 17:52:41 +08:00
clip(baseColor.a - _CutOff);
2023-11-30 17:02:48 +08:00
2023-12-07 16:41:41 +08:00
half3 albedo = baseColor * _Color;
2023-11-30 17:02:48 +08:00
half3 mainLightColor = mainLight.color.rgb;
2023-11-24 16:01:32 +08:00
//漫反射
2023-11-30 17:02:48 +08:00
half3 diffuse = saturate(dot(N, L)) * albedo * mainLightColor;
diffuse *= lerp(1, mainLight.shadowAttenuation, _ShadowIntensity);
2023-11-24 19:42:53 +08:00
2023-11-30 17:02:48 +08:00
//全局光照,目前只有环境光提供的部分
//参考URP内置SimpleLit直接采样球谐函数
//没有考虑环境光遮蔽,光照贴图等,此计算方式目前正确,但不敢保证后续不会出错
//如果没有该部分光照,由于没有光照探针和光照贴图提供的间接光照,模型背光面会很黑,所以这部分是用来弥补间接光照的
half3 gi = SampleSH(input.normalWS) * albedo * _AmbientMultiplier;
half3 finalColor = diffuse + gi;
2023-11-27 13:15:08 +08:00
float4 col = float4(finalColor, 1);
2023-11-24 19:42:53 +08:00
2023-11-23 13:34:21 +08:00
return col;
}
ENDHLSL
}
2023-11-24 19:42:53 +08:00
2023-11-23 13:34:21 +08:00
Pass
{
Name "ShadowCaster"
2023-11-24 19:42:53 +08:00
Tags
{
"LightMode" = "ShadowCaster"
}
2023-11-23 13:34:21 +08:00
ZWrite On
ColorMask 0
HLSLPROGRAM
#pragma target 3.0
2023-11-23 13:34:21 +08:00
#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment
#pragma multi_compile_instancing
2023-11-30 17:02:48 +08:00
2023-11-23 13:34:21 +08:00
// -------------------------------------
// Universal Pipeline keywords
2023-11-23 13:34:21 +08:00
// This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
2023-11-24 19:42:53 +08:00
float3 _LightDirection;
float3 _LightPosition;
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
half4 _MainTex_ST;
2024-12-13 17:52:41 +08:00
half _CutOff;
2023-11-23 13:34:21 +08:00
struct AttributesLean
{
2023-11-24 19:42:53 +08:00
float4 position : POSITION;
float3 normalOS : NORMAL;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
2023-11-23 13:34:21 +08:00
};
struct VaryingsLean
{
2023-11-24 19:42:53 +08:00
float4 clipPos : SV_POSITION;
float2 texcoord : TEXCOORD0;
2023-11-23 13:34:21 +08:00
};
VaryingsLean ShadowPassVertex(AttributesLean v)
{
VaryingsLean o = (VaryingsLean)0;
UNITY_SETUP_INSTANCE_ID(v);
2023-11-23 13:34:21 +08:00
float3 positionWS = TransformObjectToWorld(v.position.xyz);
float3 normalWS = TransformObjectToWorldNormal(v.normalOS);
float3 lightDirectionWS = _LightDirection;
float4 clipPos = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
2023-11-24 19:42:53 +08:00
#if UNITY_REVERSED_Z
2023-11-23 13:34:21 +08:00
clipPos.z = min(clipPos.z, UNITY_NEAR_CLIP_VALUE);
2023-11-24 19:42:53 +08:00
#else
2023-11-23 13:34:21 +08:00
clipPos.z = max(clipPos.z, UNITY_NEAR_CLIP_VALUE);
2023-11-24 19:42:53 +08:00
#endif
2023-11-23 13:34:21 +08:00
o.clipPos = clipPos;
2023-11-23 13:34:21 +08:00
o.texcoord = v.texcoord;
return o;
}
half4 ShadowPassFragment(VaryingsLean IN) : SV_TARGET
{
2023-11-24 19:42:53 +08:00
float4 texcol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.texcoord);
2024-12-13 17:52:41 +08:00
clip(texcol.a - _CutOff);
2023-11-23 13:34:21 +08:00
return 0;
}
ENDHLSL
}
}
2023-11-24 19:42:53 +08:00
}