Loading...
Logo
Processing Forum
Hi All, 

I'm using Processing 2.0a5, and have a PShape like this:

    theShape = createShape(QUADS);
    theShape.noStroke();
    theShape.texture(tex);
    theShape.vertex(0, 0, 0, 0, 0);
    theShape.vertex(w, 0, 0, w, 0);
    theShape.vertex(w, h, 0, w, h);
    theShape.vertex(0, h, 0, 0, h);
    theShape.end();

I would like  tex to be desaturated for a while and at a certain point fade up to full colour. How can I apply a tint to the texture on the fly? or would this require redefining the PShape object?

Thanks in advance

Dave

Calx | http://calx.co.uk

Replies(7)



I have a feeling  this post may have just answered my question.
What is missing in P3D in 2.0a5?
Quite a few things, ... GL-accelerated image masks, filters, pixel get, set and copy, ...
So, any advice on my options would be massively appreciated...

Thanks

Dave

Calx | http://calx.co.uk
I don't know if in the current alpha there is a way to get access to the texture once you have set it in a retained PShape. I tried it, but changing the original texture doesn't affect the PShape's texture after it has been set. Without access to the texture, the alternative is to not use a retained PShape, but do it directly (aka the old-fashioned way).

Code Example
Copy code
  1. PImage original, tex;
  2. int w, h;
  3.  
  4. void setup() {
  5.   size(640, 360, P3D);
  6.   original = loadImage("yourImage.jpg");
  7.   noStroke();
  8.   w = width;
  9.   h = height;
  10. }
  11.  
  12. void draw() {
  13.   float amt = (frameCount / 200.0) % 1.0;
  14.   tex = getDesaturated(original, amt);
  15.   beginShape(QUADS);
  16.   texture(tex);
  17.   vertex(0, 0, 0, 0, 0);
  18.   vertex(w, 0, 0, w, 0);
  19.   vertex(w, h, 0, w, h);
  20.   vertex(0, h, 0, 0, h);
  21.   endShape();
  22. }
  23.  
  24. PImage getDesaturated(PImage in, float amt) {
  25.   PImage out = in.get();
  26.   colorMode(HSB);
  27.   for (int i=0; i<out.pixels.length; i++) {
  28.     color c = out.pixels[i];
  29.     float h = hue(c);
  30.     float s = saturation(c)*amt;
  31.     float b = brightness(c);
  32.     out.pixels[i] = color(h,s,b);
  33.   }
  34.   colorMode(RGB);
  35.   return out;
  36. }
I had a feeling this was the case, thanks very much for your time, i'll try it, with any luck the performance won't drop through the floor, much appreciated!

Dave

EDIT: Yep, when implemented it does exactly what I wanted, only at 6fps rather than 40.

Calx | http://calx.co.uk
Hi, you will be able to change the tint of a textured PShape after creation, but this piece of functionality was still incomplete in 2.0a5. I tested it using the latest revision from the trunk, and this code works as expected:
Copy code
  1. PShape theShape;

  2. void setup() {
  3.   size(300, 300, P3D);
  4.   
  5.   PImage tex = loadImage("berlin-1.jpg");
  6.       
  7.   theShape = createShape(QUADS);
  8.   theShape.noStroke();
  9.   theShape.texture(tex);
  10.   theShape.tint(255);
  11.   theShape.vertex(0, 0, 0, 0, 0);
  12.   theShape.vertex(100, 0, 0, tex.width, 0);
  13.   theShape.vertex(100, 100, 0, tex.width, tex.height);
  14.   theShape.vertex(0, 100, 0, 0, tex.height);
  15.   theShape.end(); 
  16. }

  17. void draw() {
  18.   translate(mouseX, mouseY);
  19.   shape(theShape);  
  20. }

  21. void keyPressed() {
  22.   if (key == '1') {
  23.     theShape.tint(255, 0, 0);
  24.   } else if (key == '2') {
  25.     theShape.tint(0, 255, 0);
  26.   } else if (key == '3') {
  27.     theShape.tint(0, 0, 255);
  28.   } else if (key == '0') {
  29.     theShape.tint(255);
  30.   }
  31. }
This will be available in the next alpha release.
Amazing, thanks Andres, couldn't be happier with this!

D.
Well, there are some optimizations possible. Such as, only call getDesaturated() when you change the amt or use it only for the in-between amt (and the retained PShape for the regular state).

Personally I prefer to use GLGraphics until a stable and documented Processing 2.0 is out.

*edit* Ah, I see you have an alternative option from andres for the upcoming alpha, so good news.
Good point, thanks for your time amnon.

D.

Calx | http://calx.co.uk