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

[p5.strands] Instancing API improvements

Open aashu2006 opened this issue 5 months ago • 9 comments

Increasing access

This makes GPU instancing easier to discover and use without requiring a deep understanding of the underlying rendering pipeline. The goal is to make performant rendering techniques more approachable for artists, educators, students, and beginners.

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [x] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [x] WebGL
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [ ] Friendly errors
  • [x] p5.strands

Feature enhancement details

Summary

This issue tracks implementation work for improving the p5.strands instancing API as part of the Microgrant project.

Instancing is a powerful GPU technique for efficiently drawing large numbers of similar objects, but the current API can be difficult for newer users to discover and understand. This project builds on the existing rendering architecture to provide a more approachable and discoverable API surface.

Proposed Direction

let repeater = instances(100);
repeater.sphere(20);

// in a shader
let i = instanceIndex();

Planned Work

  • [x] Add instanceIndex() as an alias for instanceID()
  • [x] Implement instances(count) returning a drawable object
  • [x] Integrate instancing with existing primitive rendering paths
  • [x] Add tests for WebGL and WebGPU
  • [x] Add documentation and examples

Project Context

This issue serves as a tracking thread for ongoing work and related pull requests for the p5.strands instancing API.

References

aashu2006 avatar Jun 11 '26 17:06 aashu2006

cc @ksen0 @davepagurek

aashu2006 avatar Jun 11 '26 18:06 aashu2006

Does instanceIndex() need to be a function? Possibly we could define a getter, similar to how other non-functions like width work in strands, that does the same thing.

davepagurek avatar Jun 11 '26 18:06 davepagurek

I kept it as a function in this #8912 because instanceID() is currently a function and the goal here was to make instanceIndex() a minimal alias.

thinking about it more, instanceIndex does feel closer to built-in values like width or mouseX than to something that's actually performing work. Do you see it as eventually replacing instanceID() entirely as the preferred API, and if so would you lean towards making both of them values rather than functions?

aashu2006 avatar Jun 11 '26 18:06 aashu2006

I think we can keep instanceID() how it is but mark it as deprecated for now, moving towards instanceIndex as the slightly easier to use API.

davepagurek avatar Jun 11 '26 18:06 davepagurek

Got it, I think having instanceIndex() as the preferred API while keeping instanceID() around for compatibility is a good path forward. Do you see instanceIndex eventually being a value, or do you think it should stay a function?

aashu2006 avatar Jun 11 '26 18:06 aashu2006

I think since it's effectively a constant within the shader, it feels conceptually closer to a value. That also means we don't have to worry about users accidentally forgetting the braces at the end too.

davepagurek avatar Jun 11 '26 18:06 davepagurek

"forgetting the parentheses" point is especially convincing since it's something beginners are likely to run into. I'll take a look at how the other built-in strands values are exposed and see what would be involved in making instanceIndex a value instead.

aashu2006 avatar Jun 11 '26 19:06 aashu2006

Hey @aashu2006! Anything left we want to do on this issue or is it good to close?

davepagurek avatar Jul 30 '26 20:07 davepagurek

@davepagurek it is good to close now !

aashu2006 avatar Jul 30 '26 21:07 aashu2006