Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: graphic settings camera list initialization #128

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Assets/AWSIM/Scripts/UI/BirdEyeView.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
Expand All @@ -6,6 +7,7 @@ namespace AWSIM.Scripts.UI
{
public class BirdEyeView : MonoBehaviour
{
public static event Action<GameObject> OnCameraInitialized;
[SerializeField] private float _birdEyeCameraInitialHeight = 200;

[SerializeField] private float _cameraOrthographicSizeMinimum = 3;
Expand Down Expand Up @@ -52,6 +54,9 @@ private void Start()
// Initialize the BEV camera
_birdEyeCamera = InitializeBirdEyeCamera();
_birdEyeCamera.enabled = false;

// Add to the list of cameras for GraphicsSettings
OnCameraInitialized?.Invoke(_birdEyeCamera.gameObject);
}

private void Update()
Expand Down
96 changes: 59 additions & 37 deletions Assets/AWSIM/Scripts/UI/GraphicsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class GraphicsSettings : MonoBehaviour

private int _gpuMemorySize;
private int _initialQualityLevel;
private int _currentQualityLevel;
private bool _isInitialised;

private readonly List<string> _dropdownOptions = new()
Expand All @@ -31,14 +32,16 @@ private void Awake()

private void Start()
{
// Populate list of cameras. Don't include sensor cameras
// add main camera to list (BEV cam is not initialized yet will be added later)
_cameraObjectsList = new List<GameObject>
{
Camera.main?.gameObject,
GameObject.FindGameObjectWithTag("BEVCamera")
};
_sunSource = GameObject.FindGameObjectWithTag("Sun").GetComponent<Light>();

// Subscribe to camera initialization event
BirdEyeView.OnCameraInitialized += UpdateGraphicSettingsForAddedCamera;

// Set initial quality level
InitialQualityLevel();

Expand All @@ -64,23 +67,23 @@ private void InitialQualityLevel()
{
case <= 2048:
_initialQualityLevel = 0;
GraphicsLowQuality(true);
GraphicsLowQuality();
break;
case <= 4096:
_initialQualityLevel = 1;
GraphicsMediumQuality(true);
GraphicsMediumQuality();
break;
case <= 6144:
_initialQualityLevel = 2;
GraphicsHighQuality(true);
GraphicsHighQuality();
break;
case <= 8192:
_initialQualityLevel = 3;
GraphicsUltraQuality(true);
GraphicsUltraQuality();
break;
default:
_initialQualityLevel = 3;
GraphicsUltraQuality(true);
GraphicsUltraQuality();
break;
}

Expand All @@ -93,23 +96,24 @@ public void UISetQuality(int value)
switch (value)
{
case 0:
GraphicsLowQuality(true);
GraphicsLowQuality();
break;
case 1:
GraphicsMediumQuality(true);
GraphicsMediumQuality();
break;
case 2:
GraphicsHighQuality(true);
GraphicsHighQuality();
break;
case 3:
GraphicsUltraQuality(true);
GraphicsUltraQuality();
break;
}
}

private void GraphicsLowQuality(bool doExpensiveChanges)
private void GraphicsLowQuality()
{
QualitySettings.SetQualityLevel(0, doExpensiveChanges);
_currentQualityLevel = 0;
QualitySettings.SetQualityLevel(0);

// update camera parameters
foreach (var cam in _cameraObjectsList)
Expand Down Expand Up @@ -137,9 +141,10 @@ private void GraphicsLowQuality(bool doExpensiveChanges)
_sunSource.shadows = LightShadows.Hard;
}

private void GraphicsMediumQuality(bool doExpensiveChanges)
private void GraphicsMediumQuality()
{
QualitySettings.SetQualityLevel(1, doExpensiveChanges);
_currentQualityLevel = 1;
QualitySettings.SetQualityLevel(1);

// update camera and volume parameters
foreach (var cam in _cameraObjectsList)
Expand Down Expand Up @@ -169,9 +174,10 @@ private void GraphicsMediumQuality(bool doExpensiveChanges)
_sunSource.shadows = LightShadows.Hard;
}

private void GraphicsHighQuality(bool doExpensiveChanges)
private void GraphicsHighQuality()
{
QualitySettings.SetQualityLevel(2, doExpensiveChanges);
_currentQualityLevel = 2;
QualitySettings.SetQualityLevel(2);

// update camera parameters
foreach (var cam in _cameraObjectsList)
Expand Down Expand Up @@ -204,35 +210,39 @@ private void GraphicsHighQuality(bool doExpensiveChanges)
_sunSource.shadows = LightShadows.Soft;
}

private void GraphicsUltraQuality(bool doExpensiveChanges)
private void GraphicsUltraQuality()
{
QualitySettings.SetQualityLevel(3, doExpensiveChanges);
_currentQualityLevel = 3;
QualitySettings.SetQualityLevel(3);

// update camera parameters
foreach (var cam in _cameraObjectsList)
{
cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData);
cam.TryGetComponent<Volume>(out var cameraVolume);
var cameraVolumeProfile = cameraVolume.profile;

cameraData.antialiasing = AntialiasingMode.SubpixelMorphologicalAntiAliasing;
cameraData.antialiasingQuality = AntialiasingQuality.High;
cameraData.renderPostProcessing = true;
cameraData.renderShadows = true;
cameraData.allowHDROutput = true;

if (cameraVolumeProfile.TryGet(out Bloom bloom))
if (cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData))
{
bloom.active = true;
bloom.intensity.value = 1.0f;
bloom.highQualityFiltering.overrideState = true;
bloom.highQualityFiltering.value = true;
cameraData.antialiasing = AntialiasingMode.SubpixelMorphologicalAntiAliasing;
cameraData.antialiasingQuality = AntialiasingQuality.High;
cameraData.renderPostProcessing = true;
cameraData.renderShadows = true;
cameraData.allowHDROutput = true;
}

if (cameraVolumeProfile.TryGet(out Tonemapping tonemapping))
if (cam.TryGetComponent<Volume>(out var cameraVolume))
{
tonemapping.active = true;
tonemapping.mode.value = TonemappingMode.ACES;
var cameraVolumeProfile = cameraVolume.profile;
if (cameraVolumeProfile.TryGet(out Bloom bloom))
{
bloom.active = true;
bloom.intensity.value = 1.0f;
bloom.highQualityFiltering.overrideState = true;
bloom.highQualityFiltering.value = true;
}

if (cameraVolumeProfile.TryGet(out Tonemapping tonemapping))
{
tonemapping.active = true;
tonemapping.mode.value = TonemappingMode.ACES;
}
}
}

Expand All @@ -241,5 +251,17 @@ private void GraphicsUltraQuality(bool doExpensiveChanges)

// TODO: custom user settings (mozzz)
// private void CustomAssetProperties(){}

// Used for adding new cameras and updating their settings
private void UpdateGraphicSettingsForAddedCamera(GameObject cam)
{
_cameraObjectsList.Add(cam);
UISetQuality(_currentQualityLevel);
}

private void OnDestroy()
{
BirdEyeView.OnCameraInitialized -= UpdateGraphicSettingsForAddedCamera;
}
}
}
Loading