Skip to content

Commit

Permalink
fix(shader-ast-stdlib): fix thi-ng#399 update HOF function naming
Browse files Browse the repository at this point in the history
- ensure generated HOFs are using unique names to allow multiple instances
  • Loading branch information
postspectacular committed May 5, 2023
1 parent e71e2d0 commit f4b62d7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
5 changes: 3 additions & 2 deletions packages/shader-ast-stdlib/src/math/additive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { FloatSym, FloatTerm, Prim, Term } from "@thi.ng/shader-ast";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { float, FLOAT0, FLOAT05 } from "@thi.ng/shader-ast/ast/lit";
import { gensym } from "@thi.ng/shader-ast/ast/idgen";
import { FLOAT0, FLOAT05, float } from "@thi.ng/shader-ast/ast/lit";
import { add, inc, lt, mul } from "@thi.ng/shader-ast/ast/ops";
import { sym } from "@thi.ng/shader-ast/ast/sym";

Expand All @@ -30,7 +31,7 @@ export const additive = <T extends Prim>(
type: T,
fn: Fn<Term<T>, FloatTerm>,
oct: number | FloatTerm = 4,
name = "additive"
name = gensym("additive_")
) =>
defn("float", name, [[type], [type], "float"], (pos, shift, decay) => {
let n: FloatSym;
Expand Down
25 changes: 17 additions & 8 deletions packages/shader-ast-stdlib/src/noise/worley2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Func2, Vec2Sym, Vec3Sym } from "@thi.ng/shader-ast";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { gensym } from "@thi.ng/shader-ast/ast/idgen";
import { float, vec3 } from "@thi.ng/shader-ast/ast/lit";
import { add, lt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { $, $x, $xy, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
Expand Down Expand Up @@ -32,20 +33,28 @@ export const worleyDistManhattan = defn(
);

/**
* Higher order function. Computes 2D Worley noise using provided
* distance function. The returned function takes 2 args: position and
* jitter amount, the latter in [0..1] interval. Returns noise
* components as vec2, with the x component containing the distance from
* closest simplex center and y the noise value. The vector components
* can be used individually or combined (e.g. `noise.y - noise.x`)...
* Higher order function. Computes 2D Worley noise using provided distance
* function. The returned function takes 2 args: position and jitter amount, the
* latter in [0..1] interval. Returns noise components as vec2, with the x
* component containing the distance from closest simplex center and y the noise
* value. The vector components can be used individually or combined (e.g.
* `noise.y - noise.x`)...
*
* THe optional `name` arg can be used to customize the name of the generated
* function.
*
* @remarks
* Based on implementation by Stefan Gustavson:
* http://webstaff.itn.liu.se/~stegu/GLSL-cellular/GLSL-cellular-notes.pdf
*
* @param distFn -
* @param name -
*/
export const worley2 = (distFn: Func2<"vec3", "vec3", "vec3">) =>
defn("vec2", "worley2", ["vec2", "float"], (P, jitter) => {
export const worley2 = (
distFn: Func2<"vec3", "vec3", "vec3">,
name = gensym("worley2_")
) =>
defn("vec2", name, ["vec2", "float"], (P, jitter) => {
const K = float(1 / 7);
const Ko = float(3 / 7);
const oI = sym(vec3(-1, 0, 1));
Expand Down
12 changes: 9 additions & 3 deletions packages/shader-ast-stdlib/src/raymarch/ao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { FloatSym } from "@thi.ng/shader-ast";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { float, FLOAT0, FLOAT05, FLOAT1 } from "@thi.ng/shader-ast/ast/lit";
import { gensym } from "@thi.ng/shader-ast/ast/idgen";
import { FLOAT0, FLOAT05, FLOAT1, float } from "@thi.ng/shader-ast/ast/lit";
import { add, inc, lte, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { $x } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
Expand All @@ -17,9 +18,14 @@ import { clamp01 } from "../math/clamp.js";
*
* @param scene -
* @param numSamples -
* @param name -
*/
export const raymarchAO = (scene: RaymarchScene, numSamples = 5) =>
defn("float", "raymarchAO", ["vec3", "vec3"], (p, n) => {
export const raymarchAO = (
scene: RaymarchScene,
numSamples = 5,
name = gensym("raymarchAO_")
) =>
defn("float", name, ["vec3", "vec3"], (p, n) => {
let r: FloatSym;
let w: FloatSym;
let d0: FloatSym;
Expand Down
6 changes: 5 additions & 1 deletion packages/shader-ast-stdlib/src/raymarch/normal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Swizzle2_3, Vec2Sym } from "@thi.ng/shader-ast";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { gensym } from "@thi.ng/shader-ast/ast/idgen";
import { vec2, vec3 } from "@thi.ng/shader-ast/ast/lit";
import { add, sub } from "@thi.ng/shader-ast/ast/ops";
import { $, $x } from "@thi.ng/shader-ast/ast/swizzle";
Expand All @@ -16,7 +17,10 @@ import type { RaymarchScene } from "../api.js";
* @param scene -
* @param name -
*/
export const raymarchNormal = (scene: RaymarchScene, name = "raymarchNormal") =>
export const raymarchNormal = (
scene: RaymarchScene,
name = gensym("raymarchNormal_")
) =>
defn("vec3", name, ["vec3", "float"], (p, smooth) => {
let dn: Vec2Sym;
const comp = (id: Swizzle2_3) =>
Expand Down
5 changes: 3 additions & 2 deletions packages/shader-ast-stdlib/src/raymarch/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { FloatSym, Vec2Sym } from "@thi.ng/shader-ast";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { brk, forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { float, int, INT0, vec2 } from "@thi.ng/shader-ast/ast/lit";
import { gensym } from "@thi.ng/shader-ast/ast/idgen";
import { INT0, float, int, vec2 } from "@thi.ng/shader-ast/ast/lit";
import { gt, inc, lt, madd } from "@thi.ng/shader-ast/ast/ops";
import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
Expand Down Expand Up @@ -31,7 +32,7 @@ export const raymarchScene = (
_opts?: Partial<RaymarchOpts>
) => {
const opts: RaymarchOpts = {
name: "raymarchScene",
name: gensym("raymarchScene_"),
near: 0.1,
far: 10,
steps: 100,
Expand Down
4 changes: 2 additions & 2 deletions packages/shader-ast-stdlib/src/sdf/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assign } from "@thi.ng/shader-ast/ast/assign";
import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { index } from "@thi.ng/shader-ast/ast/indexed";
import { bvec3, FLOAT1, int, INT0 } from "@thi.ng/shader-ast/ast/lit";
import { FLOAT1, INT0, bvec3, int } from "@thi.ng/shader-ast/ast/lit";
import {
div,
gt,
Expand All @@ -18,7 +18,7 @@ import {
} from "@thi.ng/shader-ast/ast/ops";
import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import { all, _any } from "@thi.ng/shader-ast/builtin/bvec";
import { _any, all } from "@thi.ng/shader-ast/builtin/bvec";
import { dot, min, sqrt } from "@thi.ng/shader-ast/builtin/math";
import { clamp01 } from "../math/clamp.js";

Expand Down

0 comments on commit f4b62d7

Please sign in to comment.