Search Unity

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

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

  1. ryo0ka

    ryo0ka

    Joined:
    Sep 27, 2015
    Posts:
    32
  2. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    That sounds great! :)
     
  3. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I have a problem with Unity 5.4.3f1

    In editor everything works, but on Windows Build I got!

    "Shaders necessary for masking don't seem to be present in the project."

    I tried to copy the shader to Resources folder but without luck!

    Any advice?
     
  4. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Hi sevensails,

    Sorry for a late reply, I was away for a few days. That is very strange. Did you move both shaders? And did you copy or move them?
     
  5. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    Yes... Nothing works!

    I made it working by removing the Shader.Find on your code, making it public and setting them on Inspector.
     
  6. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Hi sevensails,

    Well, that was weird - I've never heard of this case before. But I'm glad that you found a work-around!
     
  7. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I have another problem! Disabling the mask on runtime does not work as expected! It stays visible... Is this expected?
     
  8. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    It doesn't work like that at the moment, and so far, you are the first one to mention this kind of functionality :) But it sounds very logical, so I have implemented the necessary changes for it to work. I'll send that version to you in a private message ASAP.
     
  9. jhollands

    jhollands

    Joined:
    Apr 10, 2014
    Posts:
    12
    Hey DominoOne,

    I'm trying to apply the mask to objects while the game is running using script. To do this I'm grabbing the material from another component which has the "Default UI Material" and applying it to the newly created object's materials.

    It masks the objects fine, but appears to make text turn black. Am I doing this incorrectly?

    Thanks,
    Jake

    Edit: never mind — fixed it!

    I do have a new issue though — I have 3D objects showing through UI elements which have a mask applied, but are (correctly) visible through the mask.

    i.e. a tint with a mask material applied — my 3D unlit/color objects are showing through the tint when it should cover them.

    I had this working previously, but it appears to have stopped working with particular objects...
     
    Last edited: Nov 30, 2016
  10. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Hi jhollands,

    I'm glad that you managed to solve your issue! :) Regarding your other problem: could you attach an image or two? It would be much clearer.
     
  11. jhollands

    jhollands

    Joined:
    Apr 10, 2014
    Posts:
    12
    Example attached.
    All visible UI elements here have a mask attached to them. The mask is currently over them (so it's correct that they're visible). They're on a screenspace - overlay canvas. The UI consists of a blue tint image & white & blue text.

    The yellow sphere & small blue sphere (I've highlighted them with red squares) are rendered in 3D with a seperate camera and have no mask at all. They should be rendered behind the UI, but some elements (3D unlit/color) cut through the mask as seen. There are other elements (also 3D unlit/color) being rendered by a second camera which are remaining behind the UI, as they should. The issue only appears to be on one camera.
     

    Attached Files:

  12. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Thanks for the illustration!

    Does it work as expected when you don't use the Alpha Mask on the UI elements?
     
  13. jhollands

    jhollands

    Joined:
    Apr 10, 2014
    Posts:
    12
    If I delete the 'Default UI Material' from a UI element, it renders over the 3D elements as it should.
     
  14. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    That's interesting. Have you tried changing the rendering layer (or order in layer) for the UI canvas? Or is that not an option?
     
  15. jhollands

    jhollands

    Joined:
    Apr 10, 2014
    Posts:
    12
    Changing the order of the UI canvas has no effect. What's odd is that when I change the depth of my 2 cameras however, there is a change — whichever camera is rendered on top cuts through the UI elements.
     
  16. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Sounds to me like weird Unity behaviour. The "Screen Space - Overlay" canvas should always be on top, no matter what material/shader you use :/ But let's try to find a solution, after all.

    What are the depths of the two cameras that render 3D objects? Can you try making both of their depths negative? Both of them positive? One negative and one positive?

    Can you also try double clicking on the masked material (the one that is assigned to the masked elements when you click "Apply Mask"), selecting the Debug mode on the Inspector (on the top right corner you can expand the Inspector option list and select Debug). You should see a new field named "Custom Render Queue" - could you try entering various values into this field (e. g. -3000, -1000, 0, -1, 1, 1000, 3000). Does that do anything?
     
  17. jhollands

    jhollands

    Joined:
    Apr 10, 2014
    Posts:
    12
    None of those worked.

    It's worth noting that I had this working fine previously. It's only during an optimisation overhaul that things have started to break — originally all 3D objects were there at runtime, now I instantiate them from prefabs. I've tried having them there at runtime again, but no luck.
     
  18. jhollands

    jhollands

    Joined:
    Apr 10, 2014
    Posts:
    12
    Okay, I haven't solved the problem but I've found a solution.

    During my overhaul I deleted an obsolete camera which had a higher depth than any other camera. If I check out my project pre-overhaul it works fine, but when I delete this camera I get the same issue.

    So, to solve this I've added an extra camera to the scene with Culling Mask set to Nothing. As long as there is an obsolete camera drawing after all other cameras, there's no issue.

    Certainly a messy work-around, but it'll do for now. Hopefully that can help you with whatever bug this may be though.
     
  19. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    That is very useful information, thanks! Although it still looks like a bug in Unity.. Could you try one last thing: could you manually create a new material, assign the Default UI shader to it and assign that material to the UI elements. Does that work fine or break, as well? And which Unity version are you using?
     
  20. jhollands

    jhollands

    Joined:
    Apr 10, 2014
    Posts:
    12
    This worked fine without a mask applied — once the mask was applied again, the problem persisted.

    I have a new barrier (I'm spending today getting these masks the way that I need them to). The only way that I can find to solve it is to have a completely white mask which I can easily change the dimensions of — with elements not covered by this white mask being invisible. It looks like I need black in the mask to do this though.

    Is there a good way to mask a square, pixel-perfect, and be able to accurately place it's corners over the element I'm trying to make visible?


    Edit: I've come up with a workaround for this too. I created a mask image — 3x3 square grid, with all black except the center square which is white. I apply this to a mask, then make the mask 3 * the size of the area that I want visible. Please let me know if you have a cleaner solution to this!
     
    Last edited: Dec 1, 2016
  21. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Your workaround sounds pretty good. I can't think of any better way to achieve what you are looking for :)
     
  22. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    Hi.
    I'm trying to masking Anima2D sprite mesh(2-d skinned mesh).
    I've tried alpha mask but It does not seem to work well with skinned mesh.
    Any way to do it?

    Something that show only red box area of attached image(anima2d t-rex sample).
     

    Attached Files:

    • trex.png
      trex.png
      File size:
      160.5 KB
      Views:
      396
    Last edited: Dec 9, 2016
  23. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Hi young-xyz,

    I've just tested our Alpha Mask on an object that has a SkinnedMeshRenderer, and it seems to work fine. Have you applied the mask in the same way as described in the ReadMe (and as it's used in the samples)?
     
  24. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    Thanks DominoOne!
    Alpha Mask works great with skinned mesh, but I was mistaken my problem is for skinned mesh.
    I need to mask Anima2D "Sprite Mesh"(see former attatched trex image inspector).
    Do you have a knowledge of it?
     
  25. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    young-xyz,

    Oh, OK, I thought that the "Sprite Mesh" uses the Skinned Mesh Renderer to actually show the visuals. Well, officially we don't support third party renderers, but there is an easy workaround for your situation:

    - Manually create a material within your assets. Assign the Alpha Masked Sprite shader to that material.
    - Manually assign this new material to all objects that you want to mask.
    - Create a regular Unity Sprite within your scene (it can be empty, it just needs to have a SpriteRenderer on it). Disable the SpriteRenderer on that object, but keep the object active.
    - Assign the material that you've created in the first step to the new Sprite.
    - Create an Alpha Mask, as you would normally.
    - Make the new empty Sprite a sibling of your Mask.
    - Click Apply Mask, and you should be good to go :)

    Let me know if that works.
     
  26. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    Thanks for help, DominoOne.
    I tried to follow what you said, but it' not work.
    Object hierarchy is like this(empty_sprite is the object with diabled SpriteRenderer with Alpha Masked Sprite material)

    hierarchy.png

    The inspector for the object to mask is:
    inspector.png

    The material 'house_0' in Skinned Mesh Renderer could not be changed.
    Maybe that's the cause? or did I misunderstand something of your instructions?
     
  27. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Hm.. Again, I'm not sure how exactly the "Sprite Mesh" works, so it's hard to tell. Could you also try to assign the same Alpha Masked Sprite material to the Skinned Mesh Renderer, as well? If that doesn't help, I could look into your project (or a little sample project with "Sprite Mesh" included) if you sent it to me.
     
  28. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    I'll send zipped sample project to you. How can I send it?
     
  29. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Just drop me a private message and attach it there (or if that won't work, I'll give you my e-mail address) :)
     
  30. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    It seems that file attachment is not possible in private conversation. Can you give me your email address?
     
  31. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    I just wrote you a private message, check it out :)
     
  32. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    Thanks a lot DominoOne, It works great!!
     
  33. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Sounds great! Glad I could help :)
     
  34. chenghaowei

    chenghaowei

    Joined:
    Mar 22, 2015
    Posts:
    1
    When do you expect to support TextMeshPro?
     
  35. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Hi chenghaowei,

    I've looked into TextMeshPro before. It's a bit messy, and it would be difficult to properly support it (including its updates). So, it's not off the list yet, but it's probably not coming very soon.
     
  36. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    Hi DominoOne,
    I'm trying to apply Alpha Mask in runtime.

    As shown in the attached image, mask does not work horizontally.
    How can I correct it?
     

    Attached Files:

  37. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Hi,

    I'm not sure I understand what you're trying to do. Do you have everything set up and only want to replace the Mask image in run-time? Or do you want to set everything up in run-time?
     
  38. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    First, I tried to set up prefab and use it in run-time, but it missed mask material and not worked.
    So, I set everything in run-time and made mask applying script by referring to Alpha Mask MaskEditor.cs.
    Any way is OK, I'm just want to instantiate mask object in run-time.
     
  39. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Do you instantiate only the Mask or the Mask with the masked elements?
     
  40. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    I instantiated object to mask as prefab, and other objects(mask, mask-material sprite and their parent) via script.
     
  41. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
  42. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    It works well in only one instance, but I need to instantiate multiple instance.
    To do it, I found that separate instance needs separate Alpha Mask material in object to mask.
    So this time I instantiated prefab(made as written in your link) in run-time, and make separate material each, and apply mask. But the result was the same as first(not masked horizontally).
    And I found something strange. As I resize mask object horizontally, the object to mask becomes translucent short time, and invisible all object. Is this something related?
     
  43. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Could you show me how your Mask image looks?
     
  44. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    I moved left border from left to right, and attached screenshot is the moment that mask becomes translucent, and move left border to right a bit more then the object disappears.
     

    Attached Files:

  45. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    I'm sorry, but could you show me how exactly the Mask image looks? :)
     
  46. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    Oh, about mask material image? It's just white image with its edge is black(1px).
     

    Attached Files:

  47. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Yes, that's what I wanted to see. Sorry if I was unclear before! :) Could you try making the black edge wider (~5-10 pixels)? And could you also try turning off mipmaps on this texture?
     
  48. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    I've done what you said, but it's no difference. And I tested 16x16 mask with 1px border, too. It's also not masked horizontally.
     
  49. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    413
    Could you send me your project again? :) I'll look into it.
     
  50. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    Thanks DominoOne, It works perfectly!!