113 lines
3.7 KiB
C#
113 lines
3.7 KiB
C#
using BrewMonster;
|
|
using Unity.Cinemachine;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
namespace BrewMonster.Scripts
|
|
{
|
|
public class InGameGraphicOption : MonoSingleton<InGameGraphicOption>
|
|
{
|
|
[SerializeField] private UniversalRenderPipelineAsset _renderPipelineAsset;
|
|
[SerializeField] private CinemachineCamera _cinemachineVirtualCamera;
|
|
|
|
private static bool _warnedMissingUpdAsset;
|
|
private static bool _warnedMissingCinemachineCam;
|
|
|
|
private void TryEnsureRenderPipelineAsset()
|
|
{
|
|
if (_renderPipelineAsset != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// MonoSingleton creates an empty host when absent from the scene — serialized refs stay null (e.g. animation test scenes).
|
|
// Fall back to the project Quality / Graphics active URP asset. / 未放场景中时序列化字段为空,回退到当前 URP 资源
|
|
_renderPipelineAsset =
|
|
GraphicsSettings.defaultRenderPipeline as UniversalRenderPipelineAsset;
|
|
|
|
if (_renderPipelineAsset == null && !_warnedMissingUpdAsset)
|
|
{
|
|
_warnedMissingUpdAsset = true;
|
|
BMLogger.LogWarning(
|
|
"[InGameGraphicOption] UniversalRenderPipelineAsset missing (Inspector + Graphics Settings). SetRenderScale/SetMSAA are no-ops until URP is set.");
|
|
}
|
|
}
|
|
|
|
private CinemachineCamera TryResolveCinemachineCamera()
|
|
{
|
|
if (_cinemachineVirtualCamera != null)
|
|
{
|
|
return _cinemachineVirtualCamera;
|
|
}
|
|
|
|
_cinemachineVirtualCamera = FindFirstObjectByType<CinemachineCamera>();
|
|
return _cinemachineVirtualCamera;
|
|
}
|
|
|
|
#region public functions
|
|
|
|
/// <summary>How far the objects are rendered in the scene.</summary>
|
|
/// <param name="distance"></param>
|
|
public void SetRenderDistance(float distance)
|
|
{
|
|
var cam = TryResolveCinemachineCamera();
|
|
if (cam == null)
|
|
{
|
|
if (!_warnedMissingCinemachineCam)
|
|
{
|
|
_warnedMissingCinemachineCam = true;
|
|
BMLogger.LogWarning(
|
|
"[InGameGraphicOption] No CinemachineCamera — SetRenderDistance skipped (assign _cinemachineVirtualCamera or add a vcam in scene).");
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
distance = Mathf.Clamp(distance, 50f, 500f);
|
|
cam.Lens.FarClipPlane = distance;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The scale of the render pipeline. <br/>
|
|
/// The value should be between 0.6 and 1.5.
|
|
/// </summary>
|
|
public void SetRenderScale(float scale)
|
|
{
|
|
TryEnsureRenderPipelineAsset();
|
|
if (_renderPipelineAsset == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
scale = Mathf.Clamp(scale, 0.6f, 1.5f);
|
|
_renderPipelineAsset.renderScale = scale;
|
|
}
|
|
|
|
|
|
public void SetMSAA(int msaaLevel)
|
|
{
|
|
TryEnsureRenderPipelineAsset();
|
|
if (_renderPipelineAsset == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Valid: 0,2,4
|
|
if (msaaLevel != 0 && msaaLevel != 2 && msaaLevel != 4)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_renderPipelineAsset.msaaSampleCount = msaaLevel;
|
|
}
|
|
|
|
|
|
//TODO: figure out a solution for this.
|
|
public void SetActiveShadow(bool active)
|
|
{
|
|
|
|
}
|
|
#endregion
|
|
}
|
|
} |