Search Unity

Soft Mask - A replacement of Unity UI Mask with alpha support

Discussion in 'Assets and Asset Store' started by zxkne, Feb 4, 2017.

  1. Jlempernesse

    Jlempernesse

    Joined:
    Nov 9, 2016
    Posts:
    3
    Hi, and thanks for your tool. I'm trying tu using it, but it's not working. So I'm disapointed. Is Soft Mask working only with Ui elements ?
    I need to soft mask 2D sprites on my game.
    Thanks for the help. ^^
     
  2. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hi,
    Sorry to disappoint you, but yes, Soft Mask work only with UI elements. This stated explicitly on the product page and in the documentation. If you don't want to use Soft Mask any more and want to get a refund, please, write me the invoice number in PM or email.
     
  3. Jlempernesse

    Jlempernesse

    Joined:
    Nov 9, 2016
    Posts:
    3
    Ah my bad... Thanks for the reply and thanks for you're work.
     
    zxkne likes this.
  4. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Soft Mask 1.6.2 is available now!

    This is a minor update that fixes one bug and improves shader compatibility with 2021.2.

    Improvements
    • Improved compatibility with Unity 2021.2 UI shaders.
    Bug fixes
    • Fixed a bug due to which Preserve Aspect Ratio worked incorrectly for sprites with Mesh Type set to Tight.
    A special thank you to @pfay for reporting the bug :)
     
  5. AlenazakharovaYandex

    AlenazakharovaYandex

    Joined:
    Aug 11, 2021
    Posts:
    3
    Hello! Can you tell, if it's possible to get this result using soft mask asset? I have an image with custom shader that draws gradient on this image
    upload_2021-8-21_22-19-25.png
     
  6. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello! Did I understand right, that you have a gradient Image (procedural) and you want to mask it by the text? Unfortunately, that’s not possible with Soft Mask—it cannot use a text as a mask, only sprites and textures.
     
  7. AlenazakharovaYandex

    AlenazakharovaYandex

    Joined:
    Aug 11, 2021
    Posts:
    3
    thank you for you answer
     
  8. AlenazakharovaYandex

    AlenazakharovaYandex

    Joined:
    Aug 11, 2021
    Posts:
    3
    Did I understand right, that I cannot mask UI Image with custom shader using TestMeshPro and soft mask?
     
  9. Lex4art

    Lex4art

    Joined:
    Nov 17, 2012
    Posts:
    445
    I've got a problem (very likely because I'm not educated enough in C#) - please, help if this is not hard.
    Problem is - I can't disable Soft Mask component on the object:
    Cant disable Soft Mask component.png
    Tried GetComponent<SoftMask>() - not working at all ("SoftMask" namespace couldn't be found).
    Tried variant from image above - error again.
    How to disable masking effect properly? Thanks in advance!
     
  10. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    @AlenazakharovaYandex, @Lex4art, sorry for the long delay. Email notifications do not work sometimes and I still haven't developed a habit of checking forum manually. My bad.

    You can mask UI Image with custom shader if you add special instructions to this shader. You may find more details on this process here. What you can't do with Soft Mask is to use text as a mask. For example, you have a gradient image with a custom shader and you want to cut this image by a text shape. That's not possible with Soft Mask – only texture may be used as mask. You can work around this by rendering the text into a render texture and then using this texture as a mask. But you have to write some code and pay some performance for this.

    SoftMasking is a namespace. This namespace contains a component named SoftMask. So you probably should write:
    Code (CSharp):
    1. IconContainer.GetComponent<SoftMasking.SoftMask>().active = false;
    Alternatively, you may import all the members from a namespace by adding using instruction at the top of the file. After this you may refer the SoftMask component by a simple name:
    Code (CSharp):
    1. using SoftMasking;
    2.  
    3. ...
    4.  
    5. IconContainer.GetComponent<SoftMask>().active = false;
     
    edi_boboc33 and Lex4art like this.
  11. DanielAtLevelEx

    DanielAtLevelEx

    Joined:
    Jun 4, 2019
    Posts:
    12
    Hello, my team is trying to accomplish a UI event where we have a translucent plane with a hole cut out in the center (basically a reverse UI mask). We need the ability to translate the position of the mask around the screen to reveal stuff. Is your tool capable of doing this for Unity 2021 in URP? Cheers!
     
  12. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello!

    I'm not sure that I understood the task right, but it looks like you do not need Soft Mask for this? Just apply 9-sliced image to the translucent plane with a hole in the center. Put it below all the other UI on the same level in Hierarchy. And now you can move it and see the underlying interface trough the hole.

    You may need Soft Mask if you want to hide UI parts that are not under the hole. Soft Mask can do this with Inverted Mask option. You may see an example here. Select example 05-InvertedMask at the top left corner.
     
    edi_boboc33 likes this.
  13. Flazone

    Flazone

    Joined:
    Jan 25, 2015
    Posts:
    6
    Hello @zxkne!

    Thanks for the plugin, it saved me a lot of time!

    Unfortunately, I have a problem with my TMP texts preventing me from using SoftMask in my game.

    If my text has a material preset (ie: an outline, an underlay), the colors disappear and are all white in build.

    The bug occurs on:
    - iOS, macOS and tvOS builds.
    - TMP version 3.0.6
    - Unity 2020.3.11f1 LTS


    Here is an example based on your sample scene.
    In editor:
    image (2).png

    In build:
    image.jpg


    I can't use the whole thing because of this bug, it breaks all my text's settings...

    Do you have a solution please?

    Thanks
     
    Last edited: Sep 15, 2021
  14. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello, @Flazone

    Thank you for the kind words :)

    I guess that you've encountered a known issue with TMPro integration. It's described in the Integration with TextMesh Pro / Shader Generation section (probably not the best place for this information—I'll think on it):
    Please, try the workaround and let me know if that will not help.
     
    edi_boboc33 and Flazone like this.
  15. Flazone

    Flazone

    Joined:
    Jan 25, 2015
    Posts:
    6
    It worked perfectly, thank you very much! (sorry I missed this section in the documentation)
     
    zxkne likes this.
  16. plotlessplot

    plotlessplot

    Joined:
    Sep 16, 2012
    Posts:
    29
    I've been using Soft Mask for a couple of projects and would like to know if something is possible. I'm sorry if this is obvious, but I haven't found a solution yet.

    For a tutorial, I'd need to have a color background with a semi-transparent image on top and then 3 different inverted masks to "highlight" different parts of the screen (see image below for an example).

    mask.png

    Is this possible with Soft Mask, to have 3 different masks operating on the same overlay image?

    Thanks in advance!
     
    edi_boboc33 likes this.
  17. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello,

    Unfortunately, it's not possible to use several separate Soft Masks for the same UI element :( But you can work around this limitation by merging all these masks together into a single larger mask. It could be done either manually (i.e. in an image editing tool) or dynamically (i.e. using render texture).

    The first approach only applicable if you have static layout and it's OK for you to increase the build size accordingly. I guess it's not your case. The render texture approach require some coding knowledge and also cost some runtime memory and performance. An example of how to use render texture with Soft Mask you may find in the demo scene 06-RenderTexture.
     
    edi_boboc33 likes this.
  18. plotlessplot

    plotlessplot

    Joined:
    Sep 16, 2012
    Posts:
    29
    Thank you very much for the quick reply! I will look into the render texture approach, then, as it seems the best way to tackle it. Thanks for the help! :)
     
    edi_boboc33 and zxkne like this.
  19. marchent

    marchent

    Joined:
    Jan 28, 2015
    Posts:
    7
    SoftMask border glows white Unity 2020.3.21f1 to Unity 2020.3.22f1 only on device.

    Hello,

    I’m using SoftMask 1.6.2.

    There was no problem when using it with Unity 2020.3.21f1, but after updating to Unity 2020.3.22f1, the border of SoftMask started to glow white on iOS Device.

    This event does not occur in Simulator on MacOS and it works fine.

    However, only apps that have been built iOS and transferred to the device will have problems with the SoftMask border.



    When I moved the project back from 2020.3.22f1 to 2020.3.21f1 and did an iOS build, this issue did not occur.

    Give me a good idea of how to deal with it. nice to meet you.
     
  20. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello,

    Thank you for reporting.

    I'm going to reproduce the issue and will come back to you in a day or so. Do you have any additional information that may help in this? For example, may be there's something special in your mask setup, texture formats?
     
  21. marchent

    marchent

    Joined:
    Jan 28, 2015
    Posts:
    7
    Thank you for your reply.
    The cause has been found.
    The cause was that the older version of SoftMask's Shaders remained.
    / SoftMask / Shaders
    of
    SoftMask.cginc
    SoftMask.shader
    SoftMaskETC1.shader
    SoftMaskTemplete.cginc
    The problem was solved when I deleted.
    Thank you very much.
     
    zxkne likes this.
  22. marchent

    marchent

    Joined:
    Jan 28, 2015
    Posts:
    7
    I found out the conditions under which the SoftMask border glows white on iOS Devices.

    When setting the screen of Dark Mode in Unity 2020.3.22f1 / iOS 15.1, no problem occurs.

    IMG_46085ADED493-1.jpeg IMG_2638.PNG

    but, when setting the screen of Light Mode in Unity 2020.3.22f1 / iOS 15.1, the problem is that the border of SoftMask glows white.

    IMG_2640.PNG IMG_2641.PNG
    At this time, I was able to confirm that the background screen was white.

    With Unity 2020.3.21f1, this issue does not occur in either light mode or dark mode.

    Here is the Unity project I created.
    https://www.dropbox.com/s/itbnjbi13ugk5mq/testSoftMask.zip?dl=0

    Is there any good solution?
    Thank you very much for your guidance.
     
  23. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Thank you for such a detailed description and a demo project. I reproduced the issue on my device and, I think, found the root cause.

    Since some version, Unity started to use premultiplied alpha in UI. Soft Mask supports this and uses premultiplied alpha shaders in recent Unity versions. But it seems that I made a mistake there: I thought they introduced premultiplied alpha in 2021 while they actually did it in 2020.

    I'll include the fix into the upcoming Soft Mask 1.6.3. Until then you can fix it by yourself by replacing word
    UNITY_2021_OR_NEWER in the SoftMask.cs file with
    UNITY_2020_1_OR_NEWER. There should be two occurrences in the file.

    After you did this, the bottom example in your project should work as expected but the upper one still have white glowing. This is because the bottom example uses standard Unity's UI shader while the upper one uses a custom shader. You probably should adapt your shader to use premultiplied alpha. It should have blending
    Blend One OneMinusSrcAlpha and also multiply output color with the masked alpha, not the sampled one. I've attached a fixed version of your shader just in case.

    I also want to ask you remove the link to a reproduction project from your post because it contains a full version of Soft Mask which is not quite correct.

    Thank you again for reporting the bug and helping with reproduce!
     

    Attached Files:

  24. marchent

    marchent

    Joined:
    Jan 28, 2015
    Posts:
    7
    Thank you very much!
    I’ll try it.
     
    zxkne likes this.
  25. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Soft Mask 1.6.3 released

    It's a bug fix release. One of the most important things in this update is some advancement in fighting the annoying "Destroying object multiple times" issue. I managed to come up with a test scenario and fix at least one cause of this behaviour. There're still chances to face this error, though, so, if that happen to you, please, let me know.

    Here are full release notes.

    Bug fixes

    • Fixed a bug because of which premultiplied alpha shaders were never used. Now they are automatically used in Unity 2020 and higher.
    • Fixed a bug that might cause “Destroying object multiple times” error in Editor.
    • Fixed a bug preventing invisible SoftMaskable scripts from removal on inactive GameObjects.
    As always, thanks to everyone who reported bugs and shared feedback!
     
  26. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    Hello, I'm just curious if Procedural UI Image ended up ever being fully supported by default in Soft Mask.

    You alluded to that being the case eventually here but I'm not seeing any option on the latest version :).

    It looks like I would likely have to still download those two files and add them to my project as well as apply this more recent patch.

    Thanks in advance!
     
  27. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello,

    No, Soft Mask still does not have support for Procedural UI Image out of the box. Support of 3rd-party shaders is a complicated thing because we still do not have any modularity in shaders as we have in regular code. You may check out how Soft Mask works with TextMesh Pro—it literally patches the shaders code. But even with modularity, a dependency on a 3rd-party product is always a risk because any new version may break the integration.

    So you still need a separate patch if you want to integrate Soft Mask with Procedural Image. You do not have to download the files from the original post, the last patch contains everything you need.

    If that will not work or you have any issues patch applying, please let me know.
     
    Meatloaf4 likes this.
  28. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,341
    Hi,
    does SoftMask support URP or is it a legacy shader asset?
    I tried to find out but neither the documentation nor the asset store page mention which render pipelines are supported.

    Thank you.
     
  29. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hi,
    Soft Mask works with URP because UI shaders are the same for all the rendering pipelines in Unity. Keep in mind, that Soft Mask work only with UI and that Shader Graph is not supported. So if you planned to use Shader Graph to make UI shaders (not sure if it has proper support for it now), then Soft Mask is not a good choice.
     
    oliversitan and _geo__ like this.
  30. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    Hello!

    I'm having a really strange issue where SoftMask is not masking TextMeshPro components with SubMeshUI's fast enough. Essentially I have a menus transition that starts my my menu's in a fully masked out state. It gradually get's unmasked over the course of a half second. I noticed that the TextMeshPro components with a submesh on them flick in to existence for brief moment before the intro menu transition has occurred and then quickly mask out.

    I'm left wondering what the issue could be :(. See the gif below :)
     

    Attached Files:

  31. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello!

    Can you describe how exactly you do the transition? Is it done from animation or from script? Is Soft Mask active before animation? Do you use static mask texture or dynamic (a render texture)? If you could reproduce the issue in a separate project and share it, this would be great, but we can start with just a description.

    I'll try to reproduce the issue but I need more information to improve the chances.
     
    Meatloaf4 likes this.
  32. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    @Meatloaf4,

    I think I reproduced the issue. Can you add line SpawnMaskable(); to the OnWillRenderCanvases method in SoftMask.cs? It should look like this:

    void OnWillRenderCanvases() {

    // To be sure that mask will match the state of another drawn UI elements,
    // we update material parameters when layout and graphic update is done,
    // just before actual rendering.
    SpawnMaskables();
    if (isMaskingEnabled)

    UpdateMaskParameters();
    }

    Please, let me know if it will affect the issue somehow. I'm not yet sure that this change is a proper and safe fix—I need more time to deeper investigate it.
     
    Meatloaf4 likes this.
  33. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    Wow man, you are amazing!

    Incredibly fast support & you solved my issue! Really can't thank you enough.

    Posted the fix below :)
     

    Attached Files:

    zxkne likes this.
  34. benjamin_flashbulb

    benjamin_flashbulb

    Joined:
    Aug 4, 2020
    Posts:
    25
    Hello,

    we recently bought SoftMask hoping to solve some visual issues with our UI, however we ran into the following issue:
    TMP is not masked softly but rather is cut off sharply.
    upload_2022-2-9_9-59-25.png
    The Soft Mask component is giving us this warning, but we're using the standard distance field shader for the text (no custom shader):
    upload_2022-2-9_10-2-5.png '
    Is there any way around this issue?

    Edit: We are on 2020.3f18
     
    Last edited: Feb 9, 2022
  35. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello!
    Did you run Tools / Soft Mask / Update TextMesh Pro Integration?
    If yes, can you, please, tell which exactly TMPro's shader are you using?
    Thank you.
     
  36. benjamin_flashbulb

    benjamin_flashbulb

    Joined:
    Aug 4, 2020
    Posts:
    25
    Hi,

    no, we did not run that tool or know it was required. It solved the issue, so thank you very much.
     
    zxkne likes this.
  37. justtime

    justtime

    Joined:
    Oct 6, 2013
    Posts:
    424
    Hi! Just transfered my project from 2020->2021 LTS, and got this error
    Code (CSharp):
    1. Shader error in 'UI/Soft Mask/Custom Shader with Soft Mask support': Couldn't open include file '../../Shaders/SoftMask.cginc'. at line 68
     
  38. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hi!
    Could you try to select SoftMask folder in the project view, right click on it and select Reimport?
    Please, let me know whether it helped.
     
  39. justtime

    justtime

    Joined:
    Oct 6, 2013
    Posts:
    424
    I solved it by adding "Resources" to the path.
     
  40. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Are you using the latest version of Soft Mask?
    The wrong path in an example shader was fixed in 1.6.1. The actual version is 1.6.3.
    Anyway, that's great that you've solved the problem :)
     
  41. DrinkableGames

    DrinkableGames

    Joined:
    Jun 20, 2021
    Posts:
    6
    Hi,
    first of all great plugin!

    However, I am experiencing extrem performance issues while using softmask on components in a scrollrect as well as on animated objects.
    For a frame in which i am activating an animated srollrect with only 4 softmask objects
    • SoftMask.FindGraphic()
      • Object.op_Implicit()
        • Object.CompareBaseObjects()
    takes 6500ms to complete...
    Each of those 4 soft masked objects have 15 children and it seems like softmask is doing some very expensive iteration through all children on deactivation and activation.
    Is there any workaround or am I missing something?
     
    Last edited: Sep 15, 2022
  42. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hi,

    Thank you!

    Indeed, Soft Mask may perform not so well in cases that involve many hierarchy changes or activations. I hope we could figure out a workaround but I need a bit more details about your use case to do so.

    Could you, please, provide a full hot path from the profile? And also describe the hierarchy of objects that participate in this problem (or show a screenshot of scene tree).

    6500 ms does not seem reasonable for these object numbers.
     
  43. DrinkableGames

    DrinkableGames

    Joined:
    Jun 20, 2021
    Posts:
    6
    Cant get image upload to work here
    Hierarchy:
    -Scrollrect (With Image attached, height is animated)
    -Content (LayoutGroup)
    -Button (with softmask & two TMP children)
    -Button (with softmask & two TMP children)
    -Button (with softmask & two TMP children)
    -Button (with softmask & two TMP children)​

    -> Scrollrect gets activated and then directly animated

    Profiler in that frame:
    -Softmask.LateUpdate() -> 99.9% / 0% / 6503ms / 0.02ms
    -Softmask.FindGraphic() -> 99.9% / 0% / 6503ms / 0ms
    -Object.op_Implicit() -> 99.9% / 0% / 6503ms / 0ms
    -Object.CompareBaseObjects() -> 99.9% / 99.9% / 6503ms / 6503ms
    -Object.IsNativeObjectAlive() -> 0% / 0% / 0.01ms / 0ms
    -IntPtr.op_Inequality() -> 0% / 0% / 0ms / 0ms
    -Object.GetCachedPtr() -> 0% / 0% / 0ms / 0ms​

    I made a workaround where the softmask are not in an animated scrollrect, works well
     
  44. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Thank you for the information.

    That all looks quite suspicious. As you can see from the profile, a huge amount of time is spent in a Unity method. It the profile also contain the number of calls made to a method, could you share it too? I've checked out the source code of Object.CompareBaseObject and I see no reason why it could take so long—the only explanation come to my mind is that this method is called too many times.

    Also, could you tell me the version of Unity you are using? I'm going to try to reproduce the issue.
     
  45. ShenKang4181

    ShenKang4181

    Joined:
    Aug 13, 2019
    Posts:
    14
  46. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello,

    I do not want to sell new copies any longer as I'm planning to stop the support eventually. If you're already using Soft Mask in a project under development that's OK—I'm continuing to give the support for all the existing customers. This support, however, does not propagate to any further Unity versions, so I would not recommend using Soft Mask in a new project. If you bought Soft Mask just recently and now can't safely use it because of deprecation, feel free to request a refund.

    UPDATE 12/24/2022 I finally decided to open source Soft Mask on GitHub and make it available for everyone, read details in this post.
     
    Last edited: Dec 24, 2022
    GavinoGrifoni likes this.
  47. Raffin

    Raffin

    Joined:
    Aug 11, 2015
    Posts:
    2
    I dont know if tiled soft masks don't work for anyone else, but they didnt work for me, I looked through your code and at line 775 in SoftMask.cs you have this
    return Mathr.Div(Mathr.Size(centralPart) * SpriteToCanvasScale(spritePixelsPerUnit), Mathr.Size(textureCenter));


    Changing the multiplication to division fixed my issue, the mask was not scaling in tiled mode correctly at all.

    Here is the fixed line
    return Mathr.Div(Mathr.Size(centralPart) / SpriteToCanvasScale(spritePixelsPerUnit), Mathr.Size(textureCenter));
     
    Last edited: Nov 21, 2022
  48. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Hello everyone!

    As you might notice, Soft Mask was deprecated some time ago. After a short delay I finally decided, that even if I personally don't have much time to support the project, it may be a good idea to share it with the community.

    So, starting from now Soft Mask is available free of charge under MIT license with Commons Clause. Basically, this license means that you can use Soft Mask in your commercial products unless these products are just a rebranded Soft Mask itself (i.e. provide the same functionality).

    Soft Mask on GitHub

    With this update I also changed the format of distribution from Asset Store's package to Package Manager's package. I don't provide prebuilt artefacts at this moment, but you can use GitHub URL to install the package. See the README.md on GitHub. If you're going to migrate from the commercial version of Soft Mask, just remove the SoftMask from the project and then import the package via the Package Manager.

    Besides of opening Soft Mask's source code, I also prepared a new version that contains several bug fixes. I bumped up the major version number because, well, changing a licensing and distribution model is a big change, and, more important, an actual breaking change was made—the minimum required Unity version was increased to 2020.1.

    See the full release notes on GitHub.
     
    Last edited: Dec 19, 2022
    PolyCrusher likes this.
  49. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    226
    Sorry for such a long delay. I finally checked this out and included the fix into 1.7.0 which was just released. Thank you very much for pointing on this problem and providing a working solution!
     
  50. corfing

    corfing

    Joined:
    May 20, 2014
    Posts:
    4
    hello,
    I don't know the details, but I'm asking because I think such this possible. I want masked 1 image use to 2 different image resource.

    example this image.
    is possible?

    upload_2022-12-27_15-31-29.png