Skip to content

[p5.js 2.0+ Bug Report]: Updating inputs.color.rgb inside a Strands for loop throws #9260

Description

@bhabishnu

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

main (aa192a2; 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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions