@thi.ng/webgl-shadertoy
TypeScript icon, indicating that this package has built-in type declarations

0.4.169 • Public • Published

@thi.ng/webgl-shadertoy

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 199 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

Basic WebGL scaffolding for running interactive fragment shaders via @thi.ng/shader-ast. This is a support package for @thi.ng/webgl.

Status

ALPHA - bleeding edge / work-in-progress

Search or submit any issues for this package

Related packages

Installation

yarn add @thi.ng/webgl-shadertoy

ESM import:

import * as ws from "@thi.ng/webgl-shadertoy";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/webgl-shadertoy"></script>

JSDelivr documentation

Package sizes (brotli'd, pre-treeshake): ESM: 693 bytes

Dependencies

Note: @thi.ng/api is in most cases a type-only import (not used at runtime)

Usage examples

Three projects in this repo's /examples directory are using this package:

Screenshot Description Live demo Source
Shader-AST meta-programming techniques for animated function plots Demo Source
Evolutionary shader generation using genetic programming Demo Source
Shadertoy-like WebGL setup Demo Source

API

Generated API docs

import {
    $xy, add, distance, eq, float, FloatSym, fract,
    int, min, mix, mul, neg, ret, sin, sym,
    Vec2Sym, Vec2Term, vec3, Vec3Sym, vec4
} from "@thi.ng/shader-ast";
import { aspectCorrectedUV, fit1101 } from "@thi.ng/shader-ast-stdlib";
import { glCanvas } from "@thi.ng/webgl";
import { MainImageFn, shaderToy } from "@thi.ng/webgl-shadertoy";

const mainImage: MainImageFn = (gl, unis) => {
    // predeclare local vars / symbols
    let uv: Vec2Sym;
    let mp: Vec2Sym;
    let d1: FloatSym;
    let d2: FloatSym;
    let col: Vec3Sym;

    // Inline function to create ring pattern with center at `p`
    const rings = (p: Vec2Term, speed = 0.25, freq = 50) =>
        sin(mul(add(distance(uv, p), fract(mul(unis.time, speed))), freq));

    return [
        // let's work in [-1..+1] range (based on vertical resolution)
        (uv = sym(aspectCorrectedUV($xy(gl.gl_FragCoord), unis.resolution))),
        (mp = sym(aspectCorrectedUV(unis.mouse, unis.resolution))),
        // compute ring colors
        (d1 = sym(rings(mp))),
        (d2 = sym(rings(neg(mp)))),
        // combine rings and multiply with target color based on
        // mouse button state
        (col = sym(
            mul(
                vec3(fit1101(min(d1, d2))),
                mix(
                    vec3(1),
                    vec3(d1, 0, d2),
                    float(eq(unis.mouseButtons, int(1)))
                )
            )
        )),
        // return as vec4 (mandatory)
        ret(vec4(col, 1))
    ];
};

// create WebGL canvas
const canvas = glCanvas({
    width: window.innerWidth,
    height: window.innerHeight,
    parent: document.body,
    version: 1
});

// init shader toy with canvas & shader fn
const toy = shaderToy({
    canvas: canvas.canvas,
    gl: canvas.gl,
    main: mainImage
});

toy.start();

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-webgl-shadertoy,
  title = "@thi.ng/webgl-shadertoy",
  author = "Karsten Schmidt",
  note = "https://thi.ng/webgl-shadertoy",
  year = 2019
}

License

© 2019 - 2024 Karsten Schmidt // Apache License 2.0

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.4.16910latest

Version History

VersionDownloads (Last 7 Days)Published
0.4.16910
0.4.1683
0.4.1670
0.4.1660
0.4.1650
0.4.1640
0.4.1630
0.4.1620
0.4.1610
0.4.1600
0.4.1590
0.4.1580
0.4.1570
0.4.1560
0.4.1550
0.4.1540
0.4.1530
0.4.1520
0.4.1510
0.4.1500
0.4.1490
0.4.1480
0.4.1470
0.4.1460
0.4.1450
0.4.1440
0.4.1430
0.4.1420
0.4.1410
0.4.1400
0.4.1390
0.4.1380
0.4.1370
0.4.1360
0.4.1350
0.4.1340
0.4.1330
0.4.1320
0.4.1310
0.4.1300
0.4.1290
0.4.1281
0.4.1270
0.4.1260
0.4.1250
0.4.1240
0.4.1230
0.4.1220
0.4.1210
0.4.1200
0.4.1190
0.4.1180
0.4.1170
0.4.1160
0.4.1150
0.4.1140
0.4.1130
0.4.1120
0.4.1110
0.4.1100
0.4.1090
0.4.1080
0.4.1070
0.4.1060
0.4.1050
0.4.1040
0.4.1030
0.4.1020
0.4.1010
0.4.1000
0.4.990
0.4.980
0.4.971
0.4.960
0.4.940
0.4.930
0.4.920
0.4.910
0.4.900
0.4.890
0.4.880
0.4.870
0.4.860
0.4.850
0.4.840
0.4.830
0.4.820
0.4.810
0.4.800
0.4.790
0.4.780
0.4.770
0.4.760
0.4.750
0.4.740
0.4.730
0.4.720
0.4.710
0.4.700
0.4.690
0.4.670
0.4.661
0.4.650
0.4.640
0.4.630
0.4.620
0.4.610
0.4.600
0.4.590
0.4.580
0.4.570
0.4.560
0.4.550
0.4.540
0.4.530
0.4.520
0.4.510
0.4.500
0.4.490
0.4.480
0.4.470
0.4.450
0.4.440
0.4.430
0.4.420
0.4.410
0.4.400
0.4.390
0.4.380
0.4.370
0.4.360
0.4.350
0.4.340
0.4.330
0.4.320
0.4.310
0.4.300
0.4.290
0.4.280
0.4.270
0.4.260
0.4.250
0.4.240
0.4.230
0.4.220
0.4.210
0.4.200
0.4.190
0.4.180
0.4.170
0.4.160
0.4.150
0.4.140
0.4.130
0.4.120
0.4.110
0.4.100
0.4.90
0.4.80
0.4.70
0.4.60
0.4.50
0.4.40
0.4.30
0.4.20
0.4.10
0.4.00
0.3.110
0.3.100
0.3.90
0.3.80
0.3.50
0.3.40
0.3.20
0.3.10
0.3.00
0.2.910
0.2.900
0.2.890
0.2.880
0.2.870
0.2.860
0.2.850
0.2.840
0.2.830
0.2.820
0.2.810
0.2.800
0.2.790
0.2.780
0.2.770
0.2.760
0.2.750
0.2.740
0.2.730
0.2.720
0.2.710
0.2.700
0.2.690
0.2.680
0.2.670
0.2.660
0.2.650
0.2.610
0.2.600
0.2.590
0.2.580
0.2.570
0.2.560
0.2.550
0.2.540
0.2.530
0.2.520
0.2.510
0.2.500
0.2.490
0.2.480
0.2.470
0.2.460
0.2.450
0.2.440
0.2.430
0.2.420
0.2.410
0.2.400
0.2.390
0.2.380
0.2.370
0.2.360
0.2.350
0.2.340
0.2.330
0.2.320
0.2.310
0.2.300
0.2.290
0.2.280
0.2.270
0.2.260
0.2.250
0.2.240
0.2.230
0.2.220
0.2.210
0.2.201
0.2.190
0.2.180
0.2.170
0.2.160
0.2.150
0.2.140
0.2.130
0.2.121
0.2.110
0.2.100
0.2.90
0.2.80
0.2.70
0.2.60
0.2.50
0.2.40
0.2.30
0.2.20
0.1.40
0.1.30
0.1.20
0.1.10
0.1.00

Package Sidebar

Install

npm i @thi.ng/webgl-shadertoy

Homepage

thi.ng/webgl

Weekly Downloads

18

Version

0.4.169

License

Apache-2.0

Unpacked Size

30.4 kB

Total Files

10

Last publish

Collaborators

  • thi.ng