feat(ShaderBake): 全局参数注入(光照/环境/自定义全局量)
- 渲染前把勾选的全局量写进 Shader 全局,命令缓冲执行完按相反顺序还原,不污染编辑器状态 - 常用预设:光照方向/颜色、环境光三段、反射探针、_Time - 从 shader 源文件扫描非材质属性的 uniform 声明,按类型生成可编辑条目 - 侧边栏新增可折叠的"全局参数"区,贴图引用不进 JSON(资产引用无法可靠序列化)
This commit is contained in:
@@ -26,6 +26,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
private const string kUxmlPath = "Packages/com.lrss3.xericuiactionvessel/Editor/ShaderBake/BakeShaderWindow.uxml";
|
||||
private const string kUssPath = "Packages/com.lrss3.xericuiactionvessel/Editor/ShaderBake/BakeShaderWindow.uss";
|
||||
private const string kRowUxmlPath = "Packages/com.lrss3.xericuiactionvessel/Editor/ShaderBake/BakeShaderPropertyRow.uxml";
|
||||
private const string kGlobalRowUxmlPath = "Packages/com.lrss3.xericuiactionvessel/Editor/ShaderBake/BakeShaderGlobalRow.uxml";
|
||||
|
||||
private const string kPrefSettings = "XericUI.ShaderBake.Settings";
|
||||
private const string kPrefAutoRefresh = "XericUI.ShaderBake.AutoRefresh";
|
||||
@@ -48,6 +49,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
private ShaderBakeRenderer m_Renderer;
|
||||
private ShaderBakePreviewPane m_Preview;
|
||||
private ShaderBakePropertyTable m_PropertyTable;
|
||||
private ShaderBakeGlobalTable m_Globals;
|
||||
private ShaderBakeExporter m_Exporter;
|
||||
|
||||
private ObjectField m_ShaderField;
|
||||
@@ -112,6 +114,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
|
||||
CollectReferences();
|
||||
BuildPropertyTable();
|
||||
BuildGlobalTable();
|
||||
BindFaceGrid();
|
||||
BindToolbar();
|
||||
BindSections();
|
||||
@@ -141,6 +144,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
m_Renderer = null;
|
||||
m_Preview = null;
|
||||
m_PropertyTable = null;
|
||||
m_Globals = null;
|
||||
m_Syncers.Clear();
|
||||
}
|
||||
|
||||
@@ -153,6 +157,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
private void OnSettingsReplaced()
|
||||
{
|
||||
SyncUiFromSettings();
|
||||
RefreshGlobals();
|
||||
RequestRender();
|
||||
}
|
||||
|
||||
@@ -182,6 +187,30 @@ namespace XericUI.ShaderBake.Editor
|
||||
return element;
|
||||
}
|
||||
|
||||
private void BuildGlobalTable()
|
||||
{
|
||||
VisualTreeAsset rowTemplate = LoadAsset<VisualTreeAsset>(kGlobalRowUxmlPath, "BakeShaderGlobalRow");
|
||||
if (rowTemplate == null)
|
||||
{
|
||||
Debug.LogError("[ShaderBake] 找不到全局参数行模板 " + kGlobalRowUxmlPath);
|
||||
return;
|
||||
}
|
||||
m_Globals = new ShaderBakeGlobalTable(rootVisualElement, rowTemplate);
|
||||
m_Globals.changed += RequestRender;
|
||||
}
|
||||
|
||||
private void RefreshGlobals()
|
||||
{
|
||||
if (m_Globals == null)
|
||||
return;
|
||||
m_Globals.SetTarget(m_Renderer.settings.globals);
|
||||
Label note = rootVisualElement.Q<Label>("globals-note");
|
||||
if (note != null)
|
||||
note.text = m_Globals.rowCount == 0
|
||||
? "还没有全局参数:可以扫描 shader,或直接加入常用光照/环境预设"
|
||||
: m_Globals.rowCount + " 条(勾选才生效,渲染完立即还原)";
|
||||
}
|
||||
|
||||
private void BuildPropertyTable()
|
||||
{
|
||||
VisualTreeAsset rowTemplate = LoadAsset<VisualTreeAsset>(kRowUxmlPath, "BakeShaderPropertyRow");
|
||||
@@ -264,6 +293,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
Collapsible("sec-output", "sec-output-body");
|
||||
Collapsible("sec-surface", "sec-surface-body");
|
||||
Collapsible("sec-camera", "sec-camera-body");
|
||||
Collapsible("sec-globals", "sec-globals-body");
|
||||
Collapsible("sec-props", "sec-props-body");
|
||||
}
|
||||
|
||||
@@ -320,6 +350,9 @@ namespace XericUI.ShaderBake.Editor
|
||||
if (revert != null)
|
||||
revert.clicked += RevertToSource;
|
||||
|
||||
Click("globals-detect", ScanGlobals);
|
||||
Click("globals-presets", AddGlobalPresets);
|
||||
|
||||
BindOutput();
|
||||
BindSurface();
|
||||
BindCamera();
|
||||
@@ -348,6 +381,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
|
||||
SetSourceNote();
|
||||
RefreshPropertyTable();
|
||||
RefreshGlobals();
|
||||
DoRender();
|
||||
}
|
||||
|
||||
@@ -412,6 +446,23 @@ namespace XericUI.ShaderBake.Editor
|
||||
RequestRender();
|
||||
}
|
||||
|
||||
// ── 全局参数 ────────────────────────────────────────────
|
||||
|
||||
private void ScanGlobals()
|
||||
{
|
||||
string note;
|
||||
ShaderBakeGlobalScanner.Scan(m_Source.shader, m_Renderer.settings.globals, out note);
|
||||
RefreshGlobals();
|
||||
SetStatus(note);
|
||||
}
|
||||
|
||||
private void AddGlobalPresets()
|
||||
{
|
||||
m_Renderer.settings.globals.AddEnvironmentPresets();
|
||||
RefreshGlobals();
|
||||
SetStatus("已加入常用光照/环境预设(勾选才生效)");
|
||||
}
|
||||
|
||||
// ── 输出 ────────────────────────────────────────────────
|
||||
|
||||
private void BindOutput()
|
||||
@@ -893,6 +944,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
private void ResetSettings()
|
||||
{
|
||||
m_Renderer.settings = ShaderBakeSettings.CreateDefault();
|
||||
RefreshGlobals();
|
||||
Commit();
|
||||
if (m_Preview != null)
|
||||
m_Preview.FitToView();
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:x="UnityEditor.UIElements" xmlns:xer="XericUI.ShaderBake.Editor">
|
||||
<!--
|
||||
全局参数表的一行:勾选 + 名字 + 按类型只留一个输入控件。
|
||||
取控件一律在克隆出来的行内查(row.Q),不查全局。
|
||||
-->
|
||||
<ui:VisualElement name="global-row" class="prop-row global-row">
|
||||
<ui:Toggle name="global-enabled" class="global-enabled" />
|
||||
<ui:Label name="global-name" class="prop-label global-name" text="全局量" />
|
||||
<ui:VisualElement name="global-field" class="prop-field">
|
||||
<xer:FloatField name="global-float" class="prop-inline" />
|
||||
<ui:VisualElement name="global-vector" class="prop-vector">
|
||||
<xer:FloatField name="global-x" label="X" class="prop-vec-part" />
|
||||
<xer:FloatField name="global-y" label="Y" class="prop-vec-part" />
|
||||
<xer:FloatField name="global-z" label="Z" class="prop-vec-part" />
|
||||
<xer:FloatField name="global-w" label="W" class="prop-vec-part" />
|
||||
</ui:VisualElement>
|
||||
<x:ColorField name="global-color" class="prop-inline" />
|
||||
<x:ObjectField name="global-texture" class="prop-inline" />
|
||||
</ui:VisualElement>
|
||||
</ui:VisualElement>
|
||||
</ui:UXML>
|
||||
@@ -287,6 +287,16 @@
|
||||
margin-right: 2px;
|
||||
}
|
||||
|
||||
/* ── 全局参数行 ── */
|
||||
.global-enabled {
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
.global-name {
|
||||
width: 118px;
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
.prop-vec-part > .unity-label {
|
||||
min-width: 12px;
|
||||
font-size: 10px;
|
||||
|
||||
@@ -194,6 +194,20 @@
|
||||
</ui:VisualElement>
|
||||
</ui:VisualElement>
|
||||
|
||||
<!-- 全局参数:离屏渲染没有相机流程,光照/环境这类引擎全局量要自己补 -->
|
||||
<ui:VisualElement name="sec-globals" class="section">
|
||||
<ui:Toggle name="sec-globals-head" label="全局参数(光照 / 环境)" value="false" class="section-head" />
|
||||
<ui:VisualElement name="sec-globals-body" class="section-body">
|
||||
<ui:Label name="globals-note" class="field-note"
|
||||
text="引擎在相机流程里才写入的全局量(光照方向、环境光、反射探针)这里默认缺失;勾选的条目在渲染时注入,渲染完立即还原" />
|
||||
<ui:VisualElement class="field-row">
|
||||
<ui:Button name="globals-detect" text="扫描 Shader" class="field-half" />
|
||||
<ui:Button name="globals-presets" text="加入常用预设" class="field-half" />
|
||||
</ui:VisualElement>
|
||||
<ui:VisualElement name="globals-rows" class="props-rows" />
|
||||
</ui:VisualElement>
|
||||
</ui:VisualElement>
|
||||
|
||||
<!-- 材质属性表:行来自 BakeShaderPropertyRow.uxml -->
|
||||
<ui:VisualElement name="sec-props" class="section">
|
||||
<ui:Toggle name="sec-props-head" label="材质属性" value="true" class="section-head" />
|
||||
|
||||
@@ -210,6 +210,7 @@ namespace XericUI.ShaderBake.Editor
|
||||
m_Renderer.settings.output = parsed.output;
|
||||
m_Renderer.settings.surface = parsed.surface;
|
||||
m_Renderer.settings.camera = parsed.camera;
|
||||
m_Renderer.settings.globals = parsed.globals;
|
||||
Report("烘焙参数已从剪贴板替换");
|
||||
Action apply = settingsReplaced;
|
||||
if (apply != null)
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using XericUI.ShaderBake;
|
||||
|
||||
namespace XericUI.ShaderBake.Editor
|
||||
{
|
||||
/// <summary>
|
||||
/// 从 shader 源文件里找"不是材质属性、但会被 shader 读到"的全局量。
|
||||
///
|
||||
/// <para><b>为什么只能做到这个程度</b>:Unity 没有"列出 shader 实际引用了哪些 uniform"的 API,
|
||||
/// 编译器把 include 里的东西全展开了。这里做的是文本级扫描——只看 shader 文件里自己声明的
|
||||
/// uniform,够覆盖"手写一个 _GlobalTint 却没做成 property"这类常见情况;引擎内置量
|
||||
/// (光照方向、环境光、反射探针)靠"常用预设"补齐,不假装能自动枚举。</para>
|
||||
/// </summary>
|
||||
internal static class ShaderBakeGlobalScanner
|
||||
{
|
||||
private static readonly Regex kDeclaration = new Regex(
|
||||
@"(?m)^[ \t]*(?:uniform[ \t]+)?(float|float2|float3|float4|half|half2|half3|half4|fixed|fixed2|fixed3|fixed4|int|uint|color|sampler2D|sampler3D|samplerCUBE|sampler2DArray|Texture2D|Texture3D|TextureCube|Texture2DArray)[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*(?:\[[^\]]*\])?[ \t]*;",
|
||||
RegexOptions.Compiled);
|
||||
|
||||
/// <summary> 扫描并把新发现的全局量加进参数表(默认关闭)。返回新增条数。 </summary>
|
||||
public static int Scan(Shader shader, ShaderBakeGlobalSettings settings, out string note)
|
||||
{
|
||||
if (shader == null)
|
||||
{
|
||||
note = "没有 shader";
|
||||
return 0;
|
||||
}
|
||||
string path = AssetDatabase.GetAssetPath(shader);
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
||||
{
|
||||
note = "这个 shader 没有源文件(内置或运行时创建),只能手动加常用预设";
|
||||
return 0;
|
||||
}
|
||||
|
||||
string source;
|
||||
try
|
||||
{
|
||||
source = File.ReadAllText(path);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
note = "读 shader 源文件失败:" + e.Message;
|
||||
return 0;
|
||||
}
|
||||
|
||||
var properties = new HashSet<string>();
|
||||
for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); i++)
|
||||
properties.Add(ShaderUtil.GetPropertyName(shader, i));
|
||||
|
||||
int added = 0;
|
||||
foreach (Match match in kDeclaration.Matches(source))
|
||||
{
|
||||
string name = match.Groups[2].Value;
|
||||
if (properties.Contains(name) || name.StartsWith("unity_", System.StringComparison.Ordinal))
|
||||
continue;
|
||||
if (settings.entries != null && settings.entries.Exists(entry => entry != null && entry.name == name))
|
||||
continue;
|
||||
settings.Ensure(name, KindOf(match.Groups[1].Value));
|
||||
added++;
|
||||
}
|
||||
note = added > 0 ? "扫描到 " + added + " 个全局量(默认关闭)" : "没有扫到新的全局量";
|
||||
return added;
|
||||
}
|
||||
|
||||
private static ShaderBakeGlobalKind KindOf(string type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case "float":
|
||||
case "half":
|
||||
case "fixed":
|
||||
case "int":
|
||||
case "uint":
|
||||
return ShaderBakeGlobalKind.Float;
|
||||
case "color":
|
||||
return ShaderBakeGlobalKind.Color;
|
||||
case "sampler2D":
|
||||
case "sampler3D":
|
||||
case "samplerCUBE":
|
||||
case "sampler2DArray":
|
||||
case "Texture2D":
|
||||
case "Texture3D":
|
||||
case "TextureCube":
|
||||
case "Texture2DArray":
|
||||
return ShaderBakeGlobalKind.Texture;
|
||||
default:
|
||||
return ShaderBakeGlobalKind.Vector;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor.UIElements;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using XericUI.ShaderBake;
|
||||
|
||||
namespace XericUI.ShaderBake.Editor
|
||||
{
|
||||
/// <summary>
|
||||
/// 右侧的"全局参数表":一行一条 <see cref="ShaderBakeGlobalEntry"/>,勾选才参与烘焙。
|
||||
/// 结构与材质属性表同构——模板在 BakeShaderGlobalRow.uxml,这里只决定点亮哪个控件。
|
||||
/// </summary>
|
||||
internal sealed class ShaderBakeGlobalTable
|
||||
{
|
||||
private sealed class Row
|
||||
{
|
||||
public VisualElement root;
|
||||
public Toggle enabled;
|
||||
public Label label;
|
||||
public FloatField floatValue;
|
||||
public VisualElement vectorGroup;
|
||||
public FloatField[] parts;
|
||||
public ColorField color;
|
||||
public ObjectField texture;
|
||||
public ShaderBakeGlobalEntry entry;
|
||||
public bool updating;
|
||||
}
|
||||
|
||||
private readonly VisualElement m_Container;
|
||||
private readonly VisualTreeAsset m_Template;
|
||||
private readonly List<Row> m_Rows = new List<Row>();
|
||||
|
||||
public event Action changed;
|
||||
|
||||
public int rowCount { get { return m_Rows.Count; } }
|
||||
|
||||
public ShaderBakeGlobalTable(VisualElement root, VisualTreeAsset template)
|
||||
{
|
||||
m_Container = root.Q<VisualElement>("globals-rows");
|
||||
m_Template = template;
|
||||
if (m_Container == null)
|
||||
throw new InvalidOperationException("BakeShaderWindow.uxml 缺少 globals-rows 容器");
|
||||
}
|
||||
|
||||
public void SetTarget(ShaderBakeGlobalSettings settings)
|
||||
{
|
||||
m_Settings = settings;
|
||||
Rebuild();
|
||||
}
|
||||
|
||||
private ShaderBakeGlobalSettings m_Settings;
|
||||
|
||||
public void Rebuild()
|
||||
{
|
||||
m_Rows.Clear();
|
||||
m_Container.Clear();
|
||||
if (m_Settings == null || m_Settings.entries == null || m_Template == null)
|
||||
return;
|
||||
|
||||
foreach (ShaderBakeGlobalEntry entry in m_Settings.entries)
|
||||
{
|
||||
if (entry == null || string.IsNullOrEmpty(entry.name))
|
||||
continue;
|
||||
Row row = CreateRow(entry);
|
||||
m_Rows.Add(row);
|
||||
m_Container.Add(row.root);
|
||||
}
|
||||
RefreshValues();
|
||||
}
|
||||
|
||||
/// <summary> 值被别处改过(粘贴参数、扫描新增)之后回写界面 </summary>
|
||||
public void RefreshValues()
|
||||
{
|
||||
foreach (Row row in m_Rows)
|
||||
{
|
||||
row.updating = true;
|
||||
try
|
||||
{
|
||||
row.enabled.SetValueWithoutNotify(row.entry.enabled);
|
||||
switch (row.entry.kind)
|
||||
{
|
||||
case ShaderBakeGlobalKind.Float:
|
||||
row.floatValue.SetValueWithoutNotify(row.entry.x);
|
||||
break;
|
||||
case ShaderBakeGlobalKind.Vector:
|
||||
row.parts[0].SetValueWithoutNotify(row.entry.x);
|
||||
row.parts[1].SetValueWithoutNotify(row.entry.y);
|
||||
row.parts[2].SetValueWithoutNotify(row.entry.z);
|
||||
row.parts[3].SetValueWithoutNotify(row.entry.w);
|
||||
break;
|
||||
case ShaderBakeGlobalKind.Color:
|
||||
row.color.SetValueWithoutNotify(row.entry.ToColor());
|
||||
break;
|
||||
case ShaderBakeGlobalKind.Texture:
|
||||
row.texture.SetValueWithoutNotify(row.entry.texture);
|
||||
break;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
row.updating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Row CreateRow(ShaderBakeGlobalEntry entry)
|
||||
{
|
||||
VisualElement host = new VisualElement();
|
||||
m_Template.CloneTree(host);
|
||||
if (host.childCount == 0)
|
||||
return null;
|
||||
VisualElement root = host[0];
|
||||
root.RemoveFromHierarchy();
|
||||
|
||||
var row = new Row
|
||||
{
|
||||
root = root,
|
||||
enabled = root.Q<Toggle>("global-enabled"),
|
||||
label = root.Q<Label>("global-name"),
|
||||
floatValue = root.Q<FloatField>("global-float"),
|
||||
vectorGroup = root.Q<VisualElement>("global-vector"),
|
||||
color = root.Q<ColorField>("global-color"),
|
||||
texture = root.Q<ObjectField>("global-texture"),
|
||||
entry = entry
|
||||
};
|
||||
row.parts = new[]
|
||||
{
|
||||
root.Q<FloatField>("global-x"),
|
||||
root.Q<FloatField>("global-y"),
|
||||
root.Q<FloatField>("global-z"),
|
||||
root.Q<FloatField>("global-w")
|
||||
};
|
||||
|
||||
if (row.label != null)
|
||||
{
|
||||
row.label.text = entry.name;
|
||||
row.label.tooltip = entry.name + "\n" + DescribeKind(entry.kind);
|
||||
}
|
||||
|
||||
Hide(row.floatValue);
|
||||
Hide(row.vectorGroup);
|
||||
Hide(row.color);
|
||||
Hide(row.texture);
|
||||
switch (entry.kind)
|
||||
{
|
||||
case ShaderBakeGlobalKind.Float: Show(row.floatValue); break;
|
||||
case ShaderBakeGlobalKind.Vector: Show(row.vectorGroup); break;
|
||||
case ShaderBakeGlobalKind.Color: Show(row.color); break;
|
||||
case ShaderBakeGlobalKind.Texture: Show(row.texture); break;
|
||||
}
|
||||
|
||||
if (row.enabled != null)
|
||||
{
|
||||
row.enabled.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
if (row.updating) return;
|
||||
entry.enabled = evt.newValue;
|
||||
Notify();
|
||||
});
|
||||
}
|
||||
|
||||
if (row.floatValue != null)
|
||||
{
|
||||
row.floatValue.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
if (row.updating) return;
|
||||
entry.x = evt.newValue;
|
||||
Notify();
|
||||
});
|
||||
}
|
||||
|
||||
if (row.parts != null)
|
||||
{
|
||||
for (int i = 0; i < row.parts.Length; i++)
|
||||
{
|
||||
int component = i;
|
||||
FloatField field = row.parts[i];
|
||||
if (field == null) continue;
|
||||
field.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
if (row.updating) return;
|
||||
switch (component)
|
||||
{
|
||||
case 0: entry.x = evt.newValue; break;
|
||||
case 1: entry.y = evt.newValue; break;
|
||||
case 2: entry.z = evt.newValue; break;
|
||||
default: entry.w = evt.newValue; break;
|
||||
}
|
||||
Notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (row.color != null)
|
||||
{
|
||||
row.color.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
if (row.updating) return;
|
||||
entry.x = evt.newValue.r;
|
||||
entry.y = evt.newValue.g;
|
||||
entry.z = evt.newValue.b;
|
||||
entry.w = evt.newValue.a;
|
||||
Notify();
|
||||
});
|
||||
}
|
||||
|
||||
if (row.texture != null)
|
||||
{
|
||||
row.texture.objectType = typeof(Texture);
|
||||
row.texture.allowSceneObjects = false;
|
||||
row.texture.RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
if (row.updating) return;
|
||||
entry.texture = evt.newValue as Texture;
|
||||
Notify();
|
||||
});
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
private static string DescribeKind(ShaderBakeGlobalKind kind)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
case ShaderBakeGlobalKind.Float: return "float";
|
||||
case ShaderBakeGlobalKind.Vector: return "float4";
|
||||
case ShaderBakeGlobalKind.Color: return "color";
|
||||
default: return "texture";
|
||||
}
|
||||
}
|
||||
|
||||
private void Notify()
|
||||
{
|
||||
Action handler = changed;
|
||||
if (handler != null)
|
||||
handler.Invoke();
|
||||
}
|
||||
|
||||
private static void Show(VisualElement element)
|
||||
{
|
||||
if (element != null) element.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
private static void Hide(VisualElement element)
|
||||
{
|
||||
if (element != null) element.style.display = DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user