fix(ShaderBake): 投影按内容宽高比铺满输出,不再跟着输出比例留白
正交投影原来按输出宽高比推半宽(半宽 = 半高 × 输出比例), 所以 1024×4096 的输出里一张 1×1 的面片只占中间一条,UV 也到不了 0~1。 现在框取时把内容的半宽/半高都记下来,投影按内容比例取半宽: 输出比例和面片比例不同时就靠拉伸,一格正好等于一整张图; m×n 的分格因此天然是每格 1/m × 1/n。面片宽高、细分、来源变了会顺手重框。 旧存档 fitRevision 1 → 2,开窗按新约定重框一次。 UV 测试视图去掉每格色相混色:0 点必须是纯黑,否则分辨不出原点。
This commit is contained in:
@@ -844,6 +844,8 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
kind.RegisterValueChangedCallback(evt =>
|
kind.RegisterValueChangedCallback(evt =>
|
||||||
{
|
{
|
||||||
m_Renderer.settings.surface.kind = (ShaderBakeSurfaceKind)evt.newValue;
|
m_Renderer.settings.surface.kind = (ShaderBakeSurfaceKind)evt.newValue;
|
||||||
|
// 换了来源,内容的宽高比也跟着换,投影得按新的重算
|
||||||
|
m_Renderer.FitCameraToGeometry();
|
||||||
Commit();
|
Commit();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -896,11 +898,13 @@ namespace XericUI.ShaderBake.Editor
|
|||||||
importModel.clicked += ImportModel;
|
importModel.clicked += ImportModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 面片那些"改一个分量"的写法摊成方法,绑定处只留一行,读起来不至于全是三元组
|
// 面片那些"改一个分量"的写法摊成方法,绑定处只留一行,读起来不至于全是三元组。
|
||||||
private void SetSizeX(float v) { Vector2 s = m_Renderer.settings.surface.size; s.x = v; m_Renderer.settings.surface.size = s; }
|
// 形状一变,投影里记的宽高比就过期了,画面会被裁一半或留一圈边;
|
||||||
private void SetSizeY(float v) { Vector2 s = m_Renderer.settings.surface.size; s.y = v; m_Renderer.settings.surface.size = s; }
|
// 这几项改完顺手重框一次,保证内容始终铺满输出
|
||||||
private void SetSegmentX(int v) { Vector2Int s = m_Renderer.settings.surface.segments; s.x = v; m_Renderer.settings.surface.segments = s; }
|
private void SetSizeX(float v) { Vector2 s = m_Renderer.settings.surface.size; s.x = v; m_Renderer.settings.surface.size = s; m_Renderer.FitCameraToGeometry(); }
|
||||||
private void SetSegmentY(int v) { Vector2Int s = m_Renderer.settings.surface.segments; s.y = v; m_Renderer.settings.surface.segments = s; }
|
private void SetSizeY(float v) { Vector2 s = m_Renderer.settings.surface.size; s.y = v; m_Renderer.settings.surface.size = s; m_Renderer.FitCameraToGeometry(); }
|
||||||
|
private void SetSegmentX(int v) { Vector2Int s = m_Renderer.settings.surface.segments; s.x = v; m_Renderer.settings.surface.segments = s; m_Renderer.FitCameraToGeometry(); }
|
||||||
|
private void SetSegmentY(int v) { Vector2Int s = m_Renderer.settings.surface.segments; s.y = v; m_Renderer.settings.surface.segments = s; m_Renderer.FitCameraToGeometry(); }
|
||||||
private void SetUvOffsetX(float v) { Vector2 u = m_Renderer.settings.surface.uvOffset; u.x = v; m_Renderer.settings.surface.uvOffset = u; }
|
private void SetUvOffsetX(float v) { Vector2 u = m_Renderer.settings.surface.uvOffset; u.x = v; m_Renderer.settings.surface.uvOffset = u; }
|
||||||
private void SetUvOffsetY(float v) { Vector2 u = m_Renderer.settings.surface.uvOffset; u.y = v; m_Renderer.settings.surface.uvOffset = u; }
|
private void SetUvOffsetY(float v) { Vector2 u = m_Renderer.settings.surface.uvOffset; u.y = v; m_Renderer.settings.surface.uvOffset = u; }
|
||||||
private void SetUvTileX(float v) { Vector2 u = m_Renderer.settings.surface.uvTiling; u.x = v; m_Renderer.settings.surface.uvTiling = u; }
|
private void SetUvTileX(float v) { Vector2 u = m_Renderer.settings.surface.uvTiling; u.x = v; m_Renderer.settings.surface.uvTiling = u; }
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ Shader "Hidden/XericUI/ShaderBake/Debug View"
|
|||||||
{
|
{
|
||||||
_Mode ("视图(0=UV 1=梯度 2=棋盘)", Float) = 0
|
_Mode ("视图(0=UV 1=梯度 2=棋盘)", Float) = 0
|
||||||
_LineThickness ("整数边界线宽", Range(1, 400)) = 90
|
_LineThickness ("整数边界线宽", Range(1, 400)) = 90
|
||||||
_CellMix ("每格色相强度", Range(0, 1)) = 0.22
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SubShader
|
SubShader
|
||||||
@@ -28,7 +27,6 @@ Shader "Hidden/XericUI/ShaderBake/Debug View"
|
|||||||
|
|
||||||
float _Mode;
|
float _Mode;
|
||||||
float _LineThickness;
|
float _LineThickness;
|
||||||
float _CellMix;
|
|
||||||
|
|
||||||
struct appdata
|
struct appdata
|
||||||
{
|
{
|
||||||
@@ -72,11 +70,11 @@ Shader "Hidden/XericUI/ShaderBake/Debug View"
|
|||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
// UV 视图:红 = U,绿 = V。角点即 UV,一格四角该是 黑(0,0) 红(1,0) 绿(0,1) 黄(1,1)
|
// UV 视图:红 = U,绿 = V。角点即 UV,一格四角该是 黑(0,0) 红(1,0) 绿(0,1) 黄(1,1)。
|
||||||
fixed4 UvView(float2 cell, float2 f)
|
// (0,0) 必须是纯黑——掺了每格色相就分不出哪一头是原点
|
||||||
|
fixed4 UvView(float2 f)
|
||||||
{
|
{
|
||||||
fixed3 col = fixed3(f.x, f.y, 0.06);
|
fixed3 col = fixed3(f.x, f.y, 0.0);
|
||||||
col = lerp(col, CellHue(cell), _CellMix);
|
|
||||||
return fixed4(col * EdgeMask(f), 1.0);
|
return fixed4(col * EdgeMask(f), 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +113,7 @@ Shader "Hidden/XericUI/ShaderBake/Debug View"
|
|||||||
float2 f = i.uv - cell;
|
float2 f = i.uv - cell;
|
||||||
|
|
||||||
if (_Mode < 0.5)
|
if (_Mode < 0.5)
|
||||||
return UvView(cell, f);
|
return UvView(f);
|
||||||
if (_Mode < 1.5)
|
if (_Mode < 1.5)
|
||||||
return GradientView(f);
|
return GradientView(f);
|
||||||
return CheckerView(cell, f);
|
return CheckerView(cell, f);
|
||||||
|
|||||||
@@ -40,7 +40,16 @@ namespace XericUI.ShaderBake
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int fitRevision;
|
public int fitRevision;
|
||||||
|
|
||||||
public const int kCurrentFitRevision = 1;
|
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>
|
/// <summary>
|
||||||
/// 视口(世界 → 相机)矩阵。
|
/// 视口(世界 → 相机)矩阵。
|
||||||
@@ -60,7 +69,8 @@ namespace XericUI.ShaderBake
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 投影矩阵。正交时宽高按 aspect 分配(半宽 = 半高 × aspect),与 Unity 相机一致。
|
/// 投影矩阵。正交的半宽按<b>内容</b>的宽高比取(不是输出宽高比),
|
||||||
|
/// 所以输出比例和面片比例不同时,面片会被拉伸到正好铺满整张图。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Matrix4x4 GetProjectionMatrix(float aspect)
|
public Matrix4x4 GetProjectionMatrix(float aspect)
|
||||||
{
|
{
|
||||||
@@ -68,7 +78,10 @@ namespace XericUI.ShaderBake
|
|||||||
if (orthographic)
|
if (orthographic)
|
||||||
{
|
{
|
||||||
float halfHeight = Mathf.Max(orthographicSize, 0.0001f);
|
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,
|
return Matrix4x4.Ortho(-halfWidth, halfWidth, -halfHeight, halfHeight,
|
||||||
Mathf.Max(nearClipPlane, 0.0001f), Mathf.Max(farClipPlane, nearClipPlane + 0.0001f));
|
Mathf.Max(nearClipPlane, 0.0001f), Mathf.Max(farClipPlane, nearClipPlane + 0.0001f));
|
||||||
}
|
}
|
||||||
@@ -92,10 +105,12 @@ namespace XericUI.ShaderBake
|
|||||||
|
|
||||||
if (orthographic)
|
if (orthographic)
|
||||||
{
|
{
|
||||||
// 横向不够宽时以横向为准:半高 = 半宽 / aspect
|
// 宽高各记各的:投影按内容比例取半宽,输出比例再不一样也只会拉伸,不会留白
|
||||||
float halfFromHeight = extents.y;
|
float factor = Mathf.Max(padding, 1f);
|
||||||
float halfFromWidth = extents.x / aspect;
|
frameHalfSize = new Vector2(
|
||||||
orthographicSize = Mathf.Max(halfFromHeight, halfFromWidth) * Mathf.Max(padding, 1f);
|
Mathf.Max(extents.x, 0.0001f) * factor,
|
||||||
|
Mathf.Max(extents.y, 0.0001f) * factor);
|
||||||
|
orthographicSize = frameHalfSize.y;
|
||||||
|
|
||||||
// 正交也得把目标挪到视线轴上:只改半高是"缩放到贴着边",
|
// 正交也得把目标挪到视线轴上:只改半高是"缩放到贴着边",
|
||||||
// 目标偏在一边时照样一半在画面外——按格框取时最明显,因为那一格本来就不在网格正中。
|
// 目标偏在一边时照样一半在画面外——按格框取时最明显,因为那一格本来就不在网格正中。
|
||||||
@@ -132,12 +147,15 @@ namespace XericUI.ShaderBake
|
|||||||
fieldOfView = Mathf.Clamp(Mathf.Abs(fieldOfView), 1f, 179f);
|
fieldOfView = Mathf.Clamp(Mathf.Abs(fieldOfView), 1f, 179f);
|
||||||
nearClipPlane = Mathf.Max(Mathf.Abs(nearClipPlane), 0.0001f);
|
nearClipPlane = Mathf.Max(Mathf.Abs(nearClipPlane), 0.0001f);
|
||||||
farClipPlane = Mathf.Max(Mathf.Abs(farClipPlane), 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>
|
/// <summary> 摄像机指纹:变了不需要重建网格,只需要重画 </summary>
|
||||||
public string GetCameraSignature()
|
public string GetCameraSignature()
|
||||||
{
|
{
|
||||||
return orthographic + "_" + orthographicSize + "_" + fieldOfView + "_" +
|
return orthographic + "_" + orthographicSize + "_" + frameHalfSize + "_" + fieldOfView + "_" +
|
||||||
nearClipPlane + "_" + farClipPlane + "_" + position + "_" + eulerAngles;
|
nearClipPlane + "_" + farClipPlane + "_" + position + "_" + eulerAngles;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user