Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[FREE] GPU Line of Sight / Field of View

Discussion in 'Assets and Asset Store' started by Tycho, Jul 16, 2014.

  1. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    So you would like to make an explosion reveal a part of the Line of Sight mask? Or does it need to also do the actual line of sight calculations?

    In case you just want to reveal a part of the mask, you could try adding a mesh (that covers the area you want to reveal) which is either transparent (or even better uses a shader that doesn't write to the color buffer) and attach a LOS Stencil Renderer component. This will exclude the area the mesh covers from the Line of Sight mask, without the mesh being visible.
     
  2. bitHussar

    bitHussar

    Joined:
    Jan 9, 2016
    Posts:
    33
    Brilliant, thank you! Can i do fading distance with the stencil renderer? Maybe with a texture with alpha channel?
     
  3. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    No, the Stencil Renderer does not allow fading since the Stencil Buffer uses binary operations.

    If you need fading, you could try rendering the (reveal) mesh directly to the LOS mask, but that would require quite a few changes to the scripts.
     
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
  5. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
  6. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,084
    Are there any plans for HDRP/URP support in the future? My current project handles it just fine since it's on default, but I have an upcoming URP project I'd like to use it for.
     
    bitHussar likes this.
  7. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    No, I currently don't have plans to support any of the scriptable render pipelines :oops:
     
  8. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,084
    Ah, oh well. I can probably hack something together in the next 10 months, it's all still prototype so there's no rush. Thanks for getting back to me!
     
  9. Eck0

    Eck0

    Joined:
    Jun 6, 2017
    Posts:
    48
    hi,i am interested in your product and I would like to clarify some things.
    Download the demo and it works well except when you stick a lot to a wall, see the view a bit as it crosses the wall and see the inside.
    I would like to know if this process in the same scene with 50 objects using field of view is efficiently.
    Can the view have restrictions for height? that is to say if the object is at the same height or below me I can see it but otherwise not.
     
  10. Kamil-Sobierajski

    Kamil-Sobierajski

    Joined:
    Dec 14, 2016
    Posts:
    44
    Hello.
    I try to make a barking system with a black and white aesthetic.
    Is a way to make elements UI what are over the blackness of field of view?
    It is a normal UI element with canvas scaler, parenting to the player.
     

    Attached Files:

  11. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I'm testing your asset in the example scene and I see that the frustrum does not align with where the mask is drawn.
    This results in the mask overlapping with the player but the player not being detected.

    The mask is drawn on a ground that's far beneath the camera, this looks like a bug to me.
    (I lowered the ground and the camera draws a mask on the ground, but it should not because it's not in its frustrum).
    upload_2019-12-15_1-34-59.png

    The detection of the camera is correct but not where the mask is drawn.
    Is there a way to not draw the mask on the ground when it's not in the camera's frustrum?

    EDIT: The solution is to change the outofbound area to Exclude.
    And for the player to be discovered only when he's exactly on the green mask I changed the bounds passed in the UpdateVisibleSources() of the LOSVisibilityInfo class. (It checks the bottom of the mesh only).
     
    Last edited: Dec 15, 2019
    Tycho likes this.
  12. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    1) I'm getting weird mask graphics, there are horizontal lines in the mask. Any idea why?
    upload_2019-12-15_3-32-20.png

    2) The color of the mask changes like the material is additive or something along those lines. Is there a way to change it to a blending option that doesn't change based on what color it's on? Please let me know how to change this.
     
    Last edited: Dec 15, 2019
  13. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    You can test the performance of the system in the demo by placing as many line of sight sources as you want by pressing the space bar.

    The height of the line of sight can't be restricted, but only objects that are visible will be detected, so you could add (transparent) objects to block the line of sight.
     
  14. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Barking system?

    Are you using a world space canvas for the UI elements? Or a screen space canvas and converting the world position to screen space?
     
  15. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Sorry for the slow reply and glad to hear you were able to figure out the first issue by yourself.

    To answer your other questions:
    1. Have you tried tweaking the Min Variance property on the LOS source to reduce the artifacts?
    2. Have you tried disabling the HDR Mask property on the LOS Mask component? Please check section 3.1 of the manual for more information.
     
  16. Kamil-Sobierajski

    Kamil-Sobierajski

    Joined:
    Dec 14, 2016
    Posts:
    44
    Hey Tycho.
    It is simply a World space UI message, With Ui Scaller and Look at the camera. What moves with a character, because it is a child of character.
     

    Attached Files:

    • Bark.PNG
      Bark.PNG
      File size:
      38.3 KB
      Views:
      417
  17. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Have you tried moving the UI canvas to a separate layer and excluding that layer from the LOS mask using the LOS Layer Excluder script?

    Please refer to section 2.2 of the included documentation for more information ;)
     
  18. Kamil-Sobierajski

    Kamil-Sobierajski

    Joined:
    Dec 14, 2016
    Posts:
    44
    Yes. I read the documentation (It is very good btw).
    Using LOS Layer Excluder I can show world space UI on top. But I can't use a LOS Stencil Renderer on UI elements. So Barking messages hide a lot of my level when they show.
     

    Attached Files:

  19. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978

    1. Yes I tried, it doesn't make it better, I'm maxed out at 1 and lowering it is worse.
    2. HDR Mask doesn't change anything.
    https://i.imgur.com/G7Ck7O7.gifv
     
  20. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    You can keep using the LOS Layer Excluder component if that works for your setup, but you also want to make sure the UI element is not affecting the line of sight, by excluding its layer from the Culling Mask property in the Camera component linked to the LOS Source.

    You can find more details in section 2.3 of the manual ;)
     
  21. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    There's a couple of more things you can try to reduce those artifacts:
    1. Increase the height of the LOS Source relative to the flat surface it needs to affect. The larger the height difference, the less artifacts should be visible.
    2. Reduce the far clipping plane of the camera the LOS Source is attached to.
    Those color changes in the link you posted seem really odd. What are you doing to trigger them?
     
  22. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I am switch back and forth between 2 materials on the probuilder mesh:
    1) from the standard probuilder mesh material.
    2) to a unity standard material with a texture and a darker color.

    I'll try your solutions when I'm back home
     
    Tycho likes this.
  23. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    1 and 2 make no difference.
     
  24. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Could you send a simple example project to support [at] entropi-games [dot] com ?

    This way I can hopefully reproduce the issue locally and find a fix.
     
  25. pdhr

    pdhr

    Joined:
    May 13, 2019
    Posts:
    25
    Hi Tycho,

    I want to use this to visualise the field of vision of a rather large area. I bought it, and set it up following the instructions. One problem I have is that I see artefacting in the shader area of the vision cone, it looks like lines or patterns interrupting the shading effect. See this image for an example what I mean:

    upload_2020-2-20_12-0-53.png

    Here, I created an empty gameobject and attached the 360 degree prefab and a simpe capsule object as the source just for testing. I changed the color from white to green. To get a large area, I set each of the cameras' far plane to a large value (e.g. 500 or 1000). The same effect happens when I set the far plane back to ten though, see the stripes in this image:

    upload_2020-2-20_12-2-56.png

    **Do you have any idea what I can do to fix this?**
     
  26. pdhr

    pdhr

    Joined:
    May 13, 2019
    Posts:
    25
    And a new question: for the desaturated effect, how can I make it not change the values of the skybox that is part of the maincamera? I dont want the skybox to become darker.
     
  27. pdhr

    pdhr

    Joined:
    May 13, 2019
    Posts:
    25
    Third question (sorry for the spam):

    I see that the cameras use tempbuffers, and the index number of the temp buffer keeps increasing, even outside of playmode (see image):
    upload_2020-2-20_12-45-11.png

    Is that correct behaviour? Or can this become a memory (leak) issue / otherwise become an optimisation issue.
     
  28. pdhr

    pdhr

    Joined:
    May 13, 2019
    Posts:
    25
    And a fourth question: This does not project onto a terrain-object. How can I enable projection on terrain?

    So to reiterate and consolidate I feel like I'm nearly there to having this work how I want. However I have the following issues / questions:

    1. I see a weird pattern drawn on the objects instead of one smooth coloring, how can I fix that?
    2. How can I keep the skybox as-is when using the 'desaturated' effect? (so not apply the desaturated effect to the skybox)
    3. Is the high (and continuously rising) count of Tempbuffer target textures something to be worried about?
    4. How can I enable projection of the field of view area onto a terrain object?

    Thanks in advance!
     
  29. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I hadn't seen your message before. I will send you a package but I can't do it right now I'm swamped, I first need to get to a decent use of your package and it's a small issue anyway.


    Question:
    Is it possible to make the green effect not appear on meshes that are higher (position.y) than the LOSSource?
    For example here, the LOSSource are on the units and they create green zones on the walls or on surfaces that are higher than them.
    In my code: if I walk in the zone but that I am higher than the LOSSource I am not detected. I simply check the y position, I would like this to be reflected in the effect as well.

    upload_2020-2-21_22-10-49.png
     
  30. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    What you could do is toggle a LOS Stencil Renderer or LOS Layer Excluder component (depending on which exclusion method you want to use) based on the y position check. This will toggle the green LOS effect on that object on or off.
     
  31. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Hi, to answer your questions:

    1. The line artifacts you’re seeing is caused by a lack of precision to correctly calculate the visibility for those pixels. This issue also occurs in shadow mapping algorithms which use a similar technique, and is often referred to as shadow acne.

    You can try to reduce these artifacts by:
    • Increasing the render target width and height on the LOS Source component
    • Increasing the height of the LOS source relative to the ground
    • Enable the Exclude Backfaces property on the LOS Source and tweak the Backfaces fade property
    • Tweaking the Min Variance property on the LOS Source component
    2. Not sure, but I'll check
    3. That's normal. The same buffer is always reused, but Unity just updates the name internally
    4. Terrain object should work. Which version of Unity are you using?
     
  32. pdhr

    pdhr

    Joined:
    May 13, 2019
    Posts:
    25
    Thanks for your reply Tycho. I will have a look at your suggested solutions for (1) and hope that it will help, as well as that you will find some solution for (2). As for (4), I am on 2019.3.1f1.
     
  33. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I get the impression that there is a misunderstanding, I might have explained my problem incorrectly:
    I don't want to add components on these walls.
    I just want that EVERY SINGLE mesh that gets the effect on it does not have the effect on the pixels where the worldPosition.y is above a certain height.

    Here's an image that might explain what I mean better
    https://i.imgur.com/eTxaKU6.png
     
  34. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Thanks for the version number, I'll see if I can reproduce the terrain issue locally with that version.
     
  35. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Ah, sorry for the confusion, now I understand what you mean.

    To achieve the effect you're after, you'll have to modify the CalculateVisibility function in the LOSMask shader.

    You could add a maximum world position height float property (ex: _MaxWorldPositionHeight) to the shader, and use that to exclude pixels from the mask by comparing them to the y value of the pixelWorldPos parameter using a step function after line 87.

    Untested example:

    visible *= step(pixelWorldPos.y, _MaxWorldPositionHeight);
     
    FeastSC2 likes this.
  36. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    I did a quick test with Unity 2019.3.3f1 and it looks like the LOS asset still works correctly with Unity's terrain system.

    2020-03-01-LOS-Terrain-Test.jpg

    I raised the terrain to the left of the LOS source a little bit, and the area behind it is correctly occluded by the line of sight. Other objects also correctly occlude the terrain beneath. I checked with both orthographic and perspective cameras.
     
  37. pdhr

    pdhr

    Joined:
    May 13, 2019
    Posts:
    25
    After some testing on a fresh terrain, I figured out the issue: We are using the Unity terrain with the chekmark 'Draw Instanced' enabled. This results in more efficient drawing of the terrain, by making use of the GPU. After disabling that chekmark, it draws on our terrain as I would expect (but still with the problem of the lines / patterns drawn through).

    However, we are targeting a mobile build with this project, so performance is an issue, which is why we enabled that checkmark in the first place.

    > Is there any way to make this shader play nice with an instanced terrain?

    And to come back to my earlier question 1: I tried the suggestions you provided, but so far have not been able to find a completely perfect outcome, especially for longer ranges. Increasing the width and height does not seem to help (max is 2048 apparently), increasing the height of the LOS Source is not wanted because I want to be able to have it at that exact height / PoV, Enable exclude backface was already on. I did play around with some of the settings, and I seem to be getting somewhat okay results for the current settings, but it still shows some artifacts. Would be great if there was something that could completely eliminate this shadow acne (great name by the way).

    But if I get it properly working on terrain objects, I will be able to try it out further and see if it's adequate for now.

    Edit: Also, when I turn off the 'draw instanced' checkmark, it does draw on the terrain but it does not draw as far as I expect it to. i.e. far away buildings are masked, but that same far away terrain is not... See also the image below with an oversized capsule to identify the source. Any tips there?

    upload_2020-3-6_15-42-28.png

    Edit2: Nevermind my previous edit! I solved that by setting the 'Backfaces Fade' value to zero (or near zero). Confusingly, I expected that functionality to be performaned by 'Distance Fade' instead of 'Backfaces Fade' but oh well. 'Distance Fade' also does not seem to do anything for me.
     
    Last edited: Mar 6, 2020
  38. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Mhh, this is a tricky one, but I think the LOSDepth shader would have to be modified to support terrain instancing. This thread might be a good starting point: https://forum.unity.com/threads/adding-instancing-to-a-custom-terrain-shader.580156/

    One more thing you could try is reducing the far clipping plane of the main camera (which should have the LOSMask component attached) to the smallest possible value. This could further improve the precision of the asset.


    Distance Fade
    controls how much the line of sight cone fades over distance, while Backfaces Fade controls the fade of faces pointing away from the line of sight source.
     
  39. Eck0

    Eck0

    Joined:
    Jun 6, 2017
    Posts:
    48
    hi again, are you planning to implement occlusion on objects at different heights?
    thanks
     
  40. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    No, that's not planned for now.
     
  41. bigd

    bigd

    Joined:
    Feb 14, 2014
    Posts:
    40
    Hi - I noticed this asset was taken off the asset store, are there any plans on making it open source?
     
  42. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Hi,

    I decided to deprecate the component for the time being, because I didn't have time to maintain it properly any more. The introduction of Unity's rendering pipelines has made properly maintaining assets like this a lot more time consuming.

    I might try to allocate some time to update the asset in the coming weeks, but if I don't find the time I'll consider open sourcing it.
     
  43. bigd

    bigd

    Joined:
    Feb 14, 2014
    Posts:
    40
    Thanks for the update and can certainly understand the work required to keep a component like this available especially after the render pipeline updates. My particular version of Unity is still using Built-In render pipelines - so upgrading to the latest version of Unity isn't in the cards for this project atleast. Will check back and see what you decide to do.
     
  44. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Since I don't have time to further maintain this asset, I decided to open source the project and make it available on GitHub:

    https://github.com/EntroPi-Games/GPU-Line-of-Sight

    It's released under the Apache 2.0 license, which is quite permissive.

    I'll still offer support to people who've purchased the asset before it was deprecated.
     
  45. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,084
    Oooh, thanks for this! I was worried seeing it deprecated, but it being open sourced is a great way to follow up on that.
     
    Tycho likes this.
  46. bigd

    bigd

    Joined:
    Feb 14, 2014
    Posts:
    40
    Let me go on the record by saying that I'm not expecting developer support as I'm using this asset after it became open source (a big thank you for allowing that to happen in the first place, Tycho!). That being said, I'm wondering if my particular use case is possible. I was able to get LOS working on my character, but noticed that the shader only projects "shadows" if you have an opaque floor to project them onto. In my particular situation, I was planning on using this to cast shadows but otherwise be hidden from view, like a LOS fog of war. Since this game happens "in space" there isn't a floor / terrain to cast shadows onto.

    As you can see in the picture, it casts fine when I add a black opaque box under the player, but doesn't cast in the area w/o the box and the skybox background. Making the box transparent seems to break the effect completely.
     

    Attached Files:

  47. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Yes, I'm afraid the LOS needs to be projected onto some underlying geometry.

    If the camera rotation / perspective is locked in your game, I think you could still achieve the effect you're after though by:
    1. Adding a separate camera pass to first render the skybox / background geometry
    2. Storing the result of this camera pass in a LOSBufferStorage component
    3. Adding a black plane behind the asteroids to project the LOS onto
    4. Rendering the remaining geometry in a second camera pass with LOS
    5. Assigning the LOSBufferStorage component from step 2 to the LOSMask on this second camera
    6. Changing the LOSCombine shader to blend the render textures of camera pass one and two, to lerp between both based on the mask value
     
    bigd likes this.
  48. bigd

    bigd

    Joined:
    Feb 14, 2014
    Posts:
    40
    Thanks for this! I was able to follow these steps and get the following effect (see pic). A bit stuck on the LOSCombine shader step. How is this shader used currently? My experience /w shaders is limited, normally I see them applied to materials, yet this one is hidden which makes me think it's used internally somehow.
     

    Attached Files:

  49. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    You can actually ignore that last step.

    I had a look at the LOSCombine shader again, and it's already doing what I described :D
     
  50. bigd

    bigd

    Joined:
    Feb 14, 2014
    Posts:
    40
    Nice! Got it working :)
     

    Attached Files:

    Tycho likes this.