From f5667914cc1fbc886e1cd60139769cb35af0e885 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:49:08 +0900 Subject: [PATCH] feat: global prefer sampling size close #269 --- .../Editor/UIEffectProjectSettingsEditor.cs | 3 ++ Packages/src/Runtime/Enums.cs | 8 +++++ .../src/Runtime/UIEffectProjectSettings.cs | 35 +++++++++++++++++++ Packages/src/Shaders/UIEffect.cginc | 9 ++++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs index 04c1b1ff..458411e5 100644 --- a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs +++ b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs @@ -10,6 +10,7 @@ public class UIEffectProjectSettingsEditor : Editor { private ReorderableList _reorderableList; private SerializedProperty _transformSensitivity; + private SerializedProperty _preferSamplingSize; private bool _isInitialized; private ShaderVariantRegistryEditor _shaderVariantRegistryEditor; @@ -18,6 +19,7 @@ private void InitializeIfNeeded() if (_isInitialized) return; _transformSensitivity = serializedObject.FindProperty("m_TransformSensitivity"); + _preferSamplingSize = serializedObject.FindProperty("m_PreferSamplingSize"); var runtimePresets = serializedObject.FindProperty("m_RuntimePresets"); _reorderableList = new ReorderableList(serializedObject, runtimePresets, true, true, true, true); _reorderableList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Runtime Presets"); @@ -64,6 +66,7 @@ public override void OnInspectorGUI() // Settings EditorGUILayout.PropertyField(_transformSensitivity); + EditorGUILayout.PropertyField(_preferSamplingSize); _reorderableList.DoLayoutList(); // Shader registry diff --git a/Packages/src/Runtime/Enums.cs b/Packages/src/Runtime/Enums.cs index 339d3e27..494dba0c 100644 --- a/Packages/src/Runtime/Enums.cs +++ b/Packages/src/Runtime/Enums.cs @@ -76,6 +76,14 @@ public enum ShadowMode Mirror } + public enum PreferSamplingSize + { + None = 0, + x512 = 512, + x1024 = 1024, + x2048 = 2048 + } + internal static class BlendTypeConverter { public static (BlendMode, BlendMode) Convert(this (BlendType type, BlendMode src, BlendMode dst) self) diff --git a/Packages/src/Runtime/UIEffectProjectSettings.cs b/Packages/src/Runtime/UIEffectProjectSettings.cs index b9eabf7a..6f3901f0 100644 --- a/Packages/src/Runtime/UIEffectProjectSettings.cs +++ b/Packages/src/Runtime/UIEffectProjectSettings.cs @@ -10,6 +10,8 @@ namespace Coffee.UIEffects { public class UIEffectProjectSettings : PreloadedProjectSettings { + private static readonly int s_PreferSamplingSize = Shader.PropertyToID("_UIEffect_PreferSamplingSize"); + [Tooltip( "The sensitivity of the transformation when `Use Target Transform` is enabled in the `UIEffectReplica` component.")] [Header("Setting")] @@ -19,6 +21,9 @@ public class UIEffectProjectSettings : PreloadedProjectSettings m_RuntimePresets = new List(); + [SerializeField] + private PreferSamplingSize m_PreferSamplingSize = PreferSamplingSize.None; + [HideInInspector] [SerializeField] internal ShaderVariantCollection m_ShaderVariantCollection; @@ -27,6 +32,17 @@ public class UIEffectProjectSettings : PreloadedProjectSettings instance.m_PreferSamplingSize; + set + { + if (instance.m_PreferSamplingSize == value) return; + instance.m_PreferSamplingSize = value; + instance.SetPreferSamplingSize(); + } + } + public static ShaderVariantRegistry shaderRegistry => instance.m_ShaderVariantRegistry; public static ShaderVariantCollection shaderVariantCollection => shaderRegistry.shaderVariantCollection; @@ -62,6 +78,20 @@ public static UIEffect LoadRuntimePreset(string presetName) return null; } + protected override void OnEnable() + { + base.OnEnable(); + SetPreferSamplingSize(); + } + + private void SetPreferSamplingSize() + { + if (instance == this) + { + Shader.SetGlobalInt(s_PreferSamplingSize, (int)instance.m_PreferSamplingSize); + } + } + #if UNITY_EDITOR private const string k_PresetDir = "UIEffectPresets"; private const string k_PresetSaveDir = "Assets/ProjectSettings/" + k_PresetDir; @@ -82,6 +112,11 @@ private void Reset() m_ShaderVariantRegistry.InitializeIfNeeded(this, "(UIEffect)"); } + private void OnValidate() + { + SetPreferSamplingSize(); + } + internal static UIEffect[] LoadEditorPresets() { var dirs = AssetDatabase.FindAssets(k_PresetDir + " t:folder") diff --git a/Packages/src/Shaders/UIEffect.cginc b/Packages/src/Shaders/UIEffect.cginc index 3abc4699..8e542338 100644 --- a/Packages/src/Shaders/UIEffect.cginc +++ b/Packages/src/Shaders/UIEffect.cginc @@ -1,6 +1,7 @@ #ifndef UI_EFFECT_INCLUDED #define UI_EFFECT_INCLUDED +uniform int _UIEffect_PreferSamplingSize; uniform float4 _MainTex_TexelSize; uniform float _ToneIntensity; uniform half4 _ColorValue; @@ -44,7 +45,13 @@ uniform float _TargetSoftness; float2 texel_size() { - return _MainTex_TexelSize.xy * _SamplingScale; + float2 scale = _MainTex_TexelSize.xy * _SamplingScale; + if (0 < _UIEffect_PreferSamplingSize) + { + scale *= max(_MainTex_TexelSize.z, _MainTex_TexelSize.w) / _UIEffect_PreferSamplingSize; + } + + return scale; } float3 rgb_to_hsv(float3 c)