Skip to content

Commit

Permalink
fix: add null checks when accessing camera components
Browse files Browse the repository at this point in the history
Signed-off-by: Alptuğ Cırıt <[email protected]>
  • Loading branch information
mozhoku committed Sep 19, 2024
1 parent 15c20be commit f4992d7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
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
61 changes: 39 additions & 22 deletions Assets/AWSIM/Scripts/UI/GraphicsSettings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
Expand Down Expand Up @@ -49,6 +50,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 @@ -242,35 +244,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 +287,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

0 comments on commit f4992d7

Please sign in to comment.