Skip to content

Commit

Permalink
#modify 修改Shader
Browse files Browse the repository at this point in the history
  • Loading branch information
falseeeeeeeeee committed Mar 12, 2024
1 parent b8e07bd commit b6ff600
Show file tree
Hide file tree
Showing 24 changed files with 448 additions and 331 deletions.
Binary file added RawData/3D/Character_StarRail/SmoothNormal.blend
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion ShaderLib_2022/Assets/Settings/URP/URP-HighFidelity.asset
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MonoBehaviour:
m_RendererDataList:
- {fileID: 11400000, guid: c40be3174f62c4acf8c1216858c64956, type: 2}
m_DefaultRendererIndex: 0
m_RequireDepthTexture: 0
m_RequireDepthTexture: 1
m_RequireOpaqueTexture: 0
m_OpaqueDownsampling: 1
m_SupportsTerrainHoles: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.02481086, g: 0.19641206, b: 0.8632811, a: 1}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -190,7 +190,7 @@ PrefabInstance:
- target: {fileID: 5262557494860613697, guid: dddac010d8cde7048891c0b91ddcedf6,
type: 3}
propertyPath: m_BlendShapeWeights.Array.data[29]
value: 100
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5262557494860613697, guid: dddac010d8cde7048891c0b91ddcedf6,
type: 3}
Expand Down Expand Up @@ -400,27 +400,32 @@ PrefabInstance:
- target: {fileID: 1490022623332948584, guid: d3a54affacf4dce4a90dbd501d885b8a,
type: 3}
propertyPath: m_LocalRotation.w
value: 0.631279
value: 0.23533396
objectReference: {fileID: 0}
- target: {fileID: 1490022623332948584, guid: d3a54affacf4dce4a90dbd501d885b8a,
type: 3}
propertyPath: m_LocalRotation.x
value: 0.2943702
value: 0.109738015
objectReference: {fileID: 0}
- target: {fileID: 1490022623332948584, guid: d3a54affacf4dce4a90dbd501d885b8a,
type: 3}
propertyPath: m_LocalRotation.y
value: 0.6502928
value: 0.875221
objectReference: {fileID: 0}
- target: {fileID: 1490022623332948584, guid: d3a54affacf4dce4a90dbd501d885b8a,
type: 3}
propertyPath: m_LocalRotation.z
value: -0.3032365
value: -0.4081222
objectReference: {fileID: 0}
- target: {fileID: 1490022623332948584, guid: d3a54affacf4dce4a90dbd501d885b8a,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 91.7
value: 869.9
objectReference: {fileID: 0}
- target: {fileID: 1490022623332948585, guid: d3a54affacf4dce4a90dbd501d885b8a,
type: 3}
propertyPath: m_ColorTemperature
value: 6000
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ float3 desaturation(float3 color)
return float3(grayf, grayf, grayf);
}

// 线性采样渐变映射
struct Gradient // 结构体
{
int colorsLength;
float4 colors[8];
};
Gradient GradientConstruct() // 构造函数
{
Gradient g;
g.colorsLength = 2;
g.colors[0] = float4(1, 1, 1, 0); // 第四位是在轴上的坐标
g.colors[1] = float4(1, 1, 1, 1);
g.colors[2] = float4(0, 0, 0, 0);
g.colors[3] = float4(0, 0, 0, 0);
g.colors[4] = float4(0, 0, 0, 0);
g.colors[5] = float4(0, 0, 0, 0);
g.colors[6] = float4(0, 0, 0, 0);
g.colors[7] = float4(0, 0, 0, 0);
return g;
}
float3 SampleGradient(Gradient Gradient, float Time) // 方法
{
float3 color = Gradient.colors[0].rgb;
for (int c = 1; c < Gradient.colorsLength; c++)
{
float colorPos = saturate((Time - Gradient.colors[c- 1 ].w) / (Gradient.colors[c].w - Gradient.colors[c - 1].w)) * step(c, Gradient.colorsLength - 1);
color = lerp(color, Gradient.colors[c].rgb, colorPos);
}
#ifdef UNITY_COLORSPACE_GAMMA
color = LinearToSRGB(color);
#endif
return color;
}

// -------------------------------------
// 基本结构体输入
struct Attributes
Expand Down Expand Up @@ -190,15 +224,92 @@ float4 StarRailPassFragment(Varyings input, bool isFrontFace : SV_IsFrontFace) :
specularColor *= mainLight.color;
specularColor *= _SpecularBrightness;
#endif

// Stockings
float3 stockingsEffect = 1;
#if _AREA_UPPERBODY || _AREA_LOWERBODY
float2 stockingsMapRG = 0;
float stockingsMapB = 0;
#if _AREA_UPPERBODY
stockingsMapRG = SAMPLE_TEXTURE2D(_UpperBodyStockings, sampler_UpperBodyStockings, input.uv).rg;
stockingsMapB = SAMPLE_TEXTURE2D(_UpperBodyStockings, sampler_UpperBodyStockings, input.uv * 20).b;
#elif _AREA_LOWERBODY
stockingsMapRG = SAMPLE_TEXTURE2D(_LowerBodyStockings, sampler_LowerBodyStockings, input.uv).rg;
stockingsMapB = SAMPLE_TEXTURE2D(_LowerBodyStockings, sampler_LowerBodyStockings, input.uv * 20).b;
#endif

float NoV = dot(normalWS, viewDirectionWS);
float fac = NoV;
fac = pow(saturate(fac), _StockingsTransitionPower);
fac = saturate((fac - _StockingsTransitionHardness / 2) / (1- _StockingsTransitionHardness)); // 亮暗过渡的硬度
fac = fac * (stockingsMapB * _StockingsTextureUsage + (1 - _StockingsTextureUsage)); // 混入细节纹理
fac = lerp(fac, 1, stockingsMapRG.g); // 厚度插值一下亮区

Gradient curve = GradientConstruct();
curve.colorsLength = 3;
curve.colors[0] = float4(_StockingsDarkColor.rgb, 0);
curve.colors[1] = float4(_StockingsTransitionColor.rgb, _StockingsTransitionThreshold);
curve.colors[2] = float4(_StockingsLightColor.rgb, 1);
float3 stockingsColor = SampleGradient(curve, fac);

stockingsEffect = lerp(1, stockingsColor, stockingsMapRG.r);
#endif

// 边缘光
float linearEyeDepth = LinearEyeDepth(input.positionCS.z, _ZBufferParams);
float3 normalVS = mul((float3x3)UNITY_MATRIX_V, normalWS);
float2 uvOffset = float2(sign(normalVS.x), 0) * _RimLightWidth / (1 + linearEyeDepth) / 100; // 法线的横坐标确定采样UV的偏移方向,乘偏移量,除以深度实现近粗远细,加1限制最大宽度
int2 loadTexPos = input.positionCS.xy + uvOffset * _ScaledScreenParams.xy; // 采样深度缓冲,把UV偏移转换成坐标偏移
loadTexPos = min(loadTexPos, _ScaledScreenParams.xy - 1);
float offsetSceneDepth = LoadSceneDepth(loadTexPos); // 在深度缓存上采样偏移像素的深度
float offsetLinearEyeDepth = LinearEyeDepth(offsetSceneDepth, _ZBufferParams); // 将非线性的深度缓存转换成线性的
float rimLight = saturate(offsetLinearEyeDepth - (linearEyeDepth + _RimLightThreshold)) / _RimLightFadeout;
float3 rimLightColor = rimLight * mainLight.color.rgb;
rimLightColor *= _RimLightTintColor;
rimLightColor *= _RimLightBrightness;

// 自发光
float3 emissionColor = 0;
#if _EMISSION_ON
emissionColor = areaMap.a;
emissionColor *= lerp(1, baseColor, _EmissionMixBaseColor);
emissionColor *= _EmissionTintColor;
emissionColor *=_EmissionIntensity;
#endif


// 脸部描边
float fakeOutlineEffect = 0;
float3 fakeOutlineColor = 0;
#if _AREA_FACE && _OUTLINE_ON
float fakeOutline = faceMap.b;
float3 headForwardShadow = normalize(_HeadForward);
fakeOutlineEffect = smoothstep(0.0, 0.25, pow(saturate(dot(headForwardShadow, viewDirectionWS)), 20) * fakeOutline);
float2 outlineUV = float2(0, 0.0625);
float3 coolRampShadow = SAMPLE_TEXTURE2D(_BodyCoolRamp, sampler_BodyCoolRamp, outlineUV).rgb;
float3 warmRampShadow = SAMPLE_TEXTURE2D(_BodyWarmRamp, sampler_BodyWarmRamp, outlineUV).rgb;
float3 ramp = lerp(coolRampShadow, warmRampShadow, 0.5);
fakeOutlineColor = pow(saturate(ramp), _OutlineGamma);
#endif

// Albedo
float3 albedo = 0;
albedo += indirectLightColor;
albedo += mianLightColor;
albedo += specularColor;
albedo *= stockingsEffect;
albedo += rimLightColor * lerp(1, albedo, _RimLightMixAlbedo);
albedo += emissionColor;
albedo = lerp(albedo, fakeOutlineColor, fakeOutlineEffect);

// Alpha
float alpha = _Alpha;

// 避免背部看到眉毛
#if _DRAW_OVERLAY_ON
float3 headForward = normalize(_HeadForward);
alpha = lerp(1, alpha, saturate(dot(headForward, viewDirectionWS))); // 越小Alpha越接近1
#endif

//Debug
switch(_DebugColor)
Expand All @@ -221,17 +332,15 @@ float4 StarRailPassFragment(Varyings input, bool isFrontFace : SV_IsFrontFace) :
case 7:
albedo = specularColor;
break;
/*case 8:
#if _AREA_UPPERBODY || _AREA_LOWERBODY
albedo = metallic;
#endif
break;*/
default:
// albedo = albedo;
break;
}

float4 color = float4(albedo, alpha);
clip(color.a - _AlphaClip);
color.rgb = MixFog(color.rgb, input.positionWSAndFogFactor.w);

return color;
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#ifndef SIH_STARRAIL_DRAWOUTLINE_PASS_INCLUDED
#define SIH_STARRAIL_DRAWOUTLINE_PASS_INCLUDED

// -------------------------------------
// 方法
// 根据相机距离调整描边宽度
float GetCameraFOV()
{
//https://answers.unity.com/questions/770838/how-can-i-extract-the-fov-information-from-the-pro.html
float t = unity_CameraProjection._m11;
float Rad2Deg = 180 / 3.1415;
float fov = atan(1.0f / t) * 2.0 * Rad2Deg;
return fov;
}
float ApplyOutlineDistanceFadeOut(float inputMulFix)
{
//make outline "fadeout" if character is too small in camera's view
return saturate(inputMulFix);
}
float GetOutlineCameraFovAndDistanceFixMultiplier(float positionVS_Z)
{
float cameraMulFix;
if(unity_OrthoParams.w == 0)
{
////////////////////////////////
// Perspective camera case
////////////////////////////////

// keep outline similar width on screen accoss all camera distance
cameraMulFix = abs(positionVS_Z);

// can replace to a tonemap function if a smooth stop is needed
cameraMulFix = ApplyOutlineDistanceFadeOut(cameraMulFix);

// keep outline similar width on screen accoss all camera fov
cameraMulFix *= GetCameraFOV();
}
else
{
////////////////////////////////
// Orthographic camera case
////////////////////////////////
float orthoSize = abs(unity_OrthoParams.y);
orthoSize = ApplyOutlineDistanceFadeOut(orthoSize);
cameraMulFix = orthoSize * 50; // 50 is a magic number to match perspective camera's outline width
}

return cameraMulFix * 0.00005; // mul a const to make return result = default normal expand amount WS
}

// -------------------------------------
// 基本结构体输入
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

// -------------------------------------
// 基本结构体输出
struct Varyings
{
float2 uv : TEXCOORD0;
float fogFactor : TEXCOORD1;
float4 color : TEXCOORD2;
float4 positionCS : SV_POSITION;
};

// -------------------------------------
// 基本顶点着色器
Varyings StarRailPassVertex(Attributes input)
{
Varyings output = (Varyings)0;

VertexPositionInputs positionInputs = GetVertexPositionInputs(input.positionOS.xyz);
VertexNormalInputs normalInputs = GetVertexNormalInputs(input.normalOS, input.tangentOS);

float width = _OutlineWidth;
width *= GetOutlineCameraFovAndDistanceFixMultiplier(positionInputs.positionVS.z); // 根据相机距离调整描边宽度

float3 positionWS = positionInputs.positionWS;
#if _OUTLINE_VERTEX_COLOR_SMOOTH_NORMAL
float3x3 tbn = float3x3(normalInputs.tangentWS, normalInputs.bitangentWS, normalInputs.normalWS);
positionWS += mul(input.color.rgb * 2 - 1, tbn) * width;
#else
positionWS += normalInputs.normalWS * width;
#endif

output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
output.fogFactor = ComputeFogFactor(positionInputs.positionCS.z);
output.positionCS = TransformWorldToHClip(positionWS);
return output;
}

// -------------------------------------
// 基本片元着色器
float4 StarRailPassFragment(Varyings input) : SV_Target
{
float3 coolRamp = 0;
float3 warmRamp = 0;
#if _AREA_HAIR
float2 outlineUV = float2(0, 0.5);
coolRamp = SAMPLE_TEXTURE2D(_HairCoolRamp, sampler_HairCoolRamp, outlineUV).rgb;
warmRamp = SAMPLE_TEXTURE2D(_HairWarmRamp, sampler_HairWarmRamp, outlineUV).rgb;
#elif _AREA_UPPERBODY || _AREA_LOWERBODY
float4 lightMap = 0;
#if _AREA_UPPERBODY
lightMap = SAMPLE_TEXTURE2D(_UpperBodyLightMap, sampler_UpperBodyLightMap, input.uv);
#elif _AREA_LOWERBODY
lightMap = SAMPLE_TEXTURE2D(_LowerBodyLightMap, sampler_LowerBodyLightMap, input.uv);
#endif
float materialEnum = lightMap.a;
float materialEnumOffset = materialEnum + 0.0425;
float outlineUVy = lerp(materialEnumOffset, materialEnumOffset + 0.5 > 1 ? materialEnumOffset + 0.5 - 1 : materialEnumOffset + 0.5, fmod((round(materialEnumOffset/0.0625) - 1)/2, 2));
float2 outlineUV = float2(0, outlineUVy);
coolRamp = SAMPLE_TEXTURE2D(_BodyCoolRamp, sampler_BodyCoolRamp, outlineUV).rgb;
warmRamp = SAMPLE_TEXTURE2D(_BodyWarmRamp, sampler_BodyWarmRamp, outlineUV).rgb;
#elif _AREA_FACE
float2 outlineUV = float2(0, 0.0625);
coolRamp = SAMPLE_TEXTURE2D(_BodyCoolRamp, sampler_BodyCoolRamp, outlineUV).rgb;
warmRamp = SAMPLE_TEXTURE2D(_BodyWarmRamp, sampler_BodyWarmRamp, outlineUV).rgb;
#endif

float3 ramp = lerp(coolRamp, warmRamp, 0.5);
float3 albedo = pow(saturate(ramp), _OutlineGamma);;

float4 color = float4(albedo, 1);
color.rgb = MixFog(color.rgb, input.fogFactor);

return color;
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b6ff600

Please sign in to comment.