Search Unity

2D light sort order / z-position to place it behind sprites?

Discussion in '2D' started by nicmarxp, May 27, 2020.

  1. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hey, the 2D lighting feature is great! We're making a top down game, and would like lights to go in front of and behind objects.

    In the image below, i have a foreground layer, that has the top of the walls, that the player can go behind, it's setup so the light doesn't affect that layer, and it works. However, i want it to go behind items that are higher in Z-order, like the door in the bottom of the video.

    Is that possible? In this case it could be solved without lights, and using a light-shaped sprite instead, but i guess that kinda misses the benefits of 2d lights. :)

     
    jacknkandy likes this.
  2. Sam_Ooi

    Sam_Ooi

    Unity Technologies

    Joined:
    Mar 5, 2018
    Posts:
    61
    Hi! The video is currently private, you may need to set it to unlisted and then link the URL.
     
  3. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Sorry, clumpsy of me, here it is :) Thanks for checking it!
     
  4. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    It would be so supercool to shoot a light bullet in a forest, and have it go in front of some and behind some trees :)
     
  5. Sam_Ooi

    Sam_Ooi

    Unity Technologies

    Joined:
    Mar 5, 2018
    Posts:
    61
    Hi, just checking with how you have set up your project. Are the doors or other elements in the scene on the same layer, or are on different Sorting Layers like the top of the walls? And what is the current custom axis sorting settings you are using for your project?
     
    Last edited: May 29, 2020
  6. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hey, the doors, player and interior are in the same sorting layer "Default". So no matter the Y position, the lights always come above them. If I would have made a sprite on the same sorting layer, it would be above any object with a higher Y-position.

    I can't see the "Edit > Project Settings > Graphics > Camera > Transparency Sort Axis" setting anymore, is it gone now or not available for the 2D renderer or the Universal RP?

    upload_2020-5-29_11-41-40.png
     
  7. Sam_Ooi

    Sam_Ooi

    Unity Technologies

    Joined:
    Mar 5, 2018
    Posts:
    61
    For the Transparency Sort Axis settings, they are not visible in the UI while using URP but are still available. The dev recommends the following to access the settings when using with URP:

     
  8. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hi, thanks. However the Transparency Sort Axis works for any other objects, it's just lights that doesn't go in front or behind depending on the sort axis. So the question is if it's possible to go around this?

    Thanks :)
     
  9. Sam_Ooi

    Sam_Ooi

    Unity Technologies

    Joined:
    Mar 5, 2018
    Posts:
    61
    Gotcha, I'm checking with the dev to see what can be done to get the effect you're looking for, and will update you as soon as possible.
     
  10. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks a lot, the option is to use sprites that looks like lights instead, would probably look pretty good.
    However, what is the performance difference between using just sprites and lights? Is there any limit to how many lights is recommended in 2D?

    Like would it be a bad idea to have all bullets in a game have a 2d light source?

    Also we're targeting both PC and Switch, so we're considering the Switch poorer performance, and am wondering how 2D lighting would affect that platform. Any ideas here? :)
     
  11. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    Sorry for not answering your question sooner. But unfortunately, we don't currently support z sorting on lights.

    The only workarounds you have at the moment are putting your sprites on different sorting layers or trying to work around the problem with shadow casters.

    Its hard to give you performance advice. Shadow casters could be expensive, and if they do prove to work for you, you consider a representative test on any hardware that might be of concern.

    In terms of the number of lights, the number probably won't be an issue for you if the sprites are small. However, if you find yourself cpu bound, you may have too many. Generally, the issue will be the area of the light. Lots of lights that fill the screen would be bad for performance.
     
    nicmarxp likes this.
  12. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks a lot for the comments! I will probably use sprites for bullet lights instead, as they seem to look ok, and work with Z-sorting, and save lighting for really special effects :)
     
  13. jacknkandy

    jacknkandy

    Joined:
    Aug 28, 2018
    Posts:
    2
    I'm having a similar issue with light sorting, and the Universal 2D Renderer in general. I feel like this is sort of related to this question.

    I am relying heavily on Tilemaps to build an 2d rpg game with an othorgraphic perspective. I have sorting groups set up to ensure layer sorting when the player moves up and down the y-axis.

    Now that I have introduced 2d lighting, I am having the issue where the lights shine through the other tile layers "in front" of it. I understand I can assign lights to specific sorting layers, but having lots of different sorting layers means the layer sorting with my character breaks.

    Is there any way sort lights via the y-axis or something else to overcome this issue. Cheers!
     
  14. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    We’re just using sprites that looks like lights with fake bloom instead at the moment, but lose normal maps and other cool things doing that :/
     
    jacknkandy likes this.
  15. jacknkandy

    jacknkandy

    Joined:
    Aug 28, 2018
    Posts:
    2
    Thanks. That's too bad then
     
  16. JennyHide

    JennyHide

    Joined:
    Sep 30, 2014
    Posts:
    47
    We have the same problem - we're using 2D lights with the Transparency Sort Axis set up like nicmarxp, but the lack of ordering within a layer is likely to cause us problems soon.

    How could shadow casters help?
     
    nicmarxp likes this.
  17. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hey, did you figure something out?
     
  18. JennyHide

    JennyHide

    Joined:
    Sep 30, 2014
    Posts:
    47
    No - I'm still hoping for an answer about shadow casters from @Chris_Chu
     
  19. MaikelToxix

    MaikelToxix

    Joined:
    Apr 27, 2020
    Posts:
    1
    With shadowcaster 2d you can shape your own shadow, added as an component onto your desired sprite, the rest is up to you and your brainmagic
     
  20. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    Graveyard Keeper solved this problem in the following way:

    https://www.gamasutra.com/blogs/Svy..._Keeper_How_the_graphics_effects_are_made.php

    It would be nice if something similar were possible with 2D Renderer / Lights. It would greatly enhance many use cases. Working with isometric project, 2D lights look a bit funny for that reason because they appear to shine through walls. If they didn't, and if lighting could be attenuated on the ground in the negative y via shader (a shaped light is an OK workaround for now), then that's most of the way there even without shadows, which I don't plan on for performance reasons.

    My current workaround is to have the light sources always come down from say a tall lamp post so that it kind of makes sense they shine through things but that's unacceptable in the long run. In a building game where players can place lights relatively freely, some kind of solution here is very needed.
     
    Last edited: Sep 2, 2020
  21. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hmm! Very interesting! In my case for bullets, I will just be using a sprite that looks like a light, instead of an actual light. However that makes me lose the cool effect of items actually lighting up.
     
  22. DrunkRacoon

    DrunkRacoon

    Joined:
    Aug 18, 2020
    Posts:
    13
    Hey Nicmarxp,

    Have you tried just applying a bloom shader to your bullet?

    Follow this tutorial here, only difference is don't add the 2D light:


    I think this will give you a pretty close result to what you're after. The bloom will give "light" to whats next to it and be blocked by anything in front of it.

    Let us know if it works!
     
  23. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Hi, thanks for the idea! Sure, it works, but it doesn’t light up the area/characters, so I also considered having a sprite that emulates the light, like a circle with blurred edges. But I will laborate with bloom too. Thanks :)
     
  24. drbn

    drbn

    Joined:
    Oct 4, 2020
    Posts:
    5
    I've been trying to write a shader in urp that solves this problem to replicate the behavior from that Graveyard Keeper post that @Lo-renzo mentioned, but I think we need to be able to get the position of each light source. I haven't really found any documentation on this for URP specifically; I get that things are in flux, but does anyone know how I can do that? I've aimlessly tried using some of the functions defined in that Lighting.hlsl Include file like GetAdditionalLight but I don't think they're actually returning any 2d lights in my scene (I could be doing something wrong though).

    @Chris_Chu you mentioned "unfortunately, we don't currently support z sorting on lights." -- does that mean it's not achievable by the approach I'm going for?
     
  25. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    It would be awesome if you found out and I'd love to know how you did. Graveyard keeper lighting looks so good. Please keep this thread posted :)
     
    drbn likes this.
  26. JennyHide

    JennyHide

    Joined:
    Sep 30, 2014
    Posts:
    47
    I can't answer the 2D lights part of the question, but you can definitely do this (for a Graveyard Keeper style game) using 3D lights with a shader.
     
  27. jbhunt

    jbhunt

    Joined:
    Mar 30, 2018
    Posts:
    1
    So I can use 3D lights in a 2D game to get the Graveyard Keeper effect? Or do the 3D lights only work in 3D games?
     
  28. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    77
    Same issue in my project.
     
  29. CapitalKage

    CapitalKage

    Joined:
    Jan 8, 2021
    Posts:
    2
    Just to help out if people still are wondering about this. I have a front tilemap and a light on my character I did not want to bleed through that front tilemap. The effect is like the character walking behind a wall with a lantern and the lantern light will not show.

    What I did is setup my global light to be on all layers and I set my tilemap light to be a new layer.

    Since the player layer is on the default you do not see any more bleeding on the front wall.
     
  30. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Smart idea, thanks for sharing!
     
  31. johnmiller3137

    johnmiller3137

    Joined:
    Mar 6, 2022
    Posts:
    1
    another way is to use sorting layers and have the light target only the background sorting layer and a lights layer that the light is on
     
  32. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Yeah, it might work in some cases, but imagine a big rock that you can go in front of or behind, if only the ground is lit, then the rock would look black when using a flashlight. :(

    Did anyone here try to make some special shader for this and still use the 2D renderer?
     
  33. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    223
    I am making an isometric 2d game and I made a special shader for the graveyard keeper style of lighting in this thread(still incomplete).

    In your case it doesn't seem feasible to use shadow casters and custom 2d lit material because you don't have shadows in your scene, but I believe you can still use a custom shader for the door. What you'll need to do is:
    1. Gather all light positions near the door with a trigger collider; remove those that dont effect the door(there is an object between the door and the light, the light is too small to reach the door, the light is above the door and you dont want them to effect the door etc.). You'll probably want to sort them by distance and only use the closest lights if you have a lot of lights in the scene
    2. Send the approximate light brightness effecting the door to the shader. This part is hard and annoying because I don't know a good way to approximate light brightness for different light types. You'll have to check how the 2d renderer creates the LightTextures by following the 2d renderer pipeline; they did the calculations in a shader iirc. You won't need to do them in a shader since you are only calculating the value once for the pivot point of the door.
    3. Send the approximate light brightness to the shader with https://docs.unity3d.com/ScriptReference/Material.SetColor.html
    4. Use it in a custom non-lit material by multiplying the approximate light brightness with the main texture.
     
    Neil-Corre likes this.
  34. alexennerfelt

    alexennerfelt

    Joined:
    Jan 7, 2017
    Posts:
    15
    I'm bummed that this is still an issue. It is the most important feature for 2D lights imho. I wonder if there is any work being done on this.
     
    Sl4shd and pertholdth like this.