From a769cbac00ce81321b7470824225283fd6df2c9f Mon Sep 17 00:00:00 2001 From: LiRuoChen <571244399@qq.com> Date: Thu, 5 Nov 2026 16:51:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(ShaderBake):=20=E4=BF=AE=E6=AD=A3=E9=9D=A2?= =?UTF-8?q?=E7=89=87=E7=BB=95=E5=BA=8F=E4=B8=8E=20MSAA=20=E8=90=BD?= =?UTF-8?q?=E5=9C=B0=EF=BC=8C=E8=A1=A5=E9=BD=90=20=5FMainTex=5FST=20?= =?UTF-8?q?=E4=B8=8E=E8=8F=9C=E5=8D=95=E6=A0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 四边形原来按逆时针排三角,从默认相机方向看是背面,会被整片剔除 - MSAA 时落地目标也被建成多采样,Blit 写不进去;落地目标固定单采样 - flipY 是投影镜像,会让绕序反转,绘制期间同步翻转剔除方向 - applyUvToMainTexST 之前只有开关没有实现,改用属性块写 _MainTex_ST,不污染材质副本 - 编辑器非播放状态下 Object.Destroy 会被拒绝,JPG 压平纹理改走统一回收 - 注册路径补 ToolBar 根(XericLibrary/Tool),供烘焙窗口挂菜单 --- Runtime/Core/XericRegistrationPaths.cs | 2 + Runtime/ShaderBake/ShaderBake.cs | 5 ++- .../ShaderBake/ShaderBakeGeometryBuilder.cs | 7 ++-- Runtime/ShaderBake/ShaderBakeImageFormat.cs | 3 +- .../ShaderBake/ShaderBakeOutputSettings.cs | 9 ++++- Runtime/ShaderBake/ShaderBakeReadback.cs | 6 +-- Runtime/ShaderBake/ShaderBakeRenderer.cs | 38 ++++++++++++++++--- 7 files changed, 54 insertions(+), 16 deletions(-) diff --git a/Runtime/Core/XericRegistrationPaths.cs b/Runtime/Core/XericRegistrationPaths.cs index a24affa..b6b8be1 100644 --- a/Runtime/Core/XericRegistrationPaths.cs +++ b/Runtime/Core/XericRegistrationPaths.cs @@ -72,6 +72,8 @@ namespace XericUI.Core public const string TableBar = MenuBarRoot + "/Table"; /// 通用 UI 族的工具栏入口(场景变形手柄开关) public const string UIBar = MenuBarRoot + "/UI"; + /// 工具族(离线渲染 / 烘焙之类的一次性工具) + public const string ToolBar = MenuBarRoot + "/Tool"; // ── Hierarchy 创建菜单 ──────────────────────────────────── diff --git a/Runtime/ShaderBake/ShaderBake.cs b/Runtime/ShaderBake/ShaderBake.cs index 6937fab..f7ec8ed 100644 --- a/Runtime/ShaderBake/ShaderBake.cs +++ b/Runtime/ShaderBake/ShaderBake.cs @@ -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; } diff --git a/Runtime/ShaderBake/ShaderBakeGeometryBuilder.cs b/Runtime/ShaderBake/ShaderBakeGeometryBuilder.cs index 05bedc2..b49d12e 100644 --- a/Runtime/ShaderBake/ShaderBakeGeometryBuilder.cs +++ b/Runtime/ShaderBake/ShaderBakeGeometryBuilder.cs @@ -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"); diff --git a/Runtime/ShaderBake/ShaderBakeImageFormat.cs b/Runtime/ShaderBake/ShaderBakeImageFormat.cs index 92ddd60..e5dfa9e 100644 --- a/Runtime/ShaderBake/ShaderBakeImageFormat.cs +++ b/Runtime/ShaderBake/ShaderBakeImageFormat.cs @@ -62,7 +62,8 @@ namespace XericUI.ShaderBake } finally { - UnityEngine.Object.Destroy(flattened); + // 编辑器非播放状态下 Object.Destroy 会被拒绝并报错,统一走这条能分辨时机的回收 + ShaderBakeRenderer.ReleaseTempObject(flattened); } } diff --git a/Runtime/ShaderBake/ShaderBakeOutputSettings.cs b/Runtime/ShaderBake/ShaderBakeOutputSettings.cs index 94a573a..14a02af 100644 --- a/Runtime/ShaderBake/ShaderBakeOutputSettings.cs +++ b/Runtime/ShaderBake/ShaderBakeOutputSettings.cs @@ -120,16 +120,21 @@ namespace XericUI.ShaderBake /// /// 按当前设置造一张未被使用过的 RenderTexture(调用方负责销毁)。 + /// 传负数表示按设置走;解析目标必须显式传 1—— + /// 往多采样目标上 Blit 是非法的,MSAA 那条路要靠"先画采样目标、再落地到单采样目标"。 /// - 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 只有在初始化之前赋值才生效,靠第一次使用时自动建就丢了 diff --git a/Runtime/ShaderBake/ShaderBakeReadback.cs b/Runtime/ShaderBake/ShaderBakeReadback.cs index 9db20e3..131dfd6 100644 --- a/Runtime/ShaderBake/ShaderBakeReadback.cs +++ b/Runtime/ShaderBake/ShaderBakeReadback.cs @@ -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 目标已被调用方解析过,这里读到的是解析后的像素。 /// - 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 diff --git a/Runtime/ShaderBake/ShaderBakeRenderer.cs b/Runtime/ShaderBake/ShaderBakeRenderer.cs index e4b541a..b9239fe 100644 --- a/Runtime/ShaderBake/ShaderBakeRenderer.cs +++ b/Runtime/ShaderBake/ShaderBakeRenderer.cs @@ -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; /// 当前这次烘焙用的网格(只读用途,外部别销毁) 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;