2D shader / lighting like Terraria or Starbound
Basically, 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.
Examples
In the first image you can see that light is generated by torches. It's expanding it's light at it's best through 'open spaces' and it is stopped by blocks fairly quickly.
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.
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.
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.
Thanks for reading and thanks for your interest in this subject!
Note: I love 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.
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:
https://stackoverflow.com/questions/41443130/make-pixel-lighting-like-terraria-and-starbound
https://stackoverflow.com/questions/31736654/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.
Stackoverflow: https://stackoverflow.com/questions/49288411/unity-2d-shader-lighting-like-terraria-or-starbound
Game Exchange: https://gamedev.stackexchange.com/questions/155440/unity-2d-shader-lighting-like-terraria-or-starbound
Answer by UnityCoach · Mar 15, 2018 at 03:10 PM
There are several packages on the Asset Store for 2D lighting. This one is Free.
Your answer
Follow this Question
Related Questions
apply lighting to custom shader made in shader graph? 0 Answers
Having weird problems in shader,Coding a custom 2d lighting shader 0 Answers
How do you access light information in a 2D sprite shader? 0 Answers
Why Won't my 2D Animation's Normals Render Unless I Have an Animator 1 Answer
Unity 2D - Material for light 2 Answers