Skip to content

[p5.js 2.0+ Bug Report]: p5.strands breaks if you put control flow in a uniform callback #8707

@davepagurek

Description

@davepagurek

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

2.2.3

Web browser and version

All

Operating system

All

Steps to reproduce this

Steps:

  1. Create a strands shader
  2. Create a uniform that has a default value callback
  3. In the callback, include an if statement

It throws an error because p5.strands is trying to transpile the ifs and such within the callback function. Generally, we try not to transpile anything in a function used in a uniform callback, but that seems to not be applied for control flow right now.

Snippet:

let myShader
function setup() {
  createCanvas(400, 400, WEBGL);
  myShader = buildFilterShader(testFilter)
}

function testFilter() {
  let pastOneSecond = uniformFloat(() => {
    debugger
    if (millis() > 1000) {
      return 1
    }
    return 0
  })
  
  filterColor.begin()
  filterColor.set(mix([1, 0, 0, 1], [0, 1, 0, 1], pastOneSecond))
  filterColor.end()
}

function draw() {
  background(220);
  filter(myShader)
}

If you run this with the console open, it hits the debugger and you can see that it has transpiled testFilter into:

function anonymous(__p5
) {

    let pastOneSecond = uniformFloat('pastOneSecond', () => {
        debugger;
        __p5.strandsIf(millis() > 1000, () => {
            __p5.strandsEarlyReturn(1);
        }).Else(() => {
        });
        return 0;
    });
    filterColor.begin();
    filterColor.set(mix(__p5.strandsNode([
        1,
        0,
        0,
        1
    ]), __p5.strandsNode([
        0,
        1,
        0,
        1
    ]), pastOneSecond));
    filterColor.end();

}

It should not have touched anything in the callback. This is likely because we need to transpile helper functions to find early returns, but we should not be doing so in uniform callback functions.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Completed

Relationships

None yet

Development

No branches or pull requests

Issue actions