Search Unity

Alpha Mask: UI, Sprites, Tilemaps, Particles, 3D

Discussion in 'Assets and Asset Store' started by DominoOne, Feb 16, 2015.

  1. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    Thank you! I managed to do this now, and it works like a charm and looks really good. I made the gradient with an ease in-out algorithm and the result is fantastic.

    Is it also possible to nest more of these inside each other? I would need something like 24 hard clipping masks inside a larger one with a gradient mask.
     
  2. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @kleinsan,

    Yep, you will have to use a RenderTexture for that. Please check the Multiple Masks Sample within the sample scene. What you need to do is "combine" the different images/masks into a single RenderTexture by rendering then with an additional camera. And then use that single RenderTexture as the Mask image. Let me know if you need additional assistance with that :)
     
    kleinsan likes this.
  3. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    Thanks for the quick reply. This technique looks great. I wasn't aware that you could render a camera into a texture. It will be most useful in the future.

    My problem however is a little different. I need to show a series of up to about 24+ instances of masked animated clouds that need to only be visible within their designated masks. It looks like this can be done with standards sprite masks, and applying your sprites-alpha-mask-worldcoords shader to each cloud. In this way I was able to clip each cloud to the sprite mask while clipping the collection of all the sprite masks using your mask. This works well since I'm happy with hard clipping for the clouds. One gotcha is that I needed to run the game to make the effect kick in, it didn't catch until I did that.
     
  4. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan, now I get it :) Yep, as long as hard clipping is OK, you can use that combination! Not sure why it didn't work without running, though. Maybe it's because you applied the shader manually and not by clicking "Apply Mask to Siblings".
     
    kleinsan likes this.
  5. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    Hi again,

    I'm really happy with this plugin after having tested it for a while.

    I have one question: I'm making a prefab that includes both parts that need masking and parts that don't. If I place it on a level below the mask, nothing gets masked, and if I place it as a sibling, it all gets masked. Is there any way to get only parts to be masked? I suppose that would require being able to set certain objects to be excluded from masking even thought they are siblings of the mask. Or maybe I just need to make two prefabs and link them somehow.
     
  6. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @kleinsan

    Glad you're happy with the plugin! :)

    You can actually go through the elements that you don't want to be masked and just change their material from the masked one to the default Sprite material. If you press "Apply Mask to Siblings", the masked material will be set again. But until you do, it should be fine :) Does that help?
     
    kleinsan likes this.
  7. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    @DominoOne It should work.

    I'm now having an issue that creating a bunch of prefabs with the masking material on all the sprites inside result in them not being masked by your mask. Could this be because they are inside a sprite mask? It seems to work if I manually turn off Masking Enabled, and then back on again. Is this a familiar issue with a known solution (sorry for asking agian if it has been answered before)
     
  8. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan,

    No, that shouldn't happen and is not a known issue. Also, it shouldn't be related to the Sprite Mask, but, of course, it might. Would it be possible to get a little sample project that obviously demonstrates the issue?
     
  9. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    My project is getting quite big, so I tried to recreate the issue in a new project. The funny thing is that it's not happening there and everything seems to work just fine. I must have done something wrong somewhere, but I just can't figure out what that might be. Could it be the execution order since the thing that makes it work is turning masking off and on again?
     
  10. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan, yes it could actually be execution order. Try changing it with various values. We have also experienced similar issues when the Masks are used along with Anima2D, so maybe you're also using some kind of a plugin/package for Sprites/animation?
     
  11. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    The order of things seems to be the issue here. I'm downloading some info before instantiating the prefabs, and when I finally draw them, they are not masked. If I move instantiation to early in execution, it works fine.

    I don't know what the best solution to this might be. I wouldn't really expect your script to listen for new items to be added, so the best would probably be if I could tell the mask that it needs to look at new stuff. Turning it off and on will probably result in an ugly glitch. Can you think of a better way to do this? Any interaction with the mask seems to trigger an update. If I move the mask manually in the scene editor while it runs, it kicks in.
     
    Last edited: Dec 22, 2017
  12. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    This seems to work:

    using ToJ;

    And then:
    GameObject.Find("Mask").GetComponent<Mask>().FlagForRefresh();
     
  13. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan great that it works! Will that be a good enough solution for you?
     
  14. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    If you can't think of any downside of doing this, then I'm happy with it.
     
  15. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan this should be perfectly fine :) I'm not sure why the plugin thinks that it shouldn't update after instantiating in your situation, but forcing the update is completely fine.
     
    kleinsan likes this.
  16. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    I have some more questions. Hope you don't mind.

    One is that when two objects are lying on top of each other, they are masked individually rather than as a group so that you can see the second object through the first. I have attached an example.



    The other question is if you have any plans for making the mask tiling in one or both directions?
     
  17. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @kleinsan. I really don't mind, that's what I'm here for! :)

    The Mask can tile to one or both directions without a problem. Just set Repeat on the texture Import settings. And then if you want to clamp on one axis only, select clamping options on the Mask component.

    I cannot see the images you've attached, but I know what you mean. However, technically, each object actually is masked separately – that's why you get that effect. If you could re-attach (or send me via a private message) the images, maybe I could give you some ideas how to improve the look. On the other hand, you can always render multiple Sprites/UI elements into a RenderTexture and display only the result with the Mask applied. Then it will mask is as a single object.
     
    kleinsan likes this.
  18. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    Wrap Mode Repeat seems to work great!

    Here's the images with mask off and on.

    mask1.jpg mask2.jpg
     
  19. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan that's a pretty smooth gradient, so I can't think of any other way, but to render the moon and the sun into a single RenderTexture and then mask it instead of the two separate images. Is that a possibility?
     
  20. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    I suppose that would be the solution if I run into this too often. I'll dig into it and see if I can figure out how if it becomes necessary.

    I just hit another little thing that I might not end up using because it may become too resource heavy, but it appears that the mask is not happy with me using the mask shader on a single particles in particle system. I get hundreds of these errors right after starting the particle system: " NullReferenceException: Object reference not set to an instance of an object
    ToJ.Mask.ValidateShader (UnityEngine.Material material) (at Assets/Alpha Masking/Mask.cs:621). ToJ.Mask.UpdateInstanciatedMaterials (System.Collections.Generic.List`1 differentMaterials, Matrix4x4 worldToMask) (at Assets/Alpha Masking/Mask.cs:416). ToJ.Mask.UpdateMasking () (at Assets/Alpha Masking/Mask.cs:595)
    ToJ.Mask.LateUpdate () (at Assets/Alpha Masking/Mask.cs:390)".
     
  21. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan we have added particle system support just recently. The update is still pending Asset Store review. Can you wait a little until it's approved or do you want me to send it to you now? If so, send me a private message, and I'll drop you a link there.
     
    kleinsan likes this.
  22. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @kleinsan I just got an e-mail that the update has been approved :) So, you can grab it from the store and let me know if it works with your particles.
     
    kleinsan likes this.
  23. kleinsan

    kleinsan

    Joined:
    Nov 8, 2017
    Posts:
    16
    You guys rock! I just tested it quickly, and it works right out of the box with no issues.
     
  24. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
  25. djsegler

    djsegler

    Joined:
    Apr 20, 2016
    Posts:
    19
    Just downloaded this asset and it appears to work fine when I configure in the editor and test prior to "running". Unfortunately, the object I'm placing this mask on is instantiated dynamically and even though I've saved the object with the mask added as an updated prefab, the material with the mask applied is missing every time the object is instantiated. I get this error in the console:

    NullReferenceException: Object reference not set to an instance of an object
    ToJ.Mask.DuplicateMaskedMaterials () (at Assets/Alpha Masking/Mask.cs:793)
    ToJ.Mask.Start () (at Assets/Alpha Masking/Mask.cs:354)

    If this object were to remain in the hierarchy at when run, then this error occurs. It only happens because the object is instantiated dynamically.

    Any ideas?

    Thanks!
     
  26. djsegler

    djsegler

    Joined:
    Apr 20, 2016
    Posts:
    19

    UPDATE -
    I discovered that if I specifically save the newly created "mask" material generated by the script to the sub-prefab in the object, the changes are retained and this error doesn't occur. Strange that applying changes to the prefab at the top level of the object don't get it done. Have to drill down to the specific object with the new mask material and apply the prefab changes only to that object. Anyway - problem appears to be solved.
     
  27. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @djsegler,

    Thanks for using our plugin! :) I'm glad that you found a workaround, but normally, this shouldn't happen. So, if you provide some more information (or even a stripped down project, which would help us easily reproduce the issue), we'd definitely look into it. Are you masking Sprites, UI or 3D objects?
     
  28. Nrjwolf

    Nrjwolf

    Joined:
    Mar 2, 2014
    Posts:
    2
    May you please add TextMeshPro support?
     
  29. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @Nrjwolf,

    I am actually thinking of a way to support custom shaders (such as TextMeshPro), but that would require users to copy a few lines of code to those shaders. Would that be good enough?
     
  30. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @WhereTheIDivides,

    Sorry for a late reply. We'll definitely look into this and submit an update :)
     
  31. TaylorAnderson

    TaylorAnderson

    Joined:
    Jul 28, 2012
    Posts:
    4
    Hello! I was considering buying this as it seems to give me the solution for in-game lighting that i want. I have a question though. Can a mesh with an attached material be used as a mask? I want to do something like a vision cone where everything outside of the cone is darkness. And the vision cone can be obscured with level geometry.
     
  32. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @TaylorAnderson

    Thanks for your interest in our plugin! :) Yes, that is possible. You can use an additional camera with a RenderTexture, to which you'd render the mesh (the mesh should be white, and everything else should be black). And then you can use that RenderTexture as the Alpha Mask. Quite straightforward, and you can actually find a sample in the package that demonstrates the usage of a RenderTexture for masking. Hope that clears your concerns!
     
  33. Richardbmth

    Richardbmth

    Joined:
    Mar 5, 2016
    Posts:
    29
    Hi @DominoOne We've just updated our version of the Alpha Mask plugin from 1.71 to 2.1 and experience a slightly different behaviour.

    We have a mask to create a hole in a large semi transparent black sprite overlay. The mask doesn't seem to like being scaled from zero (0f,0f) as it did before. The whole black overlay disappears and then reappears when the scaling is greater than zero. The workaround is scaling from a tiny amount such as (0.001f, 001f).

    It's no biggie as we do have a workaround, but good to let you know if you need to tweak the plugin; or we've missed something in the update from 1.71 to 2.1 :)
     
  34. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @Richardbmth,

    Version 2 was a complete rework to make things much more user friendly and flexible, so corner situations like these might have changed, indeed. One question, though: does your mask texture have mip maps enabled?
     
  35. Richardbmth

    Richardbmth

    Joined:
    Mar 5, 2016
    Posts:
    29
    @DominoOne Just double checked, but as I thought they're not set as Mip Maps.

    From what I can tell with just playing with it now, when our small feathered square mask is set as zero scale, the mask is actually being stretched across the entire black overlay. So while the overlay looked like it was disappearing, it's actually a super sized mask/hole on the overlay. I can see the mask feathering off screen on the Editor Scene window while it is paused.

    Anyhow, if it's just a weird quirk from our upgrading, no worries. I'm quite happy with setting the scale infintely small. Works fine.

    P.S it was indeed much easier in 2.1 to reapply the mask than 1.7 :)
     
  36. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @Richardbmth even though not a huge one, it still sounds like a bug :) We'll look into it, thanks for reporting!
     
  37. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    We're finalizing support for TextMeshPro! Is there anyone, who would like to test the current version? :)
     
  38. joshua_42

    joshua_42

    Joined:
    Jun 23, 2016
    Posts:
    56
    Hi Domino.

    I have a 2d planet texture applied to a sphere and I need to be able to gradually fade out the part of the texture the camera is closest to, to reveal the mesh underneath.

    Could I do this with your asset?
     
  39. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    @joshua_42,

    Do you mean a gradual fade in with distance? Something similar to how fog works, just use it as transparency, not color? In case I'm misunderstanding, it would be great to get some kind of an illustration or a sample :)

    Also, what shader do you use on your planet?
     
  40. joshua_42

    joshua_42

    Joined:
    Jun 23, 2016
    Posts:
    56

    This package has a custom unlit shader I'm using, a planet texture and a transparency map. Use it on a plane or a sphere and use the alpha channel in the mask color for transparency.
    But I need something that will work with surface shaders.
    Hope this helps. :)
     

    Attached Files:

  41. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    I haven't checked your package yet, but please let me know what I should look in it in advance :)

    On the other hand, can you actually replicate the effect that you need with the unlit shader from the package? I mean, is the only problem you're having is that you need to use a surface shader instead of the unlit one?
     
  42. joshua_42

    joshua_42

    Joined:
    Jun 23, 2016
    Posts:
    56
    Yes, I need to use a surface shader. I know nothing about shaders and find myself needing one that unity doesn't supply (apparently there is no gradient fade in the standard cut-out). That's what brought me to your asset. I get the feeling there's probably a way I can do it by writing my own shader but yours seems like an ideal tool for many purposes.
     
  43. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    We have actually added support for any shader, but that version hasn't been published yet. Would you like to try it out? If so, drop me a private message with the invoice number of your purchase, and I'll send you the updated package then.
     
  44. hawken

    hawken

    Joined:
    Aug 22, 2013
    Posts:
    459
    hi @DominoOne I'm building with the latest version of Xcode / Unity / High Sierra on iOS 11 and I'm getting the following error:

    Shaders necessary for masking don't seem to be present in the project.
    ToJ.Mask:UpdateMasking()
    (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

    runs fine in editor.

    Masking doesn't work on iPhone
     
  45. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi @hawken,

    Could you try moving the shaders from the Alpha Mask folder to the Resources folder in the home Assets directory? That usually helps keep the shaders in the build :) Let me know if it works.
     
  46. IntelligentDesign

    IntelligentDesign

    Joined:
    Jun 13, 2014
    Posts:
    47
    Does this package allow for inverse masking (UI Image visible only outside of the masking object)?
     
  47. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi there! Yes, without a problem :) All you have to do is invert the mask image, so that the center of the image is black, and the edges are white (and of course make the texture Clamp).
     
    OreoSplitter likes this.
  48. LuisCRSousa

    LuisCRSousa

    Joined:
    Oct 30, 2013
    Posts:
    4
    Hi! Does this plugin supports dynamic masks? i.e.use an animator to update the Main Tex. I bought this asset and tried to do that but it give me this warning and error:

    Animator is not playing an AnimatorController
    Can't add component 'SpriteRenderer' to Image because it conflicts with the existing 'MeshRenderer' derived component!
    Unable to create animatable SpriteRenderer component
     
  49. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    412
    Hi! The best and easiest way to do this would be to render your Mask image into a RenderTexture (then you can render any animated objects into it) and then use it as the actual Mask image. We're doing something similar in the sample scene, in the fog of war example – the "holes" in the black image are animated and rendered into a single RenderTexture, which is then used as the Mask. Let me know if that helps :)
     
  50. OreoSplitter

    OreoSplitter

    Joined:
    Oct 21, 2017
    Posts:
    25
    I have an issue where the mask looks like it's moving when I continuously move the camera. The game objects aren't actually moving. It's feels like the mask isn't able to keep up though I'm not sure why camera movement should affect masking a ui. The faster I move the camera the further away the masks gets gets from it's starting point. The camera stops, and it's back where it should be nicely centered. I move the camera slowly to the right, the mask is slightly off center to the left. Go even faster and its even further away. So basically its fine without the camera moving. Using screen space. It's odd that no objects are moving yet continuous camera movement makes the mask move opposite direction with distance based on camera speed. Am using this all wrong?
     
    Last edited: May 8, 2018