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 kUxmlPath = "Packages/com.lrss3.xericuiactionvessel/Editor/ShaderBake/BakeShaderWindow.uxml";
|
||||||
private const string kUssPath = "Packages/com.lrss3.xericuiactionvessel/Editor/ShaderBake/BakeShaderWindow.uss";
|
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 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 kPrefSettings = "XericUI.ShaderBake.Settings";
|
||||||
private const string kPrefAutoRefresh = "XericUI.ShaderBake.AutoRefresh";
|
private const string kPrefAutoRefresh = "XericUI.ShaderBake.AutoRefresh";
|
||||||
@@ -48,6 +49,7 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
private ShaderBakeRenderer m_Renderer;
|
private ShaderBakeRenderer m_Renderer;
|
||||||
private ShaderBakePreviewPane m_Preview;
|
private ShaderBakePreviewPane m_Preview;
|
||||||
private ShaderBakePropertyTable m_PropertyTable;
|
private ShaderBakePropertyTable m_PropertyTable;
|
||||||
|
private ShaderBakeGlobalTable m_Globals;
|
||||||
private ShaderBakeExporter m_Exporter;
|
private ShaderBakeExporter m_Exporter;
|
||||||
|
|
||||||
private ObjectField m_ShaderField;
|
private ObjectField m_ShaderField;
|
||||||
@@ -112,6 +114,7 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
|
|
||||||
CollectReferences();
|
CollectReferences();
|
||||||
BuildPropertyTable();
|
BuildPropertyTable();
|
||||||
|
BuildGlobalTable();
|
||||||
BindFaceGrid();
|
BindFaceGrid();
|
||||||
BindToolbar();
|
BindToolbar();
|
||||||
BindSections();
|
BindSections();
|
||||||
@@ -141,6 +144,7 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
m_Renderer = null;
|
m_Renderer = null;
|
||||||
m_Preview = null;
|
m_Preview = null;
|
||||||
m_PropertyTable = null;
|
m_PropertyTable = null;
|
||||||
|
m_Globals = null;
|
||||||
m_Syncers.Clear();
|
m_Syncers.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,6 +157,7 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
private void OnSettingsReplaced()
|
private void OnSettingsReplaced()
|
||||||
{
|
{
|
||||||
SyncUiFromSettings();
|
SyncUiFromSettings();
|
||||||
|
RefreshGlobals();
|
||||||
RequestRender();
|
RequestRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,6 +187,30 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
return element;
|
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()
|
private void BuildPropertyTable()
|
||||||
{
|
{
|
||||||
VisualTreeAsset rowTemplate = LoadAsset<VisualTreeAsset>(kRowUxmlPath, "BakeShaderPropertyRow");
|
VisualTreeAsset rowTemplate = LoadAsset<VisualTreeAsset>(kRowUxmlPath, "BakeShaderPropertyRow");
|
||||||
@@ -264,6 +293,7 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
Collapsible("sec-output", "sec-output-body");
|
Collapsible("sec-output", "sec-output-body");
|
||||||
Collapsible("sec-surface", "sec-surface-body");
|
Collapsible("sec-surface", "sec-surface-body");
|
||||||
Collapsible("sec-camera", "sec-camera-body");
|
Collapsible("sec-camera", "sec-camera-body");
|
||||||
|
Collapsible("sec-globals", "sec-globals-body");
|
||||||
Collapsible("sec-props", "sec-props-body");
|
Collapsible("sec-props", "sec-props-body");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,6 +350,9 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
if (revert != null)
|
if (revert != null)
|
||||||
revert.clicked += RevertToSource;
|
revert.clicked += RevertToSource;
|
||||||
|
|
||||||
|
Click("globals-detect", ScanGlobals);
|
||||||
|
Click("globals-presets", AddGlobalPresets);
|
||||||
|
|
||||||
BindOutput();
|
BindOutput();
|
||||||
BindSurface();
|
BindSurface();
|
||||||
BindCamera();
|
BindCamera();
|
||||||
@@ -348,6 +381,7 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
|
|
||||||
SetSourceNote();
|
SetSourceNote();
|
||||||
RefreshPropertyTable();
|
RefreshPropertyTable();
|
||||||
|
RefreshGlobals();
|
||||||
DoRender();
|
DoRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,6 +446,23 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
RequestRender();
|
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()
|
private void BindOutput()
|
||||||
@@ -893,6 +944,7 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
private void ResetSettings()
|
private void ResetSettings()
|
||||||
{
|
{
|
||||||
m_Renderer.settings = ShaderBakeSettings.CreateDefault();
|
m_Renderer.settings = ShaderBakeSettings.CreateDefault();
|
||||||
|
RefreshGlobals();
|
||||||
Commit();
|
Commit();
|
||||||
if (m_Preview != null)
|
if (m_Preview != null)
|
||||||
m_Preview.FitToView();
|
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;
|
margin-right: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ── 全局参数行 ── */
|
||||||
|
.global-enabled {
|
||||||
|
margin-right: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.global-name {
|
||||||
|
width: 118px;
|
||||||
|
font-size: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
.prop-vec-part > .unity-label {
|
.prop-vec-part > .unity-label {
|
||||||
min-width: 12px;
|
min-width: 12px;
|
||||||
font-size: 10px;
|
font-size: 10px;
|
||||||
|
|||||||
@@ -194,6 +194,20 @@
|
|||||||
</ui:VisualElement>
|
</ui:VisualElement>
|
||||||
</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 -->
|
<!-- 材质属性表:行来自 BakeShaderPropertyRow.uxml -->
|
||||||
<ui:VisualElement name="sec-props" class="section">
|
<ui:VisualElement name="sec-props" class="section">
|
||||||
<ui:Toggle name="sec-props-head" label="材质属性" value="true" class="section-head" />
|
<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.output = parsed.output;
|
||||||
m_Renderer.settings.surface = parsed.surface;
|
m_Renderer.settings.surface = parsed.surface;
|
||||||
m_Renderer.settings.camera = parsed.camera;
|
m_Renderer.settings.camera = parsed.camera;
|
||||||
|
m_Renderer.settings.globals = parsed.globals;
|
||||||
Report("烘焙参数已从剪贴板替换");
|
Report("烘焙参数已从剪贴板替换");
|
||||||
Action apply = settingsReplaced;
|
Action apply = settingsReplaced;
|
||||||
if (apply != null)
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace XericUI.ShaderBake
|
||||||
|
{
|
||||||
|
/// <summary> 能喂给 shader 的全局量类别,决定用哪个 SetGlobal 入口 </summary>
|
||||||
|
public enum ShaderBakeGlobalKind
|
||||||
|
{
|
||||||
|
Float,
|
||||||
|
Vector,
|
||||||
|
Color,
|
||||||
|
Texture
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 一条全局参数。贴图引用标了 <see cref="NonSerializedAttribute"/>:JsonUtility 存不下来资产引用,
|
||||||
|
/// 硬存一个实例 ID 反而会在换会话后变成半死的引用。
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
public class ShaderBakeGlobalEntry
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
|
||||||
|
public ShaderBakeGlobalKind kind;
|
||||||
|
|
||||||
|
[Tooltip("关掉就完全不动这个全局量,包括不捕获、不还原")]
|
||||||
|
public bool enabled;
|
||||||
|
|
||||||
|
public float x;
|
||||||
|
public float y;
|
||||||
|
public float z;
|
||||||
|
public float w;
|
||||||
|
|
||||||
|
[NonSerialized] public Texture texture;
|
||||||
|
|
||||||
|
public ShaderBakeGlobalEntry() { }
|
||||||
|
|
||||||
|
public ShaderBakeGlobalEntry(string name, ShaderBakeGlobalKind kind, bool enabled = false)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
this.kind = kind;
|
||||||
|
this.enabled = enabled;
|
||||||
|
w = 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector4 ToVector() { return new Vector4(x, y, z, w); }
|
||||||
|
|
||||||
|
public Color ToColor() { return new Color(x, y, z, w); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 一组全局参数。烘焙走的是离屏命令缓冲,没有相机在渲染,"灯在哪、环境光是什么"这类由引擎
|
||||||
|
/// 在相机流程里塞进去的全局量天然缺失——这里就是补那一块的口子。
|
||||||
|
///
|
||||||
|
/// <para><b>只改自己认识的那几条,并且画完就还原</b>:不还原等于把一次烘焙的临时值泄漏给整个编辑器。
|
||||||
|
/// 还原是尽力而为:引擎每帧会重设的内置量(例如相机相关的矩阵)可能被覆盖回去。</para>
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
public class ShaderBakeGlobalSettings
|
||||||
|
{
|
||||||
|
[Tooltip("要参与烘焙的全局参数。没勾选的行完全不参与")]
|
||||||
|
public List<ShaderBakeGlobalEntry> entries = new List<ShaderBakeGlobalEntry>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 把启用的条目写进全局,并返回一组还原动作(按相反顺序执行)。
|
||||||
|
/// </summary>
|
||||||
|
public List<Action> Apply()
|
||||||
|
{
|
||||||
|
var restores = new List<Action>();
|
||||||
|
if (entries == null)
|
||||||
|
return restores;
|
||||||
|
|
||||||
|
for (int i = 0; i < entries.Count; i++)
|
||||||
|
{
|
||||||
|
ShaderBakeGlobalEntry entry = entries[i];
|
||||||
|
if (entry == null || !entry.enabled || string.IsNullOrEmpty(entry.name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
string name = entry.name;
|
||||||
|
switch (entry.kind)
|
||||||
|
{
|
||||||
|
case ShaderBakeGlobalKind.Float:
|
||||||
|
{
|
||||||
|
float previous = Shader.GetGlobalFloat(name);
|
||||||
|
Shader.SetGlobalFloat(name, entry.x);
|
||||||
|
restores.Add(() => Shader.SetGlobalFloat(name, previous));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ShaderBakeGlobalKind.Vector:
|
||||||
|
{
|
||||||
|
Vector4 previous = Shader.GetGlobalVector(name);
|
||||||
|
Shader.SetGlobalVector(name, entry.ToVector());
|
||||||
|
restores.Add(() => Shader.SetGlobalVector(name, previous));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ShaderBakeGlobalKind.Color:
|
||||||
|
{
|
||||||
|
Color previous = Shader.GetGlobalColor(name);
|
||||||
|
Shader.SetGlobalColor(name, entry.ToColor());
|
||||||
|
restores.Add(() => Shader.SetGlobalColor(name, previous));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ShaderBakeGlobalKind.Texture:
|
||||||
|
{
|
||||||
|
Texture previous = Shader.GetGlobalTexture(name);
|
||||||
|
Shader.SetGlobalTexture(name, entry.texture);
|
||||||
|
restores.Add(() => Shader.SetGlobalTexture(name, previous));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return restores;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> 按名字找已有条目,没有就追加一条(扫描与预设都走这里,避免重复) </summary>
|
||||||
|
public ShaderBakeGlobalEntry Ensure(string name, ShaderBakeGlobalKind kind)
|
||||||
|
{
|
||||||
|
if (entries == null)
|
||||||
|
entries = new List<ShaderBakeGlobalEntry>();
|
||||||
|
for (int i = 0; i < entries.Count; i++)
|
||||||
|
{
|
||||||
|
if (entries[i] != null && entries[i].name == name)
|
||||||
|
return entries[i];
|
||||||
|
}
|
||||||
|
var entry = new ShaderBakeGlobalEntry(name, kind);
|
||||||
|
entries.Add(entry);
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 常用光照/环境量。全部默认关闭:不同渲染管线对这些名字的语义不完全一样,
|
||||||
|
/// 该不该开由用户决定,工具不替他改画面。
|
||||||
|
/// </summary>
|
||||||
|
public void AddEnvironmentPresets()
|
||||||
|
{
|
||||||
|
AddVector("_WorldSpaceLightPos0", 0.3f, 0.7f, -0.6f, 0f);
|
||||||
|
AddColor("_LightColor0", 1f, 1f, 1f, 1f);
|
||||||
|
AddColor("unity_AmbientSky", 0.21f, 0.23f, 0.25f, 1f);
|
||||||
|
AddColor("unity_AmbientEquator", 0.11f, 0.12f, 0.13f, 1f);
|
||||||
|
AddColor("unity_AmbientGround", 0.04f, 0.04f, 0.04f, 1f);
|
||||||
|
AddVector("_Time", 0f, 0f, 0f, 0f);
|
||||||
|
Ensure("unity_SpecCube0", ShaderBakeGlobalKind.Texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddVector(string name, float x, float y, float z, float w)
|
||||||
|
{
|
||||||
|
ShaderBakeGlobalEntry entry = Ensure(name, ShaderBakeGlobalKind.Vector);
|
||||||
|
entry.x = x; entry.y = y; entry.z = z; entry.w = w;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddColor(string name, float x, float y, float z, float w)
|
||||||
|
{
|
||||||
|
ShaderBakeGlobalEntry entry = Ensure(name, ShaderBakeGlobalKind.Color);
|
||||||
|
entry.x = x; entry.y = y; entry.z = z; entry.w = w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Rendering;
|
using UnityEngine.Rendering;
|
||||||
|
|
||||||
@@ -356,6 +357,7 @@ namespace XericUI.ShaderBake
|
|||||||
// 不动网格(外部模型的绕序不该被我们改),只在这一刻把剔除方向反过来。
|
// 不动网格(外部模型的绕序不该被我们改),只在这一刻把剔除方向反过来。
|
||||||
bool previousInvert = GL.invertCulling;
|
bool previousInvert = GL.invertCulling;
|
||||||
GL.invertCulling = previousInvert != settings.output.flipY;
|
GL.invertCulling = previousInvert != settings.output.flipY;
|
||||||
|
List<Action> restoreGlobals = settings.globals != null ? settings.globals.Apply() : null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Graphics.ExecuteCommandBuffer(m_Buffer);
|
Graphics.ExecuteCommandBuffer(m_Buffer);
|
||||||
@@ -363,6 +365,12 @@ namespace XericUI.ShaderBake
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GL.invertCulling = previousInvert;
|
GL.invertCulling = previousInvert;
|
||||||
|
// 画完立刻还原:这些是编辑器全局状态,留一次烘焙的值下来会污染别处的渲染
|
||||||
|
if (restoreGlobals != null)
|
||||||
|
{
|
||||||
|
for (int i = restoreGlobals.Count - 1; i >= 0; i--)
|
||||||
|
restoreGlobals[i]();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m_Buffer.Clear();
|
m_Buffer.Clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ namespace XericUI.ShaderBake
|
|||||||
[Tooltip("离屏相机")]
|
[Tooltip("离屏相机")]
|
||||||
public ShaderBakeCameraSettings camera = new ShaderBakeCameraSettings();
|
public ShaderBakeCameraSettings camera = new ShaderBakeCameraSettings();
|
||||||
|
|
||||||
|
[Tooltip("渲染前注入的全局参数(光照方向、环境光、反射贴图等)")]
|
||||||
|
public ShaderBakeGlobalSettings globals = new ShaderBakeGlobalSettings();
|
||||||
|
|
||||||
/// <summary> 画面宽高比 </summary>
|
/// <summary> 画面宽高比 </summary>
|
||||||
public float aspect
|
public float aspect
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user