Search Unity

2D Collider Light Shadows APIs

Discussion in '2D' started by Singtaa, Sep 20, 2022.

  1. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    Are there APIs we can use to determine if an arbitrary 2D collider is partially in shadows, completely in shadows, or completely in light? I'm asking for a CPU-only/headless-friendly way (no dealing with GPU or textures).

    Currently I do have my own implementation (video below), but it's only for edge colliders. So I'm wondering if Unity Physics 2D can now provide such functionality out-of-the-box and in a generalized way.

     
    Last edited: Sep 20, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    What functionality do you mean? 2D shadows directly using physics geometry? If so then know that 2D Shadows that uses collider geometry is coming from the 2D team (see video experiments below). The physics part of that work was done earlier this year; there's various shadow specific work being done on top of that. Maybe you mean some other specific functionality though?

    Our implementation only uses public physics API. The core of this is the ability to get the physics shapes from any collider using Collider2D.GetShapes which uses a PhysicsShapeGroup2D to store that information. Also using Collider2D.GetShapeHash to determine if it has changed so you're not constantly retrieving it per-frame. Although not used in our implementation, there's also Collider2D.CreateMesh to create a mesh that covers the area of 2D colliders although as you can see in the docs, this doesn't support zero-radius lines because you cannot have a planar mesh that is just a line because it's just a collinear/degenerate triangle.

    Note that this has been used for a while for things like 2D Navmesh where the physics geometry is required.



     
  3. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    Hi @MelvMay thanks! That's really helpful.

    I actually posted the original question after watching your videos. What I wasn't sure was whether your implementation was based on 2D SDFs (because I *thought* you'd need to support non-polygonal shapes like circles). ShaderToy sample. But upon closer look, the circles in your video actually have polygonal shadows.

    So I'm curious since you said your implementation only uses public physics API. Because batched 2D raycasting wasn't available in Unity, I implemented my own bursted ray-line intersection for maximum CPU performance. How is your implementation batching those raycasts (assuming your implementation is based on raycasting corner points via PhysicsShapeGroup2D.GetShapeVertices)?

    Lastly, can I request Collider2D.CreateMesh to be respecting the new PolygonCollider2D.useDelaunayMesh flag if it's not on your team's radar yet?
     
    Last edited: Sep 21, 2022
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    That's an old set-up where yes, only polygons were supported. The shadow code now correctly supports curved surfaces so circle, capsule (ends), edges and boxes with an edge-radius.

    It's not using raycasting or physics queries at all. I wasn't indicating that the shadow code itself was public and available, that in itself is the new feature but the code that touches 2D physics is using all public API that I mentioned previously i.e. getting the actual geometry if it's changed and using it to produce shadow details. The new shadow code has a "provider" which can provide any geometry it likes for shadows, the shadow code goes away and decides how it produces shadows. My point was that you'll be able to create your own if you like but the one we provide that supports physics uses all public API.

    Creating a mesh shouldn't be affected by feature. The mesh is created based upon the low-level physics shapes produced. Its job is not to create a mesh that is the same physics geometry but to cover the same area. Obviously this is true for circles/capsules because those are just a point or two and a radius internally. Did you spot a problem? Maybe you're asking if you could have an option to use Delaunay when producing the mesh? That's certainly possible and an interesting idea if so. :)
     
  5. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    That clears things up for me greatly! So does the current "shadow code" provide any way (API) to determine if a 2D collider is hit by a particular point light or spot light? (I'm thinking it's mostly GPU code that's only responsible for *rendering* shadows. But figured I'd ask anyway.)

    Yup, that's what I meant. A way to extract PolygonCollider2D's delaunay mesh (when useDelaunayMesh is set, for example).

    Thanks again @MelvMay for answering all my questions, past and new, on here and on other social platforms. Much appreciated!
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    No but understand that I'm not the dev, I only implemented (using the existing public API) the provider to support colliders. You declare the provider and the types it supports and the shadow code calls it to "provide" the relevant geometry and/or determine if it needs updating at all.

    Well physics doesn't use or have a mesh for colliders; a collider has PhysicsShape2D so if you want them, you can use the API I mentioned above. This is the "mesh" you want. If the PolygonCollider2D is set to use Delaunay, it'll return the convex polygons you can see in the gizmo. If it's off, it'll return those convex polygons you can see in the gizmo.

    Regardless, it is a good idea to have that option for the mesh but understand, this won't necessarily be identical to the physics shapes used in a collider; it's just a mesh for you to use, not something physics uses. Indeed, the physics system never sees this, it's just a nice helper call.

    You're most welcome. Likely I'll add that Delaunay option to CreateMesh, it's a great idea I didn't consider so thank you. ;)
     
    Singtaa likes this.