shader修改

This commit is contained in:
tianmo committed 2026-10-12 11:13:18 +08:00
1 parent 5bf02177df
commit ae5c4212dc
16 files changed
+725 -25

No files matched your search

+102
View File
@@ -99,3 +99,105 @@ public class UIMaskChannel : UIMaskEnumDrawer
// 下拉框的值本身就是要采样的通道号,无需同步到别的属性
}
}
// 通用模块:粒子自定义数据通道下拉框(shader 里写 [CustomDataChannel])
// 选项 9 项:null / Custom1.xyzw / Custom2.xyzw;值 0=null,1~4=Custom1,5~8=Custom2
// 右侧会提示该通道是否已被同一材质上的其它参数占用
//(约定:属性名里含 "CustomChannel" 的都会被统计,以后新增参数只要按这个命名即可自动纳入)
// shader 侧配套函数:CustomDataChannelValue(custom1, custom2, channel) → 取对应通道的值
public class CustomDataChannel : MaterialPropertyDrawer
{
private static readonly string[] Items =
{
"null",
"Custom1.x", "Custom1.y", "Custom1.z", "Custom1.w",
"Custom2.x", "Custom2.y", "Custom2.z", "Custom2.w"
};
private const float HintWidth = 150f;
public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, MaterialProperty prop, string label, MaterialEditor editor)
{
Rect popupRect = new Rect(position.x, position.y, Mathf.Max(60f, position.width - HintWidth - 4f), position.height);
Rect hintRect = new Rect(position.xMax - HintWidth, position.y, HintWidth, position.height);
int index = Mathf.Clamp(Mathf.RoundToInt(prop.floatValue), 0, Items.Length - 1);
EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = prop.hasMixedValue;
index = EditorGUI.Popup(popupRect, label, index, Items);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck())
{
prop.floatValue = index;
}
bool occupied;
string hint = DescribeOccupation(editor, prop, index, out occupied);
var hintContent = new GUIContent(hint, "数据来自粒子系统的「自定义数据」模块;"
+ "还需要在粒子渲染器的「自定义顶点流」里按 UV、Custom1.xyzw、Custom2.xyzw 的顺序勾选对应通道。");
Color oldColor = GUI.color;
if (index == 0)
{
GUI.color = new Color(0.7f, 0.7f, 0.7f);
}
else
{
GUI.color = occupied ? new Color(1f, 0.72f, 0.25f) : new Color(0.55f, 0.85f, 0.55f);
}
EditorGUI.LabelField(hintRect, hintContent, EditorStyles.miniLabel);
GUI.color = oldColor;
}
private static string DescribeOccupation(MaterialEditor editor, MaterialProperty prop, int index, out bool occupied)
{
occupied = false;
if (index == 0)
{
return "未使用";
}
var material = editor.targets.Length > 0 ? editor.targets[0] as Material : null;
if (material == null)
{
return string.Empty;
}
Shader shader = material.shader;
int count = ShaderUtil.GetPropertyCount(shader);
var owners = new System.Collections.Generic.List<string>();
for (int i = 0; i < count; i++)
{
string name = ShaderUtil.GetPropertyName(shader, i);
if (name == prop.name || !name.Contains("CustomChannel"))
{
continue;
}
if (!material.HasProperty(name) || Mathf.RoundToInt(material.GetFloat(name)) != index)
{
continue;
}
string description = ShaderUtil.GetPropertyDescription(shader, i);
owners.Add(string.IsNullOrEmpty(description) ? name : description);
}
if (owners.Count == 0)
{
return "未占用";
}
occupied = true;
return owners.Count == 1
? "已被 " + owners[0] + " 占用"
: "已被 " + owners[0] + " 等 " + owners.Count + " 处占用";
}
}
+1
View File
@@ -55,6 +55,7 @@ public class UIMaskShaderGUI : ShaderGUI
"_DissolveChannel",
"_Dissolve",
"_DissolveWidth",
"_DissolveCustomChannel",
"_DissolveEdgeColor",
"_DissolveEdgeSharpness",
"_DissolveHighlightColor",
+27 -2
View File
@@ -38,6 +38,7 @@ Properties {
[UIMaskChannel] _DissolveChannel("通道", Float) = 3
_Dissolve ("溶解进度", Range(0, 1.01)) = 0
_DissolveWidth ("溶解宽度", Range(0, 1)) = 0
[CustomDataChannel] _DissolveCustomChannel ("自定义数据通道", Float) = 0
// 默认黑色 = 不叠加任何颜色,保证新参数不影响原有溶解效果
[HDR]_DissolveEdgeColor ("溶解边缘颜色", Color) = (0, 0, 0, 1)
_DissolveEdgeSharpness ("边缘锐度 (越大越细)", Range(0.001, 8)) = 2
@@ -130,6 +131,7 @@ Category {
float _DissolveChannel;
fixed _Dissolve;
fixed _DissolveWidth;
float _DissolveCustomChannel;
fixed4 _DissolveEdgeColor;
fixed _DissolveEdgeSharpness;
fixed4 _DissolveHighlightColor;
@@ -144,6 +146,15 @@ Category {
return dot(c, w);
}
// 通用模块:按通道号取粒子自定义数据(供"自定义数据通道"下拉框使用)
// channel: 0 = null(返回 0),1~4 = Custom1.xyzw,5~8 = Custom2.xyzw
inline float CustomDataChannelValue(float4 custom1, float4 custom2, float channel)
{
float4 w1 = 1.0 - saturate(abs(channel - float4(1.0, 2.0, 3.0, 4.0)));
float4 w2 = 1.0 - saturate(abs(channel - float4(5.0, 6.0, 7.0, 8.0)));
return dot(custom1, w1) + dot(custom2, w2);
}
#if _USEUIMASKCLIP_ON
fixed4 _ClipRect;
#endif
@@ -156,7 +167,10 @@ Category {
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
fixed4 texcoord : TEXCOORD0;
// 顶点流约定(渲染器 → 自定义顶点流,按此顺序):UV, Custom1.xyzw, Custom2.xyzw
float4 texcoord : TEXCOORD0; // UV.xy + Custom1.xy
float4 texcoord1 : TEXCOORD1; // Custom1.zw + Custom2.xy
float4 texcoord2 : TEXCOORD2; // Custom2.zw
UNITY_VERTEX_INPUT_INSTANCE_ID
};
@@ -165,6 +179,9 @@ Category {
fixed4 color : COLOR;
fixed4 texcoord : TEXCOORD0;
float4 customData : TEXCOORD1; // Custom1.xyzw
float4 customData2 : TEXCOORD6; // Custom2.xyzw
#if _USEMASK_ON
fixed2 texcoordMask : TEXCOORD2;
#endif
@@ -202,6 +219,10 @@ Category {
o.texcoord.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
o.texcoord.zw = v.texcoord.zw;
// 把打包在各 texcoord 里的粒子自定义数据还原成 Custom1.xyzw / Custom2.xyzw
o.customData = float4(v.texcoord.z, v.texcoord.w, v.texcoord1.x, v.texcoord1.y);
o.customData2 = float4(v.texcoord1.z, v.texcoord1.w, v.texcoord2.x, v.texcoord2.y);
#if _USEMASK_ON
o.texcoordMask = TRANSFORM_TEX(v.texcoord,_Mask);
#endif
@@ -250,7 +271,11 @@ Category {
#endif
#if _USEDISSOLVE_ON
fixed DissolveWithParticle =(_Dissolve*(1-i.texcoord.z)*(1+_DissolveWidth)-_DissolveWidth) ;
// 溶解进度 = 滑条 × 选中的自定义数据通道;通道 = null 时完全用滑条(保持原行为)
fixed customProgress = CustomDataChannelValue(i.customData, i.customData2, _DissolveCustomChannel);
fixed useCustom = step(0.5, _DissolveCustomChannel);
fixed dissolveProgress = _Dissolve * lerp(1.0, customProgress, useCustom);
fixed DissolveWithParticle =(dissolveProgress*(1+_DissolveWidth)-_DissolveWidth) ;
fixed dissolveMap = UIMaskSampleChannel(_DissolveTex, i.texcoordDissolve, _DissolveChannel);
fixed dissolveAlpha = saturate(smoothstep( DissolveWithParticle, (DissolveWithParticle+_DissolveWidth), dissolveMap));
col.a *= dissolveAlpha;
+5 -3
View File
@@ -47,8 +47,8 @@ Material:
m_Offset: {x: 0, y: 0}
- _DissolveTex:
m_Texture: {fileID: 2800000, guid: 94c4ffff470a043f68f9d388161c3b2a, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Scale: {x: 0.5, y: 0.5}
m_Offset: {x: -2.91, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
@@ -113,8 +113,10 @@ Material:
- _Cutoff: 0.5
- _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1
- _Dissolve: 0.4
- _Dissolve: 1
- _DissolveChannel: 0
- _DissolveCustomChannel: 1
- _DissolveCustomData: 1
- _DissolveEdgeSharpness: 0.001
- _DissolveHighlightWidth: 0.203
- _DissolveWidth: 0.174