I’m trying to look ahead to my second project, and I’ll be needing 2D dynamic shadows. I’ve decided the best way to do this is to generate shadow meshes on the fly.
So, I’m truly confused on almost all fronts about how I achieve this effect.
There is an explanation, sort of, but it’s all in jargon that doesn’t carry over well into Unity.
I don’t really want someone to do this all for me, because I’m already a pretty intermediate Unity user… I just have no idea how to even start to achieve these results.
The only thing I can sort of gather from what’s being explained… is I need to “get” each edge on the object a shadow is being cast from, and somehow determine whether it’s facing away from the light (no idea how I’d do that either, even if I could get edge data from my 2D mesh). That’s probably what stumps me most, and I could probably almost figure it out if I had any idea about how I get and calculate the edge direction as being facing or not facing the light to begin with, so I even know how to start building my shadow mesh (I already know how to actually build meshes in code).
Overall, very confused. Would appreciate help from someone who understands this stuff.
Also, for my purposes, I need to only generate hard shadows. I have no need of soft shadows, so that entire section is irrelevant.
For 2D meshes (“planes” with n number of vertices) it’s relatively straightforward. The diagram you posted actually describes it rather well.
You’ll want to cast a ray from your light’s position through each vertex on the mesh until it collides with your ground plane. The point of this collision is now the position in world space for a shadow mesh’s vertex. Do this for every vertex on the plane and you will get a set of vertices position on the ground plane that you can then wind triangles with.
This is limited of course. It only works with 2D meshes where the normals are all facing the same way. It will not cause the mesh to self-shadow. It will not affect meshes standing in shadows generated by other meshes.
A better solution might be to use a shader with a shadowmap. The basic idea behind this is to render the scene twice. First from the light’s point of view and second from the camera’s point of view. The light’s pass creates the “shadow map” which is the depth of each fragment in its frustrum. In the second pass you compare the depth of the fragment from the camera’s view point to the depth of the fragment at the light’s view point. If the depth from the light is less than the depth from the camera than that fragment is in shadow and can be colored appropriately. Obviously this solution is more complex but the results are typically better.
This is actually a superior way of doing it than what I can discern from that tutorial page.
The site describes winding counter-clockwise around the 2D mesh’s hull (which as far as I know, can’t be done in Unity unless you built the mesh yourself in code).
The limitations don’t really matter, since the game is essentially all 2D anyways, and everything will of course be facing the same way and be 2-dimensional.
I’ll try this out and see how it feels.
The reason why the original technique wasn’t straightforward though (at least for the purposes of Unity) is because my mesh isn’t an n-gon, it’s a square with 2 triangles (something Unity does automatically on import), so every time I did an edge check, it checked the one inner edge along with the rest of them. And aside from that, it wasn’t properly going clockwise or counter-clockwise, because some edges simply never became marked as shadowed no matter where the light was positioned.
Anyways, I’ll try what you described above and see if this works out better.