Generate meshes from signed distance functions and constructive solid geometry operations.

Overview

sdf-csg

Generate meshes from signed distance functions and constructive solid geometry operations. This library is heavily based upon Inigo Quilez's 3D SDFs article.

Example

import { Box, Sphere, CappedCylinder } from "sdf-csg";

const sdf = new Sphere(1)
  .setUserData([0, 0, 1])
  .smoothIntersect(new Box(0.75, 0.75, 0.75).setUserData([1, 0, 0]), 0.1)
  .smoothSubtract(
    new CappedCylinder(1, 0.5)
      .smoothUnion(new CappedCylinder(1, 0.5).rotateX(Math.PI / 2), 0.1)
      .smoothUnion(new CappedCylinder(1, 0.5).rotateZ(Math.PI / 2), 0.1)
      .setUserData([0, 1, 0]),
    0.1
  );

const mesh = sdf.generateMesh([64, 64, 64], 0.2);

Live Demo

Note: this project is very much a work in progress. There may be significant changes to the API until the 1.0.0 semantic version is released.

Primitives

All of these return a new SDF.

const sdf = new Box(width: number, height: number, depth: number)
const sdf = new Sphere(radius: number)
const sdf = new BoxFrame(width: number, height: number, depth: number,  edge: number)
const sdf = new Torus(majorRadius: number,  minorRadius: number)
const sdf = new CappedTorus(majorRadius: number,  minorRadius: number,  angle: number)
const sdf = new Link(majorRadius: number,  minorRadius: number,  length: number)
const sdf = new Cone(angle: number,  height: number)
const sdf = new HexagonalPrism(radius: number,  length: number)
const sdf = new Capsule(pointA: number[],  pointB: number[],  radius: number)
const sdf = new CappedCylinder(length: number,  radius: number)
const sdf = new CappedCone(length: number,  radius1: number,  radius2: number)
const sdf = new SolidAngle(angle: number, radius: number)

Operations

All of these return a new SDF.

sdf.union(sdf: SDF)
sdf.subtract(sdf: SDF)
sdf.intersect(sdf: SDF)
sdf.smoothUnion(sdf: SDF, smoothness: number)
sdf.smoothSubtract(sdf: SDF, smoothness: number)
sdf.smoothIntersect(sdf: SDF, smoothness: number)
sdf.translate(x: number, y: number, z: number)
sdf.rotate(quat: number[])
sdf.rotateX(radians: number)
sdf.rotateY(radians: number)
sdf.rotateZ(radians: number)
sdf.scale(amount: number)
sdf.round(amount: number)

Example: smoothUnion

const sdf = new Sphere(1)
  .translate(-0.5, 0, 0)
  .smoothUnion(new Sphere(1).translate(0.5, 0, 0), 0.1)
  .setUserData([1, 0.5, 1]);

Example: smoothSubtract

const sdf = new Sphere(1)
  .translate(-0.5, 0, 0)
  .smoothSubtract(new Sphere(1).translate(0.5, 0, 0), 0.2)
  .setUserData([1, 0.5, 1]);

Example: smoothIntersect

const sdf = new Sphere(1)
  .translate(-0.5, 0, 0)
  .smoothIntersect(new Sphere(1).translate(0.5, 0, 0), 0.2)
  .setUserData([1, 0.5, 1]);

Example: round

const sdf = new HexagonalPrism(0.25, 1)
  .translate(-0.75, 0, 0)
  .union(new HexagonalPrism(0.25, 1).translate(0.75, 0, 0).round(0.1))
  .setUserData([1, 0.5, 1]);

User data

You can assign "user data" - an array of numbers - to components of your SDF. These are interpolated across the SDF and returned by the generateMesh function. Here's an example using them to interpolate color across the surface of the mesh, but you could imagine using them for other purposes, such as values for use with physically based rendering, etc.

const sdf = new Sphere(1)
  .setUserData([1, 0.5, 0.5])
  .translate(-0.75, 0, 0)
  .smoothUnion(new Sphere(1).setUserData([0.5, 0.5, 1]).translate(0.75, 0, 0), 0.5);

Mesh generation

const mesh = sdf.generateMesh(resolution: [number, number, number], padding: number);

Returns a Mesh:

interface Mesh {
  positions: number[][];
  normals: number[][];
  cells: number[][];
  userdata: number[][] | null;
}

Road map

  • The current isosurface extractor generates poor results for edges and corners. Write or use a different one for better results.
  • Isosurface extraction is slow. Speed it up (perhaps the SDF data could be used to skip large chunks of the grid?).
  • The bounds of the SDF are determined analytically, and it's a little buggy (e.g., when using the "smooth" operators), requiring the use of padding when generating the mesh. Some options:
    • Fix the bugs.
    • Determine the bounds numerically, using the value of the SDF to accelerate it.
  • Add more primitives.
  • Add more operators.

Credits

This library is heavily based upon Inigo Quilez's 3D SDFs article.

You might also like...

BI, API and Automation layer for your Engineering Operations data

BI, API and Automation layer for your Engineering Operations data

Faros Community Edition Faros Community Edition (CE) is an open-source engineering operations platform that connects the dots between all your operati

Dec 23, 2022

Provides simple and the most useful methods to string operations in JavaScript / Node.js

🔪 Strops (String Operations) Provides simple methods for the most useful operations with substrings: - remove, replace, get from A to B, get from A t

May 20, 2022

A calculation and tracker tool for one-person business operations

A calculation and tracker tool for one-person business operations

🧮 Taxemu This is the alpha version of Taxemu. A tracker tool for one-person business operations. The live project can be found here. Development Clon

Nov 30, 2022

This is a project that is used to execute python codes in the web page. You can install and use it in django projects, You can do any operations that can be performed in python shell with this package.

Django execute code This is a project that is used to execute python codes in the web page. You can install and use it in django projects, You can do

Nov 12, 2022

🍉 Water is a micro-ORM + QueryBuilder designed to facilitate queries and operations on PostgreSQL databases designed to work in Melon

🍉 Water Water is a micro-ORM + QueryBuilder designed to facilitate queries and operations on PostgreSQL databases designed to work in MelonRuntime In

Aug 6, 2022

An abstraction layer on top of @replit/crosis that makes Repl connection management and operations so easy, a Furret could do it! 🎉

An abstraction layer on top of @replit/crosis that makes Repl connection management and operations so easy, a Furret could do it! 🎉

Crosis4Furrets An abstraction layer on top of @replit/crosis that makes Repl connection management and operations so easy, a Furret could do it! 🎉 In

Dec 29, 2022

Open-source CD platform that helps developers to deliver applications efficiently by simplifying software releases and operations in any environment.

Open-source CD platform that helps developers to deliver applications efficiently by simplifying software releases and operations in any environment.

dyrector.io - The open source internal delivery platform Overview dyrector.io is an open-source internal delivery platform that helps developers to de

Jan 3, 2023

Experience Lab is a set of utilities that assist in creating instances of Microsoft Energy Data Services, performing data loads, and performing basic management operations.

Experience Lab - Microsoft Energy Data Services Build Status About Experience Lab is an automated, end-to-end deployment accelerator for Microsoft Ene

Dec 14, 2022

A simple in-memory key-value cache for function execution, allowing both sync and async operations using the same methods

A simple in-memory key-value cache for function execution, allowing both sync and async operations using the same methods. It provides an invalidation mechanism based both on exact string and regex.

Dec 15, 2022
Comments
  • Include sdf-csg in Curated Code CAD?

    Include sdf-csg in Curated Code CAD?

    Really cool project, good readme too 😁

    I maintain a list of "CodeCAD" or programable CAD projects at: Curated Code CAD

    Would you like me to include this repo as well? and if so as far as a description goes shoud I just use

    Generate meshes from signed distance functions and constructive solid geometry operations. This library is heavily based upon Inigo Quilez's 3D SDFs article.

    Or would you like to give me something different?

    I'm also curious how long you've been working on this?

    Since this is not a issue directly related to your repo I'll close this issue soon.

    opened by Irev-Dev 2
Owner
Rye Terrell
I don't find anything as satisfying as making tools that people like to use.
Rye Terrell
A string of four operations of the library, can solve the js digital calculation accuracy of scientific notation and formatting problems, support for thousands of decimal point formatting output operations

A string of four operations of the library, can solve the js digital calculation accuracy of scientific notation and formatting problems, support for thousands of decimal point formatting output operations

null 10 Apr 6, 2022
Awesome critique of crypto / web3. Curated list of high quality critique plus background. Seek to be as constructive as possible.

Awesome critique of crypto/web3 Awesome critique of crypto/web3, etc. Contributions are welcome. Critique General Stephen Diehl series - https://www.s

Rufus Pollock 1.5k Jan 1, 2023
I'm trying to create simple program for adding the digital signature to a pdf file with self-signed certificate. I use node-signpdf and pdf-lib library.

pdf-digital-signature-with-node-signpdf-ejs I'm trying to create simple program for adding the digital signature to a pdf file with self-signed certif

null 5 Dec 25, 2022
Stacks Voice is a reference project that builds on the SIP018 signed structured data standard to create an accountless internet forum.

Stacks Voice Stacks Voice is a reference project that builds on the SIP018 signed structured data standard to create an accountless internet forum. Th

Clarity Innovation Lab 4 Dec 21, 2022
A concise collection of classes for PHP, Python, JavaScript and Ruby to calculate great circle distance, bearing, and destination from geographic coordinates

GreatCircle A set of three functions, useful in geographical calculations of different sorts. Available for PHP, Python, Javascript and Ruby. Live dem

null 72 Sep 30, 2022
Break the barrier of your country, language and distance...

HACK ON 2.0 PROJECT METAFRATIS Break the barrier of your country, language and distance... PROBLEM OUR PROJECT SOLVES With the onset of Covid-19 we ha

Snehasish Dhar 16 Jul 6, 2021
⏱ Simple Alpine.js plugin to display the human-readable distance between a date and now.

⏱ Alpine TimeAgo ⏱ An Alpine.js plugin to return the distance between a given date and now in words (like "3 months ago", "about 2 hours ago" or "in a

Marc Reichel 47 Dec 22, 2022
Open-source project inspired by the idea of "Long Distance Lamp"

Project inspired by the idea of Long Distance Lamp (e.g. Friendship Lamps) for people with no soldering skills.

Davide Gabrielli 3 Mar 19, 2022
Functions Recipes is a library of examples to help you getting started with Salesforce Functions and get used to their main features.

Functions Recipes Introduction Salesforce Functions lets you use the Salesforce Platform for building event-driven, elastically scalable apps and expe

Trailhead Apps 172 Dec 29, 2022