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

408 lines
15 KiB
Plaintext

Shader "NLD_URP/NLD_Scene_Lightmap"
{
Properties
{
[MainTexture] _BaseMap("Base Map (RGB)", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
_Cutoff("Alpha Clipping", Range(0.0, 1.0)) = 0.5
_MaskTex ("Mask", 2D) = "white" {}
_OutlineColor("Outline Color",color)=(0.1,0.1,0.2,1)
_Outline("Thick of Outline",range(0,0.1))=0.02
_Factor("Factor",range(0,1)) = 0.5
_Smoothness("Smoothness", range(0,1.0)) = 0.5
_shadowScale("ShadowScale", range(0, 1.0)) = 0.5
_specularScaleB("SpecularScaleB", range(0, 10.0)) = 2.0
_Gloss("Gloss", range(0, 10.0)) = 2.0
_SpecialColor("SpecialColor", color) = (1,1,1,1)
_SpecialStrengh("_SpecialStrengh", range(0, 10.0)) = 1
_DiffuseStrengh("DiffuseStrengh", range(0, 10)) = 1
_MinDiffuse("MinDiffuse", range(0, 1)) = 0.1
_BakeGIScale("BakeGIScale", Range(0, 10)) = 3
[HideInInspector][NoScaleOffset]unity_Lightmaps("unity_Lightmaps", 2DArray) = "" {}
[HideInInspector][NoScaleOffset]unity_LightmapsInd("unity_LightmapsInd", 2DArray) = "" {}
[HideInInspector][NoScaleOffset]unity_ShadowMasks("unity_ShadowMasks", 2DArray) = "" {}
}
SubShader
{
Tags {
"RenderType"="Opaque"
"RenderPipeline" = "UniversalPipeline"
}
LOD 100
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
TEXTURE2D(_MaskTex);
SAMPLER(sampler_MaskTex);
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
half4 _BaseColor;
half _Cutoff;
half _Surface;
float4 _MaskTex_ST;
float _Smoothness;
float _specularScaleB;
float _specularScaleG;
float _shadowScale;
float _Gloss;
float4 _SpecialColor;
float _SpecialStrengh;
float _DiffuseStrengh;
float _MinDiffuse;
float _Outline;
float4 _OutlineColor;
float _Factor;
float _BakeGIScale;
CBUFFER_END
inline void InitializeSimpleLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
{
outSurfaceData = (SurfaceData)0;
half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
outSurfaceData.alpha = albedoAlpha.a * _BaseColor.a;
outSurfaceData.alpha = AlphaDiscard(outSurfaceData.alpha, _Cutoff);
outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;
outSurfaceData.albedo = AlphaModulate(outSurfaceData.albedo, outSurfaceData.alpha);
}
ENDHLSL
Pass
{
Name "ForwardLit"
Tags
{
"LightMode" = "UniversalForward"
}
// -------------------------------------
// Render State Commands
// Use same blending / depth states as Standard shader
ZWrite On
Cull Back
HLSLPROGRAM
#pragma target 2.0
// -------------------------------------
// Shader Stages
#pragma vertex LitPassVertexSimple2
#pragma fragment LitPassFragmentSimple2
// -------------------------------------
// Material Keywords
#pragma shader_feature_local _NORMALMAP
#pragma shader_feature_local_fragment _EMISSION
#pragma shader_feature_local _RECEIVE_SHADOWS_OFF
#pragma shader_feature_local_fragment _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature_local_fragment _ALPHATEST_ON
#pragma shader_feature_local_fragment _ _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON
#pragma shader_feature_local_fragment _ _SPECGLOSSMAP _SPECULAR_COLOR
#pragma shader_feature_local_fragment _GLOSSINESS_FROM_BASE_ALPHA
// -------------------------------------
// Universal Pipeline keywords
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile _ EVALUATE_SH_MIXED EVALUATE_SH_VERTEX
#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
#pragma multi_compile _ SHADOWS_SHADOWMASK
#pragma multi_compile _ _LIGHT_LAYERS
#pragma multi_compile _ _FORWARD_PLUS
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile_fragment _ _SHADOWS_SOFT
#pragma multi_compile_fragment _ _SHADOWS_SOFT_LOW _SHADOWS_SOFT_MEDIUM _SHADOWS_SOFT_HIGH
#pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
#pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3
#pragma multi_compile_fragment _ _LIGHT_COOKIES
#include_with_pragmas "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRenderingKeywords.hlsl"
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl"
// -------------------------------------
// Unity defined keywords
#pragma multi_compile _ DIRLIGHTMAP_COMBINED
#pragma multi_compile _ LIGHTMAP_ON
//--------------------------------------
// GPU Instancing
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
// -------------------------------------
// Includes
// #include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitForwardPass.hlsl"
// Used in Standard (Simple Lighting) shader
Varyings LitPassVertexSimple2(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
output.positionWS.xyz = vertexInput.positionWS;
output.positionCS = vertexInput.positionCS;
output.normalWS = NormalizeNormalPerVertex(normalInput.normalWS);
OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
output.shadowCoord = GetShadowCoord(vertexInput);
#endif
return output;
}
half3 CalculateLightingColor2(LightingData lightingData, half3 albedo)
{
half3 lightingColor = 0;
if (IsLightingFeatureEnabled(DEBUGLIGHTINGFEATUREFLAGS_GLOBAL_ILLUMINATION))
{
lightingColor += lightingData.giColor;
}
if (IsLightingFeatureEnabled(DEBUGLIGHTINGFEATUREFLAGS_MAIN_LIGHT))
{
lightingColor += lightingData.mainLightColor;
}
if (IsLightingFeatureEnabled(DEBUGLIGHTINGFEATUREFLAGS_ADDITIONAL_LIGHTS))
{
lightingColor += lightingData.additionalLightsColor;
}
if (IsLightingFeatureEnabled(DEBUGLIGHTINGFEATUREFLAGS_VERTEX_LIGHTING))
{
lightingColor += lightingData.vertexLightingColor;
}
lightingColor *= albedo;
return lightingColor;
}
half4 CalculateFinalColor2(LightingData lightingData, half alpha)
{
half3 finalColor = CalculateLightingColor2(lightingData, 1);
return half4(finalColor, alpha);
}
half3 CalculateBlinnPhong2(Light light, InputData inputData, SurfaceData surfaceData)
{
half3 attenuatedLightColor = light.color;
half3 lightDiffuseColor = LightingLambert(attenuatedLightColor, light.direction, inputData.normalWS);
return lightDiffuseColor * surfaceData.albedo;
}
////////////////////////////////////////////////////////////////////////////////
/// Phong lighting...
////////////////////////////////////////////////////////////////////////////////
half4 UniversalFragmentBlinnPhong2(InputData inputData, SurfaceData surfaceData, half4 colorMask)
{
half4 shadowMask = CalculateShadowMask(inputData);
Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, shadowMask);
half lightAttenuation = mainLight.distanceAttenuation * mainLight.shadowAttenuation;
#if LIGHTMAP_ON
half giR = inputData.bakedGI.r;
#else
half giR = 1;
#endif
// return lightAttenuation;
// return half4(inputData.bakedGI.rgb, 1);
// return giR;
giR = giR * _BakeGIScale;
// if (giR > 0.1)
// {
// giR = 1;
// }
surfaceData.albedo *= max(min(lightAttenuation, giR), 0.5);
if (colorMask.b > 0.5) {
surfaceData.albedo *= _specularScaleB;
}
if (colorMask.g > 0.5)
{
inputData.bakedGI *= surfaceData.albedo;
LightingData lightingData = CreateLightingData(inputData, surfaceData);
{
lightingData.mainLightColor += CalculateBlinnPhong2(mainLight, inputData, surfaceData);
}
return CalculateFinalColor2(lightingData, surfaceData.alpha);
}
return half4(surfaceData.albedo, surfaceData.alpha);
}
// Used for StandardSimpleLighting shader
void LitPassFragmentSimple2(
Varyings input
, out half4 outColor : SV_Target0
)
{
UNITY_SETUP_INSTANCE_ID(input);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
SurfaceData surfaceData;
InitializeSimpleLitSurfaceData(input.uv, surfaceData);
if (surfaceData.alpha < _Cutoff)
{
discard;
}
InputData inputData;
InitializeInputData(input, surfaceData.normalTS, inputData);
half4 colorMask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, input.uv);
half4 color = UniversalFragmentBlinnPhong2(inputData, surfaceData, colorMask);
outColor = color;
}
ENDHLSL
}
Pass {
Name "Outline"
Tags {"LightMode"="Outline"}
Cull Front
ZTest Less
ZWrite On
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 positionHCS : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
float3 dir = TransformObjectToWorldNormal(v.positionOS.xyz);
float3 dir2 = v.positionOS.xyz;
float D = dot(dir, dir2);
dir = dir*sign(D);
dir = dir*_Factor + dir2*(1-_Factor);
v.positionOS.xyz += dir*_Outline;
o.positionHCS = TransformObjectToHClip(v.positionOS.xyz);
return o;
}
half4 frag (v2f i) : SV_Target
{
// sample the texture
half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv);
if (color.a < 0.5) 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 _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/ShadowCasterPass.hlsl"
ENDHLSL
}
}
}