Skip to content

Commit

Permalink
feat: ShaderGraph support
Browse files Browse the repository at this point in the history
Unity 2023.2/600.0+ is required.

close #190
  • Loading branch information
mob-sakai committed Sep 4, 2024
1 parent b1331cf commit c4ebe58
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
18 changes: 18 additions & 0 deletions Packages/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Enhance Unity UI (uGUI) with advanced soft-masking features to create more visua
- [Usage with Scripts](#usage-with-scripts)
- [Usage with TextMeshPro](#usage-with-textmeshpro)
- [Usage with Your Custom Shaders](#usage-with-your-custom-shaders)
- [Usage with ShaderGraph](#usage-with-shadergraph)
- [:warning: Limitations](#warning-limitations)
- [🤝 Contributing](#-contributing)
- [Issues](#issues)
Expand Down Expand Up @@ -379,13 +380,30 @@ Here, let's make [UI/Additive](https://raw.githubusercontent.com/mob-sakai/SoftM
<br><br>
### Usage with ShaderGraph
NOTE: Unity 2023.2/600.0+ is required.
1. Open the `Package Manager` window and select the `UI Soft Mask` package in the package list and click the `ShaderGraph Support > Import` button.
2. The sample includes `UIDefault (SoftMaskable).shadergraph` and `SoftMask.subshadergraph`.
You can use the sample as references to make your own shader graph compatible with SoftMask.
1. Add `(SoftMaskable)` at the end of the shader name.
2. Add `SOFTMASK_EDITOR` as a `Boolean Keyword (Shader Feature)`.
3. Add the `Sub Graphs > SoftMask` node and connect it to the final alpha output.
![](https://github.com/user-attachments/assets/8da64af8-a4e2-4477-a253-c45fe11d3eec)
<br><br>
### :warning: Limitations
The following are the limitations of SoftMaskForUGUI.
- (Android) `RGB ETC1 (+ Split alpha channel)` texture format is not supported.
- Use a format that supports alpha, such as `RGBA ETC2`.
- Technically possible, but not supported because [ETC2 support rate is over 95%](https://developer.android.com/guide/playcore/asset-delivery/texture-compression).
- If needed, feel free to create an issue.
<br><br>
Expand Down
21 changes: 15 additions & 6 deletions Packages/src/Shaders/SoftMask.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
uniform sampler2D _SoftMaskTex;
uniform half4 _SoftMaskColor;
uniform float _AlphaClipThreshold;
uniform fixed4 _SoftMaskOutsideColor;
uniform float4 _SoftMaskOutsideColor;
uniform int _SoftMaskableEnable;
uniform int _SoftMaskableStereo;
uniform float4x4 _GameVP;
uniform float4x4 _GameTVP;
uniform float4x4 _GameVP_2;
uniform float4x4 _GameTVP_2;

fixed Approximately(float4x4 a, float4x4 b)
float Approximately(float4x4 a, float4x4 b)
{
float4x4 d = abs(a - b);
return step(
Expand All @@ -23,19 +23,19 @@ fixed Approximately(float4x4 a, float4x4 b)
0.1);
}

float2 WorldToUv(float4 worldPos)
float2 WorldToUv(float4 worldPos, float offset)
{
worldPos = mul(unity_ObjectToWorld, worldPos);
float4x4 gameVp = lerp(_GameVP, _GameVP_2, unity_StereoEyeIndex);
float4x4 gameTvp = lerp(_GameTVP, _GameTVP_2, unity_StereoEyeIndex);

fixed isSceneView = 1 - Approximately(UNITY_MATRIX_VP, gameVp);
float isSceneView = 1 - Approximately(UNITY_MATRIX_VP, gameVp);

float4 clipPos = mul(UNITY_MATRIX_VP, worldPos);
float4 clipPosG = mul(gameTvp, worldPos);
return lerp(
clipPos.xy / clipPos.w * 0.5 + 0.5,
clipPosG.xy / clipPosG.w * 0.5 + 0.5,
clipPosG.xy / clipPosG.w * 0.5 + offset,
isSceneView);
}

Expand Down Expand Up @@ -88,10 +88,19 @@ float SoftMaskSample(float2 uv, float a)
return alpha.x * alpha.y * alpha.z * alpha.w;
}

void SoftMaskForGraph_float(float4 ScreenPos, float4 WorldPos, float InAlpha, out float A)
{
#if SOFTMASK_EDITOR
A = SoftMaskSample(WorldToUv(WorldPos, 0), InAlpha) * InAlpha;
#else
A = SoftMaskSample(ScreenPos.xy, 1) * InAlpha;
#endif
}

#if SOFTMASK_EDITOR
#define EDITOR_ONLY(x) x
#define SOFTMASK_EDITOR_ONLY(x) x
#define SoftMask(_, worldPos, alpha) SoftMaskSample(WorldToUv(worldPos), alpha)
#define SoftMask(_, worldPos, alpha) SoftMaskSample(WorldToUv(worldPos, 0.5), alpha)
#else
#define EDITOR_ONLY(_)
#define SOFTMASK_EDITOR_ONLY(_)
Expand Down
5 changes: 5 additions & 0 deletions Packages/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"displayName": "TextMeshPro Support (ugui 2.0)",
"description": "TextMeshPro Support (ugui 2.0)",
"path": "Samples~/TextMeshPro Support (ugui 2.0)~"
},
{
"displayName": "ShaderGraph Support",
"description": "ShaderGraph Support",
"path": "Samples~/ShaderGraph Support"
}
]
}

0 comments on commit c4ebe58

Please sign in to comment.