Add instances() API for primitive instancing
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(), andtorus(). - Support
instances(count).model(geometry)while preserving existingmodel(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 lintpasses - [ ] Inline reference is included / updated
- [x] Unit tests are included / updated
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.
@davepagurek Done! I used the updated example as the basis and added the corresponding visual tests for both WebGL and WebGPU.
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?
Sounds good, thanks for looking into the typescript stuff!
Yeah, let's see if we can support all the shapes if we can.
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 🙂
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.