合并 dev2 的时间轴参数与序列导出(b1648b5)到 dev_chart
只共用 CHANGELOG.md,两侧都是新增条目、没有互相删改,所以两边都保留。 未推送。
This commit is contained in:
@@ -58,7 +58,7 @@ namespace XericUI.ShaderBake
|
||||
/// 非空时渲进调用方那张(要求宽高与 settings.output 一致,否则 Unity 只会画一部分)。
|
||||
/// 返回真正写入了结果的那张 RT,可能是传入的那张,也可能是内部那张。
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary> 渲成可读 Texture2D(调用方负责销毁)。导出文件与存资产都从这里出发。 </summary>
|
||||
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<Action> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把时间灌进引擎全局量:<c>_Time</c> 与派生出来的 <c>_SinTime</c> / <c>_CosTime</c>、<c>unity_DeltaTime</c>。
|
||||
/// 布局照抄 Unity 内置(_Time = (t/20, t, t*2, t*3)),shader 里照常写就行。
|
||||
///
|
||||
/// <para><b>为什么不能走材质或属性块</b>:<c>_Time</c> 是引擎的全局常量,不在材质的
|
||||
/// per-material 常量里,只有改全局才真的能让 shader 读到;代价是画完得交还,
|
||||
/// 见 <see cref="RestoreTimeGlobals"/>。</para>
|
||||
/// </summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 交还时间:Unity 每帧会重发这些全局量,这里只要回到"当前时钟",
|
||||
/// 就不会让场景或预览里的动画停在烘焙用的那一帧。
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回收一次性对象。运行时要走 <c>Destroy</c>(当帧不能立即销毁),编辑器里请传
|
||||
/// <paramref name="immediate"/> 让 <c>DestroyImmediate</c> 立刻放掉显存——导一批图时差别很大。
|
||||
|
||||
@@ -22,6 +22,9 @@ namespace XericUI.ShaderBake
|
||||
[Tooltip("渲染前注入的全局参数(光照方向、环境光、反射贴图等)")]
|
||||
public ShaderBakeGlobalSettings globals = new ShaderBakeGlobalSettings();
|
||||
|
||||
[Tooltip("时间轴:单张渲染注入的 _Time 与序列导出的帧数 / 步长")]
|
||||
public ShaderBakeTimeSettings time = new ShaderBakeTimeSettings();
|
||||
|
||||
/// <summary> 画面宽高比 </summary>
|
||||
public float aspect
|
||||
{
|
||||
@@ -38,6 +41,7 @@ namespace XericUI.ShaderBake
|
||||
output.Clamp();
|
||||
surface.Clamp();
|
||||
camera.Clamp();
|
||||
time.Clamp();
|
||||
}
|
||||
|
||||
/// <summary> 复制一份(编辑器窗口拿它当工作副本,改坏了不至于污染存下来的那份) </summary>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericUI.ShaderBake
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间轴:单张渲染注入的 <c>_Time</c>,以及序列导出(视频 / 宫格图 / Texture3D)的帧数与步长。
|
||||
///
|
||||
/// <para><b>为什么要把时间做成参数</b>:离屏渲染没有"当前帧"这回事,
|
||||
/// 同一套参数连渲两次必须得到同一张图,否则导出的序列是花的、复现问题也没法对齐。
|
||||
/// 所以时间由这里显式给定,渲染时注入、渲染完还原,不跟着编辑器的帧走。</para>
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary> 第 <paramref name="frame"/> 帧渲染用的时间 </summary>
|
||||
public float TimeOf(int frame)
|
||||
{
|
||||
return seconds + frameStep * frame;
|
||||
}
|
||||
|
||||
/// <summary> 宫格图的列数:填了就用填的,0 时取能排成正接近正方形的列数 </summary>
|
||||
public int ColumnsFor(int frames)
|
||||
{
|
||||
if (sheetColumns > 0)
|
||||
return Mathf.Min(sheetColumns, frames);
|
||||
return Mathf.Max(1, Mathf.CeilToInt(Mathf.Sqrt(frames)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49f84e8bd0ee48869ee43854449f07f5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user