Files
XericUIActionVessel/Runtime/ShaderBake/ShaderBakeReadback.cs
T
lrss3 a769cbac00 fix(ShaderBake): 修正面片绕序与 MSAA 落地,补齐 _MainTex_ST 与菜单根
- 四边形原来按逆时针排三角,从默认相机方向看是背面,会被整片剔除
- MSAA 时落地目标也被建成多采样,Blit 写不进去;落地目标固定单采样
- flipY 是投影镜像,会让绕序反转,绘制期间同步翻转剔除方向
- applyUvToMainTexST 之前只有开关没有实现,改用属性块写 _MainTex_ST,不污染材质副本
- 编辑器非播放状态下 Object.Destroy 会被拒绝,JPG 压平纹理改走统一回收
- 注册路径补 ToolBar 根(XericLibrary/Tool),供烘焙窗口挂菜单
2026-11-05 16:51:39 +08:00

96 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using UnityEngine;
namespace XericUI.ShaderBake
{
/// <summary>
/// RenderTexture → 可读 Texture2D,以及透明压平。
///
/// <para><b>为什么用 ReadPixels 而不是 AsyncGPUReadback</b>:后者要等帧,回调里的数据这一轮就拿不到,
/// 而"点导出→拿到文件"这条链要的是同步返回。烘焙一张图本来就是离线动作, stall 一帧无所谓。</para>
/// </summary>
public static class ShaderBakeReadback
{
/// <summary>
/// 把目标读回成可读纹理。<paramref name="linear"/> 决定纹理按线性还是 sRGB 解释,
/// 必须和生成它的那张 RenderTexture 一致,否则同一张图在编辑器和文件里会差一层 gamma。
/// </summary>
public static Texture2D ToTexture2D(RenderTexture source, TextureFormat format, bool linear, bool mipChain = false)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
var texture = new Texture2D(source.width, source.height, format, mipChain, linear)
{
name = source.name,
hideFlags = HideFlags.DontSave
};
CopyInto(source, texture, mipChain);
return texture;
}
/// <summary>
/// 读进一张已存在的纹理(尺寸要和 source 一致;不一致 Unity 只会填左上角,剩下留黑)。
/// 走 RenderTexture.active + ReadPixels,MSAA 目标已被调用方解析过,这里读到的是解析后的像素。
/// </summary>
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, recalculateMipMaps);
destination.Apply(false);
}
finally
{
RenderTexture.active = previous;
}
}
/// <summary>
/// 把浮点/带 alpha 的纹理压成 RGB24:透明部分按 <paramref name="background"/> 混合,
/// 分量截到 0~1。JPG 没有 alpha 通道,直接用原图编码会得到一圈发黑的边。
/// </summary>
public static Texture2D FlattenOntoBackground(Texture2D source, Color background)
{
int width = source.width;
int height = source.height;
var flattened = new Texture2D(width, height, TextureFormat.RGB24, false, false)
{
name = source.name,
hideFlags = HideFlags.DontSave
};
Color[] pixels = source.GetPixels();
for (int i = 0; i < pixels.Length; i++)
{
Color p = pixels[i];
float a = Mathf.Clamp01(p.a);
pixels[i] = new Color(
Mathf.Lerp(background.r, p.r, a),
Mathf.Lerp(background.g, p.g, a),
Mathf.Lerp(background.b, p.b, a),
1f);
}
flattened.SetPixels(pixels);
flattened.Apply(false);
return flattened;
}
/// <summary>
/// 把一张纹理/RT 画成 Sprite。注意 Sprite 不拥有纹理:纹理销毁了 Sprite 就空了,
/// 所以调用方要么留着纹理,要么连纹理一起作为资产存下来。
/// </summary>
public static Sprite ToSprite(Texture2D texture, Vector2 pivot, float pixelsPerUnit, string name = null)
{
if (texture == null)
throw new ArgumentNullException(nameof(texture));
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), pivot, pixelsPerUnit);
if (!string.IsNullOrEmpty(name))
sprite.name = name;
return sprite;
}
}
}