Skip to content

Commit 939e9a7

Browse files
committed
feat: global prefer sampling size
close #269
1 parent 2b43bc4 commit 939e9a7

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

Packages/src/Editor/UIEffectProjectSettingsEditor.cs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class UIEffectProjectSettingsEditor : Editor
1010
{
1111
private ReorderableList _reorderableList;
1212
private SerializedProperty _transformSensitivity;
13+
private SerializedProperty _preferSamplingSize;
1314
private bool _isInitialized;
1415
private ShaderVariantRegistryEditor _shaderVariantRegistryEditor;
1516

@@ -18,6 +19,7 @@ private void InitializeIfNeeded()
1819
if (_isInitialized) return;
1920

2021
_transformSensitivity = serializedObject.FindProperty("m_TransformSensitivity");
22+
_preferSamplingSize = serializedObject.FindProperty("m_PreferSamplingSize");
2123
var runtimePresets = serializedObject.FindProperty("m_RuntimePresets");
2224
_reorderableList = new ReorderableList(serializedObject, runtimePresets, true, true, true, true);
2325
_reorderableList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Runtime Presets");
@@ -64,6 +66,7 @@ public override void OnInspectorGUI()
6466

6567
// Settings
6668
EditorGUILayout.PropertyField(_transformSensitivity);
69+
EditorGUILayout.PropertyField(_preferSamplingSize);
6770
_reorderableList.DoLayoutList();
6871

6972
// Shader registry

Packages/src/Runtime/Enums.cs

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public enum ShadowMode
7676
Mirror
7777
}
7878

79+
public enum PreferSamplingSize
80+
{
81+
None = 0,
82+
x512 = 512,
83+
x1024 = 1024,
84+
x2048 = 2048
85+
}
86+
7987
internal static class BlendTypeConverter
8088
{
8189
public static (BlendMode, BlendMode) Convert(this (BlendType type, BlendMode src, BlendMode dst) self)

Packages/src/Runtime/UIEffectProjectSettings.cs

+35
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Coffee.UIEffects
1010
{
1111
public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectSettings>
1212
{
13+
private static readonly int s_PreferSamplingSize = Shader.PropertyToID("_UIEffect_PreferSamplingSize");
14+
1315
[Tooltip(
1416
"The sensitivity of the transformation when `Use Target Transform` is enabled in the `UIEffectReplica` component.")]
1517
[Header("Setting")]
@@ -19,6 +21,9 @@ public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectS
1921
[SerializeField]
2022
internal List<UIEffect> m_RuntimePresets = new List<UIEffect>();
2123

24+
[SerializeField]
25+
private PreferSamplingSize m_PreferSamplingSize = PreferSamplingSize.None;
26+
2227
[HideInInspector]
2328
[SerializeField]
2429
internal ShaderVariantCollection m_ShaderVariantCollection;
@@ -27,6 +32,17 @@ public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectS
2732
[SerializeField]
2833
private ShaderVariantRegistry m_ShaderVariantRegistry = new ShaderVariantRegistry();
2934

35+
public static PreferSamplingSize preferSamplingSize
36+
{
37+
get => instance.m_PreferSamplingSize;
38+
set
39+
{
40+
if (instance.m_PreferSamplingSize == value) return;
41+
instance.m_PreferSamplingSize = value;
42+
instance.SetPreferSamplingSize();
43+
}
44+
}
45+
3046
public static ShaderVariantRegistry shaderRegistry => instance.m_ShaderVariantRegistry;
3147

3248
public static ShaderVariantCollection shaderVariantCollection => shaderRegistry.shaderVariantCollection;
@@ -62,6 +78,20 @@ public static UIEffect LoadRuntimePreset(string presetName)
6278
return null;
6379
}
6480

81+
protected override void OnEnable()
82+
{
83+
base.OnEnable();
84+
SetPreferSamplingSize();
85+
}
86+
87+
private void SetPreferSamplingSize()
88+
{
89+
if (instance == this)
90+
{
91+
Shader.SetGlobalInt(s_PreferSamplingSize, (int)instance.m_PreferSamplingSize);
92+
}
93+
}
94+
6595
#if UNITY_EDITOR
6696
private const string k_PresetDir = "UIEffectPresets";
6797
private const string k_PresetSaveDir = "Assets/ProjectSettings/" + k_PresetDir;
@@ -82,6 +112,11 @@ private void Reset()
82112
m_ShaderVariantRegistry.InitializeIfNeeded(this, "(UIEffect)");
83113
}
84114

115+
private void OnValidate()
116+
{
117+
SetPreferSamplingSize();
118+
}
119+
85120
internal static UIEffect[] LoadEditorPresets()
86121
{
87122
var dirs = AssetDatabase.FindAssets(k_PresetDir + " t:folder")

Packages/src/Shaders/UIEffect.cginc

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef UI_EFFECT_INCLUDED
22
#define UI_EFFECT_INCLUDED
33

4+
uniform int _UIEffect_PreferSamplingSize;
45
uniform float4 _MainTex_TexelSize;
56
uniform float _ToneIntensity;
67
uniform half4 _ColorValue;
@@ -42,7 +43,13 @@ uniform float _TargetSoftness;
4243

4344
float2 texel_size()
4445
{
45-
return _MainTex_TexelSize.xy * _SamplingScale;
46+
float2 scale = _MainTex_TexelSize.xy * _SamplingScale;
47+
if (0 < _UIEffect_PreferSamplingSize)
48+
{
49+
scale *= max(_MainTex_TexelSize.z, _MainTex_TexelSize.w) / _UIEffect_PreferSamplingSize;
50+
}
51+
52+
return scale;
4653
}
4754

4855
float3 rgb_to_hsv(float3 c)

0 commit comments

Comments
 (0)