Skip to content

Commit

Permalink
fix(geom-sdf): fix rect size handling
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 20, 2022
1 parent f50e292 commit cc72bab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions packages/geom-sdf/src/as-sdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import type {
} from "@thi.ng/geom";
import type { Attribs, IShape } from "@thi.ng/geom-api";
import { __dispatch } from "@thi.ng/geom/internal/dispatch";
import { maddN2 } from "@thi.ng/vectors/maddn";
import { add2 } from "@thi.ng/vectors/add";
import { mulN2 } from "@thi.ng/vectors/muln";
import type { SDFAttribs, SDFn } from "./api.js";
import {
difference,
Expand Down Expand Up @@ -119,8 +120,10 @@ export const asSDF: MultiFn1<IShape, SDFn> = defmulti<any, SDFn>(
quadratic: ({ points: [a, b, c], attribs }: Quadratic) =>
quadratic2(a, b, c, __sdfAttribs(attribs)),

rect: ({ pos, size, attribs }: Rect) =>
box2(maddN2([], size, 0.5, pos), size, __sdfAttribs(attribs)),
rect: ({ pos, size, attribs }: Rect) => {
const s = mulN2([], size, 0.5);
return box2(add2([], s, pos), s, __sdfAttribs(attribs));
},
}
);

Expand Down
4 changes: 2 additions & 2 deletions packages/geom-sdf/src/dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const mag2 = (v: ReadonlyVec) => Math.sqrt(magSq2(v));

export const distCircle2 = (p: ReadonlyVec, radius: number) => mag2(p) - radius;

export const distBox2 = (p: ReadonlyVec, size: ReadonlyVec) => {
const d = sub2(null, abs2([], p), size);
export const distBox2 = (p: ReadonlyVec, halfSize: ReadonlyVec) => {
const d = sub2(null, abs2([], p), halfSize);
return Math.min(Math.max(d[0], d[1]), 0) + mag2(max2(null, d, ZERO2));
};

Expand Down

0 comments on commit cc72bab

Please sign in to comment.