- Home /
Detecting the location of a shadow that is being projected onto a plane
Long time developer, first time 3D Dev and Unity user...
Let's say you have a bunch of primitives in a scene that are being lit by a directional light source. Now, imagine that you have a plane in the scene as well, such that shadows of the primitives are being projected onto the plane. How might one go about trying to detect where those shadows are relative to the plane?
The eventual goal is to take the 2D geometry of the shadows as a basic shape, and then extrude them into 3D objects. The important part here is that the 2D geometry needs to be in the shape of whatever shadows are being projected onto the plane.
Thanks in advance!
Answer by Sajidfarooq · Aug 14, 2013 at 05:03 AM
O = Shape that casts shadow
L = light
1) If you had a "point" light: Find the vectors from each vertex of the O to L. These are now "rays". Find the intersection between these "rays" and the plane on which the shadows are projected using the built-in ray-casting functions in Unity.
2) Directional light:
A directional light does not have a specific location, but it does have direction. Hence, you already have a ray, but the "location" is not specified. Add this ray to each vertex on O to get a ray that is cast from L to that specific vertex of O. Again, "ray-cast" each ray to find its intersection point on the plane on which the shadow is cast.
This seems to be the exact concept I was looking for. I'm assu$$anonymous$$g I'll be able to deter$$anonymous$$e the coordinates of the intersection on the plane from the RaycastHit info. I'll poke around with that later today and see what I come up with. Thanks so much for the quick response!
So, I'm now able to deter$$anonymous$$e the coordinates of all of the intersections between the rays (from light L through the vertices of O) and plane P. Next, I'm trying to make a polygon in the shape of the shadow on plane P and extrude that polygon. The trouble is that I'm not sure how to define the polygon, since I don't know which pairs of vertices should be used to create the edges. Depending on how I construct the polygon, I might end up with a closed shape, or I might end up with a bunch of criss-crossed lines from one vertex to the next. Thinking further down the road, this becomes much more complicated when there are shadows from multiple primitives.
Since this is basically the same problem that a rendering engine must solve when producing shadows, I'm assu$$anonymous$$g there must be a well-defined solution for how to do this out there, and perhaps my explanation just led us astray.
Any suggestions on how to proceed? If I could find a way to define the polygons that make up the shadow, I'd be golden.
What you need is a "Convex Hull" of the points you just found. There are other approaches, but the Convex Hull is the simplest. It may not work for all cases, but its best to implement it first, and then worry about what is not working. http://en.wikipedia.org/wiki/Convex_hull
The Convex Hull does have connectivity information. It finds connectivity for the outermost points, i.e, the convex hull. Problems arise when the shadows are not necessarily convex...