Skip to content

dwgx/ShadertoyTool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ShadertoyTool

从 Shadertoy 下载 GLSL shader 源码及全部关联资源的命令行工具。

A Python CLI that downloads Shadertoy GLSL shader source and all linked assets (textures, audio, video, cubemaps).


Overview / 概述

ShadertoyTool is a single-file Python script (fetch_shadertoy.py) that fetches complete shader source code from Shadertoy via URL or 6-character Shader ID, exports it as .glsl, and downloads all referenced input assets with a manifest.json inventory.

To deal with Cloudflare challenges, it has a multi-tier fetch strategy: official API key, legacy POST endpoint with extracted browser cookies, and a Playwright + Microsoft Edge browser fallback.


单文件 Python 脚本,通过 Shadertoy URL 或 6 位 Shader ID 抓取完整着色器源码,导出为 .glsl 文件,同时下载着色器引用的所有输入资源并生成 manifest.json 清单。

为了绕过 Cloudflare 验证,工具内置三级获取策略:官方 API Key、模拟网页端的 legacy POST(附带从本机浏览器提取的 Cookie)、以及基于 Playwright + Microsoft Edge 的浏览器回退。

Features / 功能

  • Download complete GLSL source via URL or Shader ID
  • Extracts all Render Passes (Image / Buffer / Sound / Common etc.) into a single annotated .glsl file
  • Downloads referenced textures, audio, video, cubemaps
  • Generates manifest.json with URL, filename, download status, and reference location for each asset
  • Supports Shadertoy API Key (/api/v1) for direct, stable access
  • Auto-extracts cookies from local browsers (Chrome / Edge / Brave / Firefox)
  • Playwright + Edge browser fallback for Cloudflare bypass
  • Auto-retry on retriable HTTP codes (408/429/500/502/503/504)
  • Assets blocked by 403 / Cloudflare are automatically retried via browser context

  • 通过 URL 或 Shader ID 下载完整 GLSL 源码
  • 自动提取所有 Render Pass(Image / Buffer / Sound / Common 等),按注释分隔写入单个 .glsl 文件
  • 下载 Shader 引用的纹理、音频、视频、Cubemap 等输入资源
  • 生成资源清单 manifest.json,记录每个资源的 URL、保存文件名、下载状态与引用位置
  • 支持 Shadertoy API Key(/api/v1)直接访问,最稳定
  • 从本机浏览器(Chrome / Edge / Brave / Firefox)自动提取 Cookie
  • 内置 Playwright + Edge 浏览器回退,通过 Cloudflare 验证
  • 对可重试的 HTTP 状态码(408/429/500/502/503/504)自动重试
  • 资源下载被 403 / Cloudflare 拦截时自动通过浏览器上下文重试

Tech Stack / 技术栈

  • Language / 语言: Python 3.8+
  • Dependencies / 依赖 (requirements.txt):
    • requests>=2.28 — HTTP requests
    • browser-cookie3>=0.19 — extract cookies from local browsers
    • playwright>=1.40 — browser fallback (uses local Edge by default)

Project Structure / 项目结构

ShadertoyTool/
├── fetch_shadertoy.py     # Main script (CLI entry, fetch & asset download logic)
├── fetch_shadertoy.cmd    # Windows batch wrapper, auto-installs missing deps
├── requirements.txt       # Python dependencies
├── SECURITY.md
├── LICENSE                # MIT
└── .gitignore

Getting Started / 快速开始

Prerequisites / 依赖

  • Python 3.8+
  • pip
pip install -r requirements.txt

Playwright browser driver (only needed for the browser fallback; defaults to local Microsoft Edge):

python -m playwright install chromium

Windows shortcut / Windows 快捷方式

Run fetch_shadertoy.cmd — it checks for requests, browser_cookie3, playwright, auto-installs anything missing via pip, then runs fetch_shadertoy.py.

Usage / 使用方法

# Download by URL
python fetch_shadertoy.py https://www.shadertoy.com/view/4djyRD

# Download by Shader ID
python fetch_shadertoy.py 4djyRD

# Windows batch
fetch_shadertoy.cmd XltGDr

Running without arguments downloads https://www.shadertoy.com/view/4djyRD by default.

不带参数运行时默认下载 https://www.shadertoy.com/view/4djyRD

Options / 参数说明

Param Description / 说明 Default
shader Shadertoy URL or 6-char Shader ID (positional, optional) https://www.shadertoy.com/view/4djyRD
-o, --out-dir GLSL output directory / GLSL 输出目录 downloads
--api-key Shadertoy API Key (uses /api/v1 route)
--no-browser Disable browser fallback / 禁用浏览器回退 off
--headless Headless mode for browser fallback / 浏览器回退无头模式 off
--verify-attempts Manual verification retries in browser mode / 手动验证重试次数 5
--skip-assets Skip asset download / 跳过资源下载 off
--assets-dir Asset save directory / 资源保存目录 <out-dir>/<shader_id>_assets

Examples / 示例

# Use API Key (recommended, most stable)
python fetch_shadertoy.py XltGDr --api-key YOUR_API_KEY

# Source only, no assets
python fetch_shadertoy.py 4d33Dj --skip-assets

# Custom output directory
python fetch_shadertoy.py XtlSD7 -o ./my_shaders

# Headless browser fallback
python fetch_shadertoy.py 4djyRD --headless

Output / 输出结构

downloads/
├── XltGDr.glsl              # GLSL source (all passes)
└── XltGDr_assets/
    ├── manifest.json         # Asset download manifest
    ├── texture_01.png
    └── audio_track.mp3

Each .glsl file contains all Render Passes with annotated headers:

每个 .glsl 文件包含该 Shader 的所有 Render Pass,以注释头标注:

// ShaderToy ID: XltGDr
// Name: Contra
// Retrieved: 2025-01-01 12:00:00 UTC

// ===== Pass 0: Image (image) =====
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    // ...
}

// ===== Pass 1: Buffer_A (buffer) =====
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    // ...
}

How It Works / 工作原理

Fetch priority:

  1. API v1 (requires --api-key) — official REST API (/api/v1/shaders/<id>?key=<key>), most reliable
  2. Legacy POST — sends an internal request to https://www.shadertoy.com/shadertoy with cookies extracted from local browsers
  3. Playwright browser fallback — launches local Microsoft Edge, passes Cloudflare, then calls the same legacy endpoint via in-page fetch

Asset download also supports fallback: requests first, 403 / Cloudflare-blocked assets are retried via browser context.

Browser fallback uses locally installed Microsoft Edge (searches common install paths for msedge.exe). In visible-window mode you complete the manual verification in the opened browser.


获取优先级:

  1. API v1(需要 --api-key)— 官方 REST API,最可靠
  2. Legacy POST — 向 https://www.shadertoy.com/shadertoy 发送模拟网页端请求,附带从本机浏览器提取的 Cookie
  3. Playwright 浏览器回退 — 启动本机 Microsoft Edge,通过 Cloudflare 验证后在页面内 fetch 相同接口

资源下载同样支持回退:先用 requests 直接下载,被 403 / Cloudflare 拦截的自动通过浏览器上下文重试。

浏览器回退依赖本机安装的 Microsoft Edge(在常见安装路径中查找 msedge.exe)。可见窗口模式下需要在打开的浏览器中完成人工验证。

API Key

  1. Register at Shadertoy
  2. Go to Apps page, create an app
  3. Copy the generated API Key

Using an API Key bypasses Cloudflare entirely — it's the most stable method.

使用 API Key 可以直接走官方 API,无需处理 Cloudflare 验证,是最稳定的方式。

Status / 状态

Personal utility tool. Single-file script, functional. Fetch success rate may fluctuate as Shadertoy's internal endpoints and Cloudflare settings change.

个人实用工具,单文件脚本,功能可用。因依赖 Shadertoy 内部接口与 Cloudflare 验证,抓取成功率可能随站点变化而波动。

License / 许可证

MIT

About

Python CLI for downloading Shadertoy GLSL shaders and related assets

Topics

Resources

License

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors