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.warp

v0.0.12

Published

Fast 3D domain warping in p5

Downloads

565

Readme

p5.warp

Fast 3D domain warping in p5

warp

What does this do?

This library is secretly building and applying a shader for you from the warp function you build! It works with p5's material system so that you can keep using normal lighting and color functions as usual.

It uses a vertex shader to adjust the position of each point on a model according to the warp, and also update the normals of the model so that lighting still works. Because it's done in a shader, that means you can animate your warps and it will still run fast by leveraging your GPU! It also means that it only moves the points that already exist on the model: for a warp to look smooth, your model has to already be somewhat dense with points (which, for example, p5's box() is not.)

Usage

Adding the library

Add the library in a script tag:

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

Or on OpenProcessing, add the CDN link as a library:

https://cdn.jsdelivr.net/npm/@davepagurek/[email protected]

If you're using p5 without importing it globally, you can manually set up p5.warp:

import P5 from 'p5'
import { setupWarp } from '@davepagurek/p5.warp'
setupWarp(P5)

Making a warp

In your setup function, you can create a new warp by calling createWarp and giving it a function that takes in the warp inputs and returns a 3D offset that will be applied to each vertex of a shape:

let distort
function setup() {
  createCanvas(600, 600, WEBGL);
  distort = createWarp(({ glsl, millis, position }) => {
    const t = millis.div(1000)
    return glsl.vec3(
      t.mult(2).add(position.y().mult(4)).sin().mult(0.15),
      t.mult(0.5).add(position.z().mult(2)).sin().mult(0.15),
      t.mult(1.5).add(position.x().mult(3)).sin().mult(0.15)
    )
  })
}

Then, when you want to apply the warp, call the warp function you made before you draw shapes:

function draw() {
  background(220)
  distort()
  lights()
  sphere(150)
}

Warp inputs

createWarp has the following signature:

createWarp(offset: (params: Params) => VecOp, options?: Options)

type Params = {
  glsl: AD
  position: VecParam
  uv: VecParam
  normal: VecParam
  mouse: VecParam
  mouseX: Param
  mouseY: Param
  millis: Param
  pixelDensity: Param
  size: VecParam
  width: Param
  height: Param
  color: VecParam
}

type Options = {
  type?: 'specular' | 'normal'
  space?: 'world' | 'local'
  defs?: string
}

A params object is passed as an input to your offset function. Here's what each property is and how you might want to use them:

  • glsl is an instance of the glsl-autodiff library. From it, you can create now constant values (e.g. glsl.val(123), glsl.val(Math.PI), etc), or create vectors containing other values (e.g. glsl.vec3(0, 1, position.x())
  • position is a vec3 with the position of each vertex, by default in object space. Note that the general scale of these values will vary from shape to shape: sphere() goes from -1 to 1, while a normalized p5.Geometry will have values ranging from -100 to 100. Pass space: 'world' in the options object to use world-space coordinates, which will not have a different scale per object.
  • uv is a vec2 with the texture coordinate for each vertex.
  • normal is a vec3 with the normal for that vertex, which is a direction pointing directly out of the surface.
  • mouse is a vec2 with p5's mouseX and mouseY values stored in x and y.
  • mouseX is a float set to p5's mouseX. This is equivalent to mouse.x().
  • mouseY is a float set to p5's mouseY. This is equivalent to mouse.y().
  • millis is a float set to p5's millis().
  • pixelDensity is a float set to p5's pixelDensity().
  • size is a vec2 with p5's width and height stored in x and y.
  • width is a float set to p5's width. This is equivalent to size.x().
  • height is a float set to p5's height. This is equivalent to size.y().
  • color is a vec4 representing either the whole model's fill color, or the per-vertex fill color if it exists.

Each input property comes from glsl-autodiff. You can see a full list of the methods you can call on them in the glsl-autodiff readme.

After your offset function, you can specify an optional options object with these properties:

  • type: The type of p5 material to make, either specular or normal, corresponding to p5's specularMaterial() and normalMaterial(). The default is specular.
  • space: what coordinate space the position property of the offset params will be in, either world or local. The default is local.
  • defs: A string with any uniforms you want to splice into the header of the shader. You can then reference them by using glsl.param('myUniform') within your distortion function, and pass in an object of uniforms when you apply the distortion, e.g. distort({ myUniform: 0.5 })