SoftMaskForUGUI Documentation

repository·main·Indexed 25 days ago

https://github.com/mob-sakai/softmaskforugui

A Unity package (com.coffee.softmask-for-ugui) providing a smooth masking component for uGUI elements. It enables rounded edges, semi-transparent edges, nested masks, and various masking shapes via the SoftMask, SoftMaskable, and MaskingShape components. Supports three masking modes: SoftMasking, AntiAliasing, and Normal. Includes integration for TextMeshPro and Spine, as well as tools for creating custom soft-maskable shaders.

Tokens
3.1K
Snippets
6
Records
17
Agent score
34%

What's inside SoftMaskForUGUI

  1. Compare RectMask2D and SoftMask

    main

    Choose between the built-in RectMask2D and SoftMask based on your requirements:

    FeatureRectMask2DSoftMask
    TypeuGUI built-inAdvanced soft masking
    PerformanceVery fastRequires RenderTexture/Shader
    ShapesRectangular onlyAny graphic shape
    Nesting1 level onlyUp to 4 levels
    RotationLimitedFully supported
  2. Install SoftMaskForUGUI via UPM Package Manager UI

    main

    You can install the package directly through the Unity Package Manager:

    1. Open Window > Package Manager.
    2. Click the + button and select Add package from git URL....
    3. Enter the following URL: https://github.com/mob-sakai/SoftMaskForUGUI.git?path=Packages/src

    To target a specific version, append #version to the end of the URL (e.g., #3.2.0).

    https://github.com/mob-sakai/SoftMaskForUGUI.git?path=Packages/src
  3. Get Started with SoftMaskForUGUI

    main

    To use SoftMaskForUGUI in your project:

    1. Install the package via OpenUPM, UPM, or as an embedded package.
    2. Add a SoftMask component to a GameObject instead of the standard Unity Mask component. You can also convert an existing Mask component to SoftMask via the context menu using Convert To SoftMask.
    3. Adjust parameters in the Inspector to achieve the desired masking effect.
    4. (Optional) Add MaskingShape: Place a MaskingShape component under the SoftMask component to add or remove specific masking regions.
  4. Import Additional Resources for TextMeshPro or Spine

    main

    To use SoftMask with TextMeshPro or Spine, you must import specific support assets:

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

    Note: If you have moved TMPro_Properties.cginc or TMPro.cginc from their default Unity paths, you must manually update the paths in the imported shaders.

  5. Hot-Update Shader Variants in Build

    main

    To hot-update shader variants and presets in a build, enable Pre Load Settings In Build in the Project Settings. This adds the settings asset to PlayerSettings.preloadedAssets.

    If disabled, you must load the settings via Resources, AssetBundles, or Addressables. You can use the following pattern to load and warm up variants:

    private static IEnumerator HotUpdateCoroutine()
    {
        const string k_SettingsAddress = "Assets/ProjectSettings/UISoftMaskProjectSettings.asset";
        yield return Addressables.LoadAssetAsync<UISoftMaskProjectSettings>(k_SettingsAddress);
        while (UISoftMaskProjectSettings.shaderVariantCollection.WarmUpProgressively(5) == false) yield return null;
    }
  6. Make Custom Shaders SoftMaskable

    main

    You can make custom shaders compatible with SoftMask using two methods:

    Add soft-maskable variants to your existing shader.

    • Add the (SoftMaskable) suffix to the shader name.
    • Include Packages/com.coffee.softmask-for-ugui/Shaders/SoftMask.cginc.
    • Add #pragma shader_feature _ SOFTMASK_EDITOR and #pragma shader_feature_local _ SOFTMASKABLE.
    • Multiply the final alpha by the SoftMask function.
    // Add the ` (SoftMaskable)` suffix to the shader name.
    Shader "UI/Additive (SoftMaskable)"
    
    // Import "SoftMask.cginc" and add shader features.
    #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);

    2. Separate Method

    Create a new shader specifically for soft masking. This is useful for built-in shaders that cannot be edited.

    • Add the Hidden/ prefix and the (SoftMaskable) suffix to the shader name.
    • Follow the same include and pragma steps as the Hybrid method.
  7. Upgrade SoftMaskForUGUI from v1/v2 to v3

    main

    If you are upgrading from SoftMaskForUGUI v1.x or v2.x to v3, several breaking changes apply to APIs, shaders, and installation methods.

    API Changes (from v1)

    • SoftMask.alpha $\rightarrow$ Use Graphic.color.a.
    • SoftMask.softness $\rightarrow$ Use SoftMask.softnessRange.
    • SoftMask.partOfParent $\rightarrow$ Use MaskingShape component.
    • SoftMask.ignoreParent, SoftMask.ignoreSelfGraphic, and SoftMask.ignoreSelfStencil are removed.
    • SoftMaskable.useStencil is removed.
    • SoftMaskable.raycastFilter $\rightarrow$ Use SoftMask.alphaHitTest.
    • SoftMaskable.maskInteraction $\rightarrow$ For inverse masks, use MaskingShape component with MaskingMethod=Subtract.

    Component and Shader Changes

    • SoftMaskable component: No longer needs to be added explicitly; it is added automatically at runtime.
    • SoftMask() shader function: Now requires an additional argument.
      • Before: color.a *= SoftMask(IN.vertex, IN.worldPosition);
      • After: color.a *= SoftMask(IN.vertex, IN.worldPosition, color.a);
    • SOFTMASKABLE shader feature: Now required in shaders via #pragma shader_feature_local _ SOFTMASKABLE.

    Installation and Build Changes

    • Git URL: If installing via Git, you must append ?path=Packages/src to the URL.
    • Shader Registration: Hidden/UI/SoftMask, Hidden/UI/TerminalMaskingShape, and SoftMaskable shader variants must be registered in Project Settings > UI > SoftMask. If masks do not display correctly in the player, open/play the relevant scenes or prefabs in the editor to trigger automatic registration.

    Automatic Upgrade Process

    You can automate these changes using the built-in upgrade tool:

    1. Go to Edit > Project Settings.
    2. Select the UI > SoftMask category.
    3. Click Upgrade All Assets For V3.
      • Note: Use Dry Run to preview changes before applying them.
  8. Install SoftMaskForUGUI via OpenUPM

    main

    The preferred installation method is via the OpenUPM registry, which allows for easy updates. If you have the openupm-cli installed, run the following command in your project's directory to add the package.

    To update the package to a specific version, use the @version syntax.

    openupm add com.coffee.softmask-for-ugui
    
    # To update to a specific version
    openupm add com.coffee.softmask-for-ugui@3.2.0
  9. Configure Project Settings for SoftMaskForUGUI

    main

    Access settings via Edit > Project Settings > UI > Soft Mask. The settings file is typically saved at Assets/ProjectSettings/UISoftMaskProjectSettings.asset and should be included in version control.

    Core Settings:

    • Soft Mask Enabled: Enables SoftMasking mode. If disabled, SoftMasking behaves as Normal.
    • Stereo Enabled: Enables VR mode.
    • Transform Sensitivity: Low, Medium, or High. Adjusts how frequently the soft mask buffer updates during transformations.
    • Soft Maskable:
      • Automatic: Adds SoftMaskable components automatically.
      • Manual: Requires explicit addition of SoftMaskable components.

    Shader Settings:

    • Optional Shaders (SoftMaskable): A prioritized list of shaders to use when a soft-maskable shader is requested.
    • Registered/Unregistered Variants: Manage shader variants to optimize build size and ensure runtime availability. If Error On Unregistered Variant is enabled, unregistered variants are automatically added to the list.