Shader implementation in LÖVE and resources to study (+ shockwave and swirl shader attached)
Posted: Wed Jun 07, 2023 7:32 pm
Hello,
I am preparing to launch my LÖVE game on Steam, and I wanted to talk about something that I think is overlooked when people are considering LÖVE, which is the subject of shaders. Amongst other things, they apply shading to pixels, so by definition they're cool, but what's also cool is being able to work with them and implement them in the framework/engine you are using, and I believe that LÖVE does help a lot with this process, and it makes working with them easier than you might expect. For me, shaders are hard to understand and to program, but a lot of heavy lifting is done by the framework when you implement them, which I really appreciated when I got to using them.
I will create a post about my game at a later date. First, shaders!
Because a lot of shaders are written in a specific language, and are also written in a certain way, there are lots of conventions surrounding them, so theoretically you can easily port them across frameworks/engines. The process of porting over the shaders I attached to this post to LÖVE was quite simple. And after you adapt code to this framework, you only need one line of code to send a uniform to your shader! For someone like me, who is not a particularly good programmer, being able to get some MIT-licensed shader code and convert it to LÖVE was vital, as I currently don't have the knowledge to create them from scratch. And it's strange to explain, but the game I am working on needed these two shaders. They are not always necessary for all games, however, in this case, I believe that they did elevate the gameplay experience.
Two resources for studying shader code that can also be considered that I don't really see mentioned are also from the Phaser 3 and PixiJS JavaScript libraries (and definitely other libraries and game engines):
https://github.com/pixijs/filters
https://github.com/rexrainbow/phaser3-r ... ns/shaders
The shockwave shader comes from a PixiJS filter (they are called filters there), and the swirl shader is from Phaser 3. The background image is from ansimuz, https://ansimuz.itch.io/sunnyland-tall-forest
Both of the shaders are placed on a canvas, and I believe this really adds a lot to the swirl shader, as everything that is drawn on that canvas gets its pixels swirled/mixed. And by placing it on a canvas, you can also apply the effect to primitives, such as rectangles, circles, etc. Their center position is the current x and y position of the mouse, but you can change that to whatever you wish in the code (please look at the example code in the main.lua to check all the uniforms that can be changed).
Of course, like the code I ported from the above resources, this code also carries over the MIT license.
For the shockwave shader, I didn't include the clamped version, as I didn't 100% understand how to convert that over here, in a way as to not destroy the performance or do some silly stuff. But I think this version looks great.
I just wanted to let others know how nicely LÖVE plays with shaders, and hopefully others see this and they also get encouraged to try and port over shaders for their games as well.
I am a bit nervous that I did something wrong, like not properly removing the shaders or that I didn't create the canvas in the correct way, but there seem to be no issues. Let me know if you have any questions! Thanks
EDIT: Here are some GIFs to view the shaders in action,
Shockwave shader,
Swirl shader,
I am preparing to launch my LÖVE game on Steam, and I wanted to talk about something that I think is overlooked when people are considering LÖVE, which is the subject of shaders. Amongst other things, they apply shading to pixels, so by definition they're cool, but what's also cool is being able to work with them and implement them in the framework/engine you are using, and I believe that LÖVE does help a lot with this process, and it makes working with them easier than you might expect. For me, shaders are hard to understand and to program, but a lot of heavy lifting is done by the framework when you implement them, which I really appreciated when I got to using them.
I will create a post about my game at a later date. First, shaders!
Because a lot of shaders are written in a specific language, and are also written in a certain way, there are lots of conventions surrounding them, so theoretically you can easily port them across frameworks/engines. The process of porting over the shaders I attached to this post to LÖVE was quite simple. And after you adapt code to this framework, you only need one line of code to send a uniform to your shader! For someone like me, who is not a particularly good programmer, being able to get some MIT-licensed shader code and convert it to LÖVE was vital, as I currently don't have the knowledge to create them from scratch. And it's strange to explain, but the game I am working on needed these two shaders. They are not always necessary for all games, however, in this case, I believe that they did elevate the gameplay experience.
Two resources for studying shader code that can also be considered that I don't really see mentioned are also from the Phaser 3 and PixiJS JavaScript libraries (and definitely other libraries and game engines):
https://github.com/pixijs/filters
https://github.com/rexrainbow/phaser3-r ... ns/shaders
The shockwave shader comes from a PixiJS filter (they are called filters there), and the swirl shader is from Phaser 3. The background image is from ansimuz, https://ansimuz.itch.io/sunnyland-tall-forest
Both of the shaders are placed on a canvas, and I believe this really adds a lot to the swirl shader, as everything that is drawn on that canvas gets its pixels swirled/mixed. And by placing it on a canvas, you can also apply the effect to primitives, such as rectangles, circles, etc. Their center position is the current x and y position of the mouse, but you can change that to whatever you wish in the code (please look at the example code in the main.lua to check all the uniforms that can be changed).
Of course, like the code I ported from the above resources, this code also carries over the MIT license.
For the shockwave shader, I didn't include the clamped version, as I didn't 100% understand how to convert that over here, in a way as to not destroy the performance or do some silly stuff. But I think this version looks great.
I just wanted to let others know how nicely LÖVE plays with shaders, and hopefully others see this and they also get encouraged to try and port over shaders for their games as well.
I am a bit nervous that I did something wrong, like not properly removing the shaders or that I didn't create the canvas in the correct way, but there seem to be no issues. Let me know if you have any questions! Thanks
EDIT: Here are some GIFs to view the shaders in action,
Shockwave shader,
Swirl shader,