[p5.js 2.0+ Bug Report]: Updating inputs.color.rgb inside a Strands for loop throws
Most appropriate sub-area of p5.js?
- [ ] Accessibility
- [ ] Color
- [ ] Core/Environment/Rendering
- [ ] Data
- [ ] DOM
- [ ] Events
- [ ] Image
- [ ] IO
- [ ] Math
- [ ] Typography
- [ ] Utilities
- [x] WebGL
- [ ] WebGPU
- [x] p5.strands
- [ ] Build process
- [ ] Unit testing
- [ ] Internationalization
- [ ] Friendly errors
- [ ] Other (specify if possible)
p5.js version
main (aa192a2d; package version 2.3.2)
Web browser and version
Chromium (Vitest; browser version not recorded)
Operating system
Fedora Linux
Steps to reproduce this
Updating inputs.color.rgb inside a Strands for loop throws during shader construction. The same loop works when it updates a local colour variable and assigns it to inputs.color afterward.
Add this beside handle swizzle assignments in loops in test/unit/webgl/p5.Shader.js:
test('handle swizzle assignments on hook color in loops', () => {
myp5.createCanvas(50, 50, myp5.WEBGL);
const testShader = myp5.baseMaterialShader().modify(
() => {
myp5.getPixelInputs(inputs => {
inputs.color = [0, 0, 0, 1];
for (let i = 0; i < 3; i++) {
inputs.color.rgb += 0.1;
}
return inputs;
});
},
{ myp5 }
);
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
const pixelColor = myp5.get(25, 25);
assert.approximately(pixelColor[0], 77, 5);
assert.approximately(pixelColor[1], 77, 5);
assert.approximately(pixelColor[2], 77, 5);
});
Run:
npx vitest run --project unit-tests --reporter=default test/unit/webgl/p5.Shader.js -t "handle swizzle assignments (in loops|on hook color in loops)"
Expected behavior
Both tests should pass, producing RGB values approximately 77.
Actual behavior
The existing local-variable test passes. The hook-property test throws:
TypeError: Cannot read properties of undefined (reading 'color')
❯ StrandsFor.executeBodyCallback src/strands/strands_for.js:490:29
Additional context
Source inspection suggests the loop stores state under 'inputs.color' but reads it as vars.inputs.color.
Found while investigating #9259. This concerns explicit property updates, separate from the proposed implicit addon-state handling.
Hi! I reproduced this with the test in the report (fails on main with the same strands_for.js stack). The loop state is keyed 'inputs.color' but read back as vars.inputs.color, so member-path assignments crash; I have a small fix (computed access with the full path) that passes it plus the whole webgl suite. Happy to open the PR once this is approved - could I be assigned?
Thanks for reproducing this and checking the suite! I was hoping to take on the fix myself, since I’ve been investigating it alongside #9259 and would love to work on the PR, if that’s okay.
No problem at all - it's yours! Glad the reproduction helped. Good luck with the fix.
thanks, appreciate it
Hey @davepagurek, I dug into this further and found the issue seems to be in how loop state reads member paths back.
inputs.color is stored as a flat key, but gets read as vars.inputs.color and later __block.inputs.color. Using the flat key directly in both places fixes the original case; changing only one just moves the failure to the other.
I also checked swizzles, nested member paths, zero/multiple iterations, nested loops, and WebGPU.
Would this be the right direction for the fix?
When I add a debugger statement in the example code above to see what it has transpiled to, I see:
__p5.strandsFor(() => {
return 0;
}, loopVar0 =>
(__p5.strandsNode(loopVar0).lessThan(3)), loopVar0 => {
return loopVar0 = __p5.strandsNode(loopVar0).add(1);
}, (loopVar0, vars) => {
debugger;
inputs.color.rgb = inputs.color.rgb.add(0.1);
}, {});
...with no vars.inputs.color at all. It kind of looks like it's not detecting the update in the loop at all; it should be adding initial variables to that object at the end and returning the updated variables from each loop iteration. (Live: https://editor.p5js.org/davepagurek/sketches/UyhA3VGeW)
Note that https://github.com/processing/p5.js/pull/9082 tried to address a similar issue (if statements should work similarly). I think a next step could be to check if the code in that PR still works on the latest release, and if it does, what is different about the two situations to make one of them detect the mutated variable and the other not.
Thanks, that helps. I think the difference is the version we're looking at.
Your sketch is running p5 2.3.4, and #9082 doesn't seem to be included in that release, so the inputs.color.rgb update isn't being picked up as loop state there and the state object stays {}.
On current main, #9082 does pick it up. From there I found another issue in the loop handling: 'inputs.color' is stored as a flat key, but later read as vars.inputs.color and __block.inputs.color. The if path already handles this using the flat key directly.
I tried changing each of those loop reads separately, and fixing only one just moves the failure to the other. Updating both fixes the original case and the other loop cases I tried in WebGL and WebGPU.
Would matching the loop handling to the if path make sense here?
Aha, good catch! For anyone following along, after that PR (updated the sketch linked before to use its branch build) the transpiled code is:
function anonymous(__p5
) {
getPixelInputs(inputs => {
inputs.color = __p5.strandsNode([
0,
0,
0,
1
]);
{
const __block_1 = __p5.strandsFor(() => {
return 0;
}, loopVar0 =>
(__p5.strandsNode(loopVar0).lessThan(3)), loopVar0 => {
return loopVar0 = __p5.strandsNode(loopVar0).add(1);
}, (loopVar0, vars) => {
debugger;
let __copy_inputs_color_0 = vars.inputs.color.copy();
__copy_inputs_color_0.rgb = __copy_inputs_color_0.rgb.add(0.1);
return { 'inputs.color': __copy_inputs_color_0 };
}, { 'inputs.color': __p5.strandsNode(inputs.color) });
inputs.color = __block_1.inputs.color;
}
return inputs;
});
}
...and at the debugger line, vars is:
Object { "inputs.color": Proxy }
With that context, this makes sense to me:
8.
inputs.coloris stored as a flat key, but gets read asvars.inputs.colorand later__block.inputs.color. Using the flat key directly in both places fixes the original case; changing only one just moves the failure to the other.
If this is already true:
10. The
ifpath already handles this using the flat key directly.
...then it makes sense to do the same for for loops here too. Feel free to go ahead with a PR, I'll assign this to you!
Got it, thanks for checking that through.