Skip to content

Commit f43254d

Browse files
committed
feat: add Raycast Method option for MaskingShape component
This option allows you to set raycast determination that differs from the masking method. close #262
1 parent d3070e3 commit f43254d

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

Packages/src/Editor/MaskingShapeEditor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class MaskingShapeEditor : Editor
1212
private SerializedProperty _maskingMethod;
1313
private SerializedProperty _showMaskGraphic;
1414
private SerializedProperty _softnessRange;
15+
private SerializedProperty _raycastMethod;
1516

1617
protected void OnEnable()
1718
{
@@ -20,6 +21,7 @@ protected void OnEnable()
2021
_alphaHitTest = serializedObject.FindProperty("m_AlphaHitTest");
2122
_antiAliasingThreshold = serializedObject.FindProperty("m_AntiAliasingThreshold");
2223
_softnessRange = serializedObject.FindProperty("m_SoftnessRange");
24+
_raycastMethod = serializedObject.FindProperty("m_RaycastMethod");
2325
}
2426

2527
public override void OnInspectorGUI()
@@ -52,6 +54,7 @@ public override void OnInspectorGUI()
5254
break;
5355
}
5456

57+
EditorGUILayout.PropertyField(_raycastMethod);
5558
serializedObject.ApplyModifiedProperties();
5659

5760
// Draw alpha hit test warning

Packages/src/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ scene or prefab.
346346
- ⚠️ Enable this only if necessary, as it will require more graphics memory and processing time.
347347
- **Softness Range** (SoftMasking only): Override the softness range of the parent `SoftMask` component.
348348
- **Anti Aliasing Threshold** (AntiAliasing only): Override the anti-alias threshold of the parent `SoftMask` component.
349+
- **Raycast Method**:
350+
- `Auto (Default)`: Same behavior as before. A mode similar to `Masking Method` is selected.
351+
- `Additive`: When the pointer is within this shape, the raycast is enabled.
352+
- `Subtract`: When the pointer is within this shape, the raycast is disabled.
353+
- `Ignore`: This shape is ignored in raycast determination.
349354

350355
`MaskingShape` component allows you to add or remove the masking region.
351356
Placing `MaskingShape` component (with any `Graphic`) under `SoftMask` component.

Packages/src/Runtime/MaskingShape/MaskingShape.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public enum MaskingMethod
2222
Subtract
2323
}
2424

25+
public enum RaycastMethod
26+
{
27+
Auto,
28+
Additive,
29+
Subtract,
30+
Ignore
31+
}
32+
2533
[Tooltip("Masking method.")]
2634
[SerializeField]
2735
private MaskingMethod m_MaskingMethod = MaskingMethod.Additive;
@@ -47,6 +55,10 @@ public enum MaskingMethod
4755
[SerializeField]
4856
private MinMax01 m_SoftnessRange = new MinMax01(0, 1f);
4957

58+
[Tooltip("Method to determine whether this masking shape should be a raycast target.")]
59+
[SerializeField]
60+
private RaycastMethod m_RaycastMethod = RaycastMethod.Auto;
61+
5062
private bool _antiAliasingRegistered;
5163
private MaskingShapeContainer _container;
5264
private Graphic _graphic;
@@ -136,6 +148,30 @@ public MinMax01 softnessRange
136148
}
137149
}
138150

151+
/// <summary>
152+
/// Method to determine whether this masking shape should be a raycast target.
153+
/// </summary>
154+
public RaycastMethod raycastMethod
155+
{
156+
get => parent != null ? parent.raycastMethod : m_RaycastMethod;
157+
set => m_RaycastMethod = value;
158+
}
159+
160+
/// <summary>
161+
/// Method to determine whether this masking shape should be a raycast target.
162+
/// </summary>
163+
public RaycastMethod actualRaycastMethod
164+
{
165+
get
166+
{
167+
return raycastMethod != RaycastMethod.Auto
168+
? raycastMethod
169+
: maskingMethod == MaskingMethod.Additive
170+
? RaycastMethod.Additive
171+
: RaycastMethod.Subtract;
172+
}
173+
}
174+
139175
protected override void OnEnable()
140176
{
141177
UpdateContainer();

Packages/src/Runtime/MaskingShape/MaskingShapeContainer.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,16 @@ public bool IsInside(Vector2 sp, Camera eventCamera, bool defaultValid = false,
106106
valid = defaultValid;
107107
for (var i = 0; i < m_MaskingShapes.Count; i++)
108108
{
109-
if (!m_MaskingShapes[i] || !m_MaskingShapes[i].IsInside(sp, eventCamera, threshold)) continue;
110-
switch (m_MaskingShapes[i].maskingMethod)
109+
var maskingShape = m_MaskingShapes[i];
110+
if (!maskingShape || !maskingShape.IsInside(sp, eventCamera, threshold)) continue;
111+
switch (maskingShape.actualRaycastMethod)
111112
{
112-
case MaskingShape.MaskingMethod.Additive:
113+
case MaskingShape.RaycastMethod.Additive:
113114
valid = true;
114115
break;
115-
case MaskingShape.MaskingMethod.Subtract:
116+
case MaskingShape.RaycastMethod.Subtract:
116117
valid = false;
117118
break;
118-
default:
119-
throw new ArgumentOutOfRangeException();
120119
}
121120
}
122121

Packages/src/Runtime/SoftMask.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public enum MaskingMode
116116

117117
public MaskingShape.MaskingMethod maskingMethod => MaskingShape.MaskingMethod.Additive;
118118

119+
public MaskingShape.RaycastMethod raycastMethod => MaskingShape.RaycastMethod.Additive;
120+
119121
/// <summary>
120122
/// Masking mode.<br />
121123
/// <b>SoftMasking</b>: Use RenderTexture as a soft mask buffer. The alpha of the masking graphic can be used.<br />

Packages/src/Runtime/Utilities/ISoftMasking.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,38 @@ namespace Coffee.UISoftMask
55
{
66
public interface ISoftMasking : IMeshModifier
77
{
8-
public bool enabled { get; }
8+
bool enabled { get; }
99

10-
public MaskingShape.MaskingMethod maskingMethod { get; }
10+
MaskingShape.MaskingMethod maskingMethod { get; }
1111

1212
/// <summary>
1313
/// Show the graphic that is associated with the Mask render area.
1414
/// </summary>
15-
public bool showMaskGraphic { get; }
15+
bool showMaskGraphic { get; }
1616

1717
/// <summary>
1818
/// The transparent part of the mask cannot be clicked.
1919
/// This can be achieved by enabling Read/Write enabled in the Texture Import Settings for the texture.
2020
/// <para></para>
2121
/// NOTE: Enable this only if necessary, as it will require more graphics memory and processing time.
2222
/// </summary>
23-
public bool alphaHitTest { get; }
23+
bool alphaHitTest { get; }
2424

2525
/// <summary>
2626
/// Threshold for anti-alias masking.
2727
/// The smaller this value, the less jagged it is.
2828
/// </summary>
29-
public float antiAliasingThreshold { get; }
29+
float antiAliasingThreshold { get; }
3030

3131
/// <summary>
3232
/// The minimum and maximum alpha values used for soft masking.
3333
/// The larger the gap between these values, the stronger the softness effect.
3434
/// </summary>
35-
public MinMax01 softnessRange { get; }
35+
MinMax01 softnessRange { get; }
36+
37+
/// <summary>
38+
/// Method to determine whether this masking shape should be a raycast target.
39+
/// </summary>
40+
MaskingShape.RaycastMethod raycastMethod { get; }
3641
}
3742
}

0 commit comments

Comments
 (0)