@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
86 lines (85 loc) • 2.14 kB
JavaScript
;
import { Vector2 } from "three";
import { absV2, vector2MaxScalar, vector2Dot } from "./_VectorUtils";
import { _sizzleVec2 } from "./_Sizzle";
import { NamedFunction2, NamedFunction3, NamedFunction4, NamedFunction5 } from "./_Base";
const _q = new Vector2();
const _q2 = new Vector2();
const _b = new Vector2();
const _w = new Vector2();
export class SDF2DBox extends NamedFunction3 {
static type() {
return "SDF2DBox";
}
func(p, center, size) {
p.sub(center);
absV2(p, p);
p.sub(size);
return vector2MaxScalar(p, 0, _q2).length() + Math.min(Math.max(p.x, p.y), 0);
}
}
export class SDF2DCircle extends NamedFunction3 {
static type() {
return "SDF2DCircle";
}
func(p, center, radius) {
p.sub(center);
return p.length() - radius;
}
}
export class SDF2DCross extends NamedFunction5 {
static type() {
return "SDF2DCross";
}
func(p, center, length, width, radius) {
p.sub(center);
absV2(p, p);
if (p.y > p.x) {
_sizzleVec2(p, _q2);
p.copy(_q2);
}
_b.x = length;
_b.y = width;
_q.copy(p).sub(_b);
const k = Math.max(_q.y, _q.x);
if (k > 0) {
_w.copy(_q);
} else {
_w.x = _b.y - p.x;
_w.y = -k;
}
return Math.sign(k) * vector2MaxScalar(_w, 0, _w).length() + radius;
}
}
const SQRT_2_BY_4 = Math.sqrt(2) / 4;
export class SDF2DHeart extends NamedFunction2 {
static type() {
return "SDF2DHeart";
}
func(p, center) {
p.sub(center);
p.x = Math.abs(p.x);
if (p.y + p.x > 1) {
_q2.set(0.25, 0.75);
return Math.sqrt(vector2Dot(p.sub(_q2))) - SQRT_2_BY_4;
}
_q2.set(0.25, 0.75);
const a = vector2Dot(p.sub(_q2));
const b = vector2Dot(p.subScalar(0.5 * Math.max(p.x + p.y, 0)));
const c = Math.sqrt(Math.min(a, b));
const d = Math.sign(p.x - p.y);
return c * d;
}
}
export class SDF2DRoundedX extends NamedFunction4 {
static type() {
return "SDF2DRoundedX";
}
func(p, center, w, r) {
p.sub(center);
absV2(p, _q);
const min = Math.min(_q.x + _q.y, w) * 0.5;
_q.subScalar(min);
return _q.length() - r;
}
}