Render Area Player Would See

107 Views Asked by At

So for the game I am working on, I am trying to make a unique type of shader. The majority is very basic, there is just one difficult part. I only want to render what the player would see in their line of sight. This is a top-down 2D game so the player would normally be able to see rooms over. But take this image for example.

An example of a room and players view

Obviously the player here is the orange circle. The area that he can see is the gray ( grey? ) filled in area. The black lines represent a room and the purple lines represent his field of vision through the door of the room. I want to only render the shaded area.

I am aware GLSL has a discard statement where you can remove specific pixels. This means that by making a boolean function I could just do the following code in my vertex shader.

if ( !playerCanSeePoint( params ) ) {
    discard;
}

What I don't know how to do though is make the playerCanSeePoint function. One idea I had was to cast invisible lines from the player in all directions and find it's first intersection point. That would mean to first wall and would create the proper shape. This seems resource consuming though. So is there a good way to do this?

1

There are 1 best solutions below

0
On

You start with a square the size of the view area and then for each wall you cut out a portion of it based on where the player is.

Then you can triangulate the polygon and use a stencil to prevent drawing outside of it.