Add shader examples for math functions#8802
Open
SOUMITRO-SAHA wants to merge 2 commits into
Open
Conversation
Add comprehensive shader examples for abs, ceil, exp, floor, lerp, log, map, max, min, pow, round, sqrt, and fract functions in p5.strands, demonstrating their usage in WebGL shaders with practical color effects.
17 tasks
nbogie
reviewed
May 16, 2026
Contributor
nbogie
left a comment
There was a problem hiding this comment.
looks good though i didn't test the examples.
| * let t = millis() * 0.001; | ||
| * let value = 0.5 + 0.5 * sin(t); | ||
| * finalColor.begin(); | ||
| * finalColor.set(lerp([0, 0.8, 0.8, 1], [1, 0.5, 0.3, 1], value)); |
Contributor
There was a problem hiding this comment.
As elsewhere, i think breaking this down is clearer
let mixFraction = ...
//define the two colours we'll mix between
//both as [r, g, b, a], all from 0.0 to 1.0
let color1 = [0, 0.8, 08, 1]
let color2 = [1, 0.5, 0.3, 1]
let mixedColor = lerp(color1, color2, mixFraction);
finalColor.set(mixedColor);This is also has quite a lot going on in order to demonstrate lerp: (millis and sin, and its normalization), but:
- I think it's ok, if we're assuming the expected reader already understands lerp from a 2d context.
- Otherwise a static form like `
//mix a color 25% cyan, 75% magenta
let mixedColor = lerp(cyan, magenta, 0.25);would be better suited.
However, your example is far more inspiring! I like it!
Author
There was a problem hiding this comment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #8777 (Calculation batch)
Changes:
Adds inline p5.strands examples to the reference documentation for 13 calculation functions in src/math/calculation.js:
abs()— mirror/fold effect using abs(sin(t))ceil()— stepped color bandsfloor()— posterized banding effectfract()— repeating gradient patternsqrt()— smooth easing curvepow()— gamma curve effectexp()— accelerating brightnesslog()— decelerating color transitionmin()— maximum brightness clampmax()— minimum brightness clampround()— quantized posterized colorslerp()— color blending (maps to GLSLmix)map()— remapped time to colorEach example uses
buildColorShader()withfinalColor.begin()/end()and includes a brief explanation that the function can be used in shaders with p5.strands.Screenshots of the change:
PR Checklist
npm run lintpasses