Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 6a03c91

Browse files
authored
Merge pull request #116 from Unity-Technologies/better-eye-adapt
Improved eye adaptation
2 parents f00b860 + 7e01dcd commit 6a03c91

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

PostProcessing/Editor/Models/EyeAdaptationModelEditor.cs

+27-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class EyeAdaptationModelEditor : PostProcessingModelEditor
1212
SerializedProperty m_HighPercent;
1313
SerializedProperty m_MinLuminance;
1414
SerializedProperty m_MaxLuminance;
15-
SerializedProperty m_ExposureCompensation;
15+
SerializedProperty m_KeyValue;
16+
SerializedProperty m_DynamicKeyValue;
1617
SerializedProperty m_AdaptationType;
1718
SerializedProperty m_SpeedUp;
1819
SerializedProperty m_SpeedDown;
@@ -25,7 +26,8 @@ public override void OnEnable()
2526
m_HighPercent = FindSetting((Settings x) => x.highPercent);
2627
m_MinLuminance = FindSetting((Settings x) => x.minLuminance);
2728
m_MaxLuminance = FindSetting((Settings x) => x.maxLuminance);
28-
m_ExposureCompensation = FindSetting((Settings x) => x.exposureCompensation);
29+
m_KeyValue = FindSetting((Settings x) => x.keyValue);
30+
m_DynamicKeyValue = FindSetting((Settings x) => x.dynamicKeyValue);
2931
m_AdaptationType = FindSetting((Settings x) => x.adaptationType);
3032
m_SpeedUp = FindSetting((Settings x) => x.speedUp);
3133
m_SpeedDown = FindSetting((Settings x) => x.speedDown);
@@ -36,27 +38,39 @@ public override void OnEnable()
3638
public override void OnInspectorGUI()
3739
{
3840
if (!GraphicsUtils.supportsDX11)
39-
{
4041
EditorGUILayout.HelpBox("This effect requires support for compute shaders. Enabling it won't do anything on unsupported platforms.", MessageType.Warning);
41-
}
4242

43-
EditorGUILayout.PropertyField(m_LogMin, EditorGUIHelper.GetContent("Histogram Log Min"));
44-
EditorGUILayout.PropertyField(m_LogMax, EditorGUIHelper.GetContent("Histogram Log Max"));
43+
EditorGUILayout.LabelField("Luminosity range", EditorStyles.boldLabel);
44+
EditorGUI.indentLevel++;
45+
EditorGUILayout.PropertyField(m_LogMin, EditorGUIHelper.GetContent("Minimum (EV)"));
46+
EditorGUILayout.PropertyField(m_LogMax, EditorGUIHelper.GetContent("Maximum (EV)"));
47+
EditorGUI.indentLevel--;
4548
EditorGUILayout.Space();
4649

50+
EditorGUILayout.LabelField("Auto exposure", EditorStyles.boldLabel);
51+
EditorGUI.indentLevel++;
4752
float low = m_LowPercent.floatValue;
4853
float high = m_HighPercent.floatValue;
4954

50-
EditorGUILayout.MinMaxSlider(EditorGUIHelper.GetContent("Filter|These values are the lower and upper percentages of the histogram that will be used to find a stable average luminance. Values outside of this range will be discarded and won't contribute to the average luminance."), ref low, ref high, 1f, 99f);
55+
EditorGUILayout.MinMaxSlider(EditorGUIHelper.GetContent("Histogram filtering|These values are the lower and upper percentages of the histogram that will be used to find a stable average luminance. Values outside of this range will be discarded and won't contribute to the average luminance."), ref low, ref high, 1f, 99f);
5156

5257
m_LowPercent.floatValue = low;
5358
m_HighPercent.floatValue = high;
5459

55-
EditorGUILayout.PropertyField(m_MinLuminance);
56-
EditorGUILayout.PropertyField(m_MaxLuminance);
57-
EditorGUILayout.PropertyField(m_ExposureCompensation);
60+
EditorGUILayout.PropertyField(m_MinLuminance, EditorGUIHelper.GetContent("Minimum (EV)"));
61+
EditorGUILayout.PropertyField(m_MaxLuminance, EditorGUIHelper.GetContent("Maximum (EV)"));
62+
EditorGUILayout.PropertyField(m_DynamicKeyValue);
5863

59-
EditorGUILayout.PropertyField(m_AdaptationType);
64+
if (!m_DynamicKeyValue.boolValue)
65+
EditorGUILayout.PropertyField(m_KeyValue);
66+
67+
EditorGUI.indentLevel--;
68+
EditorGUILayout.Space();
69+
70+
EditorGUILayout.LabelField("Adaptation", EditorStyles.boldLabel);
71+
EditorGUI.indentLevel++;
72+
73+
EditorGUILayout.PropertyField(m_AdaptationType, EditorGUIHelper.GetContent("Type"));
6074

6175
if (m_AdaptationType.intValue == (int)EyeAdaptationModel.EyeAdaptationType.Progressive)
6276
{
@@ -65,6 +79,8 @@ public override void OnInspectorGUI()
6579
EditorGUILayout.PropertyField(m_SpeedDown);
6680
EditorGUI.indentLevel--;
6781
}
82+
83+
EditorGUI.indentLevel--;
6884
}
6985
}
7086
}

PostProcessing/Resources/Shaders/EyeAdaptation.shader

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Shader "Hidden/Post FX/Eye Adaptation"
88
CGINCLUDE
99

1010
#pragma target 4.5
11+
#pragma multi_compile __ AUTO_KEY_VALUE
1112
#include "UnityCG.cginc"
1213
#include "Common.cginc"
1314
#include "EyeAdaptation.cginc"
@@ -86,8 +87,12 @@ Shader "Hidden/Post FX/Eye Adaptation"
8687
{
8788
avgLuminance = max(EPSILON, avgLuminance);
8889

89-
//half keyValue = 1.03 - (2.0 / (2.0 + log2(avgLuminance + 1.0)));
90+
#if AUTO_KEY_VALUE
91+
half keyValue = 1.03 - (2.0 / (2.0 + log2(avgLuminance + 1.0)));
92+
#else
9093
half keyValue = _ExposureCompensation;
94+
#endif
95+
9196
half exposure = keyValue / avgLuminance;
9297

9398
return exposure;

PostProcessing/Runtime/Components/EyeAdaptationComponent.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public Texture Prepare(RenderTexture source, Material uberMaterial)
8484
m_EyeCompute = Resources.Load<ComputeShader>("Shaders/EyeHistogram");
8585

8686
var material = context.materialFactory.Get("Hidden/Post FX/Eye Adaptation");
87+
material.shaderKeywords = null;
8788

8889
if (m_HistogramBuffer == null)
8990
m_HistogramBuffer = new ComputeBuffer(k_HistogramBins, sizeof(uint));
@@ -124,10 +125,13 @@ public Texture Prepare(RenderTexture source, Material uberMaterial)
124125

125126
// Compute auto exposure
126127
material.SetBuffer("_Histogram", m_HistogramBuffer); // No (int, buffer) overload for SetBuffer ?
127-
material.SetVector(Uniforms._Params, new Vector4(settings.lowPercent * 0.01f, settings.highPercent * 0.01f, settings.minLuminance, settings.maxLuminance));
128+
material.SetVector(Uniforms._Params, new Vector4(settings.lowPercent * 0.01f, settings.highPercent * 0.01f, Mathf.Exp(settings.minLuminance * 0.69314718055994530941723212145818f), Mathf.Exp(settings.maxLuminance * 0.69314718055994530941723212145818f)));
128129
material.SetVector(Uniforms._Speed, new Vector2(settings.speedDown, settings.speedUp));
129130
material.SetVector(Uniforms._ScaleOffsetRes, scaleOffsetRes);
130-
material.SetFloat(Uniforms._ExposureCompensation, settings.exposureCompensation);
131+
material.SetFloat(Uniforms._ExposureCompensation, settings.keyValue);
132+
133+
if (settings.dynamicKeyValue)
134+
material.EnableKeyword("AUTO_KEY_VALUE");
131135

132136
if (m_FirstFrame || !Application.isPlaying)
133137
{

PostProcessing/Runtime/Models/EyeAdaptationModel.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ public struct Settings
2020
[Range(1f, 99f), Tooltip("Filters the bright part of the histogram when computing the average luminance to avoid very dark pixels from contributing to the auto exposure. Unit is in percent.")]
2121
public float highPercent;
2222

23-
[Min(0f), Tooltip("Minimum average luminance to consider for auto exposure.")]
23+
[Tooltip("Minimum average luminance to consider for auto exposure (in EV).")]
2424
public float minLuminance;
2525

26-
[Min(0f), Tooltip("Maximum average luminance to consider for auto exposure.")]
26+
[Tooltip("Maximum average luminance to consider for auto exposure (in EV).")]
2727
public float maxLuminance;
2828

2929
[Min(0f), Tooltip("Exposure bias. Use this to control the global exposure of the scene.")]
30-
public float exposureCompensation;
30+
public float keyValue;
31+
32+
[Tooltip("Set this to true to let Unity handle the key value automatically based on average luminance.")]
33+
public bool dynamicKeyValue;
3134

3235
[Tooltip("Use \"Progressive\" if you want the auto exposure to be animated. Use \"Fixed\" otherwise.")]
3336
public EyeAdaptationType adaptationType;
@@ -38,10 +41,10 @@ public struct Settings
3841
[Min(0f), Tooltip("Adaptation speed from a light to a dark environment.")]
3942
public float speedDown;
4043

41-
[Range(-16, -1), Tooltip("Lower bound for the brightness range of the generated histogram (Log2).")]
44+
[Range(-16, -1), Tooltip("Lower bound for the brightness range of the generated histogram (in EV). The bigger the spread between min & max, the lower the precision will be.")]
4245
public int logMin;
4346

44-
[Range(1, 16), Tooltip("Upper bound for the brightness range of the generated histogram (Log2).")]
47+
[Range(1, 16), Tooltip("Upper bound for the brightness range of the generated histogram (in EV). The bigger the spread between min & max, the lower the precision will be.")]
4548
public int logMax;
4649

4750
public static Settings defaultSettings
@@ -50,12 +53,13 @@ public static Settings defaultSettings
5053
{
5154
return new Settings
5255
{
53-
lowPercent = 65f,
56+
lowPercent = 45f,
5457
highPercent = 95f,
5558

56-
minLuminance = 0.03f,
57-
maxLuminance = 2f,
58-
exposureCompensation = 0.5f,
59+
minLuminance = -5f,
60+
maxLuminance = 1f,
61+
keyValue = 0.25f,
62+
dynamicKeyValue = true,
5963

6064
adaptationType = EyeAdaptationType.Progressive,
6165
speedUp = 2f,

0 commit comments

Comments
 (0)