Liking cljdoc? Tell your friends :D

threeagent.tsl

clj

Macros for writing TSL shaders as Clojure. See threeagent/tsl.cljs for the language reference and threeagent.tsl.compiler for the rewriting rules.

Macros for writing TSL shaders as Clojure. See threeagent/tsl.cljs for the
language reference and threeagent.tsl.compiler for the rewriting rules.
cljs

Write Three.js TSL shaders as Clojure.

Macros (see threeagent/tsl.clj):

  • shader compile an expression into a TSL node (or a map of nodes)
  • defshader defn whose body is shader code, e.g. a material's shader
  • shader-fn a TSL Fn, callable like a Clojure function
  • defshader-fn def + shader-fn; typed params emit a named WGSL function
  • compute a compute node: (compute {:count n} body...)
  • defcompute def + compute

Inside shader code, arithmetic, comparison and logic operators build nodes when any argument is a node and keep their Clojure meaning otherwise; [a b c] is a vec3; (:xyz v) swizzles; any three/tsl export can be called by its JS name or kebab-case name (position-local, mx-noise-float).

The functions below are the runtime the macros compile to. They are public so the SCI integration can reach them, and usable directly.

Write Three.js TSL shaders as Clojure.

Macros (see threeagent/tsl.clj):
- `shader`        compile an expression into a TSL node (or a map of nodes)
- `defshader`     `defn` whose body is shader code, e.g. a material's shader
- `shader-fn`     a TSL `Fn`, callable like a Clojure function
- `defshader-fn`  `def` + `shader-fn`; typed params emit a named WGSL function
- `compute`       a compute node: `(compute {:count n} body...)`
- `defcompute`    `def` + `compute`

Inside shader code, arithmetic, comparison and logic operators build nodes
when any argument is a node and keep their Clojure meaning otherwise;
`[a b c]` is a vec3; `(:xyz v)` swizzles; any three/tsl export can be called
by its JS name or kebab-case name (`position-local`, `mx-noise-float`).

The functions below are the runtime the macros compile to. They are public
so the SCI integration can reach them, and usable directly.
raw docstring

$cljs

($ n)
($ n a)
($ n a b)
($ n a b c)
($ n a b c d)
($ n a b c d & more)

Call the three/tsl export named n.

Uses Reflect.apply: many exports are Fn Proxies whose get trap turns f.call(null, a) into FnNode.call(null, a), shifting every argument.

Call the three/tsl export named `n`.

Uses Reflect.apply: many exports are `Fn` Proxies whose `get` trap turns
`f.call(null, a)` into `FnNode.call(null, a)`, shifting every argument.
raw docstring

$getcljs

($get n)

The three/tsl export named n.

The three/tsl export named `n`.
raw docstring

addcljs


and*cljs

(and* & thunks)

Evaluates thunks left to right with Clojure short-circuiting until a node shows up; from there on the rest are combined with TSL and.

Evaluates thunks left to right with Clojure short-circuiting until a node
shows up; from there on the rest are combined with TSL `and`.
raw docstring

assign!cljs

(assign! target value)

(set! target value) in shader code.

`(set! target value)` in shader code.
raw docstring

bit-andcljs


bit-notcljs

(bit-not x)

bit-orcljs


bit-shift-leftcljs


bit-shift-rightcljs


bit-xorcljs


call-fncljs

(call-fn fn-node & args)

computeclj/smacro

(compute & args)

A compute node running the body once per invocation.

(compute {:count n :workgroup-size [64]} (let [i instance-index] (set! (nth positions i) (+ (nth positions i) (nth velocities i)))))

A compute node running the body once per invocation.

(compute {:count n :workgroup-size [64]}
  (let [i instance-index]
    (set! (nth positions i) (+ (nth positions i) (nth velocities i)))))
raw docstring

compute*cljs

(compute* f count workgroup-size)

cond*cljs

(cond* stmt? clauses)

clauses alternates test thunks (or :else) and body thunks. CPU tests short-circuit as in Clojure; node tests become a select chain (expressions) or an If/ElseIf/Else chain (statements).

`clauses` alternates test thunks (or :else) and body thunks. CPU tests
short-circuit as in Clojure; node tests become a select chain (expressions)
or an If/ElseIf/Else chain (statements).
raw docstring

deccljs

(dec x)

defcomputeclj/smacro

(defcompute & args)

def + compute.

`def` + `compute`.
raw docstring

defshaderclj/smacro

(defshader & args)

defn whose body is shader code. Typical use is a node material's shader:

(defshader glow [{:keys [time tint]}] {:color (* tint (+ 0.5 (* 0.5 (sin time))))})

`defn` whose body is shader code. Typical use is a node material's shader:

(defshader glow [{:keys [time tint]}]
  {:color (* tint (+ 0.5 (* 0.5 (sin time))))})
raw docstring

defshader-fnclj/smacro

(defshader-fn & args)

def + shader-fn. Typed params produce a WGSL function named after the var.

`def` + `shader-fn`. Typed params produce a WGSL function named after the var.
raw docstring

divcljs


eqcljs


fn-argcljs

(fn-arg inputs i n)

Parameter i (named n) of a TSL Fn. Without a layout TSL passes an array-like; with one, an object keyed by parameter name.

Parameter `i` (named `n`) of a TSL Fn. Without a layout TSL passes an
array-like; with one, an object keyed by parameter name.
raw docstring

fn-nodecljs

(fn-node f layout)

tsl.Fn(f, layout). The result is a JS Proxy: call it with call-fn, never as a Clojure function (IFn dispatch breaks it in advanced builds).

`tsl.Fn(f, layout)`. The result is a JS Proxy: call it with `call-fn`,
never as a Clojure function (IFn dispatch breaks it in advanced builds).
raw docstring

fn-node-ofcljs

(fn-node-of shader-fn)

The underlying TSL Fn of a shader-fn.

The underlying TSL Fn of a `shader-fn`.
raw docstring

gtcljs


gtecljs


if*cljs

(if* test then else stmt?)

Node test: TSL select for expressions, If().Else() for statements. CPU test: plain Clojure if.

Node test: TSL `select` for expressions, `If().Else()` for statements.
CPU test: plain Clojure `if`.
raw docstring

inccljs

(inc x)

joincljs

(join & xs)

[a b c] in shader code. All numbers: vecN of that many components. With nodes: a JoinNode, whose size is the total component count, resolved when the shader builds - so [pos 1] with a vec3 pos is a vec4.

`[a b c]` in shader code. All numbers: `vecN` of that many components.
With nodes: a JoinNode, whose size is the total component count, resolved
when the shader builds - so `[pos 1]` with a vec3 `pos` is a vec4.
raw docstring

kwcljs

(kw x k)

(:k x) in shader code: a property (swizzle) on nodes, a lookup otherwise.

`(:k x)` in shader code: a property (swizzle) on nodes, a lookup otherwise.
raw docstring

layoutcljs

(layout fn-name return-type inputs)

loop-rangecljs

(loop-range name start end step body)

A TSL Loop over name from start towards end by step. body gets the loop index node.

A TSL Loop over `name` from `start` towards `end` by `step`. `body` gets the
loop index node.
raw docstring

loop-whilecljs

(loop-while test body)

ltcljs


ltecljs


maxcljs


mincljs


modcljs

Node: WGSL %. Numbers: cljs.core/mod.

Node: WGSL `%`. Numbers: `cljs.core/mod`.
raw docstring

mulcljs


neqcljs


node?cljs

(node? x)

True for TSL nodes.

True for TSL nodes.
raw docstring

notcljs

(not x)

nthcljs

(nth coll i)
(nth coll i not-found)

Node: .element(i) (arrays, buffers, matrix columns). Otherwise cljs.core/nth.

Node: `.element(i)` (arrays, buffers, matrix columns). Otherwise `cljs.core/nth`.
raw docstring

op-assign!cljs

(op-assign! method target value)

(+= target value) and friends. method is e.g. "addAssign".

`(+= target value)` and friends. `method` is e.g. "addAssign".
raw docstring

or*cljs

(or* & thunks)

remcljs


shaderclj/smacro

(shader & body)

Compile the body as shader code and return the resulting TSL node, or a map of nodes when the body returns a map literal.

Bodies that use statements (set!, +=, let-var, dotimes, for-range, while, a node-conditioned when...) are wrapped in a TSL Fn.

Compile the body as shader code and return the resulting TSL node, or a map
of nodes when the body returns a map literal.

Bodies that use statements (`set!`, `+=`, `let-var`, `dotimes`, `for-range`,
`while`, a node-conditioned `when`...) are wrapped in a TSL Fn.
raw docstring

shader-fnclj/smacro

(shader-fn & args)

A TSL Fn wrapped in a Clojure function.

(shader-fn [a b] (* a b)) ; inlined into the caller (shader-fn [a :float b :vec3] :vec3 (* a b)) ; emitted as a WGSL function (shader-fn [p :vec3] :void (set! ...)) ; statement function

A TSL Fn wrapped in a Clojure function.

(shader-fn [a b] (* a b))                     ; inlined into the caller
(shader-fn [a :float b :vec3] :vec3 (* a b))  ; emitted as a WGSL function
(shader-fn [p :vec3] :void (set! ...))        ; statement function
raw docstring

shader-fn*cljs

(shader-fn* f layout arity)

Wrap a TSL Fn in a Clojure function of arity args. The Fn itself is available as (fn-node-of f).

Wrap a TSL Fn in a Clojure function of `arity` args. The Fn itself is
available as `(fn-node-of f)`.
raw docstring

subcljs


Tcljs

The three/tsl module.

The three/tsl module.
raw docstring

var!cljs

(var! init)

A mutable shader variable initialised to init (let-var).

A mutable shader variable initialised to `init` (`let-var`).
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close