Effects.py a simple shader compositor library

effects.py is a simple to use library that is intended to make shader composition easy and is licensed under the BSD 3-Clause license.

To use the library you import the Effect class from effects.py and after initializing it - which allows you to set the shader version, the max amount of lights in the LightSourceParameters struct, the max joints in the p3d_TransformTable, and the max clip planes - you can begin to add layers to the effect using add_layer, which allows you to add shader code for different stages (vertex, frag, geom, and tess) and set their respective attributes.

After you have added the layers you want to the effect you can apply the affect to the target nodepath using apply_effect, which under the hood calls nodepath.set_shader(self.shader). There is also remove_layer if you’d like to remove a certain layer from your effect.

Here’s a quick example using the library.

from direct.showbase.ShowBase import ShowBase

s = ShowBase()

shaders = [
{
    "vertex":
        """
        gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;
        texcoord = p3d_MultiTexCoord0;
        """,

    "fragment":
        """
        vec4 color = texture(p3d_Texture0, texcoord);
        p3d_FragColor = color.bgra;
        """,

    "vertex_attributes": "out vec2 texcoord;",
    "fragment_attributes": "in vec2 texcoord;"
    },
    {
        "vertex": "texcoord *= 2;",
        "fragment": "p3d_FragColor.b -= 0.3;"
    }
]

from effects import Effect

e = Effect()
for shader in shaders:
    e.add_layer(**shader)

for value in range(3):
    p = s.loader.load_model("panda")
    p.set_pos((-10 * value, 0, 0))
    p.reparent_to(s.render)

    e.apply_effect(p)
s.run()

The results of this example should look like this:

As you can see it’s very powerful but also easy to use and you can create effects as wild as your imagination. :grinning:

If anyone has questions, comments, or feedback just let me know.

4 Likes