Allow p5.Texture inputs to setUniform #5517
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes: This PR allows you to use your own p5.Texture
const tex = new p5.Texture(canvas, new p5.Image(256, 256));as an input to a custom shader. It's a single line change, where we just check if the input is an instance of a p5.Texture, and if so just pass it along to the shader.Currently for sampler2D's the setUniform function can already accept p5.Images, p5.Graphics, and p5.MediaElement. However, the function can't actually accept texture inputs. Instead, it converts the other types of inputs to textures, and then sends them along to the shader. It seems a little silly that the function responsible for sending textures to a shader can't accept those inputs itself.
I think this change will help people extend the library to do more complex things with webGL graphics. For example, after this change is implemented, one could implement framebuffer objects with p5.js. Currently we use the p5.Graphics objects as a stand in for those, but framebuffers should be much more performant, and get around some limitations around the number of webGL contexts allowed by the browser on a single page.
I'm not quite sure how to implement a unit test for this. We don't currently unit test the setUniform function for every type, only for if the arrays are empty.
PR Checklist
npm run lintpasses