Shadertoy implementation based on wgpu-py
Project description
shadertoy
Shadertoy implementation based on wgpu-py.
Introduction
This library provides an easy to use python utility to run shader programs from the website Shadertoy.com. It provides the compability to let users copy code from the website directly and run it with the various GUIs that are supported in wgpu-py. Including Jupyter notebooks.
Shadertoys translated to wgsl are also supported using the uniforms i_resolution
, i_time
, etc.
This project is not affiliated with shadertoy.com.
Installation
pip install wgpu-shadertoy
Usage
The main Shadertoy
class takes shader code as a string.
from wgpu_shadertoy import Shadertoy
shader_code = """
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.xy;
vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));
fragColor = vec4(col,1.0);
}
"""
shader = Shadertoy(shader_code, resolution=(800, 450))
if __name__ == "__main__":
shader.show()
Texture inputs are supported by using the ShadertoyChannel
class. Up to 4 channels are supported.
from wgpu_shadertoy import Shadertoy, ShadertoyChannel
from PIL import Image
import numpy as np
shader_code = """
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.xy;
vec4 c0 = texture(iChannel0, 2.0*uv + iTime * 0.2);
fragColor = c0;
}
"""
image_data = np.array(Image.open("./examples/screenshots/shadertoy_star.png"))
channel0 = ShadertoyChannel(image_data, wrap="repeat")
shader = Shadertoy(shader_code, resolution=(800, 450), inputs=[channel0])
When passing off_screen=True
the .snapshot()
method allows you to render specific frames.
shader = Shadertoy(shader_code, resolution=(800, 450), off_screen=True)
frame0_data = shader.snapshot()
frame10_data = shader.snapshot(10.0)
frame0_img = Image.fromarray(np.asarray(frame0_data))
frame0_img.save("frame0.png")
For more examples see examples.
Status
This project is still in development. Some functionality from the Shadertoy website is missing and new features are being added. See the issues to follow the development or contribute yourself! For progress see the changelog.
License
This code is distributed under the 2-clause BSD license.
Code of Conduct
Our code of conduct can be found here: Code of Conduct
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file wgpu_shadertoy-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: wgpu_shadertoy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dece7e5c7691670ac67e7ff38cd68df501c00e36e6d56ab6989ad294c560e5a |
|
MD5 | 380d404486e1262a8e9fe822a9432878 |
|
BLAKE2b-256 | 5dd738f008570f7869bfa236b1f9ddf1391fb7bd2019c04373dd969089777714 |