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: add null checks when accessing camera components #150

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
17 changes: 11 additions & 6 deletions Assets/AWSIM/Scripts/UI/BirdEyeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@ namespace AWSIM.Scripts.UI
public class BirdEyeView : MonoBehaviour
{
public static event Action<GameObject> OnCameraInitialized;
[SerializeField] private float _birdEyeCameraInitialHeight = 200;
public static event Action<GameObject> OnCameraDestroyed;

[SerializeField] private float _birdEyeCameraInitialHeight = 200;
[SerializeField] private float _cameraOrthographicSizeMinimum = 3;
[SerializeField] private float _cameraOrthographicSizeMaximum = 150;

[SerializeField] private float _zoomChangeMultiplier = 10f;
[SerializeField] private float _zoomTime = 0.1f;

[SerializeField] private float _dragPanMultiplier = 0.2f;

[SerializeField] private float _keyPanSpeed = 50f;
[SerializeField] private float _keyPanLerp = 0.5f;

[SerializeField] private float _targetFollowOffsetX;
[SerializeField] private float _targetFollowOffsetZ;

private Camera _birdEyeCamera;
private Camera _vehicleCamera;

private Transform _cameraFollowTarget;
private GameObject _vehicleTransform;

Expand Down Expand Up @@ -59,6 +55,15 @@ private void Start()
OnCameraInitialized?.Invoke(_birdEyeCamera.gameObject);
}

private void OnDestroy()
{
// Remove the camera from the list of cameras for GraphicsSettings
if (_birdEyeCamera != null)
{
OnCameraDestroyed?.Invoke(_birdEyeCamera.gameObject);
}
}

public void Activate()
{
enabled = true;
Expand Down
182 changes: 108 additions & 74 deletions Assets/AWSIM/Scripts/UI/GraphicsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private void Start()

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

// Set initial quality level
if (PlayerPrefs.HasKey(UserGraphicsQualityKey) &&
Expand Down Expand Up @@ -139,27 +140,33 @@ private void GraphicsLowQuality()
// update camera parameters
foreach (var cam in _cameraObjectsList)
{
cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData);
cam.TryGetComponent<Volume>(out var cameraVolume);
var cameraVolumeProfile = cameraVolume.profile;
if (cam)
{
cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData);
cam.TryGetComponent<Volume>(out var cameraVolume);
var cameraVolumeProfile = cameraVolume.profile;

cameraData.antialiasing = AntialiasingMode.None;
cameraData.renderPostProcessing = false;
cameraData.renderShadows = false;
cameraData.allowHDROutput = false;
cameraData.antialiasing = AntialiasingMode.None;
cameraData.renderPostProcessing = false;
cameraData.renderShadows = false;
cameraData.allowHDROutput = false;

if (cameraVolumeProfile.TryGet(out Bloom bloom))
{
bloom.active = false;
}
if (cameraVolumeProfile.TryGet(out Bloom bloom))
{
bloom.active = false;
}

if (cameraVolumeProfile.TryGet(out Tonemapping tonemapping))
{
tonemapping.active = false;
if (cameraVolumeProfile.TryGet(out Tonemapping tonemapping))
{
tonemapping.active = false;
}
}
}

_sunSource.shadows = LightShadows.Hard;
if (_sunSource)
{
_sunSource.shadows = LightShadows.Hard;
}
}

private void GraphicsMediumQuality()
Expand All @@ -171,29 +178,35 @@ private void GraphicsMediumQuality()
// update camera and volume parameters
foreach (var cam in _cameraObjectsList)
{
cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData);
cam.TryGetComponent<Volume>(out var cameraVolume);
var cameraVolumeProfile = cameraVolume.profile;
if (cam)
{
cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData);
cam.TryGetComponent<Volume>(out var cameraVolume);
var cameraVolumeProfile = cameraVolume.profile;

cameraData.antialiasing = AntialiasingMode.FastApproximateAntialiasing;
cameraData.antialiasingQuality = AntialiasingQuality.Medium;
cameraData.renderPostProcessing = true;
cameraData.renderShadows = true;
cameraData.allowHDROutput = false;
cameraData.antialiasing = AntialiasingMode.FastApproximateAntialiasing;
cameraData.antialiasingQuality = AntialiasingQuality.Medium;
cameraData.renderPostProcessing = true;
cameraData.renderShadows = true;
cameraData.allowHDROutput = false;

if (cameraVolumeProfile.TryGet(out Bloom bloom))
{
bloom.active = false;
bloom.intensity.value = 0.25f;
}
if (cameraVolumeProfile.TryGet(out Bloom bloom))
{
bloom.active = false;
bloom.intensity.value = 0.25f;
}

if (cameraVolumeProfile.TryGet(out Tonemapping tonemapping))
{
tonemapping.active = false;
if (cameraVolumeProfile.TryGet(out Tonemapping tonemapping))
{
tonemapping.active = false;
}
}
}

_sunSource.shadows = LightShadows.Hard;
if (_sunSource)
{
_sunSource.shadows = LightShadows.Hard;
}
}

private void GraphicsHighQuality()
Expand All @@ -205,32 +218,38 @@ private void GraphicsHighQuality()
// update camera parameters
foreach (var cam in _cameraObjectsList)
{
cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData);
cam.TryGetComponent<Volume>(out var cameraVolume);
var cameraVolumeProfile = cameraVolume.profile;
if (cam)
{
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;
cameraData.antialiasing = AntialiasingMode.SubpixelMorphologicalAntiAliasing;
cameraData.antialiasingQuality = AntialiasingQuality.High;
cameraData.renderPostProcessing = true;
cameraData.renderShadows = true;
cameraData.allowHDROutput = true;

if (cameraVolumeProfile.TryGet(out Bloom bloom))
{
bloom.active = true;
bloom.intensity.value = 0.5f;
bloom.highQualityFiltering.overrideState = false;
bloom.highQualityFiltering.value = false;
}
if (cameraVolumeProfile.TryGet(out Bloom bloom))
{
bloom.active = true;
bloom.intensity.value = 0.5f;
bloom.highQualityFiltering.overrideState = false;
bloom.highQualityFiltering.value = false;
}

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

_sunSource.shadows = LightShadows.Soft;
if (_sunSource)
{
_sunSource.shadows = LightShadows.Soft;
}
}

private void GraphicsUltraQuality()
Expand All @@ -242,35 +261,41 @@ private void GraphicsUltraQuality()
// update camera parameters
foreach (var cam in _cameraObjectsList)
{
if (cam.TryGetComponent<UniversalAdditionalCameraData>(out var cameraData))
if (cam)
{
cameraData.antialiasing = AntialiasingMode.SubpixelMorphologicalAntiAliasing;
cameraData.antialiasingQuality = AntialiasingQuality.High;
cameraData.renderPostProcessing = true;
cameraData.renderShadows = true;
cameraData.allowHDROutput = true;
}

if (cam.TryGetComponent<Volume>(out var cameraVolume))
{
var cameraVolumeProfile = cameraVolume.profile;
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;
}
}
}
}

_sunSource.shadows = LightShadows.Soft;
if (_sunSource)
{
_sunSource.shadows = LightShadows.Soft;
}
}

// TODO: custom user settings (mozzz)
Expand All @@ -279,13 +304,22 @@ private void GraphicsUltraQuality()
// Used for adding new cameras and updating their settings
private void UpdateGraphicSettingsForAddedCamera(GameObject cam)
{
_cameraObjectsList.Add(cam);
UISetQuality(_currentQualityLevel);
if (cam != null)
{
_cameraObjectsList.Add(cam);
UISetQuality(_currentQualityLevel);
}
}

private void RemoveCameraFromList(GameObject cam)
{
_cameraObjectsList.Remove(cam);
}

private void OnDestroy()
{
BirdEyeView.OnCameraInitialized -= UpdateGraphicSettingsForAddedCamera;
BirdEyeView.OnCameraDestroyed -= RemoveCameraFromList;
_cameraObjectsList.Clear();
}
}
Expand Down
Loading