Files
XericUIActionVessel/Runtime/ShaderBake/ShaderBakeCameraSettings.cs
T
lrss3 7c0f35fdcb fix(ShaderBake): 修正离屏视图矩阵与侧栏数值不可见
- 投影矩阵是 OpenGL 约定(相机朝 -Z),而 TRS.inverse 给的是 Unity 的 +Z 朝前,
  两者拼起来几何全被裁剪,表现是"渲染成功但画面全透明";视图矩阵补上 Z 镜像
- 侧栏半宽/三分之一宽的字段按内容定基宽,标题又占 ~120px,输入框被挤出可视区;
  改成 flex-basis:0 + 收窄标题 + 输入区 min-width:0,并缩短过长标签
- 数值绑定改用 INotifyValueChanged<T> 按名字绑定,不再依赖跨版本会换命名空间的具体字段类型
2026-11-05 19:17:57 +08:00

112 lines
4.4 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>
/// 离屏"摄像机"参数。这里没有真的 Camera——投影与视口矩阵是自己算出来塞进 CommandBuffer 的,
/// 好处是不往场景里塞任何临时物体,坏处是 API 相关的 Y 翻转要自己管(见 output.flipY)。
/// </summary>
[Serializable]
public class ShaderBakeCameraSettings
{
[Tooltip("正交(默认):面片不会因为透视而变形,适合贴图类 shader")]
public bool orthographic = true;
[Tooltip("正交半高(世界单位):实际能看到的竖向范围是它的两倍")]
public float orthographicSize = 0.6f;
[Tooltip("透视相机的竖向视野角(度),正交时忽略")]
public float fieldOfView = 60f;
[Tooltip("近裁剪面")]
public float nearClipPlane = 0.01f;
[Tooltip("远裁剪面")]
public float farClipPlane = 100f;
[Tooltip("相机位置:正交时只有朝向影响画面,位置决定远近")]
public Vector3 position = new Vector3(0f, 0f, -10f);
[Tooltip("相机旋转(欧拉角,度):默认朝 +Z 看,即看着放在原点、面朝 -Z 的面片")]
public Vector3 eulerAngles;
/// <summary>
/// 视口(世界 → 相机)矩阵。
///
/// <para><b>为什么要镜像 Z</b>:<see cref="Matrix4x4.Perspective"/> 与 <see cref="Matrix4x4.Ortho"/>
/// 沿用 OpenGL 约定——相机朝 <b>-Z</b> 看;而 <c>TRS(...).inverse</c> 给出的是 Unity 自己的
/// "朝 +Z 看"。两者直接拼起来,正前方的物体会得到负的裁剪 w(透视)或落在近远裁剪面之外(正交),
/// 整块几何被裁光,表现就是"渲染成功但画面全透明"。</para>
/// </summary>
public Matrix4x4 viewMatrix
{
get
{
Matrix4x4 worldToLocal = Matrix4x4.TRS(position, Quaternion.Euler(eulerAngles), Vector3.one).inverse;
return Matrix4x4.Scale(new Vector3(1f, 1f, -1f)) * worldToLocal;
}
}
/// <summary>
/// 投影矩阵。正交时宽高按 aspect 分配(半宽 = 半高 × aspect),与 Unity 相机一致。
/// </summary>
public Matrix4x4 GetProjectionMatrix(float aspect)
{
if (aspect <= 0f) aspect = 1f;
if (orthographic)
{
float halfHeight = Mathf.Max(orthographicSize, 0.0001f);
float halfWidth = halfHeight * aspect;
return Matrix4x4.Ortho(-halfWidth, halfWidth, -halfHeight, halfHeight,
Mathf.Max(nearClipPlane, 0.0001f), Mathf.Max(farClipPlane, nearClipPlane + 0.0001f));
}
return Matrix4x4.Perspective(Mathf.Clamp(fieldOfView, 1f, 179f), aspect,
Mathf.Max(nearClipPlane, 0.0001f), Mathf.Max(farClipPlane, nearClipPlane + 0.0001f));
}
/// <summary>
/// 把包围盒框进画面:正交改半高,透视改距离;同时把远近裁剪面推到包围盒外侧,
/// 免得贴脸的大面片被近裁剪面削掉一块。
/// </summary>
public void FrameBounds(Bounds bounds, float aspect, float padding = 1.15f)
{
Vector3 extents = bounds.extents;
float radius = extents.magnitude;
if (radius < 0.0001f) radius = 0.0001f;
if (aspect <= 0f) aspect = 1f;
if (orthographic)
{
// 横向不够宽时以横向为准:半高 = 半宽 / aspect
float halfFromHeight = extents.y;
float halfFromWidth = extents.x / aspect;
orthographicSize = Mathf.Max(halfFromHeight, halfFromWidth) * Mathf.Max(padding, 1f);
}
else
{
float halfFov = Mathf.Clamp(fieldOfView, 1f, 179f) * 0.5f * Mathf.Deg2Rad;
float verticalFit = extents.y;
float horizontalFit = extents.x / aspect;
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);
}
float distanceFromCamera = Vector3.Distance(position, bounds.center);
nearClipPlane = Mathf.Max(0.0001f, distanceFromCamera - radius * 3f);
farClipPlane = distanceFromCamera + radius * 3f;
}
/// <summary> 摄像机指纹:变了不需要重建网格,只需要重画 </summary>
public string GetCameraSignature()
{
return orthographic + "_" + orthographicSize + "_" + fieldOfView + "_" +
nearClipPlane + "_" + farClipPlane + "_" + position + "_" + eulerAngles;
}
}
}