Using the FFGL built-in variables and parameters, it is possible to create complex and interesting plugins. The simplest way to build advanced plugins is using a GLSL-based tool like Shadertoy to write a GLSL shader and convert it to an FFGL plugin. On this page we will recreate the Flames plugin that is included with Morph Pro.
Shadertoy is a very useful platform to experiment with GLSL shaders and test ideas before writing FFGL plugins.
Writing a Shader
Once you have a Shadertoy account you can create a GLSL shader.
On the top right of your home page, click New to make a new shader. You should now have a default shader with a color gradient as its output on your webpage.
Most shaders use basic built-in GLSL Trigonometric functions like sin, cos and tan to create complex visuals. These functions create waveforms that can be used to modify various parameters such as colors over time.
The Shadertoy code to recreate the flames plugin is:
The core of this shader are the tanWave and cotanWave functions that are used to create a color variation based on which pixel is currently being rendererd onscreen. These functions take the following parameters:
- x: coordinate
- a: amplitude
- f: frequency
- tX: horizontal translation
- tY: vertical translation)
By experimenting with what values are passed to these functions and how the results are processed, interesting shapes can be created. Creating a shader is the result of trial and error with these functions to create unique results.
You can experiment with this shader to create your own variation. A few other interesting waveform functions to use are:
Notice that we have used the #define directive to declare global variables time and resolution instead of using the Shadertoy variables iTime and iResolution. We also used #define to declare the parameters Background, Degree, Speed and Color that we used to create the shader.
Writing the shader in this format will make it easy to convert it to an FFGL plugin as we’ll see below.
Converting to FFGL
There are three simple steps to convert any Shadertoy shader to an FFGL shader:
- Change the shader to match the FFGL Format shown in the example
- Remove built-in variable and parameter definitions
- Add FFGL parameters
Using the shader above as an example, we have already written the code in the FFGL Format. We used built-in FFGL variables time and resolution instead of Shadertoy’s iTime and iResolution and used #define to map these to their Shadertoy equivalents.
Below is a list of Shadertoy built-in variables and their FFGL equivalents:
- iResolution – resolution
- iTime – time
- iTimeDelta – deltaTime
- iFrame -> frame
- iChannelTime -> N/A
- iMouse -> N/A
- iDate -> N/A
- iSampleRate -> N/A
- iChannelResolution -> N/A
- iChanneli -> N/A
Another part off folllowing the FFGL format is using #define to declare any parameters we plan on providing for the user to change.
With the first step complete, it is time to move on to step two and remove the first two blocks of code with these definitions.
This is necessary because the FFGL SDK already declares and adds the built-in definitions to the shader as uniforms by default and similarly adds parameter definitions when the AddParam function is called.
After removing the first two blocks, it is time to move on to step three and add the FFGL parameter definitions in the constructor of the plugin:
It is important to note that Param::Create adds a standard parameter with the default range of 0.0-1.0 while ParamRange::Create adds a standard parameter with a custom range. This finetunes exactly how a frame is rendered.
Another important note is why the three components of an RGB color parameter are added one by one instead of using AddRGBColorParam. This allows the default value of the color parameter to be specified instead of using the predefined value used by AddRGBColorParam.
In the next topic we will learn how to adapt our advanced plugin to react to music.