npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hiccup-sdf

v0.0.10

Published

hiccup-like syntax for generating SDFs on CPU and GPU

Downloads

11

Readme

hiccup-sdf

Library for building signed distance function models using hiccup-like language.

Installation

npm install hiccup-sdf --save

Basic usage

const { displayRaw } = require("display-sdf");
const { compileShader, glslHelpers } = require("hiccup-sdf");

const tree = [
  "difference",
  [
    ["box", { s: [0.5, 0.5, 0.5] }],
    ["sphere", { r: 0.4 }]
  ]
];

const { inject, model } = compileShader(tree);
const shader = glslHelpers.createShaderFull(model, inject);

displayRaw(shader);

Language

Shapes

  • ["sphere", { r: float }]
  • ["box", { s: [float, float, float] }]
  • ["torus", { r1: float, r2: float }]
  • ["hex", { h: float, r: float }]
  • ["triangle", { h: float, r: float }]
  • ["capsule", { a: float, b: float, r: float }]
  • ["cylinder", { h: float, r: float }]

Operations

  • ["translate", { t: [float, float, float] }, [subtree]]
  • ["scale", { s: float }, [subtree]]
  • ["rotate", { r: [float, float, float] }, [subtree]]
  • ["mirror", { m: [float, float, float] }, [subtree]]
  • ["elongate", { s: [float, float, float] }, [subtree]]
  • ["repeat", { r: [float, float, float] }, [subtree]]
  • ["repeatPolar", { r: float }, [subtree]]
  • ["union", { r: float }, [subtree]]
  • ["intersection", { r: float }, [subtree]]
  • ["difference", { r: float }, [subtree]]
  • ["blend", { k: float }, [subtree]]

Map

Map is bit more complex operation allowing map-reduce style mapping over huge datasets. This works around instruction/complexity limitations for GPU shaders, and provides nicer API to work with. Unfortunately there are some differences in map usage on CPU/GPU (all the other functions/operations should work the same regardless of the backend).

const randomPositions = range(1000).map(() => randomPosition()); // random array of [x,y,z] positions

const tree = [
  "map",
  {
    // data points to map over, an object mapping key to value, multiple datasets can be supplied
    data: { position: randomPositions },

    // mapping function, runs for each of the data points, `props` contains a single data point for each of `data` keys
    map: props => [
      "translate",
      {
        t: `${props.position}.xyz` // fragment of GLSL code that will be injected when mapping over the texture when ran on GPU, if used on CPU this should be `{ t: props.position }` as we can use the js array directly
      },
      [
        ["sphere", { r: 0.1 }]
      ]
    ],

    // reducing function, generated subtree will be inserted as last parameter,
    // usually `union` / `interesction` / `difference`
    reduce: ["union", { r: 0.02 }]
  }
]

When ran on GPU this operation is turned into efficient texture traversal, and compileShader() returns uniforms with matching textures. For full example open examples/basic/map.js.

Public API

const dsl = require("hiccup-sdf")

  • dsl.compileShader(tree) - generates SDF shader, returns an object: { model, uniforms, inject }:

    • model - modeling part of SDF shader on GPU
    • inject - SDF shader code necessary to run the modeling shader
    • uniforms - data values if the tree is using map function
  • dsl.compileFunction(tree) - generate SDF function to be used on CPU, returns a function:

    • fn([x, y, z]) - returns SDF value for given point
  • dsl.glslHelpers - helper utils for generating GLSL code:

    • dsl.glslHelpers.createShaderFull(model, inject) - creates full SDF shader that can be used without any modifications
    • dsl.glslHelpers.createShaderModel(model, inject) - creates only the vec2 doModel(vec3 p) part of the shader

Acknowledgements

This project was developed in part at Laboratory, an artist residency for interactive arts: https://laboratoryspokane.com.