Skip to main content

Thin wrapper for the glslify nodejs tool, use stack.gl shader modules in Python.

Project description

pyglslify

A very thin wrapper for the glslify nodejs tool, to use stack.gl shader modules in Python during development.

(after development you're probably best just compiling the shaders with nodejs glslify cli tool to a file and using the result)

Installation

The whole point is to be able to download and use GLSL shader modules from the WebGL community, which are distributed via npm.

So first you need to install nodejs/npm: https://www.npmjs.com/get-npm

Then install the glslify tool globally and verify it is on your path:

npm install -g glslify
glslify --help

Then add pyglslify in your Python project virtualenv:

pip install pyglslify

Usage

We're still going to use npm to install stack.gl shaders. For example:

npm install glsl-noise

This will create package.json and package-lock.json files in your project root, which will track the npm dependencies of your project. These should be committed to source control along with the rest of your project. By default the modules will be downloaded to a node_modules dir in your project root, that dir should be added to .gitignore.

If you checkout your project repo on a new machine you can just run npm install to fetch the modules specified as dependencies in package.json.

On the Python side you can write GLSL shaders which make use of the glslify require pragma:

#pragma glslify: noise = require('glsl-noise/simplex/3d')

precision mediump float;
varying vec3 vpos;

void main () {
    gl_FragColor = vec4(noise(vpos*25.0),1);
}

fshader.glsl

And then in your Python code, when you want to pass the shader to your rendering pipeline, use pyglslify to resolve the pragmas:

import moderngl_window as mglw
from pyglslify import glslify


class RenderWindow(mglw.WindowConfig):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        vertex_shader = self.load_text("shaders/vshader.glsl")
        fragment_shader = self.load_text(
            glslify("shaders/fshader.glsl")
        )
        self.pipeline = self.ctx.program(
            vertex_shader=vertex_shader,
            fragment_shader=fragment_shader,
        )

You can also use glslify Source Transforms.

e.g. npm install glslify-hex allows use of CSS-style hex strings for colors in place of vec3s.

See the glslify docs - they recommend specifying these as configuration in your package.json.

You can also apply local transforms to the shader file at runtime from Python, by passing their module names as args like:

from pyglslify import glslify

fragment_shader = glslify("shaders/fshader.glsl", "glslify-hex", "glslify-import")

Happy shading!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyglslify-0.1.1.tar.gz (3.4 kB view hashes)

Uploaded Source

Built Distribution

pyglslify-0.1.1-py3-none-any.whl (3.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page