NLDClient-yudde/ProjectNLD/Assets/Code/Shaders/Character/NLD_Charactor.shader

299 lines
10 KiB
Plaintext

Shader "NLD_URP/NLD_Charactor"
{
Properties
{
// [MainColor] _BaseColor ("Main Color", color) = (1,1,1,1)
[MainTexture] _MainTex ("Texture", 2D) = "white" {}
_MaskTex ("Mask", 2D) = "white" {}
_OutlineColor("Outline Color",color)=(0.18,0.1098,0.0549,1)
_Outline("Thick of Outline",range(0,0.1))=0.008
_Factor("Factor",range(0,1)) = 0.32
// _Metillic("Metillic", range(0,1.0)) = 0.5
// _Smoothness("Smoothness", range(0,1.0)) = 0.5
// _emission("Emission", range(0, 1.0)) = 0.5
_shadowScale("ShadowScale", range(0, 1.0)) = 0.5
// _specularColor("SpecularColor", color) = (1,1,1,1)
_specularScaleG("SpecularScaleG", range(0, 10.0)) = 2.0
_specularScaleB("SpecularScaleB", range(0, 10.0)) = 2.0
}
SubShader
{
Tags {
"RenderType"="Opaque"
"RenderPipeline" = "UniversalPipeline"
}
LOD 100
Pass
{
Cull Off
Name "ForwardLit"
Tags {
"LightMode" = "UniversalForward"
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
// // 开启接收阴影
// #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS //开启额外光源
// #pragma multi_compile _ _MAIN_LIGHT_SHADOWS //主光源阴影
// #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE //主光源层级阴影是否开启
// #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS //额外光源阴影
// #pragma multi_compile _ _SHADOWS_SOFT //软阴影
#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/Shaders/SimpleLitInput.hlsl"
struct appdata
{
float4 positionOS : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 positionHCS : SV_POSITION;
float3 normalWS : TEXCOORD1;
float3 positionWS : TEXCOORD02;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_MaskTex);
SAMPLER(sampler_MaskTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
float4 _MaskTex_ST;
// float _Metillic;
// float _Smoothness;
// float4 _specularColor;
// float _emission;
float _specularScaleB;
float _specularScaleG;
float _shadowScale;
CBUFFER_END
v2f vert (appdata v)
{
UNITY_SETUP_INSTANCE_ID(v);
v2f o;
UNITY_TRANSFER_INSTANCE_ID(v,o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.positionHCS = TransformObjectToHClip(v.positionOS.xyz);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.normalWS = TransformObjectToWorldNormal(v.normal);
o.positionWS = TransformObjectToWorld(v.positionOS);
return o;
}
half4 frag (const v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
// sample the texture
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
if (color.a < 0.5) discard;
// AlphaDiscard(color.a, 0.5);
color.a = 1;
half4 colorMask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
float r = colorMask.r;
// //当前模型接收阴影
// float4 SHADOW_COORDS = TransformWorldToShadowCoord(i.positionWS);
//
// // 光照阴影数据
// half shadow = MainLightRealtimeShadow(SHADOW_COORDS);
// return half4(shadow, shadow, shadow, 1.0);
half shadow = 1.0;
if ( shadow < 0.5 || r < 0.5) {
color.xyz *= _shadowScale;
}
if (colorMask.b > 0.5) {
color.xyz *= _specularScaleB;
}
if (colorMask.g > 0.5) {
color.xyz *= _specularScaleG;
}
// if (colorMask.g > 0.5) {
// SurfaceData surfaceData = (SurfaceData)0;
// surfaceData.alpha = 1;
// surfaceData.albedo = color.rgb;
// // 金属度
// surfaceData.metallic = _Metillic;
// surfaceData.specular = half3(0,0,0);
// surfaceData.smoothness = _Smoothness;
// surfaceData.normalTS = i.normalWS;
// surfaceData.occlusion = 1.0;
// surfaceData.emission = _emission;
// InputData inputData = (InputData)0;
// inputData.positionWS = i.positionWS;
// half3 viewDirWS = GetWorldSpaceNormalizeViewDir(inputData.positionWS);
// inputData.normalWS = i.normalWS;
// inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
// viewDirWS = SafeNormalize(viewDirWS);
// inputData.viewDirectionWS = viewDirWS;
// inputData.shadowCoord = float4(0,0,0,0);
// half4 bpColor = UniversalFragmentPBR(inputData, surfaceData);
// return bpColor;
// }
return color;
}
ENDHLSL
}
Pass {
Name "Outline"
Tags {"LightMode"="SRPDefaultUnlit"}
Cull Front
ZTest Less
ZWrite On
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 positionHCS : SV_POSITION;
// float3 normal : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
// This macro declares _BaseMap as a Texture2D object.
TEXTURE2D(_MainTex);
// This macro declares the sampler for the _BaseMap texture.
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
float _Outline;
float4 _OutlineColor;
float _Factor;
CBUFFER_END
v2f vert (appdata v)
{
UNITY_SETUP_INSTANCE_ID(v);
v2f o;
UNITY_TRANSFER_INSTANCE_ID(v,o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float3 dir = TransformObjectToWorldNormal(v.normal);
float3 dir2 = v.normal;
float D = dot(dir, dir2);
dir = dir*sign(D);
dir = dir*_Factor + dir2*(1-_Factor);
v.vertex.xyz += dir*_Outline;
o.positionHCS = TransformObjectToHClip(v.vertex.xyz);
return o;
}
half4 frag (v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
// sample the texture
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
if (color.a < 0.1) discard;
half4 c = _OutlineColor;
return c;
}
ENDHLSL
}
Pass {
Name "ShadowCaster"
Tags
{
"LightMode" = "ShadowCaster"
}
// -------------------------------------
// Render State Commands
ZWrite On
ZTest LEqual
ColorMask 0
Cull Back
HLSLPROGRAM
#pragma target 2.0
// -------------------------------------
// Shader Stages
#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment
// -------------------------------------
// Material Keywords
#pragma shader_feature_local_fragment _ALPHATEST_ON
#pragma shader_feature_local_fragment _GLOSSINESS_FROM_BASE_ALPHA
// -------------------------------------
// Unity defined keywords
#pragma multi_compile_fragment _ LOD_FADE_CROSSFADE
//--------------------------------------
// GPU Instancing
#pragma multi_compile_instancing
// #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
// 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
// -------------------------------------
// Includes
#include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
ENDHLSL
}
}
}