模型手部和腿上部分布料变黑
Shader "yomiyaFace"
{
Properties
{
_BaseMap("Base Map", 2D) = "white"{}
_RampMap("Ramp Map", 2D) = "white"{}
_LightMap("Light Map", 2D) = "white"{}
_LightMapColor("LightMap Color",Color)=(1,1,1,1)
_RampShadowRange("Ramp ShadowRange",Range(0,1)) = 1
_RampMapRangStart("RampMapRang Start",Float) = 1
_RampMapFields("RampMap Fields",Float)=1
_RampAOLerp("Ramp AOLerp",Range(0,10))=1
_DarkIntensity("DarkIntensity",Float)=1
_ShadowRampLerp("Shadow RampLerp",Float)=1
_BrightIntensity("Bright Intensity",Float)=1
_CharacterIntensity("Character Intensity",Float)=1
}
SubShader
{
Pass
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
LOD 100
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#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.core/ShaderLibrary/UnityInstancing.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
float4 normalOS :NORMAL;
float4 tangentOS :TANGENT;
float4 color :COLOR;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float4 normalOS :TEXCOORD1;
float4 tangentOS :TEXCOORD2;
float4 normalWS :TEXCOORD3;
float4 tangentWS :TEXCOORD4;
float4 bitangentWS :TEXCOORD5;
float3 viewDirWS :TEXCOORD6;
float3 positionWS :TEXCOORD7;
float3 positionVS :TEXCOORD8;
float4 color :COLOR;
};
CBUFFER_START(UnityPerMaterial)
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
TEXTURE2D(_RampMap);
SAMPLER(sampler_RampMap);
TEXTURE2D(_LightMap);
SAMPLER(sampler_LightMap);
float4 LightMapColor;
float _RampShadowRange;
float _RampMapRangStart;
float _RampMapFields;
float _RampAOLerp;
float _DarkIntensity;
float _ShadowRampLerp;
float _BrightIntensity;
float _CharacterIntensity;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings output;
VertexPositionInputs vertexInput = GetVertexPositionInputs(IN.positionOS.xyz);
VertexNormalInputs normalInput = GetVertexNormalInputs(IN.normalOS, IN.tangentOS);
float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
float3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
output.positionCS = vertexInput.positionCS;
output.positionWS = TransformObjectToWorld(IN.positionOS);
output.positionVS = TransformWorldToView(output.positionWS);
output.positionCS = TransformWorldToHClip(output.positionWS);
output.uv = IN.uv;
output.color = IN.color;
output.normalWS = float4(normalInput.normalWS, viewDirWS.x);
output.tangentWS = float4(normalInput.tangentWS, viewDirWS.y);
output.bitangentWS = float4(normalInput.bitangentWS, viewDirWS.z);
output.viewDirWS = viewDirWS;
return output;
}
half4 frag(Varyings IN) : SV_Target
{
Varyings output = (Varyings)0;
//贴图采样,以及参数声明
float4 VertexColor = (1,1,1,1);
float4 BaseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv.xy);
float4 LightMap = SAMPLE_TEXTURE2D(_LightMap, sampler_LightMap, IN.uv.xy);
float SpecularLayerMask = LightMap.r; // ⾼光类型Layer
float ShadowAOMask = LightMap.g; //ShadowAOMask
float SpecularIntensityMask = LightMap.b; //SpecularIntensityMask
float LayerMask = LightMap.a; //LayerMask Ramp类型Layer
// return VertexColor.a; //描边⼤⼩
float RampOffsetMask = VertexColor.g; //Ramp偏移值,值越⼤的区域 越容易"感光"(在⼀个特定的⾓度,偏移光照明暗)
//漫反射部分
float3 lightDirWS = normalize(_MainLightPosition.xyz);
float lambert = dot(output.normalWS, lightDirWS);
lambert = lambert * 0.5f + 0.5f;
float rampVmove = 0.0;
half halfLambert = max(0.0, lambert) * 0.5 + 0.5;
ShadowAOMask = 1 - smoothstep(saturate(ShadowAOMask), 0.2, 0.6);
float rampValue = halfLambert * lerp(0.5, 1.0, ShadowAOMask) * (1.0 / _RampShadowRange - 0.003);
float Day=1;
if (Day > 0.5) {
rampVmove += 0.5; //如果是白天,采样上面
}
else {
rampVmove += 0.0; //如果是夜晚,采样下面
}
half3 ShadowRamp1 = SAMPLE_TEXTURE2D(_RampMap, sampler_RampMap, float2(rampValue, 0.45 + rampVmove)).rgb;
half3 ShadowRamp2 = SAMPLE_TEXTURE2D(_RampMap, sampler_RampMap, float2(rampValue, 0.35 + rampVmove)).rgb;
half3 ShadowRamp3 = SAMPLE_TEXTURE2D(_RampMap, sampler_RampMap, float2(rampValue, 0.25 + rampVmove)).rgb;
half3 ShadowRamp4 = SAMPLE_TEXTURE2D(_RampMap, sampler_RampMap, float2(rampValue, 0.15 + rampVmove)).rgb;
half3 ShadowRamp5 = SAMPLE_TEXTURE2D(_RampMap, sampler_RampMap, float2(rampValue, 0.05 + rampVmove)).rgb;
/*
Skin = 1.0
Silk = 0.7
Metal = 0.5
Soft = 0.3
Hand = 0.0
*/
half3 skinRamp = step(abs(LightMapColor.a - 1), 0.015) * ShadowRamp1;
float3 tightsRamp = step(abs(LightMapColor.a - 0.7), 0.015) * ShadowRamp2;
float3 softCommonRamp = step(abs(LightMapColor.a - 0.5), 0.015) * ShadowRamp3;
half3 hardSilkRamp = step(abs(LightMapColor.a - 0.3), 0.015) * ShadowRamp4;
half3 metalRamp = step(abs(LightMapColor.a - 0.0), 0.015) * ShadowRamp5;
// 组合5个Ramp,得到最终的Ramp阴影,并根据rampValue与BaseColor结合。
half3 finalRamp = skinRamp + tightsRamp + metalRamp + hardSilkRamp + softCommonRamp;
/*
原来的分布代码
因为和阴影同时 出现
rampValue = step(_RampShadowRange, halfLambert) ;
half3 RampShadowColor = lerp(finalRamp * BaseColor.rgb, BaseColor.rgb, rampValue) ;
half3 RampShadowColor = rampValue * BaseColor.rgb + (1 - rampValue) * finalRamp * BaseColor.rgb;
*/
float3 LightColor = UNITY_LIGHTMODEL_AMBIENT.rgb;
float3 BaseMapShadowed = lerp(BaseColor.rgb * finalRamp, BaseColor.rgb, ShadowAOMask); //分布Ramp
BaseMapShadowed = lerp(BaseColor.rgb, BaseMapShadowed, _ShadowRampLerp); //阴影强度
//float IsBrightSide = ShadowAOMask * step(_LightThreshold, halfLambert); //获得亮部、暗部分布
float3 Diffuse = lerp(lerp(BaseMapShadowed, BaseColor.rgb * finalRamp, _RampAOLerp) * _DarkIntensity ,
_BrightIntensity * BaseMapShadowed , //分开亮部
ShadowAOMask * 0.5) * _CharacterIntensity;
return float4(BaseColor.rgb,1);
}
ENDHLSL
}
}
}