discosultan / penumbra

2D lighting with soft shadows for MonoGame
MIT License
315 stars 32 forks source link

[Question]: Is there a way to detect which hulls are affected by light? #25

Open scott-lin opened 5 years ago

scott-lin commented 5 years ago

First of all, great library! It's light years ahead of other attempts on the internet.

I have successfully added a light into my top-down prototype, which is casting shadows beautifully on my hulls. Now I'd like to understand if there is a way to figure out which hulls are casting shadows (in other words, which hulls the light is reaching)?

I didn't see any hooks for this in the Hull class, so I'll keep looking throughout the code base, but thought I'd ask too. If I find something, I'll post it for others too. Thanks.

discosultan commented 5 years ago

Hi and thanks!

I understand you envision a property on Light class that would provide information which hulls it intersects with on that particular frame.

The checks for light / hull intersections are currently performed here: https://github.com/discosultan/penumbra/blob/4b85d2548732750319d5cbaa913c687d483f9955/Source/Hull.cs#L301-L302. Note that even if I attached those results to a light, it might not be accurate enough for your particular use-case, since it is only checking for the rectangle (AABB) intersection around light and doesn't take the exact shape of a light nor hull into account.

Let me know what would be best fit for your scenario. Perhaps I can pull the aforementioned method apart and attach that information to a light on a frame-by-frame basis.

scott-lin commented 5 years ago

Thanks for the quick response and details. You're right, that check isn't quite enough for my particular case.

To explain my scenario in more detail, I'd like to take action on the entities in my game that are not lit up (on a particular frame). To do this, I am hopeful to get to know which hull are lit up via penumbra; if I know this much, I can map them to game entities on my side.

Unfortunately, the AABB intersection isn't enough since, as you know, this might identify hulls eclipsed by other hulls. I'm guessing the checks you mentioned above are for narrowing down and identifying possible candidates to shade (for performance), but the final determination of light drawing is done in the Penumbra*.fx code? Hopefully there would be a way to return what hulls are actually shined on from the fx calculations.

scott-lin commented 5 years ago

It looks like ShadowRenderer identifies the relevant shadow/hull vertices to pass along to PenumbraShadow effect to cast shadows / illuminate hulls. Hopefully this summary is accurate.

Unfortunately, I'm not a graphics programmer, so this is as far as I can get without help / doing some deeper learning of what VAOs are, what role the hull indices play versus the hull vertices, how these integrate with the graphic effects, etc.

That said, I would defer to your opinion on whether it is possible to identify the hulls hit by light and the right approach. However, I'm happy to get my hands dirty if you do not have the time; I completely understand. Thanks.