GitHub - mob-sakai/SoftMaskForUGUI: Enhance Unity UI (uGUI) with advanced soft-masking features to create more visually appealing effects! (original) (raw)



PRs Welcome

<< 📝 Description | 📌 Key Features | 🎮 Demo | ⚙ Installation | 🚀 Usage | 🤝 Contributing >>

📝 Description

Enhance Unity UI (uGUI) with advanced soft-masking features to create more visually appealing effects!

📌 Key Features

🎮 Demo

WebGL Demo

⚙ Installation

This package requires Unity 2019.4 or later.

Install via OpenUPM

openupm add com.coffee.softmask-for-ugui  
openupm add com.coffee.softmask-for-ugui@3.2.0  

Install via UPM (with Package Manager UI)

Install via UPM (Manually)

Install as Embedded Package

  1. Download the Source code (zip) file from Releases and extract it.
  2. Move the <extracted_dir>/Packages/src directory into your project's Packages directory.
    • You can rename the src directory if needed.
    • If you intend to fix bugs or add features, installing it as an embedded package is recommended.
    • To update the package, re-download it and replace the existing contents.

Import Additional Resources

Additional resources can be imported to extend functionality.

🔄 Upgrade All Assets For V3

If you are currently using SoftMaskForUGUI v1.x/v2.x, the following breaking changes are included when upgrading to v3:

  1. (From v1) API changes: Some APIs are obsolete.
    • SoftMask.alpha: Use Graphic.color.a instead.
    • SoftMask.softness: Use SoftMask.softnessRange instead.
    • SoftMask.partOfParent: Use MaskingShape component instead.
    • SoftMask.ignoreParent: Removed.
    • SoftMask.ignoreSelfGraphic: Removed.
    • SoftMask.ignoreSelfStencil: Removed.
    • SoftMaskable.useStencil: Removed.
    • SoftMaskable.raycastFilter: Use SoftMask.alphaHitTest instead.
    • SoftMaskable.maskInteraction: If you want to use as inverse mask, use MaskingShape component andMaskingMethod=Subtract.
  2. (From v1) SoftMaskable component: SoftMaskable component is no longer required to be added explicitly.
    • It will be added automatically at runtime as needed.
  3. (From v1) SoftMaskable shader: SoftMask() function has been updated with additional arguments.
    // Before
    color.a *= SoftMask(IN.vertex, IN.worldPosition);
    // After
    color.a *= SoftMask(IN.vertex, IN.worldPosition, color.a);
  4. (From v2) SoftMaskable shader: SOFTMASKABLE shader feature is now required.

#pragma shader_feature_local _ SOFTMASKABLE 5. If you are installing via git URL, add ?path=Packages/src.
// v1/v2
"com.coffee.softmask-for-ugui": "https://github.com/mob-sakai/SoftMaskForUGUI.git",
// v3
"com.coffee.softmask-for-ugui": "https://github.com/mob-sakai/SoftMaskForUGUI.git?path=Packages/src", 6. Hidden/UI/SoftMask, Hidden/UI/TerminalMaskingShape and SoftMaskable shader variants used at runtime must be registered in the Project Settings.

To apply these changes automatically, please follow the steps below:

  1. Click Edit > Project Settings to open the Project Settings window and select UI > SoftMask category.
  2. Click Upgrade All Assets For V3 to upgrade the assets.

🚀 Usage

Getting Started

  1. Install the package.
  2. Add a SoftMask component instead of Mask component.
    Or, convert an existing Mask component to SoftMask component from the context menu (Convert To SoftMask).
  3. Adjust the soft mask parameters in the inspector.
  4. (Optional) By placing the MaskingShape component under SoftMask, you can add or remove the masking region.
  5. Enjoy!

RectMask2D vs SoftMask

RectMask2D is a built-in component that supports soft masking.
SoftMask provides more advanced soft masking.

Comparison of Masking Mode

Component: SoftMask

Component: SoftMaskable

SoftMaskable components are added automatically to GameObjects under a SoftMask component.
If the properties are set to their default values, it is marked as HideFlag.DontSave and will not be saved in the scene or prefab.

Component: MaskingShape

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

You can use it not only with SoftMask component but also with Mask component.
If the MaskingMode is AntiAliasing or Normal, or if you are using the Mask component, the MaskingShapecomponent must be placed above the masked Graphic in the hierarchy. This is a limitation based on the stencil mask.
The available features depend on the Masking Mode.

Component: RectTransformFitter

RectTransformFitter component follows the target RectTransform.
You can specify the properties to follow (position, rotation, scale, delta size) withRectTransformFitter.targetProperties.
By combining it with the MaskingShape component, you can implement an effect that displays only the buttons during the tutorial.

Project Settings

You can adjust the project-wide settings for SoftMaskForUGUI. (Edit > Project Settings > UI > Soft Mask)

Important

Usage with Scripts

var softMask = gameObject.GetComponent(); softMask.maskingMode = SoftMask.MaskingMode.SoftMasking; softMask.downSamplingRate = SoftMask.DownSamplingRate.x2; softMask.softnessRange = new MinMax01(0.5f, 0.75f);

Usage with TextMeshPro or Spine

To use SoftMask with TextMeshPro or Spine, you need to import additional resources.
When a shader included in the samples is requested, an import dialog will automatically appear.
Click the Import button.

Alternatively, you can manually import the resources by following these steps:

  1. Open the Package Manager window and select the UI Soft Mask package from the package list.
  2. Click the Import button for each sample to import the required resources.
    • TextMeshPro (Unity 2023.1 or earlier): TextMeshPro Support
    • TextMeshPro (Unity 2023.2, 6000.0 or later): TextMeshPro Support (Unity 6)
    • Spine: Spine Support
  3. The assets will be imported under Assets/Samples/UI Soft Mask/{version}.

Tip

If you have moved TMPro_Properties.cginc and TMPro.cginc from their default install path (Assets/TextMesh Pro/Shaders/...), you will need to manually update the paths in the shaders underTextMeshPro Support or TextMeshPro Support (Unity 6).

Usage with Your Custom Shaders

Here, let's make UI/Additivecustom shader soft-maskable.
There are two ways to support SoftMask with custom shaders.

#include "Packages/com.coffee.softmask-for-ugui/Shaders/SoftMask.cginc"
#pragma shader_feature _ SOFTMASK_EDITOR
#pragma shader_feature_local _ SOFTMASKABLE
// "SoftMask" function returns [0-1]. Multiply this by the final output.
color.a *= SoftMask(IN.vertex, IN.worldPosition, color.a);

#include "Packages/com.coffee.softmask-for-ugui/Shaders/SoftMask.cginc"
#pragma shader_feature _ SOFTMASK_EDITOR
#pragma shader_feature_local _ SOFTMASKABLE
// "SoftMask" function returns [0-1]. Multiply this by the final output.
color.a *= SoftMask(IN.vertex, IN.worldPosition, color.a);

Usage with Canvas ShaderGraph

NOTE: Unity 2023.2/6000.0+ is required.

  1. Open the Package Manager window and select the UI Soft Mask package in the package list and click theShaderGraph 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 and SOFTMASKABLE as a Boolean Keyword (Shader Feature).
    3. Add the Sub Graphs > SoftMask node and connect it to the final alpha output.

Usage with UIEffect

UIEffect is a package that allows you to intuitively apply rich Unity UI effects.

SoftMaskForUGUI (v3.3.0+) supports UIEffect (v5.6.0+).
When a shader included in the samples is requested, an import dialog will automatically appear.
Click the Import button.

⚠️ Limitations

The following are the limitations of SoftMaskForUGUI.

🤝 Contributing

Issues

Issues are incredibly valuable to this project:

Pull Requests

Pull requests offer a fantastic way to contribute your ideas to this repository.
Please refer to CONTRIBUTING.mdand develop branch.

Support

This is an open-source project developed during my spare time.
If you appreciate it, consider supporting me.
Your support allows me to dedicate more time to development. 😊


License

Author

See Also