Search Unity

Question Spherical 3d cone of vision effect?

Discussion in 'General Graphics' started by ROBYER1, Jan 23, 2023.

  1. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    How would one approach making a sphere which acts like a spherical cone of vision? As in cutting out any objects that pass into the sphere like a cone of vision effect from a stealth game, only in a spherical 3d shape, not a 2d cone.

    The problem with just using a shader effect is that it doesn't account for the sphere passing halfway through a thin wall to the other side where the outside of the sphere renders again, I want the sphere to cut off at the wall fully and not draw beyond it.

    Sort of like a combination of these two effects:
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    If you want a literal "3D cone of vision" effect, you only real option imho is to use volumetrics. These are rather advanced stuff, there's no built-in implementation in either URP or BiRP (HDRP only), so you'll have to either write your own or use a third-party product.
     
  3. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    That is the same conclusion I have came to, as the usual decal style effects used to enemy cone of vision in most games are 2d only, pretty much HDRP is the only option for this type of effect currently.
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    Another way I can think of is experimenting with something similar to shadow volumes. If you can extrude geometry away from the cone of vision's origin point (determine silhouette edges: those that separate back from front-facing faces, and yeet them away from the origin point), chances are you will be able to figure out a way to subtract the extruded volumes from the cone volume and render the results. This more closely resembles the approach used in 2D.

    The problem with shadow volumes is that their cost depends on the complexity of the geometry. So if your scene has a lot of geometry they can get quite costly.

    On the other hand, the cost of volumetrics depends on how finely you march trough the volume (larger steps yield lower-resolution, blocky but cheaper results).
     
    Last edited: Jan 26, 2023
    ROBYER1 likes this.
  5. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Here's how I'd approach this. Consider point light shadows. This isn't really that different. Start by rendering a cubemap of just depth (maybe 128x128 or 256x256 per face) from the perspective of the point where you want to draw the sphere from (use layers to only render things that would block vision).

    When rendering your "sphere", project from this back into world space. An easy way that might look and perform just fine is to use a dense sphere mesh and adjust the points in the vertex shader. A potentially better way would be to use tessellation. You could also try drawing a quad and doing the projection in the pixel shader/screen space.

    Volumetrics would be needed if you had multiple points to be concerned with, but there's just a single distance from the center that defines the cone of vision, so you don't need to get that fancy. Ray-marching in screen space might be more accurate and less dependent on the cubemap resolution, though, so it could look better. It'd also be a lot less draw calls, so if you're willing to sacrifice GPU perf for CPU perf, that's a reasonable solution. If you can target DXR/hardware ray tracing, that's another solution, and any-hit rays without a payload aren't actually that expensive.

    But rendering a cubemap and then sampling it in the vertex shader is the quick and dirty hack that might work out fine.

    (*) Having implemented stencil shadows, I can tell you that way lies madness. Basically, all the geometry in your scene needs to conform to some very specific requirements (mathematical manifold -- so no holes, no overlapping geometry, no pieces inside of others). If you're doing all your modelling in-house or are using extremely simple shapes, you might be able to get away with it, but be prepared to pay a lot more (and/or spend a ton more time per model), and completely forget buying anything from the asset store/TurboSquid/etc. Procedurally-generated geometry can also bite you hard, since you may need to adjust or completely rewrite it to conform to the strict requirements. Some double-shell hacks exist to turn non-manifold into manifold; I can't say for certain whether that would work for your case. Regardless, though, there are easier and more straightforward ways to do it.
     
    Last edited: Jan 26, 2023
    arkano22 and ROBYER1 like this.
  6. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    I will update here with any approach I try as I am coming back to this project soon, volumetrics does seem to be the number 1 approach to this solution so the ideas here are greatly appreciated!