Performance issues with visibility detection and object transparency
- by maul
I'm working on a 3d game that has a view similar to classic isometric games (diablo, etc.). One of the things I'm trying to implement is the effect of turning walls transparent when the player walks behind them. By itself this is not a huge issue, but I'm having trouble determining which walls should be transparent exactly.
I can't use a circle or square mask. There are a lot of cases where the wall piece at the same (relative) position has different visibility depending on the surrounding area.
With the help of a friend I came up with this algorithm:
Create a grid around the player that contains a lot of "visibility points" (my game is semi tile-based so I create one point for every tile on the grid) - the size of the square's side is close to the radius where I make objects transparent. I found 6x6 to be a good value, so that's 36 visibility points total.
For every visibility point on the grid, check if that point is in the player's line of sight.
For every visibility point that is in the LOS, cast a ray from the camera to that point and mark all objects the ray hits as transparent.
This algorithm works - not perfectly, but only requires some tuning - however this is very slow. As you can see, it requries 36 ray casts minimum, but most of the time 60-70 depending on the position. That's simply too much for the CPU.
Is there a better way to do this?
I'm using Unity 3D but I'm not looking for an engine-specific solution.