Skip to content

[p5.js 2.0 Bug Report]: p5.js overrides uSampler uniform for user shaders #8200

@davepagurek

Description

@davepagurek

Most appropriate sub-area of p5.js?

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

p5.js version

2.0.5 (2.0+)

Web browser and version

All

Operating system

All

Steps to reproduce this

Steps:

  1. Create a shader using a uniform called uSampler
  2. Set a value for it on the shader
  3. Draw something using the shader
    Instead of using the provided sampler value, it gets reset to be empty.

Snippet:

In 1.11.0 this draws a red rectangle. In 2.0.5 this is an empty canvas:

function setup() {
  createCanvas(400, 400, WEBGL);
  let myShader = createFilterShader(`precision highp float;
uniform sampler2D uSampler;
varying vec2 vTexCoord;

void main() {
  gl_FragColor = texture2D(uSampler, vTexCoord);
}
  `)

  let fbo = createFramebuffer()
  fbo.draw(() => background('red'))
  
  shader(myShader)
  myShader.setUniform('uSampler', fbo)
  noStroke()
  plane(width, height)
}

https://editor.p5js.org/davepagurek/sketches/kF84pt-Am

That's because this code gets run after the user's setUniform, overriding it:

// We need to explicitly set uSampler back to an empty texture here.
// In general, we record the last set texture so we can re-apply it
// the next time a shader is used. However, the texture() function
// works differently and is global p5 state. If the p5 state has
// been cleared, we also need to clear the value in uSampler to match.
fillShader.setUniform('uSampler', this.states._tex || empty);

It's being set back to an empty texture for good reason, as mentioned in the comments in the code, but we shouldn't do that if it's a user shader and has already had a value set.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions