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

@davepagurek/p5.buildgeometry

v0.0.6

Published

Create a reusable `p5.Geometry` using the 3D primitives you're familiar with from p5!

Downloads

6

Readme

p5.buildGeometry

Create a reusable p5.Geometry using the 3D primitives you're familiar with from p5!

Why?

P5's WebGL mode uses the GPU, and the GPU is fast! However, sending data back and forth between the CPU and GPU can slow your sketch down a lot. If you have a shape that won't change from frame to frame, you can create a p5.Geometry, which you draw with model(yourGeometry), to avoid sending the same model data to the GPU every frame.

Unfortunately, p5 on its own doesn't have any easy way to make a p5.Geometry other than manually placing vertices. This library lets you build one out of all the p5 functions you already know: 3D primitives, rotate()/translate()/scale(), push()/pop(), beginShape()/vertex()/endShape(), etc!

Get the library

Add the library to your source code, after loading p5 but before loading your own code.

Via CDN

<script src="https://cdn.jsdelivr.net/npm/@davepagurek/[email protected]/build/p5.buildGeometry.js"></script>

Self-hosted

Download the minified or unminified source code from the releases tab, then add it to your HTML:

<script type="text/javascript" src="p5.buildGeometry.js"></script>

Via Typescript

First add the dependency:

yarn add @davepagurek/p5.buildgeometry

Yes, that's right, the dependency is named @davepagurek/p5.buildgeometry (all lowercase) even though the function name and repo name are p5.buildGeometry (with a capital G.) NPM doesn't like uppercase letters in packages 🥲

import '@davepagurek/p5.buildgeometry'

Usage

Create geometry in setup by calling buildgeometry. You need to pass in a unique id for your geometry, and a function that constructs the shape.

const myGeom = buildGeometry('myGeom', (builder) => {
  builder.push()
  builder.translate(100, -50)
  builder.scale(0.5)
  builder.rotateX(PI/4)
  builder.cone()
  builder.pop()
  builder.cone()
})

Inside of your callback, you can use the methods you'd normally use on a p5.Graphics to draw shapes.

Then, you can draw your geometry to the screen as if it was an imported model:

model(myGeom)

If you want to free your geometry from memory (allowing you to reassign its ID), call freeGeometry on it:

// If you were drawing it to the main canvas
freeGeometry(myGeom)

// If you were drawing it to a graphic:
myGraphic.freeGeometry(myGeom)

If you want to write a shader that uses the builder's vertex colors, just make sure it has an attribute vec4 aVertexColor and p5 will send the colors to your shader. For convenience, there are two shader helpers that make use of the colors that mimic p5's standard shaders:

// For full lighting (make sure you set up lights too):
shadedModelColors()

// For flat colors:
flatModelColors()

Examples

A branching tree

let tree
let button

function setup() {
  createCanvas(600, 600, WEBGL)
  button = createButton('Regenerate')
  button.mousePressed(makeTree)
  makeTree()
}

function makeTree() {
  if (tree) freeGeometry(tree)

  tree = buildGeometry('tree', (builder) => {
    const addBranch = (depth) => {
      builder.push()
      builder.translate(0, -50)
      builder.cylinder(15, 100)
      builder.translate(0, -50)
      if (depth >= 5) {
        builder.sphere(30)
      } else {
        const numChildren = round(random(1, 3))
        for (let i = 0; i < numChildren; i++) {
          builder.push()
          builder.rotateZ(random(-0.3, 0.3) * PI)
          builder.rotateY(random(TWO_PI))
          addBranch(depth + 1)
          builder.pop()
        }
      }
      builder.pop()
    }
    builder.translate(0, 200)
    builder.scale(0.7)
    addBranch(0)
  })
}

function draw() {
  background(255)
  lights()
  noStroke()
  ambientMaterial(255)
  orbitControl()
  model(tree)
}

View live

Using vertex colors

let tree
let button

function setup() {
  createCanvas(600, 600, WEBGL)
  button = createButton('Regenerate')
  button.mousePressed(makeTree)
  makeTree()
}

function makeTree() {
  if (tree) freeGeometry(tree)

  tree = buildGeometry('tree', (builder) => {
    const addBranch = (depth) => {
      builder.push()
      builder.translate(0, -50)
      builder.fill('#857259')
      builder.cylinder(15, 100)
      builder.translate(0, -50)
      if (depth >= 5) {
        builder.fill('#8ae887')
        builder.sphere(30)
      } else {
        const numChildren = round(random(1, 3))
        for (let i = 0; i < numChildren; i++) {
          builder.push()
          builder.rotateZ(random(-0.3, 0.3) * PI)
          builder.rotateY(random(TWO_PI))
          addBranch(depth + 1)
          builder.pop()
        }
      }
      builder.pop()
    }
    builder.translate(0, 200)
    builder.scale(0.7)
    addBranch(0)
  })
}

function draw() {
  background(255)
  orbitControl()
  push()
  noStroke()
  if (millis() % 2000 < 1000) {
    lights()
    ambientMaterial(255)
    shadedModelColors()
  } else {
    flatModelColors()
  }
  model(tree)
  pop()
}

Flat:

Shaded:

View live

Supported methods

  • Adding geometry
    • model()
    • beginShape()
    • endShape()
    • vertex()
    • bezierVertex()
    • quadraticVertex()
    • curveVertex()
    • curveTightness()
    • normal()
    • fill() (to set vertex colors)
    • plane()
    • box()
    • sphere()
    • cylinder
    • cone()
    • torus()
    • triangle()
    • circle()
    • ellipse()
    • arc()
    • rectMode()
    • rect()
    • quad()
    • bezier()
    • curve()
    • line()
  • Updating transforms
    • push()
    • pop()
    • translate()
    • scale()
    • rotate()
    • rotateX()
    • rotateY()
    • rotateZ()
    • applyMatrix()
    • resetMatrix()