From 6d4d2ef13b2698247da6d052306c3a01e49bf914 Mon Sep 17 00:00:00 2001 From: Szymon Lis Date: Thu, 28 Mar 2024 20:42:03 +0100 Subject: [PATCH] Added Camera Feature Inspector Component --- .../CameraSensorProfile.asset | 36 +-- Assets/AWSIM/Scripts/Editor/Sensors.meta | 8 + .../CameraSensorFeatureManagerEditor.cs | 115 ++++++++ .../CameraSensorFeatureManagerEditor.cs.meta | 11 + .../Camera/CameraSensorFeatureManager.cs | 251 ++++++++++++++++++ .../Camera/CameraSensorFeatureManager.cs.meta | 11 + 6 files changed, 414 insertions(+), 18 deletions(-) create mode 100644 Assets/AWSIM/Scripts/Editor/Sensors.meta create mode 100644 Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs create mode 100644 Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs.meta create mode 100644 Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs create mode 100644 Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs.meta diff --git a/Assets/AWSIM/Scenes/Samples/CameraSensorEffectsSample/CameraSensorProfile.asset b/Assets/AWSIM/Scenes/Samples/CameraSensorEffectsSample/CameraSensorProfile.asset index 7f702217c..6cbe85ae9 100644 --- a/Assets/AWSIM/Scenes/Samples/CameraSensorEffectsSample/CameraSensorProfile.asset +++ b/Assets/AWSIM/Scenes/Samples/CameraSensorEffectsSample/CameraSensorProfile.asset @@ -178,7 +178,7 @@ MonoBehaviour: m_OverrideState: 0 m_Value: {fileID: 0} intensity: - m_OverrideState: 0 + m_OverrideState: 1 m_Value: 0 min: 0 max: 1 @@ -202,10 +202,10 @@ MonoBehaviour: active: 0 m_AdvancedMode: 0 quality: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 2 focusMode: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 1 focusDistance: m_OverrideState: 1 @@ -228,33 +228,33 @@ MonoBehaviour: m_Value: 20 min: 0 m_NearSampleCount: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 8 min: 3 max: 8 m_NearMaxBlur: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 7 min: 0 max: 8 m_FarSampleCount: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 14 min: 3 max: 16 m_FarMaxBlur: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 13 min: 0 max: 16 m_HighQualityFiltering: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 1 m_Resolution: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 1 m_PhysicallyBased: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 0 --- !u!114 &-2696452528647907726 MonoBehaviour: @@ -268,10 +268,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: bcf384b154398e341b6b29969c078198, type: 3} m_Name: MotionBlur m_EditorClassIdentifier: - active: 1 + active: 0 m_AdvancedMode: 0 quality: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 2 intensity: m_OverrideState: 1 @@ -283,7 +283,7 @@ MonoBehaviour: min: 0 max: 1500 minimumVelocity: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 0 min: 0 max: 64 @@ -314,7 +314,7 @@ MonoBehaviour: min: 0 max: 20 m_SampleCount: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 12 min: 2 --- !u!114 &-1020721212055412715 @@ -332,7 +332,7 @@ MonoBehaviour: active: 0 m_AdvancedMode: 0 quality: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 2 threshold: m_OverrideState: 0 @@ -365,13 +365,13 @@ MonoBehaviour: m_OverrideState: 0 m_Value: 1 m_Resolution: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 2 m_HighQualityPrefiltering: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 1 m_HighQualityFiltering: - m_OverrideState: 1 + m_OverrideState: 0 m_Value: 1 --- !u!114 &11400000 MonoBehaviour: diff --git a/Assets/AWSIM/Scripts/Editor/Sensors.meta b/Assets/AWSIM/Scripts/Editor/Sensors.meta new file mode 100644 index 000000000..6fc8206f8 --- /dev/null +++ b/Assets/AWSIM/Scripts/Editor/Sensors.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5bfabb55f8dce66bca5d4519cd866817 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs b/Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs new file mode 100644 index 000000000..3bf801faa --- /dev/null +++ b/Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs @@ -0,0 +1,115 @@ +using UnityEngine; +using UnityEditor; + +namespace AWSIM +{ + + [CustomEditor(typeof(CameraSensorFeatureManager))] + public class CameraSensorFeatureManagerEditor : Editor + { + + private CameraSensorFeatureManager manager; + + private SerializedProperty hueProperty; + private SerializedProperty saturationProperty; + private SerializedProperty contrastProperty; + private SerializedProperty postExposureProperty; + + private SerializedProperty exposureModeProperty; + + private SerializedProperty bloomEffectProperty; + private SerializedProperty chromaticAberrationProperty; + private SerializedProperty depthOfFieldProperty; + private SerializedProperty motionBlurProperty; + + + private void OnEnable() + { + manager = (CameraSensorFeatureManager)target; + + hueProperty = serializedObject.FindProperty("hue"); + saturationProperty = serializedObject.FindProperty("saturation"); + contrastProperty = serializedObject.FindProperty("contrast"); + postExposureProperty =serializedObject.FindProperty("postExposure"); + + exposureModeProperty = serializedObject.FindProperty("exposureMode"); + + bloomEffectProperty = serializedObject.FindProperty("bloomEffect"); + chromaticAberrationProperty = serializedObject.FindProperty("chromaticAberration"); + depthOfFieldProperty = serializedObject.FindProperty("depthOfField"); + motionBlurProperty = serializedObject.FindProperty("motionBlur"); + } + + public override void OnInspectorGUI() + { + // ------ Components ------- // + EditorGUILayout.PropertyField(serializedObject.FindProperty("camera"), true); + EditorGUILayout.PropertyField(serializedObject.FindProperty("cameraSensor"), true); + EditorGUILayout.PropertyField(serializedObject.FindProperty("volume"), true); + EditorGUILayout.PropertyField(serializedObject.FindProperty("profile"), true); + + // ------ Image Adjustment ------- // + EditorGUILayout.PropertyField(serializedObject.FindProperty("hue"), true); + if(hueProperty.boolValue) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("hueValue"), true); + } + + EditorGUILayout.PropertyField(serializedObject.FindProperty("saturation"), true); + if(saturationProperty.boolValue) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("saturationValue"), true); + } + + EditorGUILayout.PropertyField(serializedObject.FindProperty("contrast"), true); + if(contrastProperty.boolValue) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("contrastValue"), true); + } + + EditorGUILayout.PropertyField(serializedObject.FindProperty("postExposure"), true); + if(postExposureProperty.boolValue) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("postExposureValue"), true); + } + + // ------ Exposure Settings ------- // + EditorGUILayout.PropertyField(serializedObject.FindProperty("exposureMode"), true); + if(exposureModeProperty.enumValueIndex != 0) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("ISO"), true); + EditorGUILayout.PropertyField(serializedObject.FindProperty("shutterSpeed"), true); + EditorGUILayout.PropertyField(serializedObject.FindProperty("aperture"), true); + } + + + // ------ Additonal Effect ------- // + + EditorGUILayout.PropertyField(serializedObject.FindProperty("bloomEffect"), true); + if(bloomEffectProperty.boolValue) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("bloomValue"), true); + } + + EditorGUILayout.PropertyField(serializedObject.FindProperty("chromaticAberration"), true); + if(chromaticAberrationProperty.boolValue) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("chromaticAberrationValue"), true); + } + + EditorGUILayout.PropertyField(serializedObject.FindProperty("depthOfField"), true); + if(depthOfFieldProperty.boolValue) + { + EditorGUILayout.PropertyField(serializedObject.FindProperty("focusDistance"), true); + } + + EditorGUILayout.PropertyField(serializedObject.FindProperty("motionBlur"), true); + + + serializedObject.ApplyModifiedProperties(); + } + } + + +} + diff --git a/Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs.meta b/Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs.meta new file mode 100644 index 000000000..d3e5aa7f6 --- /dev/null +++ b/Assets/AWSIM/Scripts/Editor/Sensors/CameraSensorFeatureManagerEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8271a016976a93c77aa0661c2bf26b6b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs b/Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs new file mode 100644 index 000000000..99e794785 --- /dev/null +++ b/Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs @@ -0,0 +1,251 @@ +using System.Collections; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using Unity.VisualScripting; +using UnityEditor; +using UnityEngine; +using UnityEngine.Rendering; +using UnityEngine.Rendering.HighDefinition; + +namespace AWSIM +{ + + public class CameraSensorFeatureManager : MonoBehaviour + { + private enum CameraExposureMode + { + AUTO = 0, + MANUAL = 1, + } + + #region [Components] + + [Header("Components")] + [SerializeField] private Camera camera = default; + [SerializeField] private CameraSensor cameraSensor = default; + [SerializeField] private Volume volume = default; + [SerializeField] private VolumeProfile profile = null; + + #endregion + + #region [Image Adjustment"] + + [Header("Image Adjustment")] + [SerializeField] private bool hue = false; + [Range(-180f, 180f)] + [SerializeField] private float hueValue = 0f; + + [SerializeField] private bool saturation = false; + [Range(-100f, 100f)] + [SerializeField] private float saturationValue = 0f; + + [SerializeField] private bool contrast = false; + [Range(-100f, 100f)] + [SerializeField] private float contrastValue = 0f; + + [SerializeField] private bool postExposure = false; + [Range(-10f, 10f)] + [SerializeField] private float postExposureValue = 0f; + + #endregion + + [Header("Exposure Settings")] + [SerializeField] private CameraExposureMode exposureMode = CameraExposureMode.MANUAL; + [SerializeField] private int ISO = 200; + [SerializeField] private float shutterSpeed = 125; + [SerializeField] private float aperture = 16; + + + [Header("Additonal Effect")] + + [SerializeField] private bool bloomEffect = false; + [Range(0f, 1f)] + [SerializeField] private float bloomValue = 0f; + + [SerializeField] private bool chromaticAberration = false; + [Range(0f, 1f)] + [SerializeField] private float chromaticAberrationValue = 0f; + + [SerializeField] private bool depthOfField = false; + [Range(0.1f, 100f)] + [SerializeField] private float focusDistance = 0.1f; + + [SerializeField] private bool motionBlur = false; + + + #region [Effects Component] + + private ColorAdjustments colorAdjustmentsComponent = default; + private Exposure exposureComponent = default; + + private Bloom bloomComponent = default; + private ChromaticAberration chromaticAberrationComponent = default; + private DepthOfField depthOfFieldComponent = default; + private MotionBlur motionBlurComponent = default; + + #endregion + + private bool cameraObjectActive = false; + + private void Awake() + { + if(!profile.TryGet(out colorAdjustmentsComponent)) + { + + } + + if(!profile.TryGet(out exposureComponent)) + { + + } + + + if(!profile.TryGet(out bloomComponent)) + { + + } + + if(!profile.TryGet(out chromaticAberrationComponent)) + { + + } + + if(!profile.TryGet(out depthOfFieldComponent)) + { + + } + + if(!profile.TryGet(out motionBlurComponent)) + { + + } + + } + + private void Start() + { + // check if camera object have to be enable + if(motionBlur || exposureMode == CameraExposureMode.AUTO) + { + cameraObjectActive = true; + } + else + { + cameraObjectActive = false; + } + + // exposure mode + if(exposureComponent != null) + { + if(exposureMode == CameraExposureMode.AUTO) + { + exposureComponent.mode = new ExposureModeParameter(ExposureMode.Automatic, true); + } + else + { + exposureComponent.mode = new ExposureModeParameter(ExposureMode.UsePhysicalCamera, true); + } + } + + // exposure settings + ApplyExposureSettings(); + + // image adjustments + ApplyImageAdjustments(); + + // bloom effect + ApplyBloom(); + + // chromatic aberration + ApplyChromaticAberration(); + + // depth of field + ApplyDepthOfField(); + + // motion blur + ApplyMotionBlur(); + + + camera.gameObject.SetActive(cameraObjectActive); + } + + private void ApplyExposureSettings() + { + camera.GetComponent().physicalParameters.iso = ISO; + camera.GetComponent().physicalParameters.shutterSpeed = 1f/shutterSpeed; + camera.GetComponent().physicalParameters.aperture = aperture; + } + + private void ApplyImageAdjustments() + { + if(colorAdjustmentsComponent == null) + { + return; + } + + if(hue || saturation || contrast || postExposure) + { + colorAdjustmentsComponent.active = true; + } + else + { + colorAdjustmentsComponent.active = false; + } + + colorAdjustmentsComponent.hueShift.overrideState = hue; + colorAdjustmentsComponent.hueShift.value = hueValue; + + colorAdjustmentsComponent.saturation.overrideState = saturation; + colorAdjustmentsComponent.saturation.value = saturationValue; + + colorAdjustmentsComponent.contrast.overrideState = contrast; + colorAdjustmentsComponent.contrast.value = contrastValue; + + colorAdjustmentsComponent.postExposure.overrideState = postExposure; + colorAdjustmentsComponent.postExposure.value = postExposureValue; + } + + private void ApplyBloom() + { + if(bloomComponent != null) + { + bloomComponent.active = bloomEffect; + bloomComponent.intensity.overrideState = bloomEffect; + bloomComponent.intensity.value = bloomValue; + } + } + + private void ApplyChromaticAberration() + { + if(chromaticAberrationComponent != null) + { + chromaticAberrationComponent.active = chromaticAberration; + chromaticAberrationComponent.intensity.overrideState = chromaticAberration; + chromaticAberrationComponent.intensity.value = chromaticAberrationValue; + } + } + + private void ApplyDepthOfField() + { + if(depthOfFieldComponent != null) + { + depthOfFieldComponent.active = depthOfField; + depthOfFieldComponent.focusDistance.overrideState = depthOfField; + depthOfFieldComponent.focusDistance.value = focusDistance; + } + } + + private void ApplyMotionBlur() + { + if(motionBlurComponent != null) + { + motionBlurComponent.active = motionBlur; + motionBlurComponent.intensity.overrideState = motionBlur; + + float shutterSpeed = camera.GetComponent().physicalParameters.shutterSpeed; + motionBlurComponent.intensity.value = 2f * shutterSpeed / Time.fixedDeltaTime; + } + } + } + +} diff --git a/Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs.meta b/Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs.meta new file mode 100644 index 000000000..fe3e0023f --- /dev/null +++ b/Assets/AWSIM/Scripts/Sensors/Camera/CameraSensorFeatureManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 68dae1c5422cd552e83edd9b438b5358 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: