Taichi GLSL API references¶

Taichi GLSL is an extension library of the Taichi Programming Language, which provides a set of useful helper functions including but not limited to:

1. Handy scalar functions like `clamp`, `smoothstep`, `mix`, `round`.

2. GLSL-alike vector functions like `normalize`, `distance`, `reflect`.

3. Well-behaved random generators including `randUnit3D`, `randNDRange`.

4. Handy vector and matrix initializer: `vec` and `mat`.

5. Handy vector component shuffle accessor like `v.xy`.

6. Handy field sampler including `bilerp` and `sample`.

7. Useful physics helper functions like `boundReflect`.

8. Shadertoy-alike inputed GUI base class `Animation`.

Let me know if you encountered bugs or got a good idea by opening an issue on GitHub.

Note

Here’s the documentation of Taichi GLSL, the extension library. For the documentation of Taichi itself, please click here | 这里.

Installation¶

To install, make sure you have installed `taichi` first, then install `taichi_glsl` via `pip`:

```python3 -m pip install --user taichi_glsl
```

Import me using:

```import taichi as ti
import taichi_glsl as ts
```

Or simply:

```from taichi_glsl import *
```

Note that this will import `taichi` as name `ti` as well.

Scalar math¶

GLSL-alike scalar arithmetic functions.

Functions¶

 `atan`(y[, x]) Return the arc-tangent of the parameters `clamp`(x[, xmin, xmax]) Constrain a value to lie between two further values. Compute the fractional part of the argument. Return the inverse of the square root of the parameter. Determine whether the parameter is positive or negative infinity. Determine whether the parameter is a number. `mix`(x, y, a) Linearly interpolate between two values. Find the nearest integer less than or equal to the parameter. `sign`(x[, edge]) Extract the sign of the parameter. `smoothstep`(x[, a, b]) Perform Hermite interpolation between two values. `step`(edge, x) Generate a step function by comparing two values.

Vector math¶

GLSL-alike linear algebra helper functions / aliases.

Functions¶

 `cross`(a, b) Calculate the cross product of two vectors. `distance`(a, b) Calculate the distance between two points. `dot`(a, b) Calculate the dot product of two vectors. Calculate the inverse of length of a vector. Calculate the length of a vector. `mat`(*xs) Matrix initializer (WIP). Find the maximum value in all elements of a vector. Find the minimum value in all elements of a vector. Calculates the unit vector in the same direction as the original vector. `normalizePow`(v, n[, eps]) Return a vector with same direction but with a n-powered length. `outerProduct`(a, b) `reflect`(I, N) Calculate the reflection direction for an incident vector. `refract`(I, N, eta) Calculate the refraction direction for an incident vector. `shuffle`(a, *ks) Calculate the square of the length of a vector. Calculate the sum of all elements in a vector. `vec`(*xs) Create a vector by scalars or vectors in arguments (GLSL-alike). `vec2`(*xs) An alias for vecND(2, *xs). `vec3`(*xs) An alias for vecND(3, *xs). `vec4`(*xs) An alias for vecND(4, *xs). Return a 2D vector of specific phase angle. `vecFill`(n, x) Create a n-D vector whose all components are initialized by x. `vecND`(n, *xs) Create a n-D vector by scalars or vectors in arguments (GLSL-alike). `vecSimple`(*xs) Create a n-D vector whose components are specified by arguments.

Field sampling¶

Some helper functions in processing fields & images.

Functions¶

 `bilerp`(field, P) Bilinear sampling an 2D field with a real index. `dflSample`(field, P, dfl) Sampling a field, when indices out of the field shape, return the given default value. `sample`(field, P) Sampling a field with indices clampped into the field shape. `superSample2x2`(fieldFunc, P[, dx])

Taichi Classes¶

Classes¶

 `Complex`(x[, y]) Complex number support in Taichi GLSL

GUI Base Class¶

Display images or animations using Taichi GUI (WIP)

Classes¶

 `Animation`([img, circles, title, res]) Handy Shadertoy-alike GUI base class.

Random generator¶

Pseudo-random number or noise generators.

Functions¶

 Generate a random floating number distributed evenly in range [0, 1]. `randInt`(a, b) Generate a random integer in range [a, b], including both end points. Generate a n-D random vector in a n-D cube ([0, 1]). `randNDRange`(a, b) Generate a n-D random vector in a n-D cube. `randRange`([a, b]) Generate random floating numbers in range [a, b]. Generate a 2-D random unit vector whose length is <= 1.0. Generate a 2-D random unit vector whose length is equal to 1.0. Generate a 3-D random unit vector whose length is equal to 1.0.

Particle simluation¶

Some helper functions that might be useful in physics simulation.

Functions¶

 `ballBoundReflect`(pos, vel, center, radius[, …]) `boundReflect`(pos, vel[, pmin, pmax, gamma, …]) Reflect particle velocity from a rectangular boundary (if collides). `momentumExchange`(v1, v2, disp[, m1, m2, gamma]) Exchange momentum (bounce) between two objects.