feat(ShaderBake): 预览图上画分格线与每格 UV 编号

加行/加列之后只看图分不清哪一片对应哪段 UV,只能靠点选一格再看右侧。
现在多格时图上会贴一层白色半透明分格线,每格左上角写 U?/V?,
白字垫在半透明黑底上,浅色结果图上也能读。

线和编号都按百分比定位,缩放与滚动不重算;线只随行列数重建、编号按格数复用,
实时预览每秒重画几十次也不会反复增删节点。只有一格时不画——这时右侧已经写着 UV。
编号所在格在屏幕上不足一个编号宽时自动不显示,避免 16×16 那种网格糊成一片。
This commit is contained in:
2026-11-06 18:39:12 +08:00
parent a0297c2652
commit 92aed8e64b
4 changed files with 213 additions and 7 deletions
+36 -5
View File
@@ -95,6 +95,8 @@ namespace XericUI.ShaderBake.Editor
/// <summary> 测试视图那张材质:_Mode 决定画哪种图,建一次反复用 </summary>
private Material m_DebugMaterial;
private int m_SelectedFace;
/// <summary> 给预览面板的分格数据:每帧重填,避免按格新建列表 </summary>
private readonly List<ShaderBakePreviewPane.FaceCellRect> m_GridCells = new List<ShaderBakePreviewPane.FaceCellRect>();
/// <summary> 预览换成"量几何与 UV"的测试图。都不吃贴图、不吃光照,画不出来的原因只剩一个。
/// Uv/Gradient/Checker 的取值与 shader 的 _Mode 一一对应 </summary>
@@ -763,7 +765,7 @@ namespace XericUI.ShaderBake.Editor
if (y != null) y.SetEnabled(enabled);
}
/// <summary> 把选中格的屏幕框和 UV 编号贴到浏览图上 </summary>
/// <summary> 把选中格的屏幕框、分格线和每格的 UV 编号贴到浏览图上 </summary>
private void UpdateFaceOverlay()
{
if (m_Preview == null || m_Renderer == null)
@@ -772,17 +774,46 @@ namespace XericUI.ShaderBake.Editor
if (surface.kind == ShaderBakeSurfaceKind.Mesh && surface.sourceMesh != null)
{
m_Preview.SetFaceSelection(false, Vector2.zero, Vector2.zero, string.Empty);
m_Preview.SetFaceGrid(null, 0, 0);
return;
}
Vector2 min, max;
if (!m_Renderer.TryGetFaceViewportBounds(m_SelectedFace, out min, out max))
int columns = Mathf.Max(surface.gridColumns, 1);
int rows = Mathf.Max(surface.gridRows, 1);
m_GridCells.Clear();
for (int i = 0; i < columns * rows; i++)
{
Vector2 min, max;
ShaderBakeFaceCell cell = surface.GetFace(i);
if (!m_Renderer.TryGetFaceViewportBounds(i, out min, out max))
continue;
// 完全在画面外的那几格(按「选中格铺满」之后就只剩一格可见)不参与,
// 否则它们的包围盒会被夹成零面积,编号全挤在画面边上
if (max.x - min.x < 0.001f || max.y - min.y < 0.001f)
continue;
m_GridCells.Add(new ShaderBakePreviewPane.FaceCellRect
{
min = min,
max = max,
label = "U" + cell.uvX + " V" + cell.uvY
});
}
m_Preview.SetFaceGrid(m_GridCells, columns, rows);
Vector2 selMin, selMax;
if (!m_Renderer.TryGetFaceViewportBounds(m_SelectedFace, out selMin, out selMax))
{
m_Preview.SetFaceSelection(false, Vector2.zero, Vector2.zero, string.Empty);
return;
}
ShaderBakeFaceCell cell = surface.GetFace(m_SelectedFace);
m_Preview.SetFaceSelection(true, min, max, "UV (" + cell.uvX + ", " + cell.uvY + ")");
// 分了格就由每格的编号说明 UV,选中框只留框,不然同一格里两行字叠着
m_Preview.SetFaceSelection(true, selMin, selMax, columns * rows > 1 ? string.Empty : SelectedFaceLabel());
}
private string SelectedFaceLabel()
{
ShaderBakeFaceCell cell = SelectedFaceCell();
return cell == null ? string.Empty : "UV (" + cell.uvX + ", " + cell.uvY + ")";
}
// ── 面片 ────────────────────────────────────────────────
+29 -2
View File
@@ -96,12 +96,39 @@
opacity: 0.9;
}
/* 分格线浮在图上:1px 白线 + 每格一个 UV 编号,都用百分比定位,缩放/滚动不用重排 */
.grid-overlay {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display: none;
/* 贴边的编号宁可被裁掉,也不该糊到图上之外 */
overflow: hidden;
}
.grid-line {
position: absolute;
background-color: rgba(255, 255, 255, 0.38);
}
.grid-cell-label {
position: absolute;
font-size: 10px;
/* 白字压在半透明黑底上:浅色贴图上白字会糊掉,垫一层才读得出来 */
color: rgba(255, 255, 255, 0.88);
background-color: rgba(0, 0, 0, 0.38);
padding: 0 4px;
border-radius: 2px;
-unity-text-align: middle-left;
}
.preview-readout {
position: absolute;
left: 8px;
bottom: 8px;
font-size: 11px;
color: rgb(220, 220, 220);
font-size: 11px; color: rgb(220, 220, 220);
background-color: rgba(0, 0, 0, 0.45);
padding: 1px 6px;
border-radius: 3px;
+2
View File
@@ -39,6 +39,8 @@
<ui:VisualElement name="preview-frame" class="preview-frame">
<ui:Image name="preview-checker" class="preview-checker" />
<ui:Image name="preview-image" class="preview-image" />
<!-- 分格线与每格的 UV 编号:位置按百分比算,缩放时不用重排 -->
<ui:VisualElement name="grid-overlay" class="grid-overlay" />
<!-- 选中面片的高亮:位置按百分比算,缩放时不用重排 -->
<ui:VisualElement name="face-overlay" class="face-overlay">
<ui:Label name="face-overlay-label" class="face-overlay-label" text="UV (0,0)" />
+146
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
@@ -22,6 +23,14 @@ namespace XericUI.ShaderBake.Editor
private readonly Label m_Readout;
private readonly VisualElement m_FaceOverlay;
private readonly Label m_FaceOverlayLabel;
private readonly VisualElement m_GridOverlay;
// 分格层:线的条数只随行列数变,编号只随格子数变,所以按数量复用,不每帧重建
private readonly List<VisualElement> m_GridVerticals = new List<VisualElement>();
private readonly List<VisualElement> m_GridHorizontals = new List<VisualElement>();
private readonly List<Label> m_GridLabels = new List<Label>();
private int m_GridColumns = -1;
private int m_GridRows = -1;
/// <summary> 在图上点了一下(不是拖拽)。参数是 0~1、左上为原点的坐标 </summary>
public event Action<Vector2> faceClicked;
@@ -60,6 +69,9 @@ namespace XericUI.ShaderBake.Editor
m_FaceOverlayLabel = root.Q<Label>("face-overlay-label");
if (m_FaceOverlay != null)
m_FaceOverlay.pickingMode = PickingMode.Ignore;
m_GridOverlay = root.Q<VisualElement>("grid-overlay");
if (m_GridOverlay != null)
m_GridOverlay.pickingMode = PickingMode.Ignore;
if (m_Viewport == null || m_Frame == null || m_Image == null)
throw new InvalidOperationException("BakeShaderWindow.uxml 缺少预览区节点");
@@ -138,7 +150,141 @@ namespace XericUI.ShaderBake.Editor
m_FaceOverlay.style.width = Length.Percent(Mathf.Max(max.x - min.x, 0.002f) * 100f);
m_FaceOverlay.style.height = Length.Percent(Mathf.Max(max.y - min.y, 0.002f) * 100f);
if (m_FaceOverlayLabel != null)
{
// 分了格的时候每格都有自己的编号标签,选中框只留框,免得同一格里两行字叠着
m_FaceOverlayLabel.style.display = string.IsNullOrEmpty(label) ? DisplayStyle.None : DisplayStyle.Flex;
m_FaceOverlayLabel.text = label;
}
}
/// <summary> 一格在预览图里的位置(0~1,左上为原点)和它该显示的 UV 编号 </summary>
public struct FaceCellRect
{
public Vector2 min;
public Vector2 max;
public string label;
}
/// <summary>
/// 画分格线和每格的 UV 编号,回答"这条线右边/下边那一片是 UV 几"。
///
/// <para>只有一格时不画:这时右侧面片区已经写着 UV,再叠一层只会挡画面。
/// 位置全用百分比,所以缩放、拖动滚动条都不用重算。</para>
/// </summary>
public void SetFaceGrid(IList<FaceCellRect> cells, int columns, int rows)
{
if (m_GridOverlay == null)
return;
if (cells == null || cells.Count < 2)
{
m_GridOverlay.style.display = DisplayStyle.None;
return;
}
m_GridOverlay.style.display = DisplayStyle.Flex;
EnsureGridLines(columns, rows);
float left = float.MaxValue, top = float.MaxValue;
float right = float.MinValue, bottom = float.MinValue;
for (int i = 0; i < cells.Count; i++)
{
left = Mathf.Min(left, cells[i].min.x);
top = Mathf.Min(top, cells[i].min.y);
right = Mathf.Max(right, cells[i].max.x);
bottom = Mathf.Max(bottom, cells[i].max.y);
}
left = Mathf.Clamp01(left);
top = Mathf.Clamp01(top);
right = Mathf.Clamp01(right);
bottom = Mathf.Clamp01(bottom);
// 线按整个网格的范围等分。格是投影后的四边形,这里用的是它的视口包围盒,
// 正交正视时两者完全重合;斜看时线会略微跑到格边外,但仍然对得上编号
for (int i = 0; i < m_GridVerticals.Count; i++)
{
VisualElement line = m_GridVerticals[i];
line.style.left = Length.Percent(Mathf.Lerp(left, right, (float)i / Mathf.Max(columns, 1)) * 100f);
line.style.top = Length.Percent(top * 100f);
line.style.width = 1f;
line.style.height = Length.Percent((bottom - top) * 100f);
}
for (int j = 0; j < m_GridHorizontals.Count; j++)
{
VisualElement line = m_GridHorizontals[j];
line.style.left = Length.Percent(left * 100f);
line.style.top = Length.Percent(Mathf.Lerp(top, bottom, (float)j / Mathf.Max(rows, 1)) * 100f);
line.style.width = Length.Percent((right - left) * 100f);
line.style.height = 1f;
}
EnsureGridLabels(cells.Count);
// 一格在屏幕上还不到一个编号宽/高时,编号只会互相压着,索性不画。
// 布局还没算出来(宽度为 0)时不拦,等下一次重画再按真实尺寸决定
float frameWidth = m_Frame != null ? m_Frame.resolvedStyle.width : 0f;
float frameHeight = m_Frame != null ? m_Frame.resolvedStyle.height : 0f;
bool canMeasure = frameWidth > 1f && frameHeight > 1f;
for (int i = 0; i < m_GridLabels.Count; i++)
{
Label label = m_GridLabels[i];
bool used = i < cells.Count;
if (used && canMeasure)
{
float cellWidth = (cells[i].max.x - cells[i].min.x) * frameWidth;
float cellHeight = (cells[i].max.y - cells[i].min.y) * frameHeight;
used = cellWidth >= 36f && cellHeight >= 16f;
}
label.style.display = used ? DisplayStyle.Flex : DisplayStyle.None;
if (!used)
continue;
label.text = cells[i].label;
label.style.left = Length.Percent(Mathf.Clamp01(cells[i].min.x) * 100f);
label.style.top = Length.Percent(Mathf.Clamp01(cells[i].min.y) * 100f);
}
}
private void EnsureGridLines(int columns, int rows)
{
if (m_GridColumns == columns && m_GridRows == rows)
return;
m_GridColumns = columns;
m_GridRows = rows;
for (int i = 0; i < m_GridVerticals.Count; i++)
m_GridVerticals[i].RemoveFromHierarchy();
for (int i = 0; i < m_GridHorizontals.Count; i++)
m_GridHorizontals[i].RemoveFromHierarchy();
m_GridVerticals.Clear();
m_GridHorizontals.Clear();
// 列数 + 1 条竖线、行数 + 1 条横线:把外框也算进去,否则最外一圈没有边
for (int i = 0; i <= columns; i++)
m_GridVerticals.Add(CreateGridLine());
for (int j = 0; j <= rows; j++)
m_GridHorizontals.Add(CreateGridLine());
// 新加的线在最后,会把已有的编号压住;改完行列数把编号重新提到最上层
for (int i = 0; i < m_GridLabels.Count; i++)
m_GridLabels[i].BringToFront();
}
private VisualElement CreateGridLine()
{
var line = new VisualElement { pickingMode = PickingMode.Ignore };
line.AddToClassList("grid-line");
m_GridOverlay.Add(line);
return line;
}
private void EnsureGridLabels(int count)
{
while (m_GridLabels.Count < count)
{
var label = new Label { pickingMode = PickingMode.Ignore };
label.AddToClassList("grid-cell-label");
// 后加的在上面:编号要压在线之上,不能被 1px 的线切过
m_GridOverlay.Add(label);
m_GridLabels.Add(label);
}
}
/// <summary>