@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
83 lines (82 loc) • 1.9 kB
JavaScript
;
import { Vector2, Vector3 } from "three";
import { NamedFunction3, NamedFunction4 } from "./_Base";
import { absV3, maxV3Components, maxV3Component } from "./_VectorUtils";
const _q = new Vector3();
const _v2 = new Vector2();
const _sHalf = new Vector3();
export function _SDFBox(p, center, sizes, size) {
_sHalf.copy(sizes).multiplyScalar(size * 0.5);
absV3(p.sub(center), _q);
_q.sub(_sHalf);
const max = Math.min(0, maxV3Components(_q));
maxV3Component(_q, _q, 0);
const length = _q.length();
return length + max;
}
export class SDFBox extends NamedFunction4 {
constructor() {
super(...arguments);
this.func = _SDFBox;
}
static type() {
return "SDFBox";
}
}
export function _SDFPlane(p, center, normal, offset) {
_q.copy(normal).normalize();
return p.sub(center).dot(_q) + offset;
}
export class SDFPlane extends NamedFunction4 {
constructor() {
super(...arguments);
this.func = _SDFPlane;
}
static type() {
return "SDFPlane";
}
}
export function _SDFSphere(p, center, s) {
return p.sub(center).length() - s;
}
export class SDFSphere extends NamedFunction3 {
constructor() {
super(...arguments);
this.func = _SDFSphere;
}
static type() {
return "SDFSphere";
}
}
export function _SDFTorus(p, center, radius1, radius2) {
p.sub(center);
_v2.x = p.x;
_v2.y = p.z;
_q.x = _v2.length() - radius1;
_q.y = p.y;
return _q.length() - radius2;
}
export class SDFTorus extends NamedFunction4 {
constructor() {
super(...arguments);
this.func = _SDFTorus;
}
static type() {
return "SDFTorus";
}
}
export function _SDFTube(p, center, radius) {
p.sub(center);
_q.x = p.x;
_q.y = p.z;
return _q.length() - radius;
}
export class SDFTube extends NamedFunction3 {
constructor() {
super(...arguments);
this.func = _SDFTube;
}
static type() {
return "SDFTube";
}
}