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 compatibility 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
To use the Shadertoy.com API, please setup an environment variable with the key SHADERTOY_KEY. See How To for instructions.
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 ShadertoyChannelTexture class. Up to 4 channels are supported.
from wgpu_shadertoy import Shadertoy, ShadertoyChannelTexture
from PIL import Image
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;
}
"""
img = Image.open("./examples/screenshots/shadertoy_star.png")
channel0 = ShadertoyChannelTexture(img, wrap="repeat")
shader = Shadertoy(shader_code, resolution=(800, 450), inputs=[channel0])
To easily load shaders from the website make use of the .from_id or .from_json classmethods. This will also download supported input media.
shader = Shadertoy.from_id("NslGRN")
When passing off_screen=True the .snapshot() method allows you to render individual frames with chosen uniforms.
Be aware that based on your device and backend, the preferred format might be BRGA, so the channels need to be swapped to get an RGBA image.
shader = Shadertoy(shader_code, resolution=(800, 450), off_screen=True)
frame0_data = shader.snapshot()
frame600_data = shader.snapshot(time_float=10.0, frame=600)
frame0_img = Image.fromarray(np.asarray(frame0_data))
frame0_img.save("frame0.png")
For more examples see examples.
CLI Usage
A basic command line interface is provided as wgpu-shadertoy.
To display a shader from the website, simply provide its ID or url.
> wgpu-shadertoy tsXBzS --resolution 1024 640
Uniforms
The Shadertoy uniform format is directly supported for GLSL. However for WGSL the syntax is a bit different.
| Shadertoy.com | GLSL | WGSL |
|---|---|---|
vec4 iMouse |
iMouse |
i_mouse |
vec4 iDate |
iDate |
i_date |
vec3 iResolution |
iResolution |
i_resolution |
float iTime |
iTime |
i_time |
vec3 iChannelResolution[4] |
iChannelResolution |
i_channel_resolution |
float iTimeDelta |
iTimeDelta |
i_time_delta |
int iFrame |
iFrame |
i_frame |
float iFrameRate |
iFrameRate |
i_frame_rate |
sampler2D iChannel0..3 |
iChannel0..3 |
i_channel0..3 |
sampler3D iChannel0..3 |
not yet supported | not yet supported |
samplerCube iChannel0..3 |
not yet supported | not yet supported |
float iChannelTime[4] |
not yet supported | not yet supported |
float iSampleRate |
not yet supported | not yet supported |
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 Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wgpu_shadertoy-0.2.0.tar.gz.
File metadata
- Download URL: wgpu_shadertoy-0.2.0.tar.gz
- Upload date:
- Size: 27.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c605c484a73667ae977522a6981b607d5570d0c59ddfa33dbb784100f62f5501
|
|
| MD5 |
2cde00a57a1ba4a017baff503b408422
|
|
| BLAKE2b-256 |
ce42729f22b388ee2c5b83a3e82b907e5360183f443713e2add6510ec0a67936
|
Provenance
The following attestation bundles were made for wgpu_shadertoy-0.2.0.tar.gz:
Publisher:
ci.yml on pygfx/shadertoy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wgpu_shadertoy-0.2.0.tar.gz -
Subject digest:
c605c484a73667ae977522a6981b607d5570d0c59ddfa33dbb784100f62f5501 - Sigstore transparency entry: 232293318
- Sigstore integration time:
-
Permalink:
pygfx/shadertoy@a52816b87c6422dc19ebb635f3882ebd1ca2f57f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pygfx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@a52816b87c6422dc19ebb635f3882ebd1ca2f57f -
Trigger Event:
push
-
Statement type:
File details
Details for the file wgpu_shadertoy-0.2.0-py3-none-any.whl.
File metadata
- Download URL: wgpu_shadertoy-0.2.0-py3-none-any.whl
- Upload date:
- Size: 22.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d0fe5b5115ac144769386ff97480a38e5f56645d7aa5482592a266699ee7633
|
|
| MD5 |
c12d8c822767506399c3c74ba4477240
|
|
| BLAKE2b-256 |
148b88f90d957e6f560c1ae983c874a04b544cf53c226f79ef5ff55716e7124e
|
Provenance
The following attestation bundles were made for wgpu_shadertoy-0.2.0-py3-none-any.whl:
Publisher:
ci.yml on pygfx/shadertoy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wgpu_shadertoy-0.2.0-py3-none-any.whl -
Subject digest:
2d0fe5b5115ac144769386ff97480a38e5f56645d7aa5482592a266699ee7633 - Sigstore transparency entry: 232293319
- Sigstore integration time:
-
Permalink:
pygfx/shadertoy@a52816b87c6422dc19ebb635f3882ebd1ca2f57f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pygfx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@a52816b87c6422dc19ebb635f3882ebd1ca2f57f -
Trigger Event:
push
-
Statement type: