Hi guys, so i was playing angry birds 2 and i realise that stages have shadows. The shadow of the clouds moving slow in the stage, but that cloud affect only the “game layer” not the background or items. Mostly of 2d games that use shadows have a different approach. the shadow affect everything, plus the background, looks like they just put the shadow up everything and animate it.
So, how the guys from Angry birds 2 achieve that nice shadows?? Its kinda moving inside the UV maps. i have no idea…
They are probably rendering everything to different layers and on the foreground layer they are moving some shadows across the screen? The shadows are probably blended using some blending mode like in Photoshop.
To stop it from affecting the background layers they could use different cameras or render textures or some other trickery I guess.
i was thinking that maybe they use projectors. but projectors don’t work with sprites… but that would be the easier way to make it, projectors and sprite shade with shadows.
I’m thinking you’re thinking about this way too naively. Just because the game is made with Unity doesn’t mean it’s just made with the things Unity ships with. Most likely you wouldn’t recognize it as a Unity game at all if you opened their project. They’ve probably made custom tools, shaders, scripts for every facet of the game, shadows included.
But as a simple proof of concept you can try with 2 cameras. Put your background sprite on its own layer and use 1 camera to render just that. Then create another camera which renders the foreground sprite. Create a bunch of clouds on a transparent background in Photoshop or whatever, assign that to a sprite and move that across the screen horizontally. Assign the shadow sprite to the foreground as well.
Then you can expand on that with blending modes etc. as your technical skills improve.
I’m making a Terraria style game for awhile now, and this problem was becoming a bit infuriating. I couldn’t find any clear examples anywhere. Lots of vague ideas.
I finally created a solution involving 3 cameras (background, foreground, light), and I have the foreground camera write into a stencil buffer with a shader into a render texture, and the lighting camera read the stencil buffer with a shader through a render texture. In order to stencil the alpha values away on the foreground, it has discard pixels, so It’s probably not the fastest solution, if someone has a better one, I’d appreciate it. And if Unity would like to create a native masking solution it would likely be more efficient.
Everything is commented there. And you can see in the project that the shadowing is foreground layer, using a multiply/stencil shader. I also included a regular version of the stencil buffer shader if you wanted to render decals to only the foreground.
Pass it around, and teach people how to do this. If I solve anymore irritating problems, I’m going to keep posting solutions and explanations. We need to learn how to use Unity if we want the quality of our games and options to increase. It’s not easy to do that alone.
You see how the cloud texture is drawn in the editor scene, but is overlay over the foreground stuff in the game. There is a scene of this in the project itself, but it has another cloud in the background so it might be less clear what is going on. Anyway, it’s pretty much set it and forget it. Draw anything you want on the foreground camrea, draw your shadows on light layer. If I grabbed a different shader you could put additive lighting somewhere likely as well.