Search Unity

[Released] Sprite Mask - masking system for Unity Sprite

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

  1. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    Are the examples included with the asset, namely something very close to what you did cook up here. I am looking to create a cheap/easy lighting effect like the old 16bit era days for lights out levels and I think this is how I want to go, and it does seem like I could have multiple 'light' sources yes? (i.e. many masks)
     
  2. PoL231

    PoL231

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

    This example is created with a beta release of SpriteMask. If you have a license, send my your Invoice number and I will send you back beta release which will include mentioned example scene.
     
  3. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    83
    Hi, I wanted to ask a question regarding Spine Shaders & Sprites as well (This post was from page 1).

    It seems like you got the Spine Object working UNDER a rectangular mask in this picture, but the effect I would need is the other way around: I need the Spine Object to BE the mask and another sprite object to be masked by it (underneath).
    Is this in any way achievable? (I know soft edges are not possible yet but maybe with hard edges?)

    Thanks for your time & effort, if this works your asset is exactly what I need!
     
  4. PoL231

    PoL231

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

    I'll have to check it out (Spine object as mask). I'll let you know in a few hours.
     
  5. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    83
    Thanks, much appreciated! Looking forward to your answer! :)
     
  6. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Unfortunately SpriteMask in its current form will not help you with your problem. Spine objects consist of many material (they have invisible mesh renderers), which makes masking work incorrect due to overriding of Stencil buffer values. Each masking material is setting Stencil values. So if you have more material which are manipulating Stencil buffer, each next material overrides values set by te previous one. The end effect is that only one part of spine object takes part in masking (the last one that was rendered).

    In current form of SpriteMask there should be only one material which is setting Stencil buffer values.

    In my opinion you could use masking based on Stencil buffer in your scenario, but not in current form of SpriteMask.

    Maybe if you send me your scenario/example I could suggest you which way to go.
     
  7. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    83
    Thanks for your effort, that's unfortunate :(
    I posted basically the same question here:
    http://forum.unity3d.com/threads/2d...-sprite-thats-positioned-above-a-mesh.346693/

    This also shows 2 screenshots I made to show what I mean, maybe this will help you?
    Unfortunately, no one responded yet and your product actually seemed like the only possible solution to the issue (except of course programming it myself, which I can't do).

    Well, if you can think of any other way how to achieve the effect, I would be overjoyed :) Thanks for your time, much appreciated!!

    Cheers
     
  8. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    83
    Update: I bought the asset and played around with it. Actually, I kind of got it to work by using particular Sprites of the Spine Object as Sprite Masks and attaching them to the Spine-Bone in Unity. It's kind of hacky but it works!

    Now, another problem, but I'm not sure if this is even possible:

    Is it at all possible to SOFTEN the edges of the mask, bet it sprite or texture mask? I wouldn't need any fancy gradient alpha values, just a Little bit of softening because right now, it has no anti-aliasing whatsoever and looks a bit jarring.

    Or is this (as I read before) not possible at all? Maybe some trick?

    Thanks again for this awesome asset!
     
  9. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Glad to hear that you worked around the spine object masking problem.

    With the second problem (soft edges) I don't have good news. Stencil buffer allows to set only one value per pixel and that value is taken into account while rendering masked object. There is no way to pass extra info about alpha value.

    Even in Unity UI Mask there is no such thing as soft edges (they also use Stencil buffer for masking).

    I'm not shader expert so maybe there is a way to get that working, but SpriteMask in current form (and in in incoming beta) doesn't support soft edges.
     
  10. puppets

    puppets

    Joined:
    Jun 22, 2015
    Posts:
    1
    hi...
    I've bought this great plugin... it is possible of doing multiple mask on one sprite...
    I need it to create a leaf that eat by a worm little by little.... I like to do it via masking..

    so it like the leaf is have multiple mask... and when it bite by the worm.. a little mask is active on it.. to make a hole or bite effect....

    Thanks for your support
    Viky
     
  11. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi, send me your Invoice ID and I will send you SpriteMask package with multi mask example (on one sprite).
     
  12. Ben Jammin Games

    Ben Jammin Games

    Joined:
    Nov 19, 2012
    Posts:
    13
    Hi, is there any update on when the version that can use a flat hierarchy will be released?
    I bought this asset because my current masking solution is very expensive on mobile (my shader is costly and also my method forces new instances of all materials when i adjust masks). It solves all of my problems but one:

    I have an enemy type with 2 children, identical sprites, at different positions. When one "enters" a portal I'm masking it out at that position and I start masking in the other sprite at another portal.

    (see image)



    I could go about readjusting my code to work with your hierarchy system if I have to but it would be great if I could just set them at runtime and keep my current movement system.


    I tried setting up something where the masks are set to position at start based on where portals will be, and then any time I spawn a portal Enemy it sets portalInMask.updateSprites(spriteProxy); portalOutMask.updateSprites(sprite);

    which sets everything appropriately but no masking since the objects aren't in the mask hierarchy.

    Any thoughts? or an early build of the flat hierarchy version?

    Thanks!

    PS the plugin is awesome !!
     
  13. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    HI Ben,

    Flat hierarchy works already in beta package. Send me your Invoice number and I'll send you back your package.
     
  14. sagat64

    sagat64

    Joined:
    Jul 20, 2013
    Posts:
    9
    Hi. Can i mask NGUI obujects by this asset?
     
  15. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi.

    Unfortunately not. Reason for that is because NGUI renderers are hidden objects in the scene. There is no access to them, so you can not change their material properties.
     
  16. sagat64

    sagat64

    Joined:
    Jul 20, 2013
    Posts:
    9
    I well understand. Thank you very much.
     
  17. igrir

    igrir

    Joined:
    Jan 24, 2014
    Posts:
    16
    Hello @PoL. I've got the problem from the plugin of yours. My team and I used the Sprite Mask with the Unity's mask UI. The mask I used was Image with Filled type. I used it for radial mask for the player's UI. When your plugin was in front of the UI, the mask also affected of it. Can I, somehow, ignore this effect?
     

    Attached Files:

  18. PoL231

    PoL231

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

    Send my your Invoice number via private message, and I will send you a beta release which can coexist with Unity UI Mask.
     
  19. ababab5

    ababab5

    Joined:
    Apr 15, 2014
    Posts:
    508
    Hi,

    Just bought your plugin. But I have some issue.

    In fact I spawn a lot of sprite as a child of an other sprite who have to be the mask.

    But the parent sprite have a scale of 50x100.

    And it just doesn't work.

    The sprite I spawn have this structure :
    transform and sprite as child.

    Thanks for the help.

    Best regards,

    AB
     
  20. Dmsotir

    Dmsotir

    Joined:
    Feb 12, 2013
    Posts:
    43
    couple questions... Will this work on mobile? Will the mask stay the same size, no matter the resolution? im having an issue now where my camera is changing sizes on resolution revealing behind the scenes thigns that i cant have being revealed.
     
  21. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi

    If you are attaching sprites under SpriteMask at runtime, you have to call update() on your SpriteMask object.
    If this is not the case, you need to tell me more about your use case.

    PS Scale doesn't matter while masking.
     
  22. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi

    SpriteMask does work on mobile... it works on any platform.

    Size of the mask is specified in the Unity word units.
    Maybe you need to search for more info about Unity world units vs screen resolution e.g.:
    http://gamedev.stackexchange.com/qu...handle-aspect-ratio-differences-with-unity-2d
     
  23. ababab5

    ababab5

    Joined:
    Apr 15, 2014
    Posts:
    508
    Hi,

    Thanks for the reply.

    We can't use scale ?

    And gradient shader seems to not work ... is it right ?

    Thanks again !
     
  24. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    I mean... you can use any scale you want. :)

    SpriteMask doesn't support gradient alpha masking.

    Cheers!
     
  25. Dmsotir

    Dmsotir

    Joined:
    Feb 12, 2013
    Posts:
    43
    I now about units vs screen resolution, but the sprite mask will stay a consistent size then just like a normal sprite and what not?

    Ohh also, do you happen to know if things behind the mask are considered visible by the renderer? As in Renderer.isVisibile returning false if it is to the side of the mask
     
    Last edited: Sep 12, 2015
  26. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Sorry to say, but unfortunately I don't understand your problem. Maybe send me some screen shot.

    You mean to check whether Sprite is masked (isVisible=false) or is visible to the user (isVisible=true). Right?

    If so, unfortunately, it is not possible to verify it on SpriteMask. Masking is done on the GPU, and there is no way to ask GPU if specific sprite is masked out or not. You can write your own check method, for example by using Rect.Overlaps() function on your sprite and SpriteMask renderer bounds (this would work on rectangular mask type).
     
  27. Donghai.Ren

    Donghai.Ren

    Joined:
    Apr 27, 2015
    Posts:
    3
    Hi. Can i use your SpriteMask instead of Unity UI Mask in my UI? The Unity UI Mask cost so many draw call.
     
  28. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi,

    In theory yes, because both SpriteMask and Unity UI Mask use the same masking techniques - Stencil buffer. It would be possible to mask a whole Canvas, because it has it's own renderer, but NOT individual widgets.

    SpriteMask was not designed to work with the Unity UI components so I don't give any guarantee that you will achieve your goal by using SpriteMask instead of Unity UI Mask.
     
  29. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    That's strange, I've just upgraded to 5.2.1f1 and I have no troubles. Does the problem still exist?
     
  30. EParent

    EParent

    Joined:
    Feb 13, 2015
    Posts:
    36
    Nope, sorry it was my fault. I had 2 SpriteMask.cs....that's why I deleted my post
     
  31. pampapic

    pampapic

    Joined:
    Sep 14, 2013
    Posts:
    11
    Hi,

    I just bought sprite mask and i'm facing a problem on iOS device (not tested on android yet).
    I just have an empty gameobject with SpriteMask.cs attached to it and i've made a simple rectangle mask.
    I have my masked objects (outside of the mask hierarchy) dropped in the script (prefabs form my project that are spawned with poolmanager at runtime).
    It works in the editor's game view, but when I build on iOS, my sprites are purple / pink (the default missing material color) and are also not masked. I'm using Unity 5.0.2f3 (no "Use 24-bit Depth Buffer" parameter in this version build settings).

    Am I doing something wrong ?
     
  32. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi, is it possible to produce some example scene, so I can look in to it?
     
  33. pampapic

    pampapic

    Joined:
    Sep 14, 2013
    Posts:
    11
    @PoL. Hi, I found how to make it works. Here is some explanations:

    The pooling system I use is poolmanager, basically it loads prefabs as children of a "pool" gameobject (with the poolmanager script attached) at runtime.
    Then it just activate (spawn) these loaded gameobjects on demand while the game is playing.

    First I tried to add the prefabs I want to mask to "masked objects" fields of the sprite mask script, but it doesn't worked, at least on iOS (works in editor views)
    Then I tried to simply add the sprite mask script to my "pool" gameobject and it works like that.

    It seems that the "masked objects" of sprite mask script doesn't work properly with prefabs from the project, but it works properly with prefabs already in the hierarchy (in the game scene). After being spawned it seems that sprite mask can't find the original prefab material / texture from the project itself.

    I hope my explanations are understandable :)
     
  34. RoyVanDeMortel

    RoyVanDeMortel

    Joined:
    May 19, 2015
    Posts:
    3
    Hi there

    I am trying to do some rather complicated UI animations in the canvas that I thought I could really use this for (particularly because of the flat hierarchy feature which is awesome for me because we need UI animations in multiple aspect ratio's) I was aware that sprite renderer does not work in the Canvas, but I was not aware that your sprite mask script wouldn't work on image (script) in the UI with canvas renderer. I guess it wasn't a fair assumption it would but I assumed since the image script also takes sprites. Especially since there's also a Spritemask & UI Mask scene in your package, but that seems to use the regular unity mask component right?

    Any chance I am overlooking something, or that you're working on this?
    Now I am sort of stuck since there's a big bug in the current Unity preventing me from animating masks :( Untitled.png
     
  35. PoL231

    PoL231

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

    SpriteMask can mask object which has its own Renderer component (e.g. SpriteRenderer, MeshRenderer). Unity UI components are constructed by filling vertices in a buffer, and then renderer by Canvas. As I said, SpriteMask can mask objects with Renderer, and Canvas has its own renderer but it's hidden in the inspector. So in theory SpriteMask can mask everything that is under particular Canvas. There is no way to selectively mask individual components, only the whole Canvas.

    I hope my answer will be helpful for you.
     
  36. Kheryo

    Kheryo

    Joined:
    Oct 7, 2015
    Posts:
    6
    Hi,

    I recently purchased the Sprite Mask asset, and it worked wonderfully at first.
    However, I now have to have multiple objects in the same scene, each with their own Sprite Mask, and masked sprites.
    The problem is that whenever I have multiple Sprite Masks in the same scene, it seems the Mask that was enabled most recently overrides all other masks somehow.

    Here's an example of the problem I encountered :
    Single mask :

    Two masks :


    As you can see, the mask on the left completely hides any masked content outside of it. It also displays the content of other mask objects (racoon's tail on the right from the other mask).

    I tried toying with sorting order, to no avail.

    I'm using Unity 5.2.1f1 Personal

    My only guess is that I'm doing something wrong. If so, could you please point me in the right direction ?
     
  37. alecli

    alecli

    Joined:
    Jul 22, 2014
    Posts:
    4
    Hello,
    I just purchased the Sprite Mask, is this version support "Spine" object?
     
  38. PoL231

    PoL231

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

    The problem is in sorting orders of sprites and Sprite Mask. Rendering queue depends on sorting orders. So if you have 2 Sprite Mask on the same sorting order, for example on sortingOrder = 0, and sprites on sorting orders e.g. 1 - 10, then the last rendered Sprite Mask (thay both on the same sortingOrder so the order of rendering is random) hides all of the sprites.

    This is because SpriteMask (while rendering) sets Stencil buffer values, and SpriteMask rendered one after another overrides this values. Next sprites are rendered and they read Stencil buffer values and use it to determine if it's hidden or not.

    To get that example working, you should set proper sorting orders in that way:
    SpriteMask1 - sortingOrder = 0 (setting Stencil buffer values)
    Sprites - sortingOrder = 1-9 (reading Stencil buffer values and rendering)
    SpriteMask2 - sortingOrder = 10 (setting Stencil buffer values)
    Sprites - sortingOrder = 11 - 19 (reading Stencil buffer values and rendering)

    I hope my answer will be helpful for you.
     
  39. PoL231

    PoL231

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

    All you need to do is to extends spine shaders to support Stencil buffer.

    Put this in the Properties section:
    Code (CSharp):
    1.  
    2. Properties {
    3.     [...]
    4.     _Stencil ("Stencil Ref", Float) = 0
    5.     _StencilComp ("Stencil Comparison", Float) = 8
    6. }
    And this in the SubShader section:
    Code (CSharp):
    1.  
    2. SubShader {
    3.     Stencil
    4.     {
    5.         Ref [_Stencil]
    6.         Comp [_StencilComp]
    7.         Pass Keep
    8.     }
    9.     [...]
    10. }
    11.  
     
  40. Kheryo

    Kheryo

    Joined:
    Oct 7, 2015
    Posts:
    6
    Hi, thanks for the answer.

    The problem persists when modifying sorting orders.
    Pic :


    "Sorting Layer" : Default for all

    "Order In layer" :
    Mask : 0
    |- Animal1 : 2
    |- tree : 3
    |- Animal2 : 4
    '- Cloud : 5

    Mask (1) : 10
    |- Animal1 : 12
    |- tree : 13
    |- Animal2 : 14
    '- Cloud : 15

    Any Ideas ?
     
  41. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    You're right. The problem is, that after duplicating objects hierarchy, duplicated sprites are using the same material as the original sprites. So if they using the same material they behave as they were masked by the original Sprite Mask.

    If you construct this second mask - Mask (1) - by creating GameObject and attaching components (not by duplicating) this problem won't appear.

    This is a bug and I have to look deeper in the duplicating process.
     
  42. Kheryo

    Kheryo

    Joined:
    Oct 7, 2015
    Posts:
    6
    Hi, thanks for your answer.

    Adding SpriteMask to objects without duplication does work.

    Individually building the SpriteMask part of each gameObject I need with that feature in my scenes will take some time, but hey, at least it works.

    I'm looking forward to a fix for this problem.

    Thanks for your time and have a nice day o/
     
  43. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi, sorry for that delay but I was quite busy.

    After some debuging I've found why SpriteMask was behaving like that. The reason for that is that after duplicating game object with SprtieMask, _defaultSpriteMaterial field was set to the same material as on the original SpriteMask. After removing [SerializeField] from line 101 in the SpriteMask class, it works as expected.

    At this point, I don't remember why I added [SerializeField] to the _defaultSpriteMaterial field on SpriteMask class. If I don't find any reason to have that field serialized, I will send an update to the asset store.

    Please let me know if you will find any other strange behaviors.

    Just remove [SerializeField] on line 101 in the SpriteMask class:
    Code (CSharp):
    1. 101://    [SerializeField]
    2. 102:    private Material
    3. 103:        _defaultSpriteMaterial;
     
  44. BreadWeek

    BreadWeek

    Joined:
    Aug 12, 2014
    Posts:
    44
    When I try using SpriteMask with the Sprite Diffuse shader, I get a black polygon behind the sprite. I haven't used it in a while and I'm almost positive it worked fine before but it's been so long I don't know what has changed. Is there an easy answer?

    SpriteMaskDiffuseIssue.png
     
  45. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi, that's strange. It works on my side.



    If you update SpriteMask to version 1.3 you can select Diffuse shader from the Inspector:

    SpriteMask > Advance > Sprites shader

     
  46. BreadWeek

    BreadWeek

    Joined:
    Aug 12, 2014
    Posts:
    44
    Thank you for the reply. I am using 1.3, I thought setting the shader that way in the inspector is the only way to do it since that option overrides any attempt to change shaders on the sprite itself. Transparency is rendered as black even when importing Sprite Mask into an entirely new project.

    I'm also not sure if this is relevant but the Specify option in that drop down list is locked on the previous option (SpriteMask/Default or SpriteMask/Diffuse) and will not change.

    EDIT: Just to narrow it down a little, this happens if I add the SpriteMask/Diffuse shader to any sprite regardless of masking. But masks force either SpriteMask/Default or SpriteMask/Diffuse.
     
    Last edited: Oct 21, 2015
  47. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    So it happens even on an empty project. Maybe it is related with some settings in your Unity. No one earlier had that kind of issue.

    Would be helpful, if you could export me an example scene with your SprieMask codes, so I could eliminate Unity settings from being the reason for that.
     
  48. JariHuomo

    JariHuomo

    Joined:
    Feb 8, 2013
    Posts:
    38

    Any idea what might cause this problem? On left screen capture from iOS device , on right screen capture from editor. On Android working fine too.
     
  49. PoL231

    PoL231

    Joined:
    Jun 27, 2014
    Posts:
    148
    Hi Jari Huomo.

    I have absolutely no idea why there's a problem on the left side. Could you confirm, that on both side objects have the same positions? Maybe the problem is related with screen aspect ratio?

    If you could send me some more screen from the editor (object hierarchy) I could reconstruct your scenario.
     
  50. mihaialin1003

    mihaialin1003

    Joined:
    Feb 9, 2015
    Posts:
    4
    Hi, I have a big problem using your plugin, I need my images to be really sharp but all I can achieve is jagged edges. I used the same settings on my sprites as you did in the demo scene.
    The sprite on its own rendered in the scene, seen from the same camera looks good.
     

    Attached Files:

unityunity