Skip to content

support map() inside p5.strands shaders#8753

Open
Nixxx19 wants to merge 2 commits intoprocessing:dev-2.0from
Nixxx19:nityam/strands-map-8750
Open

support map() inside p5.strands shaders#8753
Nixxx19 wants to merge 2 commits intoprocessing:dev-2.0from
Nixxx19:nityam/strands-map-8750

Conversation

@Nixxx19
Copy link
Copy Markdown
Contributor

@Nixxx19 Nixxx19 commented Apr 28, 2026

Resolves #8750

Summary

  • adds support for the regular p5 map() function inside p5.strands modify() callbacks, so it works the same way as in normal p5 sketches.
  • when strands is not active, falls back to the original p5 map.
  • internally rewrites map(n, s1, e1, s2, e2) to mix(s2, e2, (n - s1) / (e1 - s1)) using the existing strands math operators and the mix builtin.
  • the optional withinBounds argument is handled with clamp(result, min(s2, e2), max(s2, e2)) so it works regardless of which bound is larger.

Tests

  • adds two unit tests in test/unit/webgl/p5.Shader.js covering the basic case and the withinBounds case.
  • both pass locally with npx vitest.

PR Checklist

  • npm run lint passes
  • Inline reference is included / updated (not applicable, no new public api)
  • Unit tests are included / updated

Copy link
Copy Markdown
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is looking good! Do you think we could add some unit tests that map from one range to a different range to test the math? Possibly a visual test that maps the texture coordinate to something?

Comment thread src/strands/strands_api.js Outdated
const start1Node = p5.strandsNode(start1);
const stop1Node = p5.strandsNode(stop1);
const t = nNode.sub(start1Node).div(stop1Node.sub(start1Node));
const result = fn.mix(start2, stop2, t);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this doesn't make a difference here since this part of the code happens just while the strands context isn't active, but maybe let's use this. instead of fn. to better match expected usage, since the intention is for these functions to not be called statically.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review @davepagurek!

swapped fn. to this. for mix/clamp/min/max in strands_api.js. you're right that it doesn't change behavior here but it's nicer to keep consistent with the rest of the file (like getTexture).

also added a few more tests in p5.Shader.js since the original two were both identity mappings (0..1 to 0..1), so they weren't really exercising the math:

  • range shrink: map(5, 0, 10, 0, 1) -> 0.5
  • offset output: map(0.5, 0, 1, 0.2, 0.8) -> 0.5 mapped to 128
  • negative input range: map(0, -1, 1, 0, 1) -> 0.5
  • a horizontal gradient test that pipes inputs.texCoord.x through map() and checks left/middle/right pixels (covers the visual side you mentioned)

all 6 pass locally with vitest. lmk if you want anything else changed!

@p5-bot
Copy link
Copy Markdown

p5-bot Bot commented May 3, 2026

Continuous Release

CDN link

Published Packages

Commit hash: 2f7206c

Previous deployments

This is an automated message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants