Rect using wrong texture when using shader

I’m using a chroma shader (to do some green screen video) but I want to use it twice
so I have

  chroma.set("u_tex",cam);
  rect(49,0,560,480);
  chroma.set("u_tex",countDown);
  //rect(600,0,560,480);

(I commented out the second rect, because it isn’t part of the problem)
The problem is that the first rect is drawn using the second chroma.set(). If I comment the second one out, it draws just fine. It is almost as if drawing of the rectangle is done after the second chroma.set() is executed. How do I work around this??

1 Like

If I’m understanding the issue correctly, have you tried using pushStyle() and popStyle()?

:nerd_face:

I’m pretty new to processing, so I don’t know all the functions. I just read the reference guide and it seems that it only pushes styles like color, line width etc, but not the shader.
Anyway I tried it by adding a pushStyle() just before the second choma.set and that didn’t work.
I also tried a pushStyle() and popStyle() around the first chroma.set and the first rec, but that too didn’t work.

Try placing pushStyle before the second chroma.set and
The popStyle after the second rect.
Basically your pushing and popping around the second instance of the chroma and rect.

yes, that’s what I tried. it didn’t work.

Like this?

chroma.set("u_tex",cam);
rect(49,0,560,480);

pushStyle();  
chroma.set("u_tex",countDown);
rect(600,0,560,480);
popStyle();

Or, try using dot syntax:

chroma.set("u_tex",cam);
rect(49,0,560,480);

chroma.pushStyle();  
chroma.set("u_tex",countDown);
rect(600,0,560,480);
chroma.popStyle();

kind of solved it (not really, but it does what I want it to do) before the second choma.set(), I added a second shader(chroma);

exactly like that (without the dot syntax).
Also I think it is kind of not the way it should work. You push some style parameters on the stack, which probably doesn’t even change the current parameters. you could change some, and the pop would just restore all the changed parameters from the stack. If it would have worked, the main effect would be that it restored the chroma.set(“u_tex”,cam) from the stack after drawing the second rectangle. There wouldn’t be any need for that, unless I wanted to use it during even more drawing.

Ok. :slightly_smiling_face:
I could only offer suggestions based on the code supplied.
You may need to show a larger section of code for people to fully understand the end result you’re working toward. IMHO. :slightly_smiling_face:
:nerd_face:

what I was trying to say is that (if it worked) the push() pop() pair would only protect the style that comes after it. The problem is the rect() that is before it, as it is using the chrome.set() call inside the push() pop() pair.

Basically how on earth does the first rect() use the chroma.set() that comes after it. That is the problem.

Ok, I didn’t understand that you wanted the 2nd style to apply to the 1st rect().

And it looks like you’ve implemented a workaround. But that you also have a feeling this is not the best solution…

Hopefully someone else will be able to provide more concrete guidance with this.

But basically keep in mind that any style you want to appear on a shape/object/etc, it must be syntactically placed above the shape to be stylized.

And again, you’ve only supplied a small bit of code, It’s difficult to know specifically what you want to visually achieve. And therefore, is there a logic error at play or is it purely a syntax error?
Please consider sharing more code so we can provide better help. :slightly_smiling_face:
:nerd_face:

exactly not!!
I wanted the first texture to be used on the first rect, and the second texture on the second rect.
If I wanted the second style to the first rect, I would have placed it before the call.

indeed that is what i expected, but it turns out that the shader setting isn’t waiting for the rect to finish so the rect is using the style set AFTER the call to rect.

What I wanted is
set some shader parameter (the texture “cam”)
draw a rect using that shader and texture
set the shader to use a different texture “countDown”
draw a different rect using shader with the updated texture

What is does is that the first rect uses the countdown texture. To me it looks likes it starts drawing the rectangle after the shader has been given the updated texture.