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
    Yes, you can choose the 3D position from where the line of sight is calculated. Each line of sight source is controlled by a unity camera component which you can move and rotate freely in the world. So anything that's visible from that camera's viewpoint will be masked as visible.

    You can combine any series of image effects to get the result you desire. There are several blur image effects that ship with the pro version of Unity you can use, and I've included a custom desaturate (black and white) effect with the asset.

    If I find the time, I'll work on creating a new demo.
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Would you think it would be easy to show only the parts covered by the light?

    On your demo if a circle is touching the light we can see the rest in a darker color. What would it need to show only the portion being lighted?
     
  3. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    This is very easy to change. You can use the included desaturate effect and change its brightness to 0. Here's an example of what it looks like:

    LOS-Black-example.jpg

    Basically, you can customize the area outside the line of sight in any way you like.
     
  4. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    First thanks for the fast reply.
    Second Nice!!

    I will look into buying it once I get payed.

    BTW you still working on mobile support?
     
  5. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    I'm working on a new update, which in theory should work on mobile devices. But since I don't have a pro android license or ios device at this moment I'm not able to test it.
    I also think performance wise, only the latest generation of devices would be able to run it smoothly.

    So it might work, but I won't support it officially.
     
    Aldo likes this.
  6. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    I've just submitted version 1.2 to the Asset Store for Unity version 4.5 and upwards. It's currently pending review, so if everything goes well, it should be available some time next week.

    This new version includes the following updates:
    • Added LOSSourceCube component for fast 360 line of sight
    • Refactored and optimized existing code base
    • Fixed world space reconstruction issues with orthographic cameras
    • Added gizmos
    • Added tooltips
    • Updated example scene
    • Improved error handling
    Next on my to-do list is porting this new version to Unity 5.
     
  7. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    This would save me a lot of work, as I was about to port a CPU implementation of this to the GPU. However, I haven't been quite happy with my CPU implementation to start, so a different base would not be a bad thing.

    However, do you know what the complications and issues are with Unity 5? Anything I should be aware of?

    edit -> corresponding via e-mail.
     
    Last edited: Feb 26, 2015
  8. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Version 4.2 is now live on the Unity Asset store for Unity 4.5 - 4.x

    Working on version 4.2.1 now, which will be a Unity 5 specific version.
     
  9. Kaemalux

    Kaemalux

    Joined:
    Aug 1, 2013
    Posts:
    45
    Hello!
    I just bought your Asset! Honestly, it is surely what i needed, but i cannot make it work... Importing on a plain new project the Example scene gives me those errors:

    "GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.NavMeshAgent:get_remainingDistance()
    AINPC:Update() (at Assets/Line Of Sight/Example Scene/Assets/Scripts/AINPC.cs:23)


    Failed to create agent because there is no valid NavMesh

    Should i just wait for Unity 5 update to use the plugin correctly?

    My purpose is to achieve the setup you showed in this post:

    Starting from a plain tilemap with a player, which are all settings i should change?

    I am almost at beginner level in Unity, sorry if it is a noob question. :)

    Cheers
     
  10. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    The error you're seeing is from the nav mesh in the example scene, just rebake it in Unity 5 and that error should go away.

    I'm still working on the Unity 5 version, but it's taking longer than expected because of some unresolved Unity bugs in their deferred renderer.
    Everything should be working fine tho, as long as you're using the forward rendering pipeline.

    To set up the effect like in the image that I posted, just follow the documentation and set the brightness of the desaturate image effect to 0.
     
    Kaemalux likes this.
  11. Kaemalux

    Kaemalux

    Joined:
    Aug 1, 2013
    Posts:
    45
    Thanks for the quick answer!

    I'm sorry but it is the first time i use Pro features (due to Unity 5 update...), could you gently explain me how to bake the NavMash? The only think i find, in Example folder, is an empty NavMash prefab, but i cannot use it anyhow...

    Later i will try to import the Asset on my 2D project and i will see if i manage to make it work. :)

    Thanks again!
     
  12. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
  13. cosmgrafik

    cosmgrafik

    Joined:
    Oct 2, 2014
    Posts:
    2

    Any idea of why I have this problem ?
    I'm using unity 4.6

    Thanks for your help !
     
  14. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    That's very odd, looks like it's outputting the depth buffer.

    Have you made any changes to the example scene?
    What platform are you running on?
    Do you get any warning in the console?
     
  15. Highstorm

    Highstorm

    Joined:
    Nov 16, 2012
    Posts:
    3
    Hi Tycho,

    I just bought your asset and it looks like it will do everything I need and more! Unfortunately I'm having some issues with the "fog" not being visibly revealed, as shown in this image:
    vision01.PNG

    As you can see, the "reveal" aspect of the vision is working fine, and the furniture will properly reveal itself once viewed, but the fog overlay is not showing where I can see. I've imported your asset into a brand new project and the results are the same. I've tried messing with various settings, but can't seem to solve this issue?

    One thing I did notice is that the LOSMask script looks quite different in your manual than it does in Unity, missing several shader assignment fields:
    vision02.PNG

    I'm assuming that's just the result of an update obsoleting their use as such, but it is also the only difference I can find that might explain this issue?

    EDIT: I am running Windows 7, using Unity 5, and the only warnings I got was the NavMesh stuff that I re-baked.

    I appreciate any help you can offer as I'm really looking forward to incorporating this into my project.
     
  16. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Yes, the example scene hasn't been updated yet for Unity 5 and won't work correctly. I'm still working on the Unity 5 version, and hope to have it released this weekend.

    To fix the example scene, all you need to do is change the materials it's using from Unlit/Texture to Legacy Shaders/Self-Illumin/Diffuse.

    And you're correct about the shader reference being removed in the newer version, I forgot to update that image in the documentation.
     
  17. Highstorm

    Highstorm

    Joined:
    Nov 16, 2012
    Posts:
    3
    Thank you for the response, I was able to get it all working.

    I have a new question. I am seeing if I can make a small 360 "bubble" of vision closely around my player, while otherwise using a standard LOSSource to project a cone of vision in the direction she is facing. This seems to work great, except that the overlapping areas of vision are stacking illumination, creating an extra bright section just before the player, as seen in the image below.
    vision03.PNG

    So my question is simply to ask if there is any way to avoid this behavior?
     
  18. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    I still haven't bought the asset but if you can make a 30° cone, you can make a 330° with 30° offset :p
     
  19. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    You can disable the HDR option on the LOS mask component. This will prevent the LOS sources from adding up.

    Another option might be using the HQ 360 prefab and extending the far clip plane of one of the 6 LOS sources.
     
  20. RyuMaster

    RyuMaster

    Joined:
    Sep 13, 2010
    Posts:
    468
    Has anyone tested it on mobile? It is worth it on mobile performance-wise? (assuming, that it works)
     
  21. Highstorm

    Highstorm

    Joined:
    Nov 16, 2012
    Posts:
    3
    The maximum angle you can set a cone to is 180°. The 360° effect is achieved by a different script, and is locked to the full circle, so far as I can tell. I appreciate your attempt to help, but without access to the asset, perhaps you're a bit unqualified?

    Thanks for this! The first method worked quite easily, but preferring to keep the HDR Mask features in tact, I messed around with the HQ 360 prefab to achieve the same result. The biggest thing I had trouble with, for anyone interested, is that I had a black box showing around the player where the LoS Sources overlapped. Changing the OutOfBoundArea to "Include" or "Clamp" cleaned it right up.
    vision04.PNG
     

    Attached Files:

  22. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    One question here.. how can this work well with transparent geometry? Having fog bleed through. I've solved this on other ways by sampling directly in the transparent shaders, but this is an entirely different system and I'm not sure how to handle that.
     
  23. fkkcn

    fkkcn

    Joined:
    Jun 4, 2014
    Posts:
    1
    Hi Tycho,

    I just bought your Asset!

    My running environment

    Unity 4.6.3

    IMac

    NVIDIA GeForce GTX 775M 2048 MB

    3.4 GHz Intel Core i5

    Why run, look something like this



    Hope you can help.
     

    Attached Files:

    • Bug.png
      Bug.png
      File size:
      663.8 KB
      Views:
      2,441
  24. Musiu-Bar

    Musiu-Bar

    Joined:
    Apr 6, 2015
    Posts:
    58
    Hi Tycho,

    Just wanted to give you a heads up that the Webplayer link on your first post is not working.

    Good job on the asset :)
     
    Tycho likes this.
  25. f3mt0

    f3mt0

    Joined:
    Jun 12, 2014
    Posts:
    4
    The library dosen't seem to work on Mac. I don't know what's the problem, I'm including screenshot.

    Second thing I've expected there will be a scene which you've shown here:
    https://dl.dropboxusercontent.com/u/12467343/LOS/index.html.

    The purchased library dosen't include it and this is an effect I was looking for.
     

    Attached Files:

  26. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    I don't own a Mac so I've never been able to test the asset on a Mac system, but I don't see why it shouldn't work.

    Can you try replacing the player object with either the HQ 360 or Single LOS prefab from the prefab folder, and see if they have the same problem?
     
  27. 64slices

    64slices

    Joined:
    Apr 8, 2015
    Posts:
    7
    Hey Tycho,

    Is there a method using this asset to constrain the visibility of the line of sight to certain assets, while still allowing others to block the line of sight? Essentially what I'm hoping to do is make it so that the effect does not appear on walls, while still letting those walls obscure line of sight.

    Any thoughts on how to go about this would be appreciated!

    EDIT - just realised - this is the whole purpose of the excluder.. Don't mind me... :)
     
    Tycho likes this.
  28. JacobDzwinel

    JacobDzwinel

    Joined:
    Dec 19, 2013
    Posts:
    26
    Hi, I'm really interested in buying your asset. I got two questions before.

    I would like to make my line of sight work on 3D terrain with a lot of heights variations. Great example of what i'm looking for is screen from Commandos Games. Is it possible to achieve this type of effect with your asset? (picture below)



    And second question, would it be possible to create two lines of sight, one shorter, and second longer with different behaviour system? (Commandos like, picture below)



    If you played Commandos games before, you probably should know what type of LOS I'm looking for.

    Thanks!
     
    Last edited: Apr 9, 2015
  29. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    This asset should work with Unity terrain, but the results will look different than in your example screenshots.

    Your commando screenshots don't show an accurate line of sight system, it's just an image overlayed on top of everything.
     
  30. Tibelius

    Tibelius

    Joined:
    Aug 21, 2013
    Posts:
    3
    Yo! Just bought this asset and I gotta say that it's really nice. BUT!

    I have a problem with the LoS hiding. I have a mesh that hides itself just ok by itself when I put the script in with the mesh. But if I have a sphere collider around it that I use for mouse selection, the hiding code does not apply after hitting that collider. Then I began to wonder about transparent materials like windows. Does the hiding work after hitting a collider? I tried to use the exlude options, but that only affects the visual representation of the LoS. Does your script only check the hiding until hitting the first collider?
     
  31. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Great asset Tycho, but I do have a slight problem.

    Does this asset render particles within the line of sight? if so how can I achieve this?
    Untitled.png

    As seen in the picture above, the line of sight does not render the particles, only pieces of it where there is a mesh behind it.
    Thanks!
     
  32. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    I'm not really clear on what you're asking, but let me explain how the object hiding works:
    The object will only be visible if one of the LOS Sources can trace a ray from that source to the object without being blocked by any collider on the layers you've selected in the script.
     
  33. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    The problem you're seeing is caused by the particle effects shader not writing to the depth buffer while rendering.

    To include the particles in the line of sight, the particles will have to use a shader that writes to the depth buffer. You can probably copy the shader you are using now, and change "ZWrite Off" to "ZWrite On". Keep in mind that this might cause some issues with sorting.

    For more information, please check the Unity Manual: http://docs.unity3d.com/Manual/SL-CullAndDepth.html
     
    Heu likes this.
  34. JacobDzwinel

    JacobDzwinel

    Joined:
    Dec 19, 2013
    Posts:
    26
    Can I simulate line of sight with green color without any masking effects around it? Just like on the images from Commandos game
     
  35. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    This is not what this asset was intended for, so there are probably better and more efficient solutions to achieve the effect you want.

    I think the easiest thing to use would be a Projector with a cone texture and a green color.
     
  36. JacobDzwinel

    JacobDzwinel

    Joined:
    Dec 19, 2013
    Posts:
    26
    Thanks Tycho for your help :) maybe your asset is actually not what i was looking for. Great asset anyway :)
     
    Tycho likes this.
  37. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    For transparent objects;

    What I am trying to look at as a possibility is to use all the line of sight shaders and have them under the transparent objects in the queue. So the transparent objects are always written over the fog effect.

    From then, I want to take the final result render texture, and set it as a global texture, and inside transparent shaders, reference it directly based on screen position.

    I'm still going through the possibilities and understanding how it is all put together, but does this sound like a possibility?

    Edit: Maybe not. But would it be possible to calculate visibility of points in the transparent shader, given that the initial render is correct (transparent objects do not block visibility)?

    The initial sources would render depth, creating the depth textures. Those are then processed in the LOSMask, calculating visibility at each point. Based on the visibility, it goes into a render texture.

    Could this same process then be applied inside a transparent shader? I guess it would need to be applied from all sources, since the mask is rendering once per source..

    Setting transparent shaders to write to depth destroys sorting, and generally makes them look rather bad. Which effectively makes this plugin unable to be used in anything involving transparent objects, which often includes anything like plants and such. There should be some type of solution to this.
     
    Last edited: Apr 28, 2015
  38. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Hi,

    Sorry for not replying to your question earlier, I must have missed it last time. Transparent objects are indeed an issue at the moment, and not easy to fix without enabling ZWrite.

    The ideal solution would be to have a separate Depth buffer and LOS mask for transparent objects, but that could have a serious impact on performance and would be hard to implement in Unity. So basically per source you would need to render into 2 LOS mask render textures (1 for opaque and 1 for transparent) before rendering anything else, set those as global shader textures, and then use custom shaders (for all of your objects), which sample those textures and shade accordingly. Using Command Buffers in unity 5.0 might make this possible, but the big problem

    For plants the solution might be to use a cutout (alpha testing / clipping) shader instead of alpha blending.
     
  39. 64slices

    64slices

    Joined:
    Apr 8, 2015
    Posts:
    7
    Hey Tycho,

    Another query for you - I'm trying to set up objects that will only render the portion which is outside of the line of sight. I can almost achieve the effect I need by:
    • placing the LOS buffer on a camera at a lower depth than the main camera, and
    • only including the layer with the objects on that camera.
    • invert the LOS mask on the line of sight object.
    This gives me the effect I want for those objects... Except that leaves me with an inverted LOS mask!

    If you look at the image below you will see the pink cube shows what I'm looking for (only the portions of the object which are outside of the line of sight are visible), however I also want to apply the image effect outside of the line of sight. I'm going to be using multiple line of sight cameras, so ideally I wouldn't want to add in another set of cameras to achieve this...

    example.png

    Is there another way to achieve this same effect?

    Cheers
     
  40. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    Here's some thoughts - shader replacement, build a second depth map in a second camera that only samples transparent objects w/ shader replacement to sub in an alpha tested.

    The LOSMask shader could then process from both depth maps, taking the closest depth value. Alternatively, you could construct the mask with more detail using both.

    It's not perfect, but a step in the right direction. My second thought is some way to properly build a transparent mask, so building two masks, one under image effect opaque, so it's placed before transparent objects in the render queue entirely, and a second mask being built from the transparent queue.

    A key point is that transparent objects do NOT block line of sight, nor do they put off shadows, but they do need to receive shadows. This means that the manipulation should not come from the sources, and should not require multiple renders of the sources, but rather multiple renders from the main camera, one for opaque, one for transparent.

    The goal being an effect like this..
    http://i0.wp.com/www.blackinkgamest...enshot-2014-08-06-19.22.29-e1407438819724.png

    http://i1.wp.com/www.blackinkgamest...enshot-2014-08-06-19.22.07-e1407449417548.png

    Those being old pictures, and it was building a visibility map by tracing from the sources. Rather slow, and 2d, but the effect itself is what I'm desiring.

    I'll be working on it, it's fairly critical to what I'm doing.

    As a note, building the mask is rather cheap.. by having the camera render only when a transform moves, this can be used to build static lightmaps by caching the cubemaps. By this, you can have dozens of static lights that render shadows onto geometry. I'm still confused as to why Unity has never offered a solution like this for procedural levels.

    Btw: I do know the subject is rather complex, so no concerns. I don't expect the package to deal with this. But, I am interested in finding a solution, and I'm absolutely sure one is possible, though whether or not it is possible with the performance I want.. that is another story.
     
    Last edited: Apr 29, 2015
  41. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    The easiest ( but not most efficient ) solution to your problem would be to not only render the pink cube with the first camera, but the whole scene and the cube. Then apply the image effects you want to affect the area outside the line of sight to that same camera (with lower depth).
    Then render the scene again with the second camera and use the inverted mask to blend the 2 renders correctly.

    This will off course result in the scene being rendered twice, but would not require any code changes to the system. I'll think it over and see if I can come up with a more efficient solution.
     
  42. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    I am working on an AI for a First Person Shooter. For gun battles, I need the AI to calculate areas that it can move to, that are out of sight from the enemy.

    Could GPU Line of Sight, be used to do this?
     
  43. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    If you only want transparent object to receive line of sight and not block it, then your solution should indeed work I think.

    So basically you should:

    Before rendering scene:
    • Create separate depth buffers for opaque and transparent objects from the main camera (by using a second camera, replacement shaders or command buffers)
    • Use those buffers to create a LOS mask for both opaque and transparent objects, using the same LOS source depth textures (no changes required to how the depth is rendered from the LOS source)
    During rendering:
    • Render opaque objects and sample opaque LOS mask set as global shader texture with custom shader
    • Render transparent objects and sample transparent LOS mask set as global shader texture with custom shader
    This method will only work in forward rendering though. If you want to use deferred rendering, you'll have to figure out a way to apply the opaque LOS mask after the lighting pass using command buffers.
     
  44. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    This looks amazing. I've been looking EVERYWHERE for a high-speed line of sight algorithm but now there is one, based on the GPU! Just wondering, is there any chance source code is provided and I can use my influence map for the line of sight detection?
     
    Tycho likes this.
  45. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    This asset focuses on visualizing the line of sight, and would not be able to provide the data you need out of the box.
     
    Zaddo67 likes this.
  46. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    Thanks Tycho, I appreciate the prompt response.
     
  47. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    After quite a bit of work and testing, the method I had mentioned would not work. Seems like there's a one-frame delay when going between the cameras. I wasn't able to actually isolate what the problem was, but I had a delay, consistently. I saw a few notes online about similar issues but no solutions.

    The end solution I had was extending LOSInclude and wrapping some methods to calculate the visibility right inside a transparent shader, relying on world position.

    Since this is a very static approach, I only use it for the main LOS. However, for everything else, I'm using this as a shadow-mapping system with some edits to the visibility and removing the buffer + image effects system and the methods for shadows of differing strengths. I've also moved the camera renders to static and moving objects, so there's separate cameras rendering and I can use the static renders as lightmaps. The moving objects are dynamic, and optional (caching the static renders means shadows are an almost negligible performance hit, as the mask calculation is rather cheap compared to anything involving a camera render).

    It's all working quite well! Rather happy. Unity's shadow system is just too expensive to use in procedural levels that require dynamic lighting, with no ways to bake in shadows. The original intent was simply just to prevent lights from bleeding across through walls.
     
    Tycho likes this.
  48. Miyth

    Miyth

    Joined:
    Dec 19, 2012
    Posts:
    1
    This is looking like one of the most professional LoS solutions on the asset store right now!

    I'm considering purchasing it- but I'd be planning on using this in a TPS with a fog of war overlay. Do you think it's relatively straightforward to get this asset working with a simple Fog of War reveal system, or would it be too complex/slow to plug this into a shader based FoW approach?
     
  49. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    Glad you got it working the way you wanted it to and I'm curious to see your results. Would it be possible to send me a video or build of your final result?
     
  50. Tycho

    Tycho

    Joined:
    Nov 24, 2012
    Posts:
    205
    It will depend on how the FoW system is implemented, but I don't expect there to be any problems.

    The line of sight effect is applied as an image effect, so as long as the FoW is rendered before the LOS image effect, it should work fine.