p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Add instances() API for primitive instancing

Open aashu2006 opened this issue 5 months ago • 1 comments

Addresses #8911

Changes

  • Add a new instances(count) API for primitive instancing in WebGL.
  • Support instancing for primitives including sphere(), box(), plane(), ellipsoid(), cylinder(), cone(), and torus().
  • Support instances(count).model(geometry) while preserving existing model(geometry, count) behavior.
  • Forward instance counts through the existing rendering pipeline to _drawGeometry().
  • Add unit tests covering primitive instancing, model instancing, explicit count precedence, and cleanup of temporary instance state.

PR Checklist

  • [x] npm run lint passes
  • [ ] Inline reference is included / updated
  • [x] Unit tests are included / updated

aashu2006 avatar Jun 22 '26 01:06 aashu2006

Continuous Release

CDN link

  • https://raw.esm.sh/pr/p5@d334ff2/lib/p5.min.js

Published Packages

  • https://pkg.pr.new/p5@d334ff2

Commit hash: 204dd21

Previous deployments

fee8970

  • https://raw.esm.sh/pr/p5@d1b93a0/lib/p5.min.js
  • https://pkg.pr.new/p5@d1b93a0

f134f1d

  • https://raw.esm.sh/pr/p5@1b0fdde/lib/p5.min.js
  • https://pkg.pr.new/p5@1b0fdde

2d446bf

  • https://raw.esm.sh/pr/p5@fa42873/lib/p5.min.js
  • https://pkg.pr.new/p5@fa42873

f35f290

  • https://raw.esm.sh/pr/p5@84b1dd6/lib/p5.min.js
  • https://pkg.pr.new/p5@84b1dd6

057f727

  • https://raw.esm.sh/pr/p5@2e9be52/lib/p5.min.js
  • https://pkg.pr.new/p5@2e9be52

This is an automated message.

p5-bot[bot] avatar Jun 22 '26 19:06 p5-bot[bot]

@davepagurek Done! I used the updated example as the basis and added the corresponding visual tests for both WebGL and WebGPU.

aashu2006 avatar Jun 25 '26 19:06 aashu2006

thanks for the quick feedback @davepagurek

I pushed another update, also added the visual test snapshots this time, my bad for missing them in the previous push.

I looked into the convertTypeToTypeScript() suggestion as well. It already handles OptionalType recursively, but convertFunctionTypeForInterface() still needs the parameter name and optionality first, so I kept the unwrapping there before passing the type through convertTypeToTypeScript().

I also took another pass through 3d_primitives.js. The omitted ones like line() use the immediate-mode path, so they don't make use of _instanceCount. I did notice a few retained-geometry shapes like triangle(), rect(), quad(), and ellipse()/arc() could technically be supported too. Would you want instances() to cover those as well, or keep it focused on the current set?

aashu2006 avatar Jun 25 '26 20:06 aashu2006

Sounds good, thanks for looking into the typescript stuff!

Yeah, let's see if we can support all the shapes if we can.

davepagurek avatar Jun 25 '26 20:06 davepagurek

Thanks, I updated the remaining retained-geometry shapes as well. I think that should cover everything from my side now, but happy to make any further changes if needed 🙂

aashu2006 avatar Jun 25 '26 21:06 aashu2006

I added support for immediate mode APIs by forwarding instance count through endShape(), so line(), point(), bezier(), and spline() now work with instances(). I also updated the examples to use instanceIndex and instances(), and added visual tests for both WebGL and WebGPU.

For curve(), I kept it conditional since spline() is the standard API in 2.x, while curve() is only available in compatibility setups.

aashu2006 avatar Jun 26 '26 16:06 aashu2006