Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add isPointInCircle(), isPointInRect()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 25, 2023
1 parent d5f21c5 commit de4b1ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/shader-ast-stdlib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export * from "./fog/exp.js";
export * from "./fog/exp2.js";
export * from "./fog/linear.js";

export * from "./isec/point.js";

export * from "./light/lambert.js";
export * from "./light/trilight.js";

Expand Down
36 changes: 36 additions & 0 deletions packages/shader-ast-stdlib/src/isec/point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Vec2Sym } from "@thi.ng/shader-ast";
import { F, V2 } from "@thi.ng/shader-ast/api/types";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { bool } from "@thi.ng/shader-ast/ast/lit";
import { add } 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 { distance, min, step } from "@thi.ng/shader-ast/builtin/math";

/**
* Predicate function to check if given point `p` is inside the circle defined
* by `pos` and `radius`.
*/
export const isPointInCircle = defn(
"bool",
null,
[V2, V2, F],
(p, pos, radius) => [ret(bool(step(distance(p, pos), radius)))]
);

/**
* Predicate function to check if given point `p` is inside the axis-aligned
* rect defined by `pos` and `size`. Branchless.
*/
export const isPointInRect = defn(
"bool",
null,
[V2, V2, V2],
(p, pos, size) => {
let q: Vec2Sym;
return [
(q = sym(min(step(pos, p), step(p, add(pos, size))))),
ret(bool(min($x(q), $y(q)))),
];
}
);

0 comments on commit de4b1ab

Please sign in to comment.