From b1648b5397d1d930e0577da6f80358729c9ff32b Mon Sep 17 00:00:00 2001
From: LiRuoChen <571244399@qq.com>
Date: Mon, 9 Nov 2026 09:55:07 +0800
Subject: [PATCH] =?UTF-8?q?feat(ShaderBake):=20=E6=97=B6=E9=97=B4=E8=BD=B4?=
=?UTF-8?q?=E5=8F=82=E6=95=B0=E4=B8=8E=E5=BA=8F=E5=88=97=E5=AF=BC=E5=87=BA?=
=?UTF-8?q?=EF=BC=88=E5=AE=AB=E6=A0=BC=E5=9B=BE=20/=20=E8=A7=86=E9=A2=91?=
=?UTF-8?q?=20/=20Texture3D=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
离屏渲染没有"当前帧",同一套参数连渲两次必须得到同一张图,否则序列是花的、
问题也没法复现。新增 ShaderBakeTimeSettings(时间 / 每帧时间 / 帧数 / 宫格列):
渲染时把 _Time、_SinTime、_CosTime、unity_DeltaTime 灌进引擎全局量,画完交还
当前时钟;单张导出同样用这个固定时间。
导出菜单新增三项,共用这组时间参数:
- 序列·宫格图:逐帧渲进一张 RT,用 SetViewport + Blit 拼贴,只在最后读回一次;
单张上限 8192,列数 0 时按帧数取近似正方形。
- 序列·视频:MediaEncoder 写 mp4(H.264,无 alpha)或 webm(VP8,带 alpha),
帧率由每帧时间反推,宽高要求偶数。
- 序列·Texture3D:每帧一层,深度=帧数,存成 .asset。
---
CHANGELOG.md | 2 +
Editor/ShaderBake/BakeShaderAsImageWindow.cs | 4 +
Editor/ShaderBake/BakeShaderWindow.uxml | 8 +
Editor/ShaderBake/ShaderBakeExporter.cs | 236 +++++++++++++++++-
README.md | 12 +
Runtime/ShaderBake/ShaderBakeRenderer.cs | 55 +++-
Runtime/ShaderBake/ShaderBakeSettings.cs | 4 +
Runtime/ShaderBake/ShaderBakeTimeSettings.cs | 50 ++++
.../ShaderBake/ShaderBakeTimeSettings.cs.meta | 11 +
9 files changed, 373 insertions(+), 9 deletions(-)
create mode 100644 Runtime/ShaderBake/ShaderBakeTimeSettings.cs
create mode 100644 Runtime/ShaderBake/ShaderBakeTimeSettings.cs.meta
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b33925..2b1cda0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -119,6 +119,8 @@ P29 方向的单位="差折法"(只缩不移位)+ 四档方向来源到底
修 Range 属性的滑块区间取错:`ShaderUtil.GetRangeLimits` 的 0/1/2 依次是 默认值/最小/最大,按 0/1 取会让区间变成 0..默认值,手柄永远顶在右端
修属性行数值框被挤出面板:主题滑块默认不收缩,显式 `flex-shrink: 1` 并把数值框钉死 58px
细节面板六个板块改用 Foldout 折叠(原来是带复选框的 Toggle,看着不像能折),标题左侧的三角就是状态,整条可点;全局参数默认折叠
+新增时间轴参数(时间 / 每帧时间 / 帧数 / 宫格列):渲染时注入 `_Time`、`_SinTime`、`_CosTime` 与 `unity_DeltaTime`,画完交还当前时钟;单张导出也走这个固定时间,同参数可复现
+新增序列导出:宫格图(逐帧 GPU blit 拼接,单张上限 8192)、视频(mp4 无 alpha / webm 带 alpha,帧率由每帧时间反推)、Texture3D(每帧一层,存 .asset)
## [0.1.6] - 2026-07-01
diff --git a/Editor/ShaderBake/BakeShaderAsImageWindow.cs b/Editor/ShaderBake/BakeShaderAsImageWindow.cs
index be3b653..7fac4b7 100644
--- a/Editor/ShaderBake/BakeShaderAsImageWindow.cs
+++ b/Editor/ShaderBake/BakeShaderAsImageWindow.cs
@@ -582,6 +582,10 @@ namespace XericUI.ShaderBake.Editor
preset.RegisterValueChangedCallback(evt => ApplySizePreset(evt.newValue));
}
Click("out-fit-aspect", FitOutputAspect);
+ BindFloat("time-seconds", () => m_Renderer.settings.time.seconds, v => m_Renderer.settings.time.seconds = v);
+ BindFloat("time-step", () => m_Renderer.settings.time.frameStep, v => m_Renderer.settings.time.frameStep = v);
+ BindInt("time-frames", () => m_Renderer.settings.time.frameCount, v => m_Renderer.settings.time.frameCount = v);
+ BindInt("time-columns", () => m_Renderer.settings.time.sheetColumns, v => m_Renderer.settings.time.sheetColumns = v);
BindInt("out-width", () => m_Renderer.settings.output.width, v => m_Renderer.settings.output.width = v);
BindInt("out-height", () => m_Renderer.settings.output.height, v => m_Renderer.settings.output.height = v);
diff --git a/Editor/ShaderBake/BakeShaderWindow.uxml b/Editor/ShaderBake/BakeShaderWindow.uxml
index 325235a..d88c865 100644
--- a/Editor/ShaderBake/BakeShaderWindow.uxml
+++ b/Editor/ShaderBake/BakeShaderWindow.uxml
@@ -87,6 +87,14 @@
+
+
+
+
+
+
+
+
diff --git a/Editor/ShaderBake/ShaderBakeExporter.cs b/Editor/ShaderBake/ShaderBakeExporter.cs
index 4b5bdcf..1ae0e6b 100644
--- a/Editor/ShaderBake/ShaderBakeExporter.cs
+++ b/Editor/ShaderBake/ShaderBakeExporter.cs
@@ -1,7 +1,9 @@
using System;
using System.IO;
using UnityEditor;
+using UnityEditor.Media;
using UnityEngine;
+using UnityEngine.Rendering;
namespace XericUI.ShaderBake.Editor
{
@@ -36,6 +38,11 @@ namespace XericUI.ShaderBake.Editor
menu.AddItem(new GUIContent("TGA 文件…"), false, () => SaveFile(ShaderBakeImageFormat.Tga, name));
menu.AddItem(new GUIContent("EXR 文件…"), false, () => SaveFile(ShaderBakeImageFormat.Exr, name));
+ menu.AddSeparator(string.Empty);
+ menu.AddItem(new GUIContent("序列 · 宫格图(png)…"), false, () => SaveSpriteSheet(name));
+ menu.AddItem(new GUIContent("序列 · 视频(mp4 / webm)…"), false, () => SaveVideo(name));
+ menu.AddItem(new GUIContent("序列 · Texture3D 资产(.asset)…"), false, () => SaveTexture3D(name));
+
menu.AddSeparator(string.Empty);
menu.AddItem(new GUIContent("Texture2D 资产(.asset)…"), false, () => SaveTextureAsset(name));
menu.AddItem(new GUIContent("Sprite 资产(png + 导入设置)…"), false, () => SaveSpriteAsset(name));
@@ -50,7 +57,7 @@ namespace XericUI.ShaderBake.Editor
public void SaveFile(ShaderBakeImageFormat format, string suggestedName)
{
- Texture2D texture = m_Renderer.RenderToTexture2D(false);
+ Texture2D texture = m_Renderer.RenderToTexture2D(false, m_Renderer.settings.time.seconds);
if (texture == null)
{
Fail(m_Renderer.lastError);
@@ -105,7 +112,7 @@ namespace XericUI.ShaderBake.Editor
public void SaveTextureAsset(string suggestedName)
{
- Texture2D texture = m_Renderer.RenderToTexture2D(false);
+ Texture2D texture = m_Renderer.RenderToTexture2D(false, m_Renderer.settings.time.seconds);
if (texture == null)
{
Fail(m_Renderer.lastError);
@@ -140,7 +147,7 @@ namespace XericUI.ShaderBake.Editor
if (string.IsNullOrEmpty(path))
return;
- Texture2D texture = m_Renderer.RenderToTexture2D(false);
+ Texture2D texture = m_Renderer.RenderToTexture2D(false, m_Renderer.settings.time.seconds);
if (texture == null)
{
Fail(m_Renderer.lastError);
@@ -177,7 +184,7 @@ namespace XericUI.ShaderBake.Editor
// 存成资产要能被别处直接采样/读回,单采样即可;采样样式由烘焙设置里的 MSAA 只在预览时体现
var rt = m_Renderer.settings.output.CreateRenderTexture(Path.GetFileNameWithoutExtension(path), 1);
- if (m_Renderer.Render(rt) == null)
+ if (m_Renderer.Render(rt, m_Renderer.settings.time.seconds) == null)
{
Fail(m_Renderer.lastError);
ShaderBakeRenderer.ReleaseTempObject(rt, true);
@@ -190,6 +197,226 @@ namespace XericUI.ShaderBake.Editor
Report("已保存 " + path);
}
+ // ── 序列 ────────────────────────────────────────────────
+
+ /// 宫格图单边上限:再大显卡和内存都吃不住,也不像"一张图"了
+ private const int kMaxSheetSide = 8192;
+
+ ///
+ /// 把 N 帧按宫格拼成一张图。每帧按设置里的时间轴渲染,拼贴走 GPU(SetViewport + Blit),
+ /// 只在最后读回一次——逐帧读回再 SetPixels 在 32×32 这种规模上会慢到不可用。
+ ///
+ public void SaveSpriteSheet(string suggestedName)
+ {
+ ShaderBakeTimeSettings time = m_Renderer.settings.time;
+ ShaderBakeOutputSettings output = m_Renderer.settings.output;
+ int frames = time.frameCount;
+ int columns = time.ColumnsFor(frames);
+ int rows = Mathf.CeilToInt(frames / (float)columns);
+ int sheetWidth = columns * output.width;
+ int sheetHeight = rows * output.height;
+ if (sheetWidth > kMaxSheetSide || sheetHeight > kMaxSheetSide)
+ {
+ Fail("宫格图 " + sheetWidth + "×" + sheetHeight + " 超过单张纹理上限 " + kMaxSheetSide +
+ ",把帧数或单帧尺寸调小");
+ return;
+ }
+
+ string path = EditorUtility.SaveFilePanel("导出宫格图", LastDir(),
+ suggestedName + "_" + columns + "x" + rows, "png");
+ if (string.IsNullOrEmpty(path))
+ return;
+
+ RenderTexture sheet = RenderTexture.GetTemporary(sheetWidth, sheetHeight, 0,
+ output.format, output.renderTextureColorSpace);
+ try
+ {
+ var buffer = new CommandBuffer { name = "ShaderBakeSheet" };
+ try
+ {
+ buffer.SetRenderTarget(sheet);
+ buffer.ClearRenderTarget(false, true, output.clearColor);
+ for (int i = 0; i < frames; i++)
+ {
+ RenderTexture frame = m_Renderer.Render(null, time.TimeOf(i));
+ if (frame == null)
+ {
+ Fail(m_Renderer.lastError);
+ return;
+ }
+ buffer.SetViewport(new Rect((i % columns) * output.width, (i / columns) * output.height,
+ output.width, output.height));
+ buffer.Blit(frame, BuiltinRenderTextureType.CurrentActive);
+ }
+ Graphics.ExecuteCommandBuffer(buffer);
+ }
+ finally
+ {
+ buffer.Release();
+ }
+
+ Texture2D texture = ShaderBakeReadback.ToTexture2D(sheet, output.textureFormat,
+ output.isLinearTexture, false);
+ try
+ {
+ WritePng(path, texture);
+ }
+ finally
+ {
+ ShaderBakeRenderer.ReleaseTempObject(texture, true);
+ }
+ }
+ finally
+ {
+ RenderTexture.ReleaseTemporary(sheet);
+ }
+ }
+
+ ///
+ /// 把 N 帧写成一段视频。帧率由"每帧时间"反推(步长 1/30 就是 30fps),
+ /// .webm 走 VP8 带 alpha,.mp4 走 H.264 不带 alpha——H.264 没有透明通道,这点没得选。
+ ///
+ public void SaveVideo(string suggestedName)
+ {
+ ShaderBakeTimeSettings time = m_Renderer.settings.time;
+ ShaderBakeOutputSettings output = m_Renderer.settings.output;
+ if (output.width % 2 != 0 || output.height % 2 != 0)
+ {
+ Fail("视频宽高必须是偶数(现在是 " + output.width + "×" + output.height + ")");
+ return;
+ }
+
+ string path = EditorUtility.SaveFilePanel("导出序列视频", LastDir(), suggestedName, "mp4");
+ if (string.IsNullOrEmpty(path))
+ return;
+
+ bool withAlpha = path.EndsWith(".webm", StringComparison.OrdinalIgnoreCase);
+ int fps = Mathf.Clamp(Mathf.RoundToInt(1f / Mathf.Max(time.frameStep, 0.0001f)), 1, 120);
+ var attributes = new VideoTrackAttributes
+ {
+ frameRate = new MediaRational(fps, 1),
+ width = (uint)output.width,
+ height = (uint)output.height,
+ includeAlpha = withAlpha,
+ bitRateMode = VideoBitrateMode.High
+ };
+
+ try
+ {
+ using (var encoder = new MediaEncoder(path, attributes))
+ {
+ for (int i = 0; i < time.frameCount; i++)
+ {
+ Texture2D frame = m_Renderer.RenderToTexture2D(false, time.TimeOf(i));
+ if (frame == null)
+ {
+ Fail(m_Renderer.lastError);
+ return;
+ }
+ try
+ {
+ encoder.AddFrame(frame);
+ }
+ finally
+ {
+ ShaderBakeRenderer.ReleaseTempObject(frame, true);
+ }
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ Fail("视频写入失败:" + e.Message);
+ return;
+ }
+
+ RememberDir(path);
+ Report("已导出 " + path + "(" + time.frameCount + " 帧 / " + fps + "fps)");
+ }
+
+ /// 每帧一层,第 0 层是最早的那一帧;深度就是帧数
+ public void SaveTexture3D(string suggestedName)
+ {
+ ShaderBakeTimeSettings time = m_Renderer.settings.time;
+ ShaderBakeOutputSettings output = m_Renderer.settings.output;
+ int depth = time.frameCount;
+ int width = output.width;
+ int height = output.height;
+ long total = (long)width * height * depth;
+ if (total > 64L * 1024 * 1024)
+ {
+ Fail("Texture3D " + width + "×" + height + "×" + depth + " 太大(约 " +
+ (total * 16 / (1024 * 1024)) + " MB),把帧数或单帧尺寸调小");
+ return;
+ }
+
+ string path = EditorUtility.SaveFilePanelInProject("保存 Texture3D 资产", suggestedName, "asset",
+ "每一帧是这张 3D 纹理的一层,按时间顺序从第 0 层往上排");
+ if (string.IsNullOrEmpty(path))
+ return;
+
+ var volume = new Texture3D(width, height, depth, output.textureFormat, false)
+ {
+ name = Path.GetFileNameWithoutExtension(path),
+ wrapMode = output.wrapMode,
+ filterMode = output.filterMode,
+ hideFlags = HideFlags.None
+ };
+ var colors = new Color[(int)total];
+ int sliceSize = width * height;
+ for (int i = 0; i < depth; i++)
+ {
+ Texture2D frame = m_Renderer.RenderToTexture2D(false, time.TimeOf(i));
+ if (frame == null)
+ {
+ Fail(m_Renderer.lastError);
+ ShaderBakeRenderer.ReleaseTempObject(volume, true);
+ return;
+ }
+ Color[] slice = frame.GetPixels();
+ Array.Copy(slice, 0, colors, i * sliceSize, slice.Length);
+ ShaderBakeRenderer.ReleaseTempObject(frame, true);
+ }
+ volume.SetPixels(colors);
+ volume.Apply(false, false);
+ AssetDatabase.CreateAsset(volume, path);
+ AssetDatabase.SaveAssets();
+ RememberDir(path);
+ Report("已保存 " + path + "(" + depth + " 层)");
+ }
+
+ private void WritePng(string path, Texture2D texture)
+ {
+ byte[] bytes = ShaderBakeImageFormat.Png.Encode(texture, m_Renderer.settings.output.clearColor);
+ if (bytes == null || bytes.Length == 0)
+ {
+ Fail("PNG 编码失败");
+ return;
+ }
+ try
+ {
+ Directory.CreateDirectory(Path.GetDirectoryName(path));
+ File.WriteAllBytes(path, bytes);
+ }
+ catch (Exception e)
+ {
+ Fail("写入失败:" + e.Message);
+ return;
+ }
+
+ RememberDir(path);
+ string assetPath = ToAssetPath(path);
+ if (assetPath != null)
+ {
+ ImportAndConfigure(assetPath, false);
+ Report("已导出 " + assetPath);
+ }
+ else
+ {
+ Report("已导出 " + path);
+ }
+ }
+
// ── 参数搬运 ────────────────────────────────────────────
private void CopySettingsJson()
@@ -211,6 +438,7 @@ namespace XericUI.ShaderBake.Editor
m_Renderer.settings.surface = parsed.surface;
m_Renderer.settings.camera = parsed.camera;
m_Renderer.settings.globals = parsed.globals;
+ m_Renderer.settings.time = parsed.time;
Report("烘焙参数已从剪贴板替换");
Action apply = settingsReplaced;
if (apply != null)
diff --git a/README.md b/README.md
index 61756be..81453f9 100644
--- a/README.md
+++ b/README.md
@@ -80,6 +80,18 @@ public class MeasuringPointPageManager : FlipPageComponentManager
尺寸预设或自定义、MSAA、HDR、sRGB 存储、翻转 Y、Pass 下标、背景色。导出支持 PNG / JPG / TGA / EXR,以及直接生成 Sprite 资产。
+### 时间与序列
+
+「输出」里的**时间**就是渲染时注入的 `_Time`(秒):单张导出用的也是它,同一套参数导两次得到同一张图。「每帧时间」是序列每帧前进多少秒,同时也是时间倍率——想放慢就把它调小。
+
+导出菜单里有三种序列导出,帧数 / 每帧时间 / 宫格列共用这组参数:
+
+- **宫格图**:把 N 帧拼成一张 png,列数填 0 时按帧数取近似正方形(帧数 1024 就是 32×32);单张上限 8192。
+- **视频**:帧率由每帧时间反推(1/30 秒 → 30fps)。`.mp4` 不带透明通道,`.webm` 带 alpha;宽高必须是偶数。
+- **Texture3D**:每帧一层,第 0 层是最早的那一帧,深度=帧数,存成 .asset。
+
+预览仍按编辑器的实时时间播放,只有导出才用这组固定时间——这样你可以边看动画边产出可复现的结果。
+
### 面片与 UV
Quad 网格里每一格对应一段 UV 区域,格号(UV X / Y)就是这段区域原点的整数部分;加行加列会按贴边那一格往外推算编号,「恢复 1×1」回到单片。也可以切到外部网格,把模型的某一层按平面投影烘出来。
diff --git a/Runtime/ShaderBake/ShaderBakeRenderer.cs b/Runtime/ShaderBake/ShaderBakeRenderer.cs
index e2049b7..4166a24 100644
--- a/Runtime/ShaderBake/ShaderBakeRenderer.cs
+++ b/Runtime/ShaderBake/ShaderBakeRenderer.cs
@@ -58,7 +58,7 @@ namespace XericUI.ShaderBake
/// 非空时渲进调用方那张(要求宽高与 settings.output 一致,否则 Unity 只会画一部分)。
/// 返回真正写入了结果的那张 RT,可能是传入的那张,也可能是内部那张。
///
- public RenderTexture Render(RenderTexture target = null)
+ public RenderTexture Render(RenderTexture target = null, float? timeOverride = null)
{
lastError = string.Empty;
@@ -92,7 +92,7 @@ namespace XericUI.ShaderBake
bool needResolve = settings.output.antiAliasing > 1 && target == null && m_MultiSampleSource != null;
RenderTexture drawn = needResolve ? m_MultiSampleSource : dest;
- DrawInto(drawn);
+ DrawInto(drawn, timeOverride);
if (needResolve)
Graphics.Blit(m_MultiSampleSource, dest);
@@ -101,9 +101,9 @@ namespace XericUI.ShaderBake
}
/// 渲成可读 Texture2D(调用方负责销毁)。导出文件与存资产都从这里出发。
- public Texture2D RenderToTexture2D(bool generateMips = false)
+ public Texture2D RenderToTexture2D(bool generateMips = false, float? timeOverride = null)
{
- RenderTexture rt = Render();
+ RenderTexture rt = Render(null, timeOverride);
if (rt == null)
return null;
var texture = ShaderBakeReadback.ToTexture2D(rt, settings.output.textureFormat, settings.output.isLinearTexture, generateMips);
@@ -390,7 +390,7 @@ namespace XericUI.ShaderBake
return m_OwnedTarget;
}
- private void DrawInto(RenderTexture rt)
+ private void DrawInto(RenderTexture rt, float? timeOverride)
{
if (m_Buffer == null)
{
@@ -427,6 +427,9 @@ namespace XericUI.ShaderBake
bool previousInvert = GL.invertCulling;
GL.invertCulling = previousInvert != settings.output.flipY;
List restoreGlobals = settings.globals != null ? settings.globals.Apply() : null;
+ bool timed = timeOverride.HasValue;
+ if (timed)
+ ApplyTimeGlobals(timeOverride.Value);
try
{
Graphics.ExecuteCommandBuffer(m_Buffer);
@@ -434,6 +437,9 @@ namespace XericUI.ShaderBake
finally
{
GL.invertCulling = previousInvert;
+ // 时间改的是引擎全局量,画完必须交还,否则场景/预览里的动画会停在烘焙的那一帧
+ if (timed)
+ RestoreTimeGlobals();
// 画完立刻还原:这些是编辑器全局状态,留一次烘焙的值下来会污染别处的渲染
if (restoreGlobals != null)
{
@@ -444,6 +450,45 @@ namespace XericUI.ShaderBake
m_Buffer.Clear();
}
+ ///
+ /// 把时间灌进引擎全局量:_Time 与派生出来的 _SinTime / _CosTime、unity_DeltaTime。
+ /// 布局照抄 Unity 内置(_Time = (t/20, t, t*2, t*3)),shader 里照常写就行。
+ ///
+ /// 为什么不能走材质或属性块:_Time 是引擎的全局常量,不在材质的
+ /// per-material 常量里,只有改全局才真的能让 shader 读到;代价是画完得交还,
+ /// 见 。
+ ///
+ private static void ApplyTimeGlobals(float seconds)
+ {
+ Shader.SetGlobalVector("_Time", TimeVector(seconds));
+ Shader.SetGlobalVector("_SinTime", new Vector4(
+ Mathf.Sin(seconds / 8f), Mathf.Sin(seconds / 4f), Mathf.Sin(seconds / 2f), Mathf.Sin(seconds)));
+ Shader.SetGlobalVector("_CosTime", new Vector4(
+ Mathf.Cos(seconds / 8f), Mathf.Cos(seconds / 4f), Mathf.Cos(seconds / 2f), Mathf.Cos(seconds)));
+ // 逐帧烘焙没有真实帧间隔,按 1/60 秒给个定值,免得依赖它的 shader 被 0 除出无穷大
+ Shader.SetGlobalVector("unity_DeltaTime", new Vector4(1f / 60f, 60f, 1f / 60f, 60f));
+ }
+
+ ///
+ /// 交还时间:Unity 每帧会重发这些全局量,这里只要回到"当前时钟",
+ /// 就不会让场景或预览里的动画停在烘焙用的那一帧。
+ ///
+ private static void RestoreTimeGlobals()
+ {
+ float now = Application.isPlaying ? Time.time : Time.realtimeSinceStartup;
+ Shader.SetGlobalVector("_Time", TimeVector(now));
+ Shader.SetGlobalVector("_SinTime", new Vector4(
+ Mathf.Sin(now / 8f), Mathf.Sin(now / 4f), Mathf.Sin(now / 2f), Mathf.Sin(now)));
+ Shader.SetGlobalVector("_CosTime", new Vector4(
+ Mathf.Cos(now / 8f), Mathf.Cos(now / 4f), Mathf.Cos(now / 2f), Mathf.Cos(now)));
+ Shader.SetGlobalVector("unity_DeltaTime", new Vector4(1f / 60f, 60f, 1f / 60f, 60f));
+ }
+
+ private static Vector4 TimeVector(float seconds)
+ {
+ return new Vector4(seconds / 20f, seconds, seconds * 2f, seconds * 3f);
+ }
+
///
/// 回收一次性对象。运行时要走 Destroy(当帧不能立即销毁),编辑器里请传
/// 让 DestroyImmediate 立刻放掉显存——导一批图时差别很大。
diff --git a/Runtime/ShaderBake/ShaderBakeSettings.cs b/Runtime/ShaderBake/ShaderBakeSettings.cs
index 4209214..4bd8a94 100644
--- a/Runtime/ShaderBake/ShaderBakeSettings.cs
+++ b/Runtime/ShaderBake/ShaderBakeSettings.cs
@@ -22,6 +22,9 @@ namespace XericUI.ShaderBake
[Tooltip("渲染前注入的全局参数(光照方向、环境光、反射贴图等)")]
public ShaderBakeGlobalSettings globals = new ShaderBakeGlobalSettings();
+ [Tooltip("时间轴:单张渲染注入的 _Time 与序列导出的帧数 / 步长")]
+ public ShaderBakeTimeSettings time = new ShaderBakeTimeSettings();
+
/// 画面宽高比
public float aspect
{
@@ -38,6 +41,7 @@ namespace XericUI.ShaderBake
output.Clamp();
surface.Clamp();
camera.Clamp();
+ time.Clamp();
}
/// 复制一份(编辑器窗口拿它当工作副本,改坏了不至于污染存下来的那份)
diff --git a/Runtime/ShaderBake/ShaderBakeTimeSettings.cs b/Runtime/ShaderBake/ShaderBakeTimeSettings.cs
new file mode 100644
index 0000000..9fe7c5b
--- /dev/null
+++ b/Runtime/ShaderBake/ShaderBakeTimeSettings.cs
@@ -0,0 +1,50 @@
+using System;
+using UnityEngine;
+
+namespace XericUI.ShaderBake
+{
+ ///
+ /// 时间轴:单张渲染注入的 _Time,以及序列导出(视频 / 宫格图 / Texture3D)的帧数与步长。
+ ///
+ /// 为什么要把时间做成参数:离屏渲染没有"当前帧"这回事,
+ /// 同一套参数连渲两次必须得到同一张图,否则导出的序列是花的、复现问题也没法对齐。
+ /// 所以时间由这里显式给定,渲染时注入、渲染完还原,不跟着编辑器的帧走。
+ ///
+ [Serializable]
+ public class ShaderBakeTimeSettings
+ {
+ [Tooltip("注入的 _Time(秒)。单张导出与序列的第一帧都用它")]
+ public float seconds;
+
+ [Tooltip("序列每帧前进多少秒:它同时就是时间倍率,把动画放慢/加快都靠它")]
+ public float frameStep = 1f / 30f;
+
+ [Tooltip("序列帧数(视频帧数 / 宫格图格数 / 3D 纹理深度)")]
+ public int frameCount = 64;
+
+ [Tooltip("宫格图每行多少列,0=按帧数自动取近似正方形")]
+ public int sheetColumns;
+
+ public const int kMaxFrames = 4096;
+
+ public void Clamp()
+ {
+ frameCount = Mathf.Clamp(frameCount, 1, kMaxFrames);
+ sheetColumns = Mathf.Clamp(sheetColumns, 0, 256);
+ }
+
+ /// 第 帧渲染用的时间
+ public float TimeOf(int frame)
+ {
+ return seconds + frameStep * frame;
+ }
+
+ /// 宫格图的列数:填了就用填的,0 时取能排成正接近正方形的列数
+ public int ColumnsFor(int frames)
+ {
+ if (sheetColumns > 0)
+ return Mathf.Min(sheetColumns, frames);
+ return Mathf.Max(1, Mathf.CeilToInt(Mathf.Sqrt(frames)));
+ }
+ }
+}
diff --git a/Runtime/ShaderBake/ShaderBakeTimeSettings.cs.meta b/Runtime/ShaderBake/ShaderBakeTimeSettings.cs.meta
new file mode 100644
index 0000000..a8554fd
--- /dev/null
+++ b/Runtime/ShaderBake/ShaderBakeTimeSettings.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 49f84e8bd0ee48869ee43854449f07f5
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant: