FOV shader/algorithm optimization
Posted: Mon Dec 17, 2018 9:11 pm
First of all, there is very little content on the internet regarding 2D shaders optimization and using it for FOV algorithms, so if you do understand (even if only a little) about this stuff, I'll be immensely thankful for your help.
My algorithm works like this: I cast a ray towards all 4 vertices of all solid tiles on the map. When a ray hits, I cast 2 more with a 0.001 angle displacement so I can hit the walls behind it too. Once all rays are cast, I organize them by order of angle, and finally I generate an array of triangles from all collisions. Anything inside this polygon gets rendered. My algorithm is quite literally the same as the ones in these websites, which contain great explanations:
https://www.redblobgames.com/articles/visibility/
https://ncase.me/sight-and-light/
Right now the algorithm is working, but with two issues:
First: It runs like shit
Second: all walls, even those behind other walls, are rendered
I'm VERY new to shaders, I only began to learn them for this FOV thing. I have experimented a little, but my greatest issue right now is with iteration. It's scaringly expensive for the GPU to iterate through an array, and like I said my algorithm generates an array of triangles, therefore each fragment has to read this array. This reduces the FPS to 15 or less with just a little more than 20 solid tiles. Would it be faster to draw the triangles onto a Canvas and then use it as a stencil?
Second, how would I render close solid tiles in their entirety, but only render part of the solid tiles behind them?
I uploaded a .LOVE file. WASD moves the player, left click instantiates/deletes solid tiles, right click turns on/off the shader.
My algorithm works like this: I cast a ray towards all 4 vertices of all solid tiles on the map. When a ray hits, I cast 2 more with a 0.001 angle displacement so I can hit the walls behind it too. Once all rays are cast, I organize them by order of angle, and finally I generate an array of triangles from all collisions. Anything inside this polygon gets rendered. My algorithm is quite literally the same as the ones in these websites, which contain great explanations:
https://www.redblobgames.com/articles/visibility/
https://ncase.me/sight-and-light/
Right now the algorithm is working, but with two issues:
First: It runs like shit
Second: all walls, even those behind other walls, are rendered
I'm VERY new to shaders, I only began to learn them for this FOV thing. I have experimented a little, but my greatest issue right now is with iteration. It's scaringly expensive for the GPU to iterate through an array, and like I said my algorithm generates an array of triangles, therefore each fragment has to read this array. This reduces the FPS to 15 or less with just a little more than 20 solid tiles. Would it be faster to draw the triangles onto a Canvas and then use it as a stencil?
Second, how would I render close solid tiles in their entirety, but only render part of the solid tiles behind them?
I uploaded a .LOVE file. WASD moves the player, left click instantiates/deletes solid tiles, right click turns on/off the shader.