fix(ShaderBake): 修正面片绕序与 MSAA 落地,补齐 _MainTex_ST 与菜单根

- 四边形原来按逆时针排三角,从默认相机方向看是背面,会被整片剔除
- MSAA 时落地目标也被建成多采样,Blit 写不进去;落地目标固定单采样
- flipY 是投影镜像,会让绕序反转,绘制期间同步翻转剔除方向
- applyUvToMainTexST 之前只有开关没有实现,改用属性块写 _MainTex_ST,不污染材质副本
- 编辑器非播放状态下 Object.Destroy 会被拒绝,JPG 压平纹理改走统一回收
- 注册路径补 ToolBar 根(XericLibrary/Tool),供烘焙窗口挂菜单
This commit is contained in:
2026-11-05 16:51:39 +08:00
parent 63173c6ec2
commit a769cbac00
7 changed files with 54 additions and 16 deletions
+2
View File
@@ -72,6 +72,8 @@ namespace XericUI.Core
public const string TableBar = MenuBarRoot + "/Table";
/// <summary> 通用 UI 族的工具栏入口(场景变形手柄开关) </summary>
public const string UIBar = MenuBarRoot + "/UI";
/// <summary> 工具族(离线渲染 / 烘焙之类的一次性工具) </summary>
public const string ToolBar = MenuBarRoot + "/Tool";
// ── Hierarchy 创建菜单 ────────────────────────────────────
+3 -2
View File
@@ -26,9 +26,10 @@ namespace XericUI.ShaderBake
if (rt == null)
return null;
// renderer.Dispose 会把内部那张一起销毁,所以交出去的是按同一套参数另建的一张
// renderer.Dispose 会把内部那张一起销毁,所以交出去的是按同一套参数另建的一张。
// 这张要能被 Blit 写入,只能是单采样(采样已在 Render 里解析过了)。
var effective = settings != null ? settings : ShaderBakeSettings.CreateDefault();
var copy = effective.output.CreateRenderTexture("ShaderBakeResult");
var copy = effective.output.CreateRenderTexture("ShaderBakeResult", 1);
Graphics.Blit(rt, copy);
return copy;
}
@@ -98,13 +98,14 @@ namespace XericUI.ShaderBake
int bottomRight = bottomLeft + 1;
int topLeft = bottomLeft + stride;
int topRight = topLeft + 1;
// 绕序按"从 -Z 往 +Z 看是顺时针"排;翻转投影时靠 output.flipY 修,不靠这里换绕序
// Unity 认定顺时针为正面(视线方向上的屏幕顺时针),所以是 左下→左上→右下;
// 顺序写反了整张面片会被背面剔除掉,画面全空还查不出原因
triangles.Add(bottomLeft);
triangles.Add(bottomRight);
triangles.Add(topLeft);
triangles.Add(bottomRight);
triangles.Add(topLeft);
triangles.Add(topRight);
triangles.Add(topLeft);
triangles.Add(bottomRight);
}
var mesh = NewMesh("ShaderBakeQuad");
+2 -1
View File
@@ -62,7 +62,8 @@ namespace XericUI.ShaderBake
}
finally
{
UnityEngine.Object.Destroy(flattened);
// 编辑器非播放状态下 Object.Destroy 会被拒绝并报错,统一走这条能分辨时机的回收
ShaderBakeRenderer.ReleaseTempObject(flattened);
}
}
@@ -120,16 +120,21 @@ namespace XericUI.ShaderBake
/// <summary>
/// 按当前设置造一张未被使用过的 RenderTexture(调用方负责销毁)。
/// <paramref name="samples"/> 传负数表示按设置走;解析目标必须显式传 1——
/// 往多采样目标上 Blit 是非法的,MSAA 那条路要靠"先画采样目标、再落地到单采样目标"。
/// </summary>
public RenderTexture CreateRenderTexture(string name = "ShaderBakeTarget")
public RenderTexture CreateRenderTexture(string name = "ShaderBakeTarget", int samples = -1)
{
Clamp();
int actualSamples = samples < 0 ? antiAliasing : SnapAntiAliasing(samples);
if (hdr && actualSamples > 1)
actualSamples = 1;
var rt = new RenderTexture(width, height, depthBuffer, format, renderTextureColorSpace)
{
name = name,
filterMode = filterMode,
wrapMode = wrapMode,
antiAliasing = antiAliasing,
antiAliasing = actualSamples,
hideFlags = HideFlags.DontSave
};
// 显式 Create:antiAliasing 只有在初始化之前赋值才生效,靠第一次使用时自动建就丢了
+3 -3
View File
@@ -25,7 +25,7 @@ namespace XericUI.ShaderBake
name = source.name,
hideFlags = HideFlags.DontSave
};
CopyInto(source, texture);
CopyInto(source, texture, mipChain);
return texture;
}
@@ -33,13 +33,13 @@ namespace XericUI.ShaderBake
/// 读进一张已存在的纹理(尺寸要和 source 一致;不一致 Unity 只会填左上角,剩下留黑)。
/// 走 RenderTexture.active + ReadPixels,MSAA 目标已被调用方解析过,这里读到的是解析后的像素。
/// </summary>
public static void CopyInto(RenderTexture source, Texture2D destination)
public static void CopyInto(RenderTexture source, Texture2D destination, bool recalculateMipMaps = false)
{
RenderTexture previous = RenderTexture.active;
try
{
RenderTexture.active = source;
destination.ReadPixels(new Rect(0, 0, source.width, source.height), 0, 0, false);
destination.ReadPixels(new Rect(0, 0, source.width, source.height), 0, 0, recalculateMipMaps);
destination.Apply(false);
}
finally
+33 -5
View File
@@ -32,6 +32,7 @@ namespace XericUI.ShaderBake
private string m_TargetSignature = string.Empty;
private RenderTexture m_MultiSampleSource;
private CommandBuffer m_Buffer;
private MaterialPropertyBlock m_Block;
/// <summary> 当前这次烘焙用的网格(只读用途,外部别销毁) </summary>
public Mesh geometry { get { return m_Geometry; } }
@@ -188,11 +189,12 @@ namespace XericUI.ShaderBake
ReleaseTempObject(m_OwnedTarget);
ReleaseTempObject(m_MultiSampleSource);
m_OwnedTarget = settings.output.CreateRenderTexture("ShaderBakeTarget");
// 落地目标永远单采样:它要能被 ReadPixels 读、能被 Blit 写入、也直接拿去显示
m_OwnedTarget = settings.output.CreateRenderTexture("ShaderBakeTarget", 1);
m_TargetSignature = m_OwnedTarget != null ? signature : string.Empty;
if (settings.output.antiAliasing > 1 && m_OwnedTarget != null)
m_MultiSampleSource = settings.output.CreateRenderTexture("ShaderBakeMultiSample");
m_MultiSampleSource = settings.output.CreateRenderTexture("ShaderBakeMultiSample", settings.output.antiAliasing);
return m_OwnedTarget;
}
@@ -212,9 +214,35 @@ namespace XericUI.ShaderBake
m_Buffer.SetRenderTarget(rt, 0, CubemapFace.Unknown, 0);
m_Buffer.ClearRenderTarget(true, true, settings.output.clearColor, 1f);
m_Buffer.SetViewProjectionMatrices(settings.camera.viewMatrix, projection);
// UV 已经烘进顶点了;只有用户显式要求时再补一份 _MainTex_ST 给依赖 ST 的 shader。
// 走属性块而不是改材质,是为了不让这一笔写进材质副本、进而被"写回原材质"带走。
MaterialPropertyBlock block = null;
if (settings.surface.applyUvToMainTexST)
{
if (m_Block == null)
m_Block = new MaterialPropertyBlock();
m_Block.Clear();
m_Block.SetVector("_MainTex_ST", new Vector4(
settings.surface.uvTiling.x, settings.surface.uvTiling.y,
settings.surface.uvOffset.x, settings.surface.uvOffset.y));
block = m_Block;
}
m_Buffer.DrawMesh(m_Geometry, settings.surface.localMatrix, material,
Mathf.Max(settings.surface.meshSubMesh, 0), settings.output.shaderPass);
Graphics.ExecuteCommandBuffer(m_Buffer);
Mathf.Max(settings.surface.meshSubMesh, 0), settings.output.shaderPass, block);
// 翻 Y 的投影是一次镜像,会把绕序反过来,背面剔除于是剔掉本该看见的正面。
// 不动网格(外部模型的绕序不该被我们改),只在这一刻把剔除方向反过来。
bool previousInvert = GL.invertCulling;
GL.invertCulling = previousInvert != settings.output.flipY;
try
{
Graphics.ExecuteCommandBuffer(m_Buffer);
}
finally
{
GL.invertCulling = previousInvert;
}
m_Buffer.Clear();
}
@@ -227,7 +255,7 @@ namespace XericUI.ShaderBake
ReleaseTempObject(obj, false);
}
internal static void ReleaseTempObject(UnityEngine.Object obj, bool immediate)
public static void ReleaseTempObject(UnityEngine.Object obj, bool immediate)
{
if (obj == null)
return;