visual representation for Line OF Sight

Hey guys,

we’re looking for a way to visually render a “Commandos” style LOS for NPC’s on an isometric 2.5 d stealth-puzzle game. This means some sort of a triangular (or cone, or something) shaped mesh that will not render any parts of it that are beyond another objects’ collider box in relevant angles.

something like this:

or this:

Hoping you might have some ideas, we’re pretty stumped! only thing we could come up with is about 500 raycasts being rendered in every angle. Which is a problem, since we’re aiming at an iOS mobile paltform for the game.

thanks in advance!

Well that’s not an easy task :wink:

Since you said it’s 2.5 d you can treat it as 2d. It would make it way easier to limit the collider types as much as you can. I would exclusively use box colliders for the environment.

Use GeometryUtility.TestPlanesAABB to test if an collider is within the viewing frustum / cone. For all colliders that are within the frustum, determine the two points that made up the silhouette of the collider. A cube only has 4 points (in 2d ;)).

Now just “project” the two points along the vectors between the viewer and the points. Now you have 4 points. Create two triangles with those points and add them to a mesh. Once you have all those occluding trapezoids, just render the mesh with the depth mask shader. To create occlude the areas which can’t be viewed by the viewer.

Finally just render the viewing cone.

Keep in mine to offset the masking geometry a little bit upwards to prevent z-fighting. The drawing order should be: normal geometry, depthmask, view-cone.

This technique has some problems if you have multiple viewers. In this case you would have to clear the z buffer after each viewer. I’m not sure how this would perform on the iPhone.


It’s also possible to create directly a mesh which represents your view area, but this is a bit more difficult.

You still would search for the two “silhouette points” of each collider. Start with the nearest point and raycast through it (ignoring this collider) and determine on which collider and where the line of sight ends. The big problem is to determine which points you have to connect. It’s a lot of calculating, sorting and testing, but as result you could combine all view areas into one mesh which would be great for the iPhone since it’s just one drawcall :wink:

http://zammyart.com/UA/LineOfSight/WebPlayer.html

spot light at man’s eyes position with shadow enabled in deffered lighting mode 8)

you did, i’m just too stupid to notice. thanks again!