diff --git a/Packages/src/README.md b/Packages/src/README.md index 986b8e8..967135b 100644 --- a/Packages/src/README.md +++ b/Packages/src/README.md @@ -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) @@ -379,6 +380,22 @@ Here, let's make [UI/Additive](https://raw.githubusercontent.com/mob-sakai/SoftM

+### 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) + +

+ ### :warning: Limitations The following are the limitations of SoftMaskForUGUI. @@ -386,6 +403,7 @@ 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.

diff --git a/Packages/src/Shaders/SoftMask.cginc b/Packages/src/Shaders/SoftMask.cginc index 1475af8..b98408c 100644 --- a/Packages/src/Shaders/SoftMask.cginc +++ b/Packages/src/Shaders/SoftMask.cginc @@ -4,7 +4,7 @@ uniform sampler2D _SoftMaskTex; uniform half4 _SoftMaskColor; uniform float _AlphaClipThreshold; -uniform fixed4 _SoftMaskOutsideColor; +uniform float4 _SoftMaskOutsideColor; uniform int _SoftMaskableEnable; uniform int _SoftMaskableStereo; uniform float4x4 _GameVP; @@ -12,7 +12,7 @@ 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( @@ -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); } @@ -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(_) diff --git a/Packages/src/package.json b/Packages/src/package.json index aa1ad5f..a0e6246 100644 --- a/Packages/src/package.json +++ b/Packages/src/package.json @@ -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" } ] }