Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Sep 25, 2024
1 parent e7f4539 commit e49dc30
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
4 changes: 3 additions & 1 deletion Packages/src/Runtime/SoftMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public enum MaskingMode
internal RenderTexture _softMaskBuffer;
private UnityAction _updateParentSoftMask;
private CanvasViewChangeTrigger _viewChangeTrigger;
private List<SoftMask> children => _children != null ? _children : _children = UISoftMaskInternal.ListPool<SoftMask>.Rent();

private List<SoftMask> children =>
_children != null ? _children : _children = UISoftMaskInternal.ListPool<SoftMask>.Rent();

/// <summary>
/// Masking mode.<br />
Expand Down
8 changes: 8 additions & 0 deletions Packages/src/Samples~/Demo.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Packages/src/Samples~/Demo/Scripts/MinMax01Slider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ private void UpdateDrag(PointerEventData eventData, Camera cam)
{
if (!m_MinSlider || !m_MaxSlider || !m_Background) return;


var axisIndex = (int)m_MinSlider.direction / 2;
var fillSize = m_Background.rect.size[axisIndex];
if (fillSize <= 0) return;
Expand Down Expand Up @@ -187,7 +186,6 @@ public class MinMax01SliderEvent : UnityEvent<MinMax01>
{
}


#if UNITY_EDITOR
protected override void OnValidate()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ private IEnumerator Co_Move(Transform t)
}
}


private IEnumerator Co_Tween(float from, float to, float duration, Action<float> callback)
{
var value = from;
Expand Down
8 changes: 8 additions & 0 deletions Packages/src/Samples~/ShaderGraph Support.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 17 additions & 18 deletions Packages/src/Shaders/SoftMask.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ uniform int _AllowDynamicResolution;

float Approximately(float4x4 a, float4x4 b)
{
float4x4 d = abs(a - b);
return step(
max(d._m00,max(d._m01,max(d._m02,max(d._m03,
max(d._m10,max(d._m11,max(d._m12,max(d._m13,
max(d._m20,max(d._m21,max(d._m22,max(d._m23,
max(d._m30,max(d._m31,max(d._m32,d._m33))))))))))))))),
0.1);
const float4x4 d = abs(a - b);
const float max0 = max(d._m00, max(d._m01, max(d._m02, d._m03)));
const float max1 = max(d._m10, max(d._m11, max(d._m12, d._m13)));
const float max2 = max(d._m20, max(d._m21, max(d._m22, d._m23)));
const float max3 = max(d._m30, max(d._m31, max(d._m32, d._m33)));
return step(max(max0, max(max1, max(max2, max3))), 0.1);
}

float2 WorldToUv(float4 worldPos, float offset)
Expand All @@ -49,7 +48,7 @@ float2 WorldToUv(float4 worldPos, float offset)
float2 ScreenToUv(const float2 screenPos)
{
half2 uv = screenPos;
#if UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION
#if UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION
float ratio = _ScreenParams.x / _ScreenParams.y;
switch (UNITY_DISPLAY_ORIENTATION_PRETRANSFORM)
{
Expand All @@ -60,18 +59,18 @@ float2 ScreenToUv(const float2 screenPos)
case UNITY_DISPLAY_ORIENTATION_PRETRANSFORM_270:
return half2((uv.y + ratio - 1) / ratio, 1 - uv.x * ratio);
}
#endif
#endif

return uv;
}

float2 ClipToUv(const float2 clipPos)
{
half2 screenPos = clipPos / _ScreenParams.xy;
#if UNITY_UV_STARTS_AT_TOP
#if UNITY_UV_STARTS_AT_TOP
if (0 <= _ProjectionParams.x)
screenPos.y = 1 - screenPos.y;
#endif
#endif
return ScreenToUv(screenPos);
}

Expand Down Expand Up @@ -101,10 +100,10 @@ float SoftMaskSample(float2 uv, float a)
half4 alpha = saturate(lerp(half4(1, 1, 1, 1),
lerp(mask, half4(1, 1, 1, 1) - mask, _SoftMaskColor - half4(1, 1, 1, 1)),
_SoftMaskColor));
#if SOFTMASK_EDITOR
#if SOFTMASK_EDITOR
int inScreen = step(0, uv.x) * step(uv.x, 1) * step(0, uv.y) * step(uv.y, 1);
alpha = lerp(_SoftMaskOutsideColor, alpha, inScreen);
#ifdef UNITY_UI_ALPHACLIP
#ifdef UNITY_UI_ALPHACLIP
if (_MaskingShapeSubtract == 1)
{
if (_SoftMaskInSceneView == 1)
Expand All @@ -114,15 +113,15 @@ float SoftMaskSample(float2 uv, float a)
}
clip (a * alpha.x * alpha.y * alpha.z * alpha.w - _AlphaClipThreshold - 0.001);
}
#endif
#endif
#endif
#endif

return alpha.x * alpha.y * alpha.z * alpha.w;
}

void SoftMaskForGraph_float(float4 ScreenPos, float4 WorldPos, float InAlpha, out float A)
{
#if SOFTMASK_EDITOR
#if SOFTMASK_EDITOR
if (_SoftMaskInGameView == 1)
{
A = SoftMaskSample(ScreenToUv(ScreenPos.xy), 1) * InAlpha;
Expand All @@ -135,9 +134,9 @@ void SoftMaskForGraph_float(float4 ScreenPos, float4 WorldPos, float InAlpha, ou
{
A = SoftMaskSample(WorldToUv(WorldPos, 0.5), InAlpha) * InAlpha;
}
#else
#else
A = SoftMaskSample(ScreenToUv(ScreenPos.xy), 1) * InAlpha;
#endif
#endif
}

#if SOFTMASK_EDITOR
Expand Down

0 comments on commit e49dc30

Please sign in to comment.