227 lines
6.6 KiB
C#
227 lines
6.6 KiB
C#
using UnityEngine;
|
|
|
|
[ExecuteInEditMode]
|
|
public class DeluxeEyeAdaptation : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
public DeluxeEyeAdaptationLogic m_Logic;
|
|
|
|
[SerializeField]
|
|
public bool m_LowResolution;
|
|
|
|
[SerializeField]
|
|
public bool m_ShowHistogram;
|
|
|
|
private bool m_LastShowHistogram;
|
|
|
|
[SerializeField]
|
|
public bool m_ShowExposure;
|
|
|
|
[SerializeField]
|
|
public float m_HistogramSize = 0.5f;
|
|
|
|
private Material m_HistogramMaterial;
|
|
|
|
private Shader m_HistogramShader;
|
|
|
|
private Material m_BrightnessMaterial;
|
|
|
|
private Shader m_BrightnessShader;
|
|
|
|
public bool m_LastUseGLSLFix;
|
|
|
|
public bool m_UseGLSLFix;
|
|
|
|
public bool m_ExposureOpened = true;
|
|
|
|
public bool m_SpeedOpened = true;
|
|
|
|
public bool m_HistogramOpened = true;
|
|
|
|
public bool m_OptimizationOpened = true;
|
|
|
|
public bool m_DebugOpened = true;
|
|
|
|
public bool m_BannerIsOpened = true;
|
|
|
|
public bool RenderDebugInfos
|
|
{
|
|
get
|
|
{
|
|
return m_ShowExposure || m_ShowHistogram;
|
|
}
|
|
}
|
|
|
|
private Material CreateMaterial(Shader shader)
|
|
{
|
|
if (!shader)
|
|
{
|
|
return null;
|
|
}
|
|
Material material = new Material(shader);
|
|
material.hideFlags = HideFlags.HideAndDontSave;
|
|
return material;
|
|
}
|
|
|
|
private void DestroyMaterial(Material mat)
|
|
{
|
|
if ((bool)mat)
|
|
{
|
|
Object.DestroyImmediate(mat);
|
|
mat = null;
|
|
}
|
|
}
|
|
|
|
public void OnDisable()
|
|
{
|
|
if (m_Logic != null)
|
|
{
|
|
m_Logic.ClearMeshes();
|
|
}
|
|
DestroyMaterial(m_HistogramMaterial);
|
|
m_HistogramMaterial = null;
|
|
m_HistogramShader = null;
|
|
DestroyMaterial(m_BrightnessMaterial);
|
|
m_BrightnessMaterial = null;
|
|
m_BrightnessShader = null;
|
|
}
|
|
|
|
private void CreateMaterials()
|
|
{
|
|
if (m_LastUseGLSLFix != m_UseGLSLFix)
|
|
{
|
|
m_HistogramShader = null;
|
|
m_HistogramMaterial = null;
|
|
}
|
|
m_LastUseGLSLFix = m_UseGLSLFix;
|
|
if (m_HistogramShader == null)
|
|
{
|
|
if (m_UseGLSLFix)
|
|
{
|
|
m_HistogramShader = Shader.Find("Hidden/Deluxe/EyeAdaptationGLSLFix");
|
|
}
|
|
else
|
|
{
|
|
m_HistogramShader = Shader.Find("Hidden/Deluxe/EyeAdaptation");
|
|
}
|
|
m_LastShowHistogram = !m_ShowHistogram;
|
|
}
|
|
if (m_HistogramMaterial == null && m_HistogramShader != null && m_HistogramShader.isSupported)
|
|
{
|
|
m_HistogramMaterial = CreateMaterial(m_HistogramShader);
|
|
}
|
|
if (m_BrightnessShader == null)
|
|
{
|
|
m_BrightnessShader = Shader.Find("Hidden/Deluxe/EyeAdaptationBright");
|
|
}
|
|
if (m_BrightnessMaterial == null && m_BrightnessShader != null && m_BrightnessShader.isSupported)
|
|
{
|
|
m_BrightnessMaterial = CreateMaterial(m_BrightnessShader);
|
|
}
|
|
else if (m_BrightnessShader == null)
|
|
{
|
|
Debug.LogError("Cant find brightness shader");
|
|
}
|
|
else if (!m_BrightnessShader.isSupported)
|
|
{
|
|
Debug.LogError("Brightness shader unsupported");
|
|
}
|
|
}
|
|
|
|
public void OnEnable()
|
|
{
|
|
CreateMaterials();
|
|
VerifyAndUpdateShaderVariations(true);
|
|
}
|
|
|
|
public void VerifyAndUpdateShaderVariations(bool forceUpdate)
|
|
{
|
|
if (m_HistogramMaterial == null)
|
|
{
|
|
return;
|
|
}
|
|
if (m_ShowHistogram != m_LastShowHistogram || forceUpdate)
|
|
{
|
|
m_HistogramMaterial.DisableKeyword("DLX_DEBUG_HISTOGRAM");
|
|
if (m_ShowHistogram)
|
|
{
|
|
m_HistogramMaterial.EnableKeyword("DLX_DEBUG_HISTOGRAM");
|
|
}
|
|
}
|
|
m_LastShowHistogram = m_ShowHistogram;
|
|
}
|
|
|
|
private void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
{
|
|
CreateMaterials();
|
|
VerifyAndUpdateShaderVariations(false);
|
|
if (m_Logic == null)
|
|
{
|
|
m_Logic = new DeluxeEyeAdaptationLogic();
|
|
}
|
|
DeluxeTonemapper component = GetComponent<DeluxeTonemapper>();
|
|
RenderTexture temporary = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0, source.format);
|
|
RenderTexture temporary2 = RenderTexture.GetTemporary(temporary.width / 2, temporary.height / 2, 0, source.format);
|
|
if (m_BrightnessMaterial == null)
|
|
{
|
|
Graphics.Blit(source, destination);
|
|
return;
|
|
}
|
|
m_BrightnessMaterial.SetTexture("_UpTex", source);
|
|
source.filterMode = FilterMode.Bilinear;
|
|
m_BrightnessMaterial.SetVector("_PixelSize", new Vector4(1f / (float)source.width * 0.5f, 1f / (float)source.height * 0.5f));
|
|
Graphics.Blit(source, temporary, m_BrightnessMaterial, 1);
|
|
m_BrightnessMaterial.SetTexture("_UpTex", temporary);
|
|
temporary.filterMode = FilterMode.Bilinear;
|
|
m_BrightnessMaterial.SetVector("_PixelSize", new Vector4(1f / (float)temporary.width * 0.5f, 1f / (float)temporary.height * 0.5f));
|
|
Graphics.Blit(temporary, temporary2, m_BrightnessMaterial, 1);
|
|
source.filterMode = FilterMode.Point;
|
|
if (m_LowResolution)
|
|
{
|
|
RenderTexture temporary3 = RenderTexture.GetTemporary(temporary2.width / 2, temporary2.height / 2, 0, source.format);
|
|
m_BrightnessMaterial.SetTexture("_UpTex", temporary2);
|
|
temporary2.filterMode = FilterMode.Bilinear;
|
|
m_BrightnessMaterial.SetVector("_PixelSize", new Vector4(1f / (float)temporary2.width * 0.5f, 1f / (float)temporary2.height * 0.5f));
|
|
Graphics.Blit(temporary2, temporary3, m_BrightnessMaterial, 1);
|
|
m_HistogramMaterial.SetTexture("_FrameTex", temporary3);
|
|
m_Logic.ComputeExposure(temporary3.width, temporary3.height, m_HistogramMaterial);
|
|
RenderTexture.ReleaseTemporary(temporary3);
|
|
}
|
|
else
|
|
{
|
|
m_HistogramMaterial.SetTexture("_FrameTex", temporary2);
|
|
m_Logic.ComputeExposure(temporary2.width, temporary2.height, m_HistogramMaterial);
|
|
}
|
|
RenderTexture.ReleaseTemporary(temporary);
|
|
RenderTexture.ReleaseTemporary(temporary2);
|
|
if (!Application.isPlaying)
|
|
{
|
|
RenderTexture active = RenderTexture.active;
|
|
RenderTexture.active = m_Logic.m_BrightnessRT;
|
|
GL.Clear(false, true, Color.white);
|
|
RenderTexture.active = active;
|
|
}
|
|
m_BrightnessMaterial.SetFloat("_ExposureOffset", m_Logic.m_ExposureOffset);
|
|
m_BrightnessMaterial.SetFloat("_BrightnessMultiplier", m_Logic.m_BrightnessMultiplier);
|
|
m_BrightnessMaterial.SetTexture("_BrightnessTex", m_Logic.m_BrightnessRT);
|
|
m_BrightnessMaterial.SetTexture("_ColorTex", source);
|
|
if (RenderDebugInfos)
|
|
{
|
|
m_BrightnessMaterial.SetFloat("_VisualizeExposure", (!m_ShowExposure) ? 0f : 1f);
|
|
m_BrightnessMaterial.SetFloat("_VisualizeHistogram", (!m_ShowHistogram) ? 0f : 1f);
|
|
m_BrightnessMaterial.SetVector("_MinMaxSpeedDt", m_Logic.MinMaxSpeedDT);
|
|
m_BrightnessMaterial.SetTexture("_Histogram", m_Logic.m_HistogramList[0]);
|
|
m_BrightnessMaterial.SetTexture("_ColorTex", source);
|
|
m_BrightnessMaterial.SetFloat("_LuminanceRange", m_Logic.m_Range);
|
|
m_BrightnessMaterial.SetVector("_HistogramCoefs", m_Logic.m_HistCoefs);
|
|
m_BrightnessMaterial.SetVector("_MinMax", new Vector4(0.01f, m_HistogramSize * 0.75f, 0.01f, m_HistogramSize * 0.5f));
|
|
m_BrightnessMaterial.SetFloat("_TotalPixelNumber", m_Logic.m_CurrentWidth * m_Logic.m_CurrentHeight);
|
|
Graphics.Blit(source, destination, m_BrightnessMaterial, 2);
|
|
}
|
|
else
|
|
{
|
|
Graphics.Blit(source, destination, m_BrightnessMaterial, 0);
|
|
}
|
|
}
|
|
}
|