PShader as background

Hi, everyone, and thanks in advance for any help.
In order to achieve this, I’m trying actually to apply the PShader to a PGraphics object, and then pass this PGraphics as an argument for the background.
When I use the resultant PGraphics in an image() function, it works perfectly, but nothing is shown when I use it for the background().
If I draw anything inside the PGraphics (for example a rect), then that is shown in the background, but not the PShader I need;

Has anyone faced this problem and got a solution? Thanks a lot.

I paste the Processing code below

PShader shader;
float wE, hE;
PGraphics backImgPG;

void setup() {
  size(1024, 768, P3D);
  background(0);
  shader=loadShader("../frag/72.frag");
  shader.set("u_resolution", float(width), float(height));
  backImgPG=createGraphics(width, height);
  rectMode(CENTER);
  noStroke();
}
void draw() {
  shader.set("u_time", millis() / 1000.0);
  shader(shader);

  //image(shaderPG(backImgPG), 0, 0);//works
  background(shaderPG(backImgPG));//doesn't work
}

PGraphics shaderPG(PGraphics _pg) {
  _pg.beginDraw();
  _pg.endDraw();
  return backImgPG;
}

see

 An image can also be used as the background for a sketch,
although the image's width and height must match that of the sketch window.
Images used with **background()** will ignore the current **tint()** setting. 
To resize an image to the size of the sketch window, use image.resize(width, height).

so the size of the image needs to fit

1 Like

Hi!
I’ve tried with the PImage approach (thanks @kll). The only way I got it to work is applying PShader as filter and then using the get() method.
It works, but the performance is pretty slow. It’s a good approach but not still what it should be.
Finally I found this simple solution, that works perfectly even I don’t understand exactly why, because any background method is being called.


void setup() {
  size(1024, 768, P3D);
  shader=loadShader("data/frag/72.frag");
  shader.set("u_resolution", float(width), float(height));
}

void draw() {
  shader.set("u_time", millis() / 1000.0);
  filter(shader);
}


1 Like