Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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

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

  1. Igualop

    Igualop

    Joined:
    Mar 1, 2017
    Posts:
    23
    Adding to this post, I'm getting the same errors when I'm editing prefabs that have the SoftMask component (Usually when I delete them from the scene after I finish editing them)
     
  2. Igualop

    Igualop

    Joined:
    Mar 1, 2017
    Posts:
    23
    Hey again,

    I noticed a GC alloc happening every frame on the FindGraphic method. I've added a mask source check (SoftMask.cs, Line 448) to avoid calling the GetComponent method, the result look like this:

    void FindGraphic() {
    if (_source == MaskSource.Graphic && !_graphic) {
    _graphic = maskTransform.GetComponent<Graphic>();
    }
     
    zxkne likes this.
  3. NolwennB

    NolwennB

    Joined:
    Mar 4, 2014
    Posts:
    15
    Hello,
    Thank you for the latest update.
    I've found another issues with the separate mask functionnality. If I change the width or height of the wiewport element in the inspector, the mask is not updated accordingly, it is updated only when I change the position (and then take the correct width / height value). The scale value is working correctly, unless it is set to 0 : the mask doesn't work with scale 0.
     
    Last edited: Jul 19, 2018
  4. pondo

    pondo

    Joined:
    Mar 22, 2015
    Posts:
    7
    I'm seeing this as well. Here's the easiest way to reproduce:
    1. Create a new project in Unity 2018.2 and import Soft Mask.
    2. Create a standard Canvas (right click in Hierarchy and then UI -> Canvas)
    3. Add a sub-object to that canvas and add a SoftMask component
    4. Now delete that sub-object and see the error.
    The problem is that UpdateMaskParameters() gets called when the object is deleted and it fails the assert. That's because it no longer has a parent at that point and so can't find a canvas. This can be reliably reproduced in Play mode by deleting the object at runtime.
     
    zxkne and Igualop like this.
  5. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Oh, damn! I didn't receive notifications from this forum for some reason and haven't seen all these messages. @Igualop, @ElasticSea, @NolwennB, @pondo, guys, apologize me, that's my fault. I'll research the issues you listed and come back to you as soon as possible.
     
    Last edited: Aug 4, 2018
    ElasticSea and Igualop like this.
  6. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    I've fixed assertion failure issue and unnecessary GC allocation (well, I just used your solution, @Igualop—it's exactly what's needed). These fixes will be included in the following version 1.3. Besides this, this version will incorporate TMPro support into the main package. Currently, I'm working on documentation update and testing. I expect that I will be able to submit the updated package in a few days.

    But I still haven't found a bug with separate mask functionality. @NolwennB, do you have any other information that could help me to localize this bug? I tried to change viewport dimensions via Inspector, but it works correctly in my case. Maybe there are some special conditions? For example, whether you put your UI into a canvas or not, in which mode this canvas works, etc. Also, what version of Unity are you using? I'm going to test as many combinations as I can, but still not sure, that I'll catch this.

    Thanks to everyone, who posted bugs and bugfixes here! You have helped me a lot with all these issues, guys.

    Oh, almost forgot. Who suffers from an assertion failure, you may apply the following changes to function OnWIllRenderCanvases() (SoftMask.cs, line 401):

    if (_dirty && isMaskingEnabled)
    UpdateMaskParameters();

    The changed fragment is highlighted in bold.
     
    ElasticSea and Igualop like this.
  7. cloud_canvas

    cloud_canvas

    Joined:
    Mar 31, 2015
    Posts:
    13
    Hi @zxkne , I wanted to get clarification from you about something. You say in your documentation that nested Soft Masks don't work, but that nesting standard Mask or Rect Mask 2D Components in combination with your Soft Mask Component works.

    Do you mean that Soft Mask is in the parent, or in the child? I'm getting an issue where the entire space my UI should be in is using Soft Mask so that the elements fade out as they go to the extreme sides of the viewport, but the individual items are supposed to have Masks on them as well that give them rounded corners. Instead, my whole Image is disappearing. The only way to see the item with a separate Mask on it is to disable the Soft Mask component in the parent Rect Transform.

    Am I doing something wrong based on this high-level description? Can provide more details if necessary. Thanks!
     
    zxkne likes this.
  8. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hi @cloud_canvas,
    The standard Mask may be either a child or a parent of a Soft Mask. But while thinking about it, I just remembered that there was a peculiarity in Unity that broke interaction of Soft Mask with a standard Mask. I will be able to investigate this further at evening and will come back to you. Meanwhile, could you tell me what version of Unity are you using?

    Also, as a possible direction for a workaround, you may try to play with Mask / Soft Mask activation order. In particular, try to activate the Soft Mask after standard Masks.

    As for your structure on a whole, I think you're doing everything right.
     
  9. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    @cloud_canvas,

    I just checked it out and also found the response from Unity Technologies on this case. It's a 'bug' in standard Mask implementation. The thing is that if you put an IMaterialModifier (it's just a standard interface from Unity UI) after the Mask component, Mask may drop changes in material properties produced by the IMaterialModifier. Mask caches a cloned material, which is good, but it doesn't re-cache it when some of the previous modifiers in the chain changes the material.

    I've reported this bug a year ago and got a response from Unity Technologies. They said that due to the nature of this bug, they've decided to not fix it for now.

    So, the workaround is to ensure that the invisible SoftMaskable component (it's spawned automatically on all the elements masked by a Soft Mask) is the last component in the components list. The simplest way to do so is to instantiate Soft Mask at runtime when child Masks are already spawned. To see SoftMaskables in Editor you can replace the line 76 in the file SoftMaskable.cs with the following:

    hideFlags = HideFlags.None;

    I'm going to add information about this issue to the documentation. I'm also thinking on whether it's possible to automatically guarantee that SoftMaskable is the last child on the GameObject, but currently, I see no way to make it without performance penalty.

    Sorry for this inconvenience and potential misinforming you about Soft Mask capabilities. As always, if you're unhappy with this asset and don't want to use it anymore, I'm glad to provide a refund.
     
  10. cloud_canvas

    cloud_canvas

    Joined:
    Mar 31, 2015
    Posts:
    13
    @zxkne , I'm on Unity 2018.2.2f1. Thanks for the thorough follow-up on this. I'll give your workaround a go. If I can't get it to work, it's not a mission-critical feature I'd be patching around, just trying to see if I can get it to work for aesthetic purposes. Cheers mate.
     
  11. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Soft Mask 1.3 is ready! The most important change of this version is the inclusion of TextMesh Pro integration to the main package.

    IMPORTANT INFO for TextMesh Pro users
    If you're using the Soft Mask for TextMesh Pro package, please remove it before updating to this version. TextMesh Pro support is now included in the main package and a separate integration asset isn't needed. Moreover, it may cause errors due to reusing the same GUIDs. As usual, don't forget to click Update TextMesh Pro Integration after updating.

    The old Soft Mask for TextMesh Pro package is now deprecated and should be not used anymore.

    Release Notes

    Improvements
    • Integration with TextMesh Pro, which was previously implemented in a separate integration package, now included in the main package. It corresponds to Unity’s decision to include TextMesh Pro in standard Unity installation.
    • Removed unnecessary garbage allocation which was taking place when non-Graphic mask source was used.
    Bug fixes
    • Fixed an assertion failure which was introduced in the previous version.
    • Fixed a bug caused masked UI to entirely disappear sometimes in Editor (after scene saving, after code reload, etc.).
    • Fixed a bug caused Update TextMesh Pro Integration to not work on some Unity versions in the case when the path to the project contained “Soft Mask” text.
    And again, thank you for the reported bugs and feedback provided.
     
    Igualop likes this.
  12. yanivng

    yanivng

    Joined:
    May 21, 2017
    Posts:
    35
    Hi,

    I am upgrading from Unity 2017.4.3f1 to Unity 2018.2.4f1:
    1. I am using TMPro. Used the upgrade instructions on https://forum.unity.com/threads/textmesh-pro-unity-2018-1.511748/.
    2. After the upgrade I have noticed the following error:
    "SoftMask doesn't support tight packed sprites"
    3. I have also upgraded my Soft mask version to the recently released version 1.3.

    Can you please help? Everything was perfectly working with 2017.4.3f1 / TMPro / SoftMask 1.2.

    Thanks,
    Yaniv
     
  13. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hi, @yanivng

    Thank you for choosing Soft Mask and for a detailed description of the issue.

    Are you using tight packed sprites as masks? That is, sprites with packing mode option set to tight in Inspector?

    I'll try to perform TextMesh Pro upgrade you mentioned by myself and then come back to you. But there are chances that I'll be not able to reproduce this issue as it may be related to the way Soft Mask and TextMesh Pro are used in your project. I would be very appreciated if you could provide a minimal bug reproduction project.
     
  14. yanivng

    yanivng

    Joined:
    May 21, 2017
    Posts:
    35
    Thanks @zxkne .
    For some unknown (and weird) reason, upgrading to 2018.2.4f1 also changed the all of my sprite atlases settings automatically and turned on tight packing. After discarding those changes, everything is back to normal.

    Thanks,
    Yaniv
     
    zxkne likes this.
  15. Numa

    Numa

    Joined:
    Oct 7, 2014
    Posts:
    54
    Edit: Fixed----
    Hi, I'm using Unity 2018.2.2.f1, and I'm getting new warnings after updating while using TMP and Soft Mask (I've followed the update instructions for both TMP and SM)

    SoftMask will not work on TextElement1 (TMPro.TextMeshProUGUI) because material SOURCESANSPRO-REGULAR SDF Material (UnityEngine.Material) doesn't support masking. Add masking support to your material or set Graphic's material to None to use a default one.

    ----
    Edit: I ran the "Update TMP Integration" AGAIN and checked new shaders got generated as per the documentation. This time it picked up the tmp shaders and everything is fine now.

    Thanks
     
    Last edited: Aug 24, 2018
    zxkne likes this.
  16. daville

    daville

    Joined:
    Aug 5, 2012
    Posts:
    267
    Hi, I just realized I needed this Asset, and after a quick search in the forums, I found this asset and just got it.

    I works like charm, exactly what I needed, I just imported it added the component and just works as I needed it.

    Thanks for making this possible.

    Anyways, I have one suggestion / request.

    Do you think it would be possible to add a slider to fade the alpha of the image?

    I already can make it totally invisible by adding a "Canvas Group" component and decreasing the Alpha value, but I want the Oposite, I want the mask to be totally Opaque as I increase a value.
     
    zxkne likes this.
  17. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    That is, you updated Soft Mask, run Update TextMesh Pro integration and got these warnings? Then you run command again, and everything worked? It seems strange, it should work for first time. Anyway, I'm glad to hear your problem is solved!

    Thank you for the kind words, @daville

    As I understood, you want a slider that adds to texture's alpha value? Suppose we have a pixel with alpha 0.5 in the mask texture. When the slider is set to 0, this pixel is used as is. When the slider is, say, 0.3, the actual mask value at this point is 0.5 + 0.3 = 0.8. And, finally, if the slider is set to 1, this pixel will be 1, and the whole mask will be fully opaque because 1 plus anything is 1 (due to 0..1 clamping).

    Did I understood your suggestion right?
     
    daville likes this.
  18. daville

    daville

    Joined:
    Aug 5, 2012
    Posts:
    267

    yes exaclty that.

    that way if my mask is already a gradient I can fade it in and out nicely... I don't know about other users if they would use that actually but yes that could help me in my current project.

    right now I'm exporting multiple masks and replacing them by code.
     
  19. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    This feature is not hard to implement and it may accompany another feature I was asked about—Inverted Mask. I definitely include it to a list for future updates, but I have no specific plans yet so don't rely on this. Thank you for the feature idea!
     
    daville likes this.
  20. YardenSG

    YardenSG

    Joined:
    Jun 10, 2018
    Posts:
    3
    Hi,

    We have bought the plugin and started using it to fade mask a large list of scrollable objects. For the sake of performance all objects in the scroller are pooled meaning they are pulled from a shared pool manager and then placed in the correct place, when objects are moved in and out of the scroller and back to the pool their parents change. When looking at the profiler when an object is added from the pool to the scroller(where the soft mask is) the method SpawnMaskablesInChildren is running and killing performance and when objects are moved from the scroller to the pool (out of the softmask) the method FindMaskOrDie is running and killing performance. Is there anyway around this?
     
  21. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hi @YardenSG

    You could try to replace GetComponent() calls in SoftMaskable.cs by GetComponents(). An example of this approach can be found in SoftMask.cs, see how static fields s_masks and s_maskables are used. This should, theoretically, speed up FindMaskOrDie a little bit.

    As for SpawnMaskablesInChildren, I see no simple way to solve this. In essence, in your case with pooling, all this work is unnecessary. Ideally, SoftMaskable should be not destroyed when you return it to a pool nor re-created later when you put into the UI again. I'll investigate whether it's possible to adapt Soft Mask design to make this scenario possible.

    Anyway, I would appreciate if you try GetComponents and share results. Also, if you don't want to investigate this manually and couldn't use Soft Mask because of performance issues, I could provide a refund.
     
    Last edited: Sep 22, 2018
  22. YardenSG

    YardenSG

    Joined:
    Jun 10, 2018
    Posts:
    3
    Hi zxkne,
    I tried replacing the GetComponent method with GetComponents, difference was not really significant. Couple to 4 ms of improvements. The real killer is still the SpawnMaskablesInChildren method. I tried overriding functionality on the softmask and softmaskables, so that the softmaskable will work with dependency injection instead of searching for an instance of ISoftMask by itself.
    That didn't work as nothing was masked but it is very possible i missed something. If you have any idea how to solve this you have my mail :)
     
  23. blastproofcoding

    blastproofcoding

    Joined:
    Apr 3, 2014
    Posts:
    12
    Any idea why this might happen?
    The mask looks like it's lagging when a scroll rect is moving.
    It adjusts back to normal when the movement stops
    I don't really have many details, it's just a soft mask script that is used to round the content of a button in this case.
    Usually has a UI.Mask component on top of the main content, but behaves the same without it too.
    Notice the corners of the bottom-left object?

    Any help would be much appreciated.
    Thanks Screenshot 2018-09-20 at 18.02.12.png
     
  24. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hi @blastproofcoding

    Thank you for reporting the bug! I think I've reproduced it.

    Does the mask lag behind only when you release the mouse button?

    It could take some time to figure out what is going out and how to fix it correctly. Anyway, I'll write you in no later than 2 days.
     
  25. blastproofcoding

    blastproofcoding

    Joined:
    Apr 3, 2014
    Posts:
    12
    Hi @zxkne

    Thanks for replying.
    Yes, you are correct. It only reproduces when you release mouse button/finger.

    Thanks!
     
  26. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hi @blastproofcoding

    I think I understood the root cause of the bug. Could you try to modify UpdateMaskParameters function (SoftMask.cs, line 468) in the following way?

    void UpdateMaskParameters() {
    Assert.IsTrue(isMaskingEnabled);
    if (_dirty || maskTransform.hasChanged) {

    ...
    Changes are highlighted in bold. I would appreciate it if you could tell me whether this will solve your issue.
     
  27. blastproofcoding

    blastproofcoding

    Joined:
    Apr 3, 2014
    Posts:
    12
    Hi @zxkne,

    Thank you very much for the fix.
    It absolutely did the job!
    You rock!
     
  28. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Soft Mask version 1.3.1 has been released recently. It's a minor patch release that fixes some bugs and introduces small performance improvements.

    Release Notes

    Improvements
    • Improved the error message that is shown in the case when an unreadable texture is used with the Raycast Threshold value greater than zero. Also, this error is now displayed in Inspector window at edit time, not only in the Console window at runtime.
    • Removed an unnecessary garbage allocation that might be happening because of the GetComponent<> call.
    Bug fixes
    • Fixed a bug caused the mask to lag behind when being moved. In particular, it was causing the lagging of the mask during the automatic movement inside a ScrollRect.
    • Fixed a compilation error when targeting .NET Standard 2.0.
    Thank you for everyone who shared their issues and improvement suggestions! I very appreciate that you took the time to do so.
     
  29. DeathApocalypse

    DeathApocalypse

    Joined:
    Sep 29, 2016
    Posts:
    3
    Good Asset, but 30$ is unacceptable price for such a small addition.
     
  30. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hello @DeathApocalypse,

    Yes, I understand this point. But no one price can satisfy everyone. There are other assets available on Asset Store that solve the alpha masking problem, you probably could try them.
     
  31. unity_emt4Mpz8KBYwEA

    unity_emt4Mpz8KBYwEA

    Joined:
    Jan 25, 2018
    Posts:
    2
    does it work for masking camera objects too ? not the canvas ui ..
     
  32. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Not sure that I understood your question right. If you mean masking 3D objects (in either screen or scene space) then Soft Mask can't do this. If you mean using masks inside a canvas that is rendered into a camera (and not directly into the viewport as by default) then you can do it with Soft Mask. Soft Mask works in any type of canvas: viewport, camera, world. Hopefully, I answered the question.
     
  33. martin_tr

    martin_tr

    Joined:
    Nov 18, 2014
    Posts:
    2
  34. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hi @martin_tr,

    I've just checked—Soft Mask works with Image Warp:

    SoftMaskAndImageWarp-small.png

    Image Warp works as regular UI element so Soft Mask is able to mask it. But be aware that the use of Soft Mask together with Image Warp has certain limitations:
    • It's not possible to use a warped image as a mask itself.
    • The Image Warp package contains also a script for the world-space rendering of a warped image. It's a regular Renderer, not a UI element so Soft Mask will not work with it.
     
  35. martin_tr

    martin_tr

    Joined:
    Nov 18, 2014
    Posts:
    2
    Thanks for checking this - that's great - although for my project I was hoping it would be possible to use the warped image as a mask.
     
  36. neoRiley

    neoRiley

    Joined:
    Dec 12, 2008
    Posts:
    131
    What's the best way to animate the mask? I need to do a reveal on some text, and I have the mask working, just wondering what best practice is for moving the actual mask for a reveal type animation?

    I read through some of the posts and one person changed out the mask image to simulate a reveal. What I'd like to do is just move the mask that has one edge set to an alpha value and move it over the text to "reveal" it. Is it possible to do this and what's the best practice?
     
  37. neoRiley

    neoRiley

    Joined:
    Dec 12, 2008
    Posts:
    131
    I'll answer my own question:
    1. Create another child UI element like an Image (remove Image component once created). So you should have a parent that has SoftMask component applied, a text component and another Image component that just simply is a RectTransform.

    2. Select the parent where SoftMask component is attached.
    3. Drag reference of the Image component into "Separate Mask"
    4. Set the transform.x/y/z of the Image component values as you like

    Changing my star rating to 5 stars :)
     
  38. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    I see you found the 'best way' by your own, @neoRiley. That's great, glad to hear your problem is solved! Thank you for sharing the answer—it may be helpful for someone else in the future.

    And thank you for 5 stars! :)
     
  39. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    4
    Hello!
    Is there a way to set an object to ignore the soft mask?

    I have a nested element that I want to float above the mask in the corner.
     
  40. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hello!

    Unfortunately, there is no such functionality. The simplest and most straightforward way to achieve the desired effect is to assign the non-masked element a custom shader that doesn't support Soft Mask. For example, you can copy the default Unity UI material which could be downloaded in a separate archive from here. This solution will lead to a warning message which you probably can just comment out in the source code.

    You can also try to play around with the Separate Mask feature but using it you still need to include the masked elements into a parent that is different from the parent of non-masked elements.

    I'll add this feature to my task list but currently, I can't tell you for sure whether it will be implemented and when. If you already bought Soft Mask and can't use it without this functionality, please write me the invoice number in PM or email and I'll refund.
     
  41. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    4
    The functionality is great, just surprised this isn't in there. I did a little workaround with your suggestion.
    I'm setting soft mask through code, so had to do a little extra step.

    I added this line to your Replace function so it doesn't override my material:
    if (original.name.Contains("Ignore")) return null;


    Then when I instantiate my soft mask in code I just do this:
    checkmarkImg.material = CanvasManager.instance.IgnoreMaskMaterial;


    Code (CSharp):
    1. public Material Replace(Material original)
    2.             {
    3.                 if (original.name.Contains("Ignore")) return null;
    4.  
    5.                 if (original == null || original.HasDefaultUIShader())
    6.                     return Replace(original, _owner._defaultShader);
    7. #if UNITY_5_4_OR_NEWER
    8.                 else if (original.HasDefaultETC1UIShader())
    9.                     return Replace(original, _owner._defaultETC1Shader);
    10. #endif
    11.                 else if (original.SupportsSoftMask())
    12.                     return new Material(original);
    13.                 else
    14.                     return null;
    15.             }
     
  42. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    @Npicouet,

    Your solution is even better than I suggested, good job! Thank you for sharing. Someone else may found it helpful until per-object ignore is implemented.
     
  43. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    52
    Hello, I want to buy this plugin, does it work with Unity 2018 and also, it's said that it can restrict mouse clicks. This is the main reason why I want to buy it. Waiting for your confirmations so I can proceed with payment. Thanks
     
  44. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Hello @andrew_pearce_,

    Yes, it works with 2018.x. Restriction of mouse clicks means that you can specify a threshold value that determines whether input should be passed to a masked element or blocked. An input event is passed if the mask alpha at a given pixel is greater than this threshold. Keep in mind that this feature requires the texture that is used as the mask source to be readable on CPU (you should set a special flag in Importer settings for this texture).

    If doubt, you can purchase Soft Mask and if it will not suit your needs, I'll provide you a refund.
     
  45. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    52
    Thanks for your reply and provided information. Just bought plugin... works as a good replacement for native mask so I will keep it at least for this reason. Basically I want to make a hole in a membrane which will cover a canvas.

    The first solution seems to be: making a hole using stencil shaders and then hiding raw edges with your plugin. That will do the visual part but what about input restriction?

    The second possible solution is to place an hole image with your plugin and fill up the space with four other dummy images (using UI layout components).

    What do you suggest? Thanks again and waiting for your reply
     
    Last edited: Dec 17, 2018
  46. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    Ouch, it seems that I hurried to assure you that Soft Mask could manage this task. Should your membrane be visible or it's just an input blocker? If latter, Soft Mask isn't a good solution for this task although it's still possible to use it.

    I prepared an example that shows how the desired effect could be achieved using Soft Mask. I'm not quite sure that I understood you right, though. Create an empty project (I've used Unity 2018.2), import Soft Mask first and then the attached package (Assets / Import Package / Custom Package). The Soft Mask component is attached to the MaskRoot object. Here you can choose one of three mask images that I've prepared. Note that the mask covers the whole canvas and we explicitly specify UV coordinates outside of [0-1] range to position the circle.

    As I already said, that solution is OK only if you want to visually shadow UI too (Mask_50% or Mask_0%). Mask_99% is a hack and I don't recommend using it because it still draws all the UI using a more complex shader than needed.

    If some parts of the solution aren't clear enough, feel free to ask me.
     

    Attached Files:

  47. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    52
    Thanks for your quick reply and solution. Despite some limitations (I may not be able to change size of circle) it is still the simplest and I like it. I just have a question regarding x/y and w/h

    1) The 0,0 seems to be bottom left corner (looks like fixed coordinates, not a pivot of container or sprite), plus x axes is reverted (goes to left while it should go to right), am I right?

    2) However I do not understand what does w/h changes... it's not the size of sprite or sprite image which is resized before used as mask.Could you please explain? Thanks!

    Thanks for great support and awesome plugin

    UPDATE: found the answer, you also mentioned it in your previous post. Mask is applied to whole canvas, x/y sets the lower left corner, w/h is the top right corner =) What I still do not understand how those numbers are calculated... I guess it should be relative to camera... you help is highly appreciated =)
     
    Last edited: Dec 18, 2018
  48. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    52
    I found a solution but I need your help to modify your plugin =) I found how to add extra field (x/y and w/h) when sprite mask type is selected. So I will try to modify location the same way as you are doing with texture. I should around here lines 600 - 615. I will go and use the circle mask as texture the same way as you suggested so I can continue working on my game but I will definitely update this code. It must be very simple but RectTransform in Unity is way more complicated (I moved from PIXI). If you can give me a help, I will be very thankful but I understand that you may be busy. However, I am sure this is very useful feature and no existing plugin ever implemented it!
     
    Last edited: Dec 18, 2018
  49. zxkne

    zxkne

    Joined:
    Jan 27, 2016
    Posts:
    133
    @andrew_pearce_, sorry for a long delay, I have a pretty short window on workdays to answer questions here.

    X, Y, W, H specifies UV rectangle that is used for the mask. As you said, the mask is applied to the whole canvas, so we essentially have a quad that fills the whole canvas. X and Y determine UV coordinates of the top left corner of this quat whereas W and H determine width and height respectively. Usually, UV coordinates fall into [0-1] range where (0, 0) and (1, 1) corresponds to opposite corners of a texture. We specify the width of 6 units which means that we shrink texture 6 times below the normal size. Anything that falls outside of [0-1] range is considered to have the same color as pixels at the texture border because texture's border mode is set to Clamp. You can make clearer what's going on by changing the border mode of mask textures to Repeat.

    If you're interested in modification of Soft Mask code, I would recommend to not change sprite rectangle calculation. Instead, try to change the way the mask behaves in the area outside of the RectTransform. Currently, these pixels considered transparent (as if a mask contains zero). If you change it so that those pixels would be considered non-transparent, you'll get the desired effect. The first place to look at is the SoftMask.MaterialParameters.SampleMask method. It emulates mask sampling on the CPU to make input blocking work. The second point is the SoftMask_GetMask function in SoftMask.cginc file. Besides of texture sampling, this function also clamps pixels outside of the mask by using UnityGet2DClipping(). You probably could try to modify those methods to make them return a non-zero value outside of the mask. Then you can create a small RectTrasform and set Soft Mask's Separate Mask property to point on this transform. All this should result in behavior as if you have a large monotone mask with a small part of it replaced by a concrete sprite.

    Be warned that changing code in these places will change the behavior of all Soft Masks in your project. I hope that in future version such functionality will be available out of the box, but I'm not promising something right now.
     
  50. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    52
    @zxkne Thanks a lot for your detailed answer... I got the idea about UV, it's normilized position (you use it to get pixel's color). I studied your code to write small custom component... as you already understood, I needed an option to separate mask from object which is masked =)

    I used stencil shader to make a hole in object with mask image. Then I used your code to check if pointer is within rect of masked image and that pixel's alpha value is greater than threshold value. It was not easy but it's working. I also reduced alpha value of tint color to get as smooth border as possible BUT it's not alpha transparency.

    So how do you do that smooth border? The only idea which came to my mind (you mention in description that you clone texture) is to make a clone of image mask. Then update its sprite with portion of membrane's texture underneath it (how?). Finally apply shader with inverted alpha of mask image =)

    Thank you very much for your help and time. Your plugin's code is worth 30 USD without any doubts!

    UPDATE with fresh mind, I found an answer inside your last post "The second point is the SoftMask_GetMask function in SoftMask.cginc file". OMG so you have implemented sprite's Slice mode within shader itself! So I need to get dirty with shaders and its include files :D
     
    Last edited: Dec 19, 2018