diff --git a/Editor/ShaderBake/ShaderBakeUVDebug.shader b/Editor/ShaderBake/ShaderBakeDebugView.shader
similarity index 100%
rename from Editor/ShaderBake/ShaderBakeUVDebug.shader
rename to Editor/ShaderBake/ShaderBakeDebugView.shader
diff --git a/Editor/ShaderBake/ShaderBakeUVDebug.shader.meta b/Editor/ShaderBake/ShaderBakeDebugView.shader.meta
similarity index 100%
rename from Editor/ShaderBake/ShaderBakeUVDebug.shader.meta
rename to Editor/ShaderBake/ShaderBakeDebugView.shader.meta
diff --git a/Runtime/ShaderBake/ShaderBakeRenderer.cs b/Runtime/ShaderBake/ShaderBakeRenderer.cs
index 1ed9c2b..38d6d4a 100644
--- a/Runtime/ShaderBake/ShaderBakeRenderer.cs
+++ b/Runtime/ShaderBake/ShaderBakeRenderer.cs
@@ -157,6 +157,74 @@ namespace XericUI.ShaderBake
settings.camera.FrameBounds(geometryBounds, settings.aspect);
}
+ ///
+ /// 把相机挪到刚好框住某一格。
+ ///
+ /// 为什么需要单独按格框: 框的是整个网格,
+ /// 多格时每格只占画面宽高的 1/列数、1/行数,还额外带 15% 余量。于是"场景里一张面片贴满物体"
+ /// 的效果在这里变成画面正中的一小块,看上去像 shader 只画了九宫格的中间那格。
+ /// 想要和场景一致的比例,就按这一格来框(余量取 1,不留边)。
+ ///
+ public bool FitCameraOnFace(int faceIndex)
+ {
+ Bounds bounds;
+ if (!TryGetFaceWorldBounds(faceIndex, out bounds))
+ return false;
+ // padding 给 1:这一格就是要铺满整张输出图,留边就等于把内容缩小
+ settings.camera.FrameBounds(bounds, settings.aspect, 1f);
+ return true;
+ }
+
+ /// 某一格在世界空间的包围盒(外部网格不参与,返回 false)
+ 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;
+ }
+
+ /// 某一格在面片局部空间里的矩形。网格整体以原点为中心,第 0 行在最下
+ 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;
+ }
+
///
/// 把浏览图上的一个点(0~1,左上为原点)打到面片网络上,返回命中的格子序号。
/// 用的是与烘焙完全相同的投影,所以"看到的哪一格"和"点到的哪一格"是同一套坐标。
@@ -214,19 +282,14 @@ 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;