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

203 lines
7.2 KiB
Plaintext
Raw Normal View History

2023-12-01 12:39:19 +08:00
Shader "NLD_URP/NLD_Scene_Transparent"
{
//目前效果还有问题可能需要新加pass写入深度一次
Properties
{
[Header(Main map)]
_MainTex ("MainTex", 2D) = "white" {}
2023-12-07 16:41:41 +08:00
_Color("Base Tint", color) = (1,1,1,1)
2023-12-01 12:39:19 +08:00
_ShadowIntensity("阴影强度",Range(0,1)) = 1
_AmbientMultiplier("环境光强度",Range(0,2)) = 1
}
SubShader
{
Tags
{
"RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" "Queue" = "Transparent"
}
pass
{
name "Base"
Tags
{
"LightMode" = "UniversalForward"
}
Cull Back
ZWrite Off
ZTest On
Blend SrcAlpha OneMinusSrcAlpha
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile_instancing
#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;
2023-12-07 16:41:41 +08:00
half4 _Color;
2023-12-01 12:39:19 +08:00
half _ShadowIntensity;
half _AmbientMultiplier;
struct Attributes
{
float3 positionOS : POSITION;
float3 normalOS : NORMAL;
// float4 tangentOS : TANGENT;
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 || _MAIN_LIGHT_SHADOWS_CASCADE
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);
2023-12-07 16:41:41 +08:00
half4 albedo = baseColor * _Color;
2023-12-01 12:39:19 +08:00
half3 mainLightColor = mainLight.color.rgb;
//漫反射
half3 diffuse = saturate(dot(N, L)) * albedo * mainLightColor;
diffuse *= lerp(1, mainLight.shadowAttenuation, _ShadowIntensity);
//全局光照,目前只有环境光提供的部分
//参考URP内置SimpleLit直接采样球谐函数
//没有考虑环境光遮蔽,光照贴图等,此计算方式目前正确,但不敢保证后续不会出错
//如果没有该部分光照,由于没有光照探针和光照贴图提供的间接光照,模型背光面会很黑,所以这部分是用来弥补间接光照的
half3 gi = SampleSH(input.normalWS) * albedo * _AmbientMultiplier;
half3 finalColor = diffuse + gi;
float4 col = float4(finalColor, albedo.a);
return col;
}
ENDHLSL
}
2023-12-10 15:21:33 +08:00
Pass
{
Name "ShadowCaster"
Tags
{
"LightMode" = "ShadowCaster"
}
ZWrite On
ColorMask 0
HLSLPROGRAM
#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment
//#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile_instancing
// #pragma shader_feature _ALPHA_TEST_ON
// -------------------------------------
// Universal Pipeline keywords
// This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
#pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW
#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;
UNITY_VERTEX_OUTPUT_STEREO
};
VaryingsLean ShadowPassVertex(AttributesLean v)
{
VaryingsLean o = (VaryingsLean)0;
UNITY_SETUP_INSTANCE_ID(v);
float3 positionWS = TransformObjectToWorld(v.position.xyz);
float3 normalWS = TransformObjectToWorldNormal(v.normalOS);
#if _CASTING_PUNCTUAL_LIGHT_SHADOW
float3 lightDirectionWS = normalize(_LightPosition - positionWS);
#else
float3 lightDirectionWS = _LightDirection;
#endif
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 = v.texcoord;
return o;
}
half4 ShadowPassFragment(VaryingsLean IN) : SV_TARGET
{
float4 texcol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.texcoord);
clip(texcol.a - 0.5);
return 0;
}
ENDHLSL
}
2023-12-01 12:39:19 +08:00
}
2023-12-10 15:21:33 +08:00
//Fallback "VertexLit"
2023-12-01 12:39:19 +08:00
}