Unity URP Lit shader自定义阴影颜色
由于需要修改URP实现,首先需要将插件源码移动到Package目录避免被Unity还原。原URP目录在 Library\PackageCache内,直接剪切即可
1.打开Lit.shader文件,在Properties块内添加可编辑属性
2.shader添加自定义阴影的Keyword
Shader "Universal Render Pipeline/Lit"
{
Properties
{
····省略····
_ShadowColCustom("打开阴影颜色自定义", Float) = 0
_ShadowColor("阴影颜色", Color) = (0,0,0,1)
····省略····
}
····省略····
SubShader
{
// ------------------------------------------------------------------
//Forward pass. Shades all light in a single pass. GI + emission + Fog
Pass
{
// Lightmode matches the ShaderPassName set in UniversalRenderPipeline.cs. SRPDefaultUnlit and passes with
// no LightMode tag are also rendered by Universal Render Pipeline
Name "ForwardLit"
Tags{"LightMode" = "UniversalForward"}
Blend
ZWrite
Cull
HLSLPROGRAM
// -------------------------------------
// Material Keywords
#pragma shader_feature_local _SHADOWCOLCUSTOM_ON
}
}
}
_SHADOWCOLCUSTOM_ON宏添加进第一个Pass内即可
3.为了支持SRP Batcher,需要将_ShadowColor添加进CBUFFER内,打开LitInput.hlsl源码进行修改
4.在源码中找到阴影部分实现,进行颜色的差值混合
#ifdef _SHADOWCOLCUSTOM_ON
half3 radiance = lerp(_ShadowColor, lightColor, lightAttenuation * _ShadowColor.a) * NdotL;
#else
half3 radiance = lightColor * lightAttenuation * NdotL;
#endif由于Lit的属性面板做了自定义,需要修改编辑器面板绘制部分的代码逻辑
1.找到LitShader.cs,找一个位置插入阴影颜色属性的绘制逻辑
private void DrawShadowColorProperties(Material material)
{
bool customShadowColor = material.IsKeywordEnabled("_SHADOWCOLCUSTOM_ON");
float viewWidth = EditorGUIUtility.currentViewWidth;
Rect drawRect = EditorGUILayout.GetControlRect();
Rect rc = drawRect;
rc.width = 140;
EditorGUI.LabelField(rc, "自定义阴影颜色");
rc.width = viewWidth - 35;
EditorGUI.BeginChangeCheck();
customShadowColor = EditorGUI.Toggle(rc, " ", customShadowColor);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(material);
Undo.RecordObject(material, "");
material.SetFloat("_ShadowColCustom", customShadowColor ? 1 : 0);
MaterialChanged(material);
}
EditorGUI.BeginDisabledGroup(!customShadowColor);
{
drawRect = EditorGUILayout.GetControlRect();
rc = drawRect;
rc.width = 140;
EditorGUI.LabelField(rc, "阴影颜色");
rc.width = viewWidth - 35;
EditorGUI.BeginChangeCheck();
Color fogColor = EditorGUI.ColorField(rc, " ", material.GetColor("_ShadowColor"));
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(material);
Undo.RecordObject(material, "");
material.SetColor("_ShadowColor", fogColor);
}
}
EditorGUI.EndDisabledGroup();
}2.在BaseShaderGUI.cs内,找到对Keyword的修改逻辑,添加上我们自定义的Keyword处理
bool customShadowColor = false;
if (material.HasProperty("_ShadowColCustom"))
{
customShadowColor = material.GetFloat("_ShadowColCustom") >= 0.5;
}
if (customShadowColor)
{
material.EnableKeyword("_SHADOWCOLCUSTOM_ON");
}
else
{
material.DisableKeyword("_SHADOWCOLCUSTOM_ON");
}
接下来我们回到编辑器,Lit的材质球面板会多出阴影自定义部分的编辑
自定义阴影颜色
到此,可以对物体进行阴影颜色自定义了 [赞同]
页:
[1]