Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

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

  1. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Glad I could help ;) And it's great that it was just a minor issue in your setup!
     
  2. pgeorges-dpt

    pgeorges-dpt

    Joined:
    Apr 7, 2016
    Posts:
    43
    Another voice here requesting TextMeshPro support please.
    Thanks!
     
    DominoOne likes this.
  3. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    An update for "Alpha Mask: UI, Sprites and Unlit 3D Objects" has just been rolled out. It introduces a multitude of new features:

    - Added more thorough Documentation, which answers some frequent questions and explains new features, as well.
    - Moved the masked shaders to the Resources folder, so that Unity would always include them in builds (NOTE: if you have two instances of each masked shader after the update, remove the old ones and keep the ones in the Resources folder).
    - Added the possibility to easily change the Alpha Mask texture in run-time.
    - Added the possibility to easily disable and enable the overall effect of an Alpha Mask.
    - Added the support for standard Unity UI masks for the UI elements that are already masked with the Alpha Mask.
    - Added the possibility to easily duplicate masked materials in run-time (useful when the masked hierarchy is duplicated or multiple instances of a prefab are used).
    - Fixed an issue, where after applying a new Alpha Mask on Unity UI Texts, the texts would become black.
    - Other minor improvements.
     
  4. Nickromancer

    Nickromancer

    Joined:
    Jul 31, 2016
    Posts:
    92
    Do you think you could have time to make a step by step VIDEO tutorial on how to use your mask in prefab? (and also update the mask texuture in runtime.)
    I think a video tutorial would definitely help people like me to understand exactly how to use it correctly.

    I updated to latest version and followed your instructions from PDF but failed to make things work.
    The masked objects are now even at the wrong position and i have to roll back.

    My usage is to use the mask in a prefeb and have that prefab instantiate a lot of instances which its mask texture need to change to another shape when needed.
     
  5. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    There was a small issue for some (or maybe most) cases of using DuplicateMaskedMaterials() and possibly ChangeMaskTexture(Texture texture). I have just submitted an update with that issue fixed, so let's hope it will reach the store early next week. Sorry about that!

    Nickromancer, is it possible that you called commands in this order (within the same block of code):
    - Instantiate()
    - DuplicateMaskedMaterials()
    - ChangeMaskTexture()

    If so, then it should start working after the update (let me know if you need the script earlier - I can send it to you directly).
     
  6. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    OK, so the fix is out. Duplicating materials in prefabs should now work as intended, when they are duplicated right after instantiating.
     
  7. monish_97

    monish_97

    Joined:
    Sep 15, 2016
    Posts:
    2
    Hello,

    I just stumbled upon this plugin for masking and I really really liked it.
    Now I want to create a system where I want to reveal parts of the sprite on which the player drags the pointer. Like a coloring book game or like those kids game where water and foam are displayed as the player drags the finger.
     
  8. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi monish_97,

    That sounds great! Although, was that a question or simply an expression of joy? :)
     
  9. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi wheretheidivides,

    Thanks for your interest in our plugin! What you're looking for can be achieved this way:

    - Create a RenderTexture.
    - Create a simple shader that always renders sprites in plain white.
    - Make a duplicate of the nearest camera and make it's "depth" (order) earlier than the nearest camera. For this camera set the RenderTexture as its target texture. Set the white sprite shader as the replacement shader of this camera. Also set the camera clear color to black.
    - Set the RenderTexture as the Alpha Mask of the black overlay that you have. In this step you can use our plugin :)

    Hope that helps!
     
    Last edited: Feb 21, 2017
  10. Ibukii

    Ibukii

    Joined:
    Jun 23, 2016
    Posts:
    45
    Hello, the alpha mask is bugged?? I have 2 similar projects. (The older one) works fine and uses alpha mask as intended. The new project which i copied without editting the alpha mask is not showing despite the mask having the same attributes as the older one. Is there a fix?
     
  11. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi wheretheidivides,

    I've just checked, and there actually is a shader in Unity that you can use for that – "GUI/Text Shader". So, no need to create a new shader, you can do everything with what you have (except Alpha Mask, which you still need to get) :)
     
  12. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi Ibukii,

    Could you elaborate what goes wrong and how exactly it looks in the broken project? I'd be glad to help, but it's very hard to say anything without knowing the details :)
     
  13. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    wheretheidivides,

    For the replacement shader you'll have to create a very simple script that does something like this on Start (if you want it to work in the editor, you can also make the script run in the edit mode) and assign it to the new camera that you've created:
    Code (CSharp):
    1. Shader whiteShader = Shader.Find("GUI/Text Shader");
    2. if (whiteShader != null)
    3. {
    4.     GetComponent<Camera>().SetReplacementShader(whiteShader, null);
    5. }
    Regarding your other question: if you have already purchased the plugin, there is Documentation that explains how to create the Alpha Mask, assign an image to it (in this case, the RenderTexture that you've created) and apply it to the other visuals. You can also see these steps in the video on the Asset Store.

    Let me know if you need anything else :)
     
  14. Nanoxin

    Nanoxin

    Joined:
    Mar 25, 2015
    Posts:
    3
    Hello,

    Copying sprites seems not to work, is that intended? And if yes: How can I change that?
    Reproduction can be done in the sample scene -> Copy the Unity UI Sample GameObject and rotate the new copy, the alpha mask for the other one will be messed up.

    Thanks!

    EDIT: Turns out, it didn't read the manually until the end, however the method described there does not allow me to use my own shaders for the actual sprite. Is there a way around that?
     
    Last edited: Mar 2, 2017
  15. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi Nanoxin,

    I'm glad you found how to make it work in the end :) As for shaders, the Alpha Mask only works with the default ones, but you could probably copy your custom functionality from your own shaders to the masked shaders.
     
  16. Ibukii

    Ibukii

    Joined:
    Jun 23, 2016
    Posts:
    45
    Hi DominoOne,

    Thanks for the reply. Reimporting the asset fixed the issue. Thank you
     
  17. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi Ibukii,

    That's great!
     
  18. markzorn

    markzorn

    Joined:
    Oct 6, 2016
    Posts:
    12
    Is it possible to create a mask from a custom mesh (generated on the fly) using this asset? If so, will I will be able to give it "soft" edges, so that there is a transition from on to off rather than a hard edge?
     
  19. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi markzorn,

    You cannot use a mesh as the Alpha Mask with our plugin directly – you need to have an image. However, you can render that mesh into a RenderTexture and then use it with our plugin. Also, when rendering to the RenderTexture you can apply anti-aliasing (RenderTexture has that functionality out of the box) or even blur (by using a full screen effect on the camera that renders the RenderTexture). This is not directly related with the functionality of our plugin, but I'm just explaining this to let you know that it's possible :)
     
  20. Ibukii

    Ibukii

    Joined:
    Jun 23, 2016
    Posts:
    45
    Hi, I'm having a problem with the mask. It works well in editor but doesn't seem to work at all in build. I'm not sure if it is the plugin problem or i just screw up settings somewhere. When i tried to build it in development build, this error keeps coming up: Shaders necessary for masking don't seem to be present in the project.

    However it works normally in Unity editor. Do you know how to solve the issue? Build.png Editor Version.png
     
  21. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hm.. I might know what the problem is, but it should have been fixed with one of the updates. Do you use the newest version of the plugin? If not, you should update. But if you're already using the newest version, could you try creating a Resources folder in the root of the Assets folder and then moving the shaders that are in the Alpha Masking/Resources folder to the root Resources folder? Let me know if that changes anything.
     
  22. Ibukii

    Ibukii

    Joined:
    Jun 23, 2016
    Posts:
    45
    Thanks! I fixed the issue by moving the shaders.

    Also, does the alpha mask works on TextMeshPro elements? I can mask the text UGUI but unable to mask the 3D text mesh element
     
  23. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Ibukii,

    As long as you use one of the two shaders – Unlit/Transparent or Sprite/Default – you can use the mask on those elements. However, TextMeshPro is using their own custom shaders (and about a dozen of them), so it's a little difficult to support them.

    Now that Unity is planning to integrate TextMeshPro as a native text framework, maybe they'll clean it up :) We'll have to see.
     
  24. Gstewart

    Gstewart

    Joined:
    Jul 23, 2013
    Posts:
    11
    Hi DominoOne,

    Can this be used to mask 3D objects rather than 2D panels. i.e. have a 3D object in world space mask another 3D object in world space. The same as this old asset did?... https://www.assetstore.unity3d.com/en/#!/content/33978
    If not, do you know of any asset that does manage this?
     
  25. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi Gstewart,

    Our Alpha Mask does work with 3D objects, but only those that use the Unlit/Transparent shader. And there is a limitation that it can only be mapped over 6 axes (X, X negative, Y, Y negative, Z and Z negative), so if you need the Mask to move along with the Camera or render a perspective view, it won't work. So, all in all, from what I see it looks like our plugin is not perfect for you, but in some specific cases it would work absolutely fine with 3D objects :)
     
  26. Gstewart

    Gstewart

    Joined:
    Jul 23, 2013
    Posts:
    11
    Thanks for getting back to me. Sounds like you are correct. I'm looking to be able to render objects only when they are inside a another object (sphere) and mask off the parts that are outside the sphere. Haven't found anything yet but if you know something I'd appreciate any advice or recommendations.
     
  27. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi Gstewart,

    Unfortunately, I don't know of any particular plugins that do that.
     
  28. soulareus

    soulareus

    Joined:
    Mar 2, 2014
    Posts:
    7
    This asset is wonderful! A couple issues I ran across though. First, I couldn't seem to set up a mask in runtime for quick testing. Secondly, I am not able to get a mask to work with uGui Images. I see it working in the demo though on my version. any chance you know what I'm doing wrong?

    included is an image showing sprites working but not images, as well as the inspector foldout for the mask.
    Thanks!
     

    Attached Files:

  29. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi soulareus,

    Thanks for the kind words! :) In your situation, I'm not fully sure what's happening. Are you using a "Screen Space - Overlay" UI canvas? It should work with this option, but it makes things confusing, as it doesn't map equally between the Scene view and Game view (e. g. I cannot see your Sprite in the Game view, which means that they're aligned differently, which might mean that the Mask isn't aligned with the UI, either). Also, could you try increasing/decreasing the size of the Mask within the canvas? Maybe it's just too small or large. Again, if you're using "Screen Space - Overlay" UI, you shouldn't look at the scene view at all, because Unity shows random sizes and positions there :) That's why I always recommend using "Screen Space - Camera" or "World Space" UI – it's much more intuitive.
     
  30. soulareus

    soulareus

    Joined:
    Mar 2, 2014
    Posts:
    7
    H DominoOne,
    I am using screen space - overlay. you are right, it does work I just had to move the mask down a bit for it to work. I think this little trick will work for me. I can't get around screenspace overlay so hopefully I can just make a positional map to keep it aligned. thanks for the quick reply!
     
  31. Argiris

    Argiris

    Joined:
    Nov 14, 2014
    Posts:
    23
    Hello.

    Does the plugin perform well on mobile with the RenderTexture technique?

    Thank you,
    Argiris
     
  32. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    soulareus,

    Great that it worked! :)
     
  33. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi Argiris,

    It shouldn't be a problem on mobile, but it all depends on how complex your Mask is. E. g. if you're only rendering a few soft masks into the RenderTexture, you can use a pretty low resolution RenderTexture – it should be pretty fast.
     
    Argiris likes this.
  34. soulareus

    soulareus

    Joined:
    Mar 2, 2014
    Posts:
    7
    Hello,
    I added a mask to a prefab and instantiated it but the mask does not seem to apply and gives me the following error when I click on the mask object:

    NullReferenceException: Object reference not set to an instance of an object
    ToJ.MaskEditor.OnInspectorGUI () (at Assets/Alpha Masking/Editor/MaskEditor.cs:17)
    UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1236)
    UnityEditor.DockArea:OnGUI()

    are prefabs not supported?
    thanks!
    -nic
     
  35. soulareus

    soulareus

    Joined:
    Mar 2, 2014
    Posts:
    7
    I saw you walking someone through this earlier and was able to get the error to no longer show up following this thread:
    Could you try following these steps:

    1. Create the Mask and the Sprite that you will mask (as you have done already).
    2. Manually create a material (within the assets) and assign the "Alpha Masked/Sprites Alpha Masked - World Coords" shader to it.
    3. Drag this material into the material slot of the Sprite that you've created in step 1 (the one that you want to mask).
    4. Create another material and assign the "Unlit/Transparent" shader to it. Also assign the Mask texture to this material.
    5. Drag this material into the material slot of the Mask that you've created (on the Mesh Renderer of the Mask).
    6. On the Mesh Filter of the Mask object choose "Quad" (the standard Unity mesh object) instead of the already assigned mesh.
    7. Hit "Apply Mask to Siblings" on the object with the Alpha Mask. You should now see the Sprite being masked as it should be.
    8. If everything worked so far, create a prefab from the hierarchy. You can now instantiate the prefab, and it should work as intended.

    Please let me know if that works :)


    so I'm closer to the solution. however the mask does not seem to effect anything yet.
     

    Attached Files:

  36. soulareus

    soulareus

    Joined:
    Mar 2, 2014
    Posts:
    7
    I think I figured it out. I didn't have the sprite material set up properly.
     
  37. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi soulareus,

    Hehe, it's great that you managed to find help in my previous comments. And I'm happy that you made it work! :)
     
  38. mmvlad

    mmvlad

    Joined:
    Dec 31, 2014
    Posts:
    98
    Does your plugin support inverse mask?
     
  39. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi mmvlad,

    You mean cut a hole in a visual element? If so, then, yes, it definitely supports that :) You just need to make a mask, which has white edges and black middle.
     
  40. dkollmann

    dkollmann

    Joined:
    Jul 25, 2009
    Posts:
    28
    I am trying to set up two objects with masks but I always end up with both objects using the same mask resulting in:
    • The mask image is always applied to one objects origin (right) so the other object gets the clamped alpha since the mask does not cover both objects (left).
    • Moving the mask moves it for both objects.
    First I thought the problem origins from a shared prefab but also when I set up both masks independantly I end up with one actual mask for both objects. Using different materials also does not help. So maybe the problem is a shared mesh quad?

    So the right object looks correct. The left one does not.

    Any suggestion on how to fix this? This really is a problem for me right now.

    Cheers,
    Dan

     
  41. dkollmann

    dkollmann

    Joined:
    Jul 25, 2009
    Posts:
    28
    Okay I found the solution. You have to set all the masked objects materials to None and then press "Apply Mask to Siblings..." button again. Then the object is independent of any other mask.

    Maybe some code should be added if the used material is already used by another mask?

    Also this is the result of using a prefab, maybe the code should also handle this?
     
  42. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi dkollmann,

    I'm glad that you found the solution! Prefab instantiating in the Editor is a little difficult to handle, and materials are handled this way to make it more optimal. However, we're constantly thinking of how we could improve that, and sharing use-cases like yours always helps identify what the real-world situations are and what people actually need :) So, thank you for that! We have greatly improved prefab handling, when they're instantiated in run-time (there are a couple of sections in the Documentation about that), but we'll also see if we could improve instantiating in the Editor.
     
  43. dkollmann

    dkollmann

    Joined:
    Jul 25, 2009
    Posts:
    28
    Hello Domino,

    I encountered another issue. In the editor I apply the mask and all objects get correctly updated. The material is correctly assigned to the Image component. Once I press "Play", the material reverts back to None.

    I added some logs to the mask game and editor script but could not find anything obvious.

    EDIT: The issue seems to be related to the masked object being disabled in the Start() function. I noticed that when I deactivate an object the mask does not get applied which imho is incorrect, because the object is enabled during gameplay.

    Do you have a suggestion what I could check for?

    Cheers Dan
     
    Last edited: Apr 15, 2017
  44. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi again Dan,

    I tried reproducing the situation from your description, but I couldn't... Would it be possible to get a project that demonstrated this flawed behavior? I would be happy to look at it.
     
  45. dkollmann

    dkollmann

    Joined:
    Jul 25, 2009
    Posts:
    28
    I am very busy right now since I need to finish that project but I will send you something to reproduce my issues after that.
     
  46. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
  47. Arkasis

    Arkasis

    Joined:
    Nov 22, 2010
    Posts:
    61
    Hi DominoOne,
    I just bought your plugin and I would like to know if it works with line renderers?
    And does it works with sprites dynamically generated?
    Thanks
     
  48. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi Arkasis,

    Yes, it works with Line Renderers :) And it works with dynamically generated Sprites, but a couple additional steps and/or lines of code might be needed. The Documentation should help with that, though (you can use prefabs if possible, and the Documentation has a section about instantiating masked prefabs).
     
  49. natsupy

    natsupy

    Joined:
    Feb 5, 2016
    Posts:
    17

    http://i.imgur.com/UnxKlNT.gif

    hi, i try to mask UI camera on the BG sprite like this, above i using sprite masking on unity 2017.1, it work so good, but still not stable, error when i moving camera using render mode "screen space - camera" so your asset can make like above ?
    Thanks your help!
     
  50. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Hi natsupy,

    Yes, our plugin can definitely do that :)