Unity - 2D shader / lighting like Terraria or Starbound
Asked Answered
A

3

9

I have a map with a lot of sprites. I could add a material to the sprite with diffuse shading and than add lots of lights. But that won't give me the result I want. And is performance heavy.


Examples

In the first image you can see that light is generated by torches. It's expanding its light at its best through 'open spaces' and it is stopped by blocks fairly quickly.

enter image description here


Here is a great example of the top layer. We can see some kind of 2D directional light? Please note that the lighting inside the house is generated by torches again. The cave on the right side shows a better example of how light is handled. Also, note the hole in the background, this is generating some extra light into the cave. As if the light is really shining through the background there.

enter image description here


What I have

You can clearly see the issue here. Lights increase their intensity. And light creates a squared edge around some of the tiles for some reason. Also, lots of lights will cause performance issues very quickly.

enter image description here


Raycasting?

I read that you can somehow use raycasting? To target 'open space' or something? I have no experience with shaders or with lighting in games at all. I'd love a well-explained answer with how to achieve this Terraria/Starbound lighting effect. This does not mean I'm saying that raycasting is the solution.


Minecraft

In Minecraft, light can travel for a certain amount of air blocks. It gradually fades to completely dark. In the Graphic settings you can enable Smooth Lightning, which will (obviously) smooth the lightning on the blocks.

I guess this is done with shaders, but I'm not sure. My guess is that this is performance heavy. But I'm thinking about air blocks (which are gameobjects) and maybe I have the wrong logic.


Note: I love a helpful answer, but please provide a link with a detailed explanation. Or provide an explanation with source code or links to the Unity docs in your answer. I wouldn't like to see theories worked out or something. I'd love to get an answer on how to implement this kind of lighting in Unity.

I'd also like to know if it's possible to NOT use a package from the Unity Marketplace.


Similar, but no good

Take a look at similar posts with links to articles that cover the basics of raycasting. But no explanation on how to implement this in Unity and not the Terraria/Starbound effect I'd like to achieve:

Make pixel lighting like terraria and starbound

How to achieve Terraria/Starbound 2d lighting?


Video impression

For example, take a look at this video for a really good impression on how 2d light works in Starbound:

https://www.youtube.com/watch?v=F5d-USf69SU

I know this is a bit more advanced, but also the point light generated by the player's flash light is stopped by blocks and let through by the open spaces.


Other help forums

Also posted by me.

Gamedev Exchange: https://gamedev.stackexchange.com/questions/155440/unity-2d-shader-lighting-like-terraria-or-starbound

Unity Forum: https://answers.unity.com/questions/1480518/2d-shader-lighting-like-terraria-or-starbound.html


Apocopate answered 14/3, 2018 at 22:9 Comment(8)
Very well written question, but I feel it might be more appropriate for gamedev.stackexchange rather than Stack OverflowAircrew
@Aircrew I was asking that question myself. And I feel stackoverflow has a allround programmer community that might be able to solve this question. I will probably post it on the Unity forum as well and provide my question with a link.Apocopate
Note that cross-posting is discouraged on StackExchange sites. I agree with Bejasc that this question will likely get better answers on GameDev.StackExchange, but I'll have to close it there if you keep this version up.Deese
@Deese you think it will get a better answer faster at gamedev?Apocopate
Faster, hard to say. It depends on our traffic volume and folks' free time.Deese
@Deese I'd simply love to see this question answered. Rather close the gamedev post if you really have to.Apocopate
Unfortunately I can't keep both open, so if you don't agree that this belongs better on GameDev then I'll have to close it there. Please ping me if you decide to close this one instead and I'll re-open it.Deese
Let us continue this discussion in chat.Apocopate
N
2

I can mention 2 main elements about 2d Dynamic Lighting for unity.

  1. One of the main asset used for 2d lighting in unity essentials is DDL light, with an official tutorial here.
  2. Most importantly Unity itself is working on a Dynamic lighting system, currently in beta (for 2019.2). Unity introduced the package Lightweight RP for 2d lights here and there's a tutorial here.
Neidaneidhardt answered 11/7, 2019 at 6:54 Comment(0)
A
0

Apparently, there are several packages on the Unity Asset Store. Search for "2d lighting" or "2d shader". You can easily import the source code of these packages and test it in Unity yourself. If you like a package after testing, you can use it.

I highly advise you not to import it directly into an existing project. For your own sake, test it in a test environment (or a copy of your project) before including it into your project.

Apocopate answered 16/3, 2018 at 20:51 Comment(0)
B
0

Here's my hacky solution

GitHub Link

  1. There's 2 cameras.
  2. Empty tiles are filled in with white blocks (only camera 2 render this)
  3. A gaussian blur is applied to the camera rendering the white blocks
  4. SpriteLightKit blends the two cameras, darkening everything not covered by the white blur.
  5. You can adjust the "light" penetration by changing the white tile's sprite's Pixels Per Unit.
Buckskins answered 11/1, 2023 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.