Files
com.misaki.hdrp-toon/Runtime/DebugShadowmap.cs
Misaki a7a9ad16b5 Updated namespace;
Added UTSPass;
Chnaged the RTHanlde _HairShadowMap to not reallocate when screen resolution decreased;
2025-01-08 22:19:04 +09:00

231 lines
5.1 KiB
C#

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Misaki.HdrpToon
{
[ExecuteAlways]
[DisallowMultipleComponent]
[RequireComponent(typeof(Camera))]
public class DebugShadowmap : MonoBehaviour
{
// flags
bool m_initialized = false;
bool m_srpCallbackInitialized = false;
public bool m_enableShadowmapDebugging = false;
public bool m_enableSelfShadowDebugging = false;
public bool m_enableBinalization = false;
public bool m_enableOutlineDebugging = false;
const string kDebugShadowmapDefine = "UTS_DEBUG_SHADOWMAP";
const string kDebugSelfShadowDefine = "UTS_DEBUG_SELFSHADOW";
const string kDebugDefineNoOutline = "UTS_DEBUG_SHADOWMAP_NO_OUTLINE";
const string kDebugDefineBinalization = "UTS_DEBUG_SHADOWMAP_BINALIZATION";
private static DebugShadowmap instance;
#if UNITY_EDITOR
#pragma warning restore CS0414
bool m_isCompiling = false;
#endif
void Awake()
{
if (instance == null)
{
instance = this as DebugShadowmap;
return;
}
else if (instance == this)
{
return;
}
Debug.LogError("There is DebugShadowmap instance in hierarchy.");
#if UNITY_EDITOR
DestroyImmediate(this);
Selection.activeGameObject = instance.gameObject;
#else
Destroy(this);
#endif
}
void Reset()
{
OnDisable();
OnEnable();
}
void Update()
{
Initialize();
#if UNITY_EDITOR
// handle script recompile
if (EditorApplication.isCompiling && !m_isCompiling)
{
// on compile begin
m_isCompiling = true;
// Release(); no need
return; //
}
else if (!EditorApplication.isCompiling && m_isCompiling)
{
// on compile end
m_isCompiling = false;
}
#endif
ApplyDebuggingFlag();
}
void EnableSelfShadowKeyword()
{
Shader.EnableKeyword(kDebugSelfShadowDefine);
}
void DisableSelfShadowKeyword()
{
Shader.DisableKeyword(kDebugSelfShadowDefine);
}
void EnableShadowmapKeyword()
{
Shader.EnableKeyword(kDebugShadowmapDefine);
}
void DisableShadowmapKeyword()
{
Shader.DisableKeyword(kDebugShadowmapDefine);
}
void EnableOutlineKeyword()
{
Shader.EnableKeyword(kDebugDefineNoOutline);
}
void DisableOutlineKeyword()
{
Shader.DisableKeyword(kDebugDefineNoOutline);
}
void EnableBinalizationKeyword()
{
Shader.EnableKeyword(kDebugDefineBinalization);
}
void DisableBinalizationKeyword()
{
Shader.DisableKeyword(kDebugDefineBinalization);
}
void EnableSrpCallbacks()
{
if (!m_srpCallbackInitialized)
{
m_srpCallbackInitialized = true;
}
}
void DisableSrpCallbacks()
{
if (m_srpCallbackInitialized)
{
m_srpCallbackInitialized = false;
}
}
void OnEnable()
{
Initialize();
EnableSrpCallbacks();
}
void OnDisable()
{
DisableSrpCallbacks();
Release();
}
void OnValidate()
{
ApplyDebuggingFlag();
}
void ApplyDebuggingFlag()
{
if (!this.enabled)
{
return;
}
if (m_enableShadowmapDebugging)
{
EnableShadowmapKeyword();
}
else
{
DisableShadowmapKeyword();
}
if (m_enableSelfShadowDebugging)
{
EnableSelfShadowKeyword();
}
else
{
DisableSelfShadowKeyword();
}
if (m_enableOutlineDebugging)
{
EnableOutlineKeyword();
}
else
{
DisableOutlineKeyword();
}
if (m_enableBinalization)
{
EnableBinalizationKeyword();
}
else
{
DisableBinalizationKeyword();
}
}
void Initialize()
{
if (m_initialized)
{
return;
}
#if UNITY_EDITOR
// initializing renderer can interfere GI baking. so wait until it is completed.
if (EditorApplication.isCompiling)
return;
#endif
m_initialized = true;
}
void Release()
{
if (m_initialized)
{
DisableShadowmapKeyword();
}
m_initialized = false;
}
}
}