Search Unity

Applying Alpha cut off to a shader ? [better explanation in post]

Discussion in '2D' started by Aedous, Feb 7, 2015.

  1. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Hey people! please help :).

    I've been using spot light's to achieve lighting in the Isometric 2D game I'm working on. I've noticed that lights are generally just... bad, so I'm trying some alternatives to replace them.

    One alternative I've found is using a Multiply Double shader on a sprite which represents a light to get the desired effect, which greatly improves the performance and you can't really tell the difference between the two.

    The problem I'm having is my fake light is on the top most layer and therefore draws on top of everything.
    I have all my moving objects in one sorting layer "Actors" to control when objects are infront and behind each other, and one sorting layer "Super Foreground" which is on top of everything ( e.g GUI, Lights etc ).

    Is there a way to have it so it draws on top of everything apart from one gameobject ? for example the main character ? without having to change the sorting layers, perhaps maybe even a mask of the character on top of the fake light ?
    A better example is when you actually use a light and can set what object layers it affects through it's Culling Mask.

    Any help would be really greatful!
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    This is probably NOT a good idea but it was the first thing I thought of. I thought I'd mention it so you could consider it and say why it's not a good idea, plus it may spark another solution.

    Would it be a performance killer or bad to have one light per layer (all setup the same and moving as one light) to effectively control what is illuminated and what isn't per layer? Would - say 5 lights, each affecting 5 object each be any more 'heavy' than one light affecting 25 objects? Just a sideways idea how to overcome the issue - without having to destroy the layering already sorted out.
     
  3. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Thanks for the reply, I actually had already ended up doing what you suggested and have a few layers for dynamic lights(spot lights). So I only select what objects the light is currently affecting. I'm also using the fake lights and ordering by sorting layer either behind the character or on top of everything.

    So far so good, because I just have a mixture of spot lights that only affect certain objects by using layers, and the other "fake" lights to brighten up the environment a bit. Has really improved the performance :)

    Although....still would like it so that the shader I'm using which is "MultiplyDouble" found it from here http://www.reddit.com/r/Unity2D/comments/1r2mem/sprite_shader_variants_includes_normalmapping_on/ could have an alpha cut out so that some objects i.e the player appear to be in front of it.
     
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Cool deal @Aedous, glad I could help.
    I checked out your game, it looks Awesome! It's nice to see quality game play with an equal importance placed on visual quality and aesthetics. That's an element I'm banking on as well.

    Question1 - Regarding the solution you ended up using, did you notice any performance hit with using multiple lights?
    Question2 - What are the 'fake' lights you speak of? Is this a Unity specific trick for 2D games?

    Thanks for your response. Keep up the awesome work!
     
  5. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Thanks! glad you like it, we're trying to get it to look as visually impressive as possible while also keeping up the frame rate, so far.. so good :).

    Yes most definately, it's the best thing I've come across and it looks good as well, it such a huge performance boost, and you can essentially have multiple fake lights while just using one real light. Then just using a spot light when you can't achieve the effect with the fake light. I may even use 3 spot lights that only affect one layer each which also works quite well.

    I simply take a texture similar to this http://www.magix.info/mcpool01/10/8...D2/D7/5F/9404BCC0671811E190B559D82178A9F7.jpg and use it as a sprite for the light.
    Then create a material with the Mutliply Double shader which is at the link I sent earlier, also make sure your other sprites e.g character can be affected by actual lights.

    You'll notice that it does give a similar effect to the lights, and you can adjust the colours accordingly, stacking multiple sprites on top of each other gives you a brighter light. This is when you have to get creative to achieve the effect you're looking for. The only issue of course is having it only affect certain objects, which I think you could do with an Alpha Mask not sure.
     
    theANMATOR2b likes this.
  6. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    614
  7. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
  8. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    614
    yea i needed them as particles so i could integrate them into my flamethrower and burning effects, works pretty great!, but also now that i think about it i kind of forgot we could just use them as sprites LOL there's some spots i use them as particles when i didn't need to!
     
  9. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    haha, I'm using spot lights as well, because they seem to give a better performance than point lights, and I can apply a texture to them to give it a better shape.
    Anyone have any ideas about the shaders ? and how alpha cut out's work ? :confused: