Files
XericUIActionVessel/Runtime/ShaderBake/ShaderBakeCameraSettings.cs
T
lrss3 63173c6ec2 feat(ShaderBake): runtime 侧把 shader 渲成 RT/Texture2D/Sprite/文件字节
离屏渲染走 CommandBuffer + 自建视图投影矩阵,不往场景塞相机和 GameObject,
代价是 API 相关的 Y 翻转要露成一个开关(output.flipY);灯光/雾/阴影不参与,
所以光照类 shader 会渲成无灯效果,这一点写在注释里而不是假装支持。

外部网格一律复制到新网格再改 UV:直接改 MeshFilter.sharedMesh 等于改了项目资产。
2026-11-05 15:59:02 +08:00

101 lines
3.9 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> 视口(世界 → 相机)矩阵 </summary>
public Matrix4x4 viewMatrix
{
get { return Matrix4x4.TRS(position, Quaternion.Euler(eulerAngles), Vector3.one).inverse; }
}
/// <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;
}
}
}