Skip to content

Commit 0588755

Browse files
Fix issue when switching back to custom sensor type in physical camera (#417)
* Fix issue when switching back to custom sensor type in physical camera * make if comparison more explicit * make if comparison more safe * Per-camera history for the custom settings using a static dictionary * Use ConditionalWeakTable instead of a Dictionary to avoid keeping alive deleted cameras Co-authored-by: sebastienlagarde <[email protected]>
1 parent 943e8be commit 0588755

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
590590
- Fix conflicts with Handles manipulation when performing a Reset in DecalComponent (case 1238833)
591591
- Fixed depth prepass and postpass being disabled after changing the shader in the material UI.
592592
- Fixed issue with sceneview camera settings not being saved after Editor restart.
593+
- Fixed issue when switching back to custom sensor type in physical camera settings (case 1244350).
593594

594595
### Changed
595596
- Improve MIP selection for decals on Transparents

com.unity.render-pipelines.high-definition/Editor/RenderPipeline/Camera/HDCameraUI.Drawers.cs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4+
using System.Runtime.CompilerServices;
45
using UnityEngine;
56
using UnityEngine.Rendering.HighDefinition;
67
using UnityEngine.Rendering;
@@ -62,6 +63,8 @@ enum ShutterSpeedUnit
6263
"Custom"
6364
};
6465

66+
static readonly int k_CustomPresetIndex = k_ApertureFormatNames.Length - 1;
67+
6568
static readonly Vector2[] k_ApertureFormatValues =
6669
{
6770
new Vector2(4.8f, 3.5f),
@@ -76,6 +79,10 @@ enum ShutterSpeedUnit
7679
new Vector2(70.41f, 52.63f)
7780
};
7881

82+
// Saves the value of the sensor size when the user switches from "custom" size to a preset per camera.
83+
// We use a ConditionalWeakTable instead of a Dictionary to avoid keeping alive (with strong references) deleted cameras
84+
static ConditionalWeakTable<Camera, object> s_PerCameraSensorSizeHistory = new ConditionalWeakTable<Camera, object>();
85+
7986
static bool s_FovChanged;
8087
static float s_FovLastValue;
8188

@@ -298,14 +305,51 @@ static void Drawer_PhysicalCamera(SerializedHDCamera p, Editor owner)
298305
using (new EditorGUI.IndentLevelScope())
299306
{
300307
EditorGUI.BeginChangeCheck();
301-
int filmGateIndex = Array.IndexOf(k_ApertureFormatValues, new Vector2((float)Math.Round(cam.sensorSize.vector2Value.x, 3), (float)Math.Round(cam.sensorSize.vector2Value.y, 3)));
302-
if (filmGateIndex == -1)
303-
filmGateIndex = EditorGUILayout.Popup(cameraTypeContent, k_ApertureFormatNames.Length - 1, k_ApertureFormatNames);
304-
else
305-
filmGateIndex = EditorGUILayout.Popup(cameraTypeContent, filmGateIndex, k_ApertureFormatNames);
306308

307-
if (EditorGUI.EndChangeCheck() && filmGateIndex < k_ApertureFormatValues.Length)
308-
cam.sensorSize.vector2Value = k_ApertureFormatValues[filmGateIndex];
309+
int oldFilmGateIndex = Array.IndexOf(k_ApertureFormatValues, new Vector2((float)Math.Round(cam.sensorSize.vector2Value.x, 3), (float)Math.Round(cam.sensorSize.vector2Value.y, 3)));
310+
311+
// If it is not one of the preset sizes, set it to custom
312+
oldFilmGateIndex = (oldFilmGateIndex == -1) ? k_CustomPresetIndex: oldFilmGateIndex;
313+
314+
// Get the new user selection
315+
int newFilmGateIndex = EditorGUILayout.Popup(cameraTypeContent, oldFilmGateIndex, k_ApertureFormatNames);
316+
317+
if (EditorGUI.EndChangeCheck())
318+
{
319+
// Retrieve the previous custom size value, if one exists for this camera
320+
object previousCustomValue;
321+
s_PerCameraSensorSizeHistory.TryGetValue((Camera)p.serializedObject.targetObject, out previousCustomValue);
322+
323+
// When switching from custom to a preset, update the last custom value (to display again, in case the user switches back to custom)
324+
if (oldFilmGateIndex == k_CustomPresetIndex)
325+
{
326+
if (previousCustomValue == null)
327+
{
328+
s_PerCameraSensorSizeHistory.Add((Camera)p.serializedObject.targetObject, cam.sensorSize.vector2Value);
329+
}
330+
else
331+
{
332+
previousCustomValue = cam.sensorSize.vector2Value;
333+
}
334+
}
335+
336+
if (newFilmGateIndex < k_CustomPresetIndex)
337+
{
338+
cam.sensorSize.vector2Value = k_ApertureFormatValues[newFilmGateIndex];
339+
}
340+
else
341+
{
342+
// The user switched back to custom, so display by deafulr the previous custom value
343+
if (previousCustomValue != null)
344+
{
345+
cam.sensorSize.vector2Value = (Vector2)previousCustomValue;
346+
}
347+
else
348+
{
349+
cam.sensorSize.vector2Value = new Vector2(36.0f, 24.0f); // this is the value new cameras are created with
350+
}
351+
}
352+
}
309353

310354
EditorGUILayout.PropertyField(cam.sensorSize, sensorSizeContent);
311355
EditorGUILayout.PropertyField(p.iso, isoContent);

0 commit comments

Comments
 (0)