Skip to content

Commit

Permalink
feat: global prefer sampling size
Browse files Browse the repository at this point in the history
close #269
  • Loading branch information
mob-sakai committed Dec 13, 2024
1 parent f1ce949 commit f566791
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Packages/src/Editor/UIEffectProjectSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class UIEffectProjectSettingsEditor : Editor
{
private ReorderableList _reorderableList;
private SerializedProperty _transformSensitivity;
private SerializedProperty _preferSamplingSize;
private bool _isInitialized;
private ShaderVariantRegistryEditor _shaderVariantRegistryEditor;

Expand All @@ -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");
Expand Down Expand Up @@ -64,6 +66,7 @@ public override void OnInspectorGUI()

// Settings
EditorGUILayout.PropertyField(_transformSensitivity);
EditorGUILayout.PropertyField(_preferSamplingSize);
_reorderableList.DoLayoutList();

// Shader registry
Expand Down
8 changes: 8 additions & 0 deletions Packages/src/Runtime/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions Packages/src/Runtime/UIEffectProjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Coffee.UIEffects
{
public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectSettings>
{
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")]
Expand All @@ -19,6 +21,9 @@ public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectS
[SerializeField]
internal List<UIEffect> m_RuntimePresets = new List<UIEffect>();

[SerializeField]
private PreferSamplingSize m_PreferSamplingSize = PreferSamplingSize.None;

[HideInInspector]
[SerializeField]
internal ShaderVariantCollection m_ShaderVariantCollection;
Expand All @@ -27,6 +32,17 @@ public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectS
[SerializeField]
private ShaderVariantRegistry m_ShaderVariantRegistry = new ShaderVariantRegistry();

public static PreferSamplingSize preferSamplingSize
{
get => 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;
Expand Down Expand Up @@ -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;
Expand All @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion Packages/src/Shaders/UIEffect.cginc
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f566791

Please sign in to comment.