Search Unity

[Released] Sprite Mask - masking system for Unity Sprite

Discussion in 'Assets and Asset Store' started by PoL231, Jan 21, 2015.

  1. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi, SpriteMask works on Stencil buffer, therefore it does not support gradient mask (like Unity UI Mask). Object are masked when mask sprite alpha is less than 0.1 and visible when mask sprite alpha is greater than 0.1. See shader "SpriteMask/Mask":

    Code (CSharp):
    1. Line 81:     if (c.a < 0.1) discard;
    The mask sprite should be high resolution to get best result.
     
  2. ElKrullo

    ElKrullo

    Joined:
    Feb 24, 2013
    Posts:
    17
    Hi,

    I have a problem with SpriteMask in my game. I have several masks in different places in my level, and whenever i run "updateSprites()" on one of them, the objects masked by other masks disappear. All the masked objects use the same material. Could that be the problem, or do you have any other ideas?
     
  3. ElKrullo

    ElKrullo

    Joined:
    Feb 24, 2013
    Posts:
    17
    Actually, I think I can answer my own question. I tried instantiating the masked objects material whenever it gets masked by a new mask, and it seems to work now!
     
  4. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi ElKrullo,

    Yes, if you use the same material on different SpriteMasks then one instance will override "masking" of another one.

    Just ensure that each mask work on different material.
     
  5. Jeps66

    Jeps66

    Joined:
    Jun 23, 2015
    Posts:
    9
    Hi

    Just a quick question: Is it possible to mask TK2D sprites? I tried some simple tests but I can't get it to work. Before I quit and use regular sprites for the masked sprites I just wanted to ask to make sure I'm not missing something obvious
     
  6. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi Jeps66,

    SpriteMask works on Renderers, so it is completely independent of any toolkit, provided that objects are rendered via standard Renderer component.

    Assuming, that 2D Toolkit uses standard renderers, SpriteMask can work with it. All you need to do is to check if 2D Toolkit shaders support Stencil buffer.

    To get shader to work with Stencil buffer you need to check the Readme.txt from SpriteMask directory.
    Code (CSharp):
    1. // Can I use my own shader?
    2.  
    3. //    Yes, it is possible, but you should ensure that your shader support Stencil buffer.
    4. //    In your shader source code you should have this:
    5.  
    6.         Properties
    7.        {
    8.            // [...]
    9.  
    10.             _Stencil ("Stencil Ref", Float) = 0
    11.             _StencilComp ("Stencil Comparison", Float) = 8
    12.        }
    13.  
    14.         SubShader
    15.        {
    16.             Pass
    17.            {
    18.                 Stencil
    19.                {
    20.                    Ref [_Stencil]
    21.                     Comp [_StencilComp]
    22.                     Pass Keep
    23.                }
    24.            
    25.                // [...]
    26.            }
    27.        }
    28.  
    29. //    For more info see source code of shader "SpriteMask/Default" in file "Resources\SpriteDefault.shader".
    If you need more help in geting 2D Toolkit to work with SpriteMask let me know.
     
  7. Jeps66

    Jeps66

    Joined:
    Jun 23, 2015
    Posts:
    9
    Hi

    Thanks for the reply. I took a quick look at it and I think it would be possible. Not being used to working with shaders I'll stick to using Unity sprites for the stuff I need to mask. It's not a problem for what I'm doing right now.

    J.
     
  8. Graybull

    Graybull

    Joined:
    Jul 16, 2013
    Posts:
    2
    Hey PoL.

    I've been having an issue where the Stencil Ref number on the sprite will automatically increment after changing a scene, hiding it from the mask. Is there a fix for this?

    Thanks!

    Also,

    @Jeps66, I was able to get this to work with TK2D sprites. you can PM me if you'd like help with the shader.
     
  9. pep_dj

    pep_dj

    Joined:
    Nov 7, 2014
    Posts:
    179
    Hi!

    Would be possible to apply transparency to the inverted mask? I say, instead of make totally transparent the area behind the inverted mask, would be possible to make objects behind mask partially transparent?
     
  10. Clever Crow Games

    Clever Crow Games

    Joined:
    Jun 16, 2015
    Posts:
    1
    I'm also getting a black polygon around my sprites and the diffuse shader isn't making my items responsive to 2D lighting. As you mentioned earlier your example scene runs fine, but in all my scenes it isn't working properly (odd). Using latest sprite mask and Unity 5.1.
     
  11. Morgondag

    Morgondag

    Joined:
    Dec 29, 2014
    Posts:
    35
    Having a pretty basic problem with this plugin.
    I have this simple sprite, with a basic material based on unity's sprite-diffuse shader, nothing fancy.
    The sprite is marked here in the picture:

    sp1.PNG


    I then add a mask around the sprite and it loses the material and no longer revives light:
    as seen in this picture:

    sp2.PNG

    I then add the spritemask/diffuse to the sprite and it is able to pickup light again.
    But with a nasty black box around it!
    sp3.PNG

    How do we get rid of this nasty black box?
    Do i have to write a custom shader to solve this?
     
  12. Morgondag

    Morgondag

    Joined:
    Dec 29, 2014
    Posts:
    35
    is this related to this? this reported issue? dosen't that just mean that the "spritemask/diffuse" is just outdated???
    Where is the author??
     
  13. Morgondag

    Morgondag

    Joined:
    Dec 29, 2014
    Posts:
    35
    Solved the black border mess by creating a new updated diffuse shader.
    If anyone have problems with black borders just use this shader instead.
     

    Attached Files:

    ElKrullo likes this.
  14. Morgondag

    Morgondag

    Joined:
    Dec 29, 2014
    Posts:
    35
    custom shader solves this
     
  15. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi Graybull,

    Sorry for the delay, but I didn't get any notifications from the forum about new messages.

    So you are changing the scene - LoadLevel(), or you are adding a new scene - LoadLevelAdditive()?
     
  16. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi pep_dj,

    Sorry for the delay, but I didn't get any notifications from the forum about new messages.

    Unfortunately alpha on mask is not possible. SpriteMask works the same as Unity UI Mask; it can mask a pixel or not.
     
    pep_dj likes this.
  17. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi Morgondag,

    Sorry for the delay, but I didn't get any notifications from the forum about new messages.

    You are right. Shaders are a little bit out of date. I will update them based on the newest one from Unity Built-In shaders.

    Thanks for your updated shader and response to 'Clever Crow Games' message.

    Chears!
     
  18. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi 'Clever Crow Games',

    Current SpriteMask diffuse shader is out of date. I will send an update to the Asset Store. The shader provided by Morgondag in this post message is exactly the same as you will find in updated SpriteMask package.
     
  19. todsaporn-w

    todsaporn-w

    Joined:
    Jan 12, 2015
    Posts:
    4
    Does it work for Image Component under UGUI ( ui canvas ) ?
     
  20. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi,

    No it doesn't work for individual UI Image components, but you can mask a whole Canvas (CanvasRenderer).
     
  21. vladpazych

    vladpazych

    Joined:
    Aug 26, 2015
    Posts:
    6
    Hello. I have isometric game, where all sprites can be updated at runtime.

    I found this answer:
    But I don't understand how can I update sorting layer of mask at runtime? There is not setters as I see.
     
  22. todsaporn-w

    todsaporn-w

    Joined:
    Jan 12, 2015
    Posts:
    4
    I just bought it. Here is my need please you help.

    Please you checkout my image below.
    I need material of "sprite renderer component" place instead to material of "Image (Script)"
    Is it possible?



    ===============
     
  23. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi vladpazych,

    To change sorting order and sorting layer of SpriteMask, just get its renderer and set your values:
    Code (CSharp):
    1. SpriteMask sm = GetComponent<SpriteMask>();
    2. Renderer r = sm.GetComponent<Renderer>();
    3. r.sortingLayerName = ...;
    4. r.sortingOrder = ...;
     
  24. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi todsaporn.w,

    I'm not sure what you want to do... you want to set Unity UI Image material the same as on Sprite renderer? SpriteMask doesn't work on individual Unity UI Images. It works only on components that inherits from Renderer and CanvasRenderer doesn't inherit from Renderer.

    Please provide me some more info about your use case, so I can help you.
     
    Last edited: Jan 14, 2016
  25. todsaporn-w

    todsaporn-w

    Joined:
    Jan 12, 2015
    Posts:
    4
    Thank you for replying.

    Why I don't have material and shader on instantiate gameobject ?

     
  26. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi,

    I the SpriteRenderer above is masked then just after Instantiate() call this:

    Code (CSharp):
    1. SpriteMask.updateFor(yourSpriteRenderer.transform);
     
  27. vladpazych

    vladpazych

    Joined:
    Aug 26, 2015
    Posts:
    6
    PoL., thank you very much. Now it works perfectly.
     
  28. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    First off, great plugin, works as advertised and then some. I've used it to create a dynamic clipping set-up for a 2D combat game -- allowing an attacker's weapon to pierce the target, rather than just go in front or back of it. (I'm going to add an entry to my devlog about it, and then share it here.)

    One question, I'm dynamically adding sprites to the 'Masked Objects (outside this mask hierarchy)' list, after fighting an enemy I often have 10+ objects in this list, many of them duplicates. Should I worry about these adding up?
     
    Last edited: Jan 22, 2016
  29. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi '2 Ton Studios',

    I think, you shouldn't worry about many objects on the list. There are no Update() calls on them, so they don't have any impact on CPU performance. They are just rendered via regular Renderer (e.g. SpriteRenderer). If your sprites are on the same atlas, they also should be batched which makes it "cheap" to render.

    Cheers!
     
    v2-Ton-Studios likes this.
  30. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
  31. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
  32. daddylonglegs2014

    daddylonglegs2014

    Joined:
    Feb 13, 2016
    Posts:
    7
    Hi, I want to achieve both inverted masking part & masking part like the example scene 07 & 11. Using both "circle" and "blood" on the same "tree" mask. But when I try, I only can achieve one of them, please help. Thanks
     
  33. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi daddylonglegs2014,

    Unfortunately it's impossible to have normal and inverted masking on the same time on one SpriteMask. Maybe send me your scene description, so I can help you to achieve your target be using two masks.

    Cheers!
     
  34. daddylonglegs2014

    daddylonglegs2014

    Joined:
    Feb 13, 2016
    Posts:
    7
    I've sent you my scene via email. Please check it. It almost worked except a tiny problem, some black circle like this. It's great if you can solve this. Click on the body to "shoot"
     
    Last edited: Feb 15, 2016
  35. TheWarper

    TheWarper

    Joined:
    Nov 3, 2013
    Posts:
    112
    Hi, is it possible to apply multiple inverted masks to 1 sprite?
     
  36. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi,

    Yes it's possible. See this demo.

    You can see how it works on example scene: '11 - Inverted & Masking part'
     
    TheWarper likes this.
  37. TheWarper

    TheWarper

    Joined:
    Nov 3, 2013
    Posts:
    112
    Works great, Thanks!
     
  38. Tinytouchtales

    Tinytouchtales

    Joined:
    Dec 30, 2013
    Posts:
    23
    Hey,

    bought the plugin and it does work pretty nicely. I do have one question tho:
    It seems that it's not possible to have multiple sprites that share the same sprite texture atlas (2D Toolkit) be individually masked by different masks. For me only one mask is applied to all sprites that use atlas 1 for example.
    Is there a way to get it to work with atlases ?

    Thanks
    Arnold!
     
  39. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi Arnold,

    Masking works on rederers (e.g. SpriteRenderer), so if each of your sprite has its own renderer, regardless whether the sprite comes from atlas or not, each sprite can have its individual mask.
    Check whether your sprites on your scene have own renderers. If not then individual masking will not work.

    See also: http://forum.unity3d.com/threads/re...m-for-unity-sprite.292453/page-4#post-2414799

    I hope this will help you.
    Cheers!
     
  40. Tinytouchtales

    Tinytouchtales

    Joined:
    Dec 30, 2013
    Posts:
    23
    Ok,

    i'm not sure but both my objects have individual mesh renderes. I'm using a plugin called uniSWF to use flash animations within Unity. Those i need masked.
    here's a gif to see what happens:

     
  41. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
  42. Zeldor

    Zeldor

    Joined:
    Mar 29, 2016
    Posts:
    2
    Got a question, (I didn't see this if it's already asked and answered) I am using Tiled2Unity to make and import my maps. When the maps are imported, they are made with a mesh renderer. Now, let's say I have a normal sprite with sprite renderer that I want to be masked by my imported level (that is a mesh)- like a paint splash on the level. Is this possible? (and does it require extra work?)
    EDIT:
    I just want to be sure that I wouldn't be wasting money. If what I described will work, I'll be thrilled.
    EDIT2:
    Upon further reading, it appears that this will work! (Unless I'm retarded, which is totally possible)
    EDIT3: So, after some experimenting, Tiled2Unity does not import textures or sprite, just materials and mesh, and I can get the masking to work with just the mesh, but not with the material applied, is this just something that SpriteMask does not do, or would you like more info?
     
    Last edited: Mar 30, 2016
  43. Mempel

    Mempel

    Joined:
    Feb 20, 2016
    Posts:
    1
    Hi,

    I'm interested in your plugin to make a jigsaw puzzle, using individual sprite pieces as masks for the puzzle image. My question is, will unity only draw what the mask reveals or will it draw the entire underlying image, under the hood?

    In other words if I have, say, 30 puzzle piece masks, each masking the full image, will unity need to draw the entire large image 30 times, even if only the masked areas are drawn?

    I hope that makes sense.

    Thanks
     
  44. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi Zeldor,

    I'm not sure what you mean, please give me some more info, your hierarchy, screenshots or maybe an example scene. That would be very helpful.
     
  45. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    H Mempel,

    You can have 1 puzzle image and unlimited number of mask parts. All parts can be batched as one draw call. So in theory you will have only 2 draw calls for your scenario.

    I hope this will help you.
     
  46. Zeldor

    Zeldor

    Joined:
    Mar 29, 2016
    Posts:
    2
    Sure thing, how can I send you a sample scene? Email? (if so, what's the email address?)
     
  47. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    I've sent you PM.
     
  48. detournment_19

    detournment_19

    Joined:
    Jan 20, 2015
    Posts:
    22
    I'm having a bit of trouble figuring out how to set the sprite for mask programmatically at runtime. I've tried setting the sprite property of SpriteMask, but when I look at it in the editor after that, the sprite renderer and sprite property are still empty. I couldn't find any example code of how to do this. Any hints?
     
  49. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi,

    Check out the scene '05 - Runtime mask'. You can set SpriteMask at runtime like this:

    Code (CSharp):
    1. SpriteMask sm = GetComponent<SpriteMask>();
    2. sm.type = SpriteMask.Type.Sprite;
    3. sm.GetComponent<SpriteRenderer>().sprite = yourSprite;
    4. sm.update();
    Cheers!
     
  50. dev_alleylabs

    dev_alleylabs

    Joined:
    Sep 30, 2015
    Posts:
    2
    Hi there,
    Is this compatible with 2dtoolkit? Can I use it with tk2dSprite without changing shader?

    Thanks