合并 dev2 的 23 条 ShaderBake 提交(2026-11-07)到 dev_chart

文件集与 XChart 那侧不交叉(只共用 CHANGELOG.md),冲突两侧都是各自新增的行、
没有任何一方删过对方的内容,所以两边条目都保留:P29b/P25-P23/P26 三条在前,
Bake Shader 窗口那一块接在后面。未推送。
This commit is contained in:
2026-11-09 08:33:11 +08:00
13 changed files with 1210 additions and 286 deletions
+48 -10
View File
@@ -14,7 +14,7 @@ namespace XericUI.ShaderBake
public bool orthographic = true;
[Tooltip("正交半高(世界单位):实际能看到的竖向范围是它的两倍")]
public float orthographicSize = 0.6f;
public float orthographicSize = 0.5f;
[Tooltip("透视相机的竖向视野角(度),正交时忽略")]
public float fieldOfView = 60f;
@@ -31,6 +31,26 @@ namespace XericUI.ShaderBake
[Tooltip("相机旋转(欧拉角,度):默认朝 +Z 看,即看着放在原点、面朝 -Z 的面片")]
public Vector3 eulerAngles;
/// <summary>
/// 这份相机参数是按哪一版"框住"约定算出来的。
///
/// <para>早先的"框住"框的是整个网格、还带 15% 余量,画面里于是留了一圈空白;
/// 宽度约定改成刚好铺满之后,读到的旧存档会在开窗时重框一次。
/// 只认大于等于 <see cref="kCurrentFitRevision"/> 的存档,所以老配置只被纠正一次。</para>
/// </summary>
public int fitRevision;
public const int kCurrentFitRevision = 2;
/// <summary>
/// 正交视口的半宽 / 半高(世界单位),由"框住"写入。
///
/// <para><b>为什么不用输出宽高比推半宽</b>:烘焙的约定是一格 UV 区域对一整张图。
/// 若按输出比例算,1024×4096 的输出里一张 1×1 的面片只能占中间一条,UV 也到不了 0~1。
/// 这里记的是内容本身的宽高,投影按它的比例取半宽,比例不一致时就靠拉伸铺满。</para>
/// </summary>
public Vector2 frameHalfSize = new Vector2(0.5f, 0.5f);
/// <summary>
/// 视口(世界 → 相机)矩阵。
///
@@ -49,7 +69,8 @@ namespace XericUI.ShaderBake
}
/// <summary>
/// 投影矩阵。正交时宽高按 aspect 分配(半宽 = 半高 × aspect),与 Unity 相机一致。
/// 投影矩阵。正交的半宽按<b>内容</b>的宽高比取(不是输出宽高比),
/// 所以输出比例和面片比例不同时,面片会被拉伸到正好铺满整张图。
/// </summary>
public Matrix4x4 GetProjectionMatrix(float aspect)
{
@@ -57,7 +78,10 @@ namespace XericUI.ShaderBake
if (orthographic)
{
float halfHeight = Mathf.Max(orthographicSize, 0.0001f);
float halfWidth = halfHeight * aspect;
float contentAspect = frameHalfSize.y > 0.0001f
? frameHalfSize.x / frameHalfSize.y
: aspect;
float halfWidth = halfHeight * Mathf.Max(contentAspect, 0.0001f);
return Matrix4x4.Ortho(-halfWidth, halfWidth, -halfHeight, halfHeight,
Mathf.Max(nearClipPlane, 0.0001f), Mathf.Max(farClipPlane, nearClipPlane + 0.0001f));
}
@@ -76,12 +100,25 @@ namespace XericUI.ShaderBake
if (radius < 0.0001f) radius = 0.0001f;
if (aspect <= 0f) aspect = 1f;
// 沿当前朝向平移,不改朝向——转着找角度是用户的事,这里只负责"把它挪到正中间、离远一点"
Vector3 dir = Quaternion.Euler(eulerAngles) * Vector3.forward;
if (orthographic)
{
// 横向不够宽时以横向为准:半高 = 半宽 / aspect
float halfFromHeight = extents.y;
float halfFromWidth = extents.x / aspect;
orthographicSize = Mathf.Max(halfFromHeight, halfFromWidth) * Mathf.Max(padding, 1f);
// 宽高各记各的:投影按内容比例取半宽,输出比例再不一样也只会拉伸,不会留白
float factor = Mathf.Max(padding, 1f);
frameHalfSize = new Vector2(
Mathf.Max(extents.x, 0.0001f) * factor,
Mathf.Max(extents.y, 0.0001f) * factor);
orthographicSize = frameHalfSize.y;
// 正交也得把目标挪到视线轴上:只改半高是"缩放到贴着边",
// 目标偏在一边时照样一半在画面外——按格框取时最明显,因为那一格本来就不在网格正中。
// 沿视线那段距离照原样保留,所以本来就在目标平面前面的不会被吸到平面上去
float depthAlongView = Vector3.Dot(bounds.center - position, dir);
if (Mathf.Abs(depthAlongView) < 0.0001f)
depthAlongView = radius;
position = bounds.center - dir * depthAlongView;
}
else
{
@@ -91,8 +128,6 @@ namespace XericUI.ShaderBake
float neededHalfHeight = Mathf.Max(verticalFit, horizontalFit) * Mathf.Max(padding, 1f);
float distance = radius + neededHalfHeight / Mathf.Max(Mathf.Tan(halfFov), 0.0001f);
// 沿当前朝向平移,不改朝向——转着找角度是用户的事,这里只负责"离远一点"
Vector3 dir = Quaternion.Euler(eulerAngles) * Vector3.forward;
position = bounds.center - dir * Mathf.Max(distance, radius + 0.0001f);
}
@@ -112,12 +147,15 @@ namespace XericUI.ShaderBake
fieldOfView = Mathf.Clamp(Mathf.Abs(fieldOfView), 1f, 179f);
nearClipPlane = Mathf.Max(Mathf.Abs(nearClipPlane), 0.0001f);
farClipPlane = Mathf.Max(Mathf.Abs(farClipPlane), nearClipPlane + 0.0001f);
frameHalfSize = new Vector2(
Mathf.Max(Mathf.Abs(frameHalfSize.x), 0.0001f),
Mathf.Max(Mathf.Abs(frameHalfSize.y), 0.0001f));
}
/// <summary> 摄像机指纹:变了不需要重建网格,只需要重画 </summary>
public string GetCameraSignature()
{
return orthographic + "_" + orthographicSize + "_" + fieldOfView + "_" +
return orthographic + "_" + orthographicSize + "_" + frameHalfSize + "_" + fieldOfView + "_" +
nearClipPlane + "_" + farClipPlane + "_" + position + "_" + eulerAngles;
}
}
+82 -13
View File
@@ -154,7 +154,78 @@ namespace XericUI.ShaderBake
EnsureGeometry();
if (m_Geometry == null)
return;
settings.camera.FrameBounds(geometryBounds, settings.aspect);
// 余量给 1:烘出来的图和场景里"把材质贴在面片上正对着看"是一个范围,不留白边
settings.camera.FrameBounds(geometryBounds, settings.aspect, 1f);
settings.camera.fitRevision = ShaderBakeCameraSettings.kCurrentFitRevision;
}
/// <summary>
/// 把相机挪到刚好框住<b>某一格</b>。
///
/// <para><b>为什么需要单独按格框</b>:<see cref="FitCameraToGeometry"/> 框的是整个网格,
/// 多格时每格只占画面宽高的 1/列数、1/行数,还额外带 15% 余量。于是"场景里一张面片贴满物体"
/// 的效果在这里变成画面正中的一小块,看上去像 shader 只画了九宫格的中间那格。
/// 想要和场景一致的比例,就按这一格来框(余量取 1,不留边)。</para>
/// </summary>
public bool FitCameraOnFace(int faceIndex)
{
Bounds bounds;
if (!TryGetFaceWorldBounds(faceIndex, out bounds))
return false;
// padding 给 1:这一格就是要铺满整张输出图,留边就等于把内容缩小
settings.camera.FrameBounds(bounds, settings.aspect, 1f);
settings.camera.fitRevision = ShaderBakeCameraSettings.kCurrentFitRevision;
return true;
}
/// <summary> 某一格在世界空间的包围盒(外部网格不参与,返回 false) </summary>
public bool TryGetFaceWorldBounds(int faceIndex, out Bounds bounds)
{
bounds = default(Bounds);
ShaderBakeSurfaceSettings surface = settings.surface;
Vector2 rectMin, rectMax;
if (!TryGetFaceLocalRect(surface, faceIndex, out rectMin, out rectMax))
return false;
Matrix4x4 local = surface.localMatrix;
Vector3 min = new Vector3(rectMin.x, rectMin.y, 0f);
Vector3 max = new Vector3(rectMax.x, rectMax.y, 0f);
Vector3 worldMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
Vector3 worldMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
for (int i = 0; i < 8; i++)
{
Vector3 corner = new Vector3((i & 1) == 0 ? min.x : max.x,
(i & 2) == 0 ? min.y : max.y,
(i & 4) == 0 ? min.z : max.z);
Vector3 world = local.MultiplyPoint3x4(corner);
worldMin = Vector3.Min(worldMin, world);
worldMax = Vector3.Max(worldMax, world);
}
bounds = new Bounds((worldMin + worldMax) * 0.5f, worldMax - worldMin);
return true;
}
/// <summary> 某一格在面片局部空间里的矩形。网格整体以原点为中心,第 0 行在最下 </summary>
private static bool TryGetFaceLocalRect(ShaderBakeSurfaceSettings surface, int faceIndex,
out Vector2 rectMin, out Vector2 rectMax)
{
rectMin = Vector2.zero;
rectMax = Vector2.zero;
if (surface.kind == ShaderBakeSurfaceKind.Mesh)
return false;
int columns = Mathf.Max(surface.gridColumns, 1);
int rows = Mathf.Max(surface.gridRows, 1);
if (faceIndex < 0 || faceIndex >= columns * rows)
return false;
int column = faceIndex % columns;
int row = faceIndex / columns;
float x0 = -surface.size.x * columns * 0.5f + column * surface.size.x;
float y0 = -surface.size.y * rows * 0.5f + row * surface.size.y;
rectMin = new Vector2(x0, y0);
rectMax = new Vector2(x0 + surface.size.x, y0 + surface.size.y);
return true;
}
/// <summary>
@@ -214,26 +285,24 @@ namespace XericUI.ShaderBake
return false;
ShaderBakeSurfaceSettings surface = settings.surface;
int columns = Mathf.Max(surface.gridColumns, 1);
int rows = Mathf.Max(surface.gridRows, 1);
if (faceIndex < 0 || faceIndex >= columns * rows)
Vector2 rectMin, rectMax;
if (!TryGetFaceLocalRect(surface, faceIndex, out rectMin, out rectMax))
return false;
float faceWidth = surface.size.x;
float faceHeight = surface.size.y;
int column = faceIndex % columns;
int row = faceIndex / columns;
float x0 = -faceWidth * columns * 0.5f + column * faceWidth;
float y0 = -faceHeight * rows * 0.5f + row * faceHeight;
float x1 = x0 + faceWidth;
float y1 = y0 + faceHeight;
float x0 = rectMin.x;
float y0 = rectMin.y;
float x1 = rectMax.x;
float y1 = rectMax.y;
Matrix4x4 local = surface.localMatrix;
bool first = true;
for (int i = 0; i < 4; i++)
{
Vector3 corner = new Vector3((i & 1) == 0 ? x0 : x1, (i & 2) == 0 ? y0 : y1, 0f);
Vector4 clip = viewProjection * local.MultiplyPoint3x4(corner);
Vector3 world = local.MultiplyPoint3x4(corner);
// 必须显式给 w=1:Matrix4x4 * Vector3 会走隐式 Vector3→Vector4(w=0),
// 于是 clip.w 恒为 0,下面的可见性判断会把每一格都判成"不在画面里"
Vector4 clip = viewProjection * new Vector4(world.x, world.y, world.z, 1f);
if (clip.w <= 0.0001f)
return false;
float u = (clip.x / clip.w + 1f) * 0.5f;