- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 3.6k
 
Open
Description
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:
- Create a shader using a uniform called 
uSampler - Set a value for it on the shader
 - 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:
p5.js/src/webgl/p5.RendererGL.js
Lines 2378 to 2383 in f78009a
| // 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.