How to affect a PShape's transparency, but not color?

edited May 2016 in How To...

I am trying to dynamically affect a PShape's transparency without changing its color.

There are methods setFill(R,G,B,A) and then disableStyle() combined with fill(R,G,B,A) but they require me to also set the svg's color.

But what if the original .svg has several colors which need to be conserved? Using the aforementioned methods will simply set one color for the entire PShape, which is definitely undesirable.

Other issue: Setting the opacity of the PShape to 0 does not actually make the PShape transparent, it sets its color to the color of the background. Which means, if the background is white, then if the PShape is over an ellipse of a different color, instead of displaying simply the ellipse (because opacity of PShape is 0, so should be invisible) the sketch displays the parts of the PShape that overlap the ellipse in white color, which is also definitely undesirable.

The renderer I am using is P3D.

Do you know any solutions to the two problems described above?

Tagged:

Answers

  • You can render the PShape into a PGraphics, which allows you to use tint() to set the alpha channel.

    PGraphics test;
    PShape currShape;
    
    void setup(){
    size(800,600);
    moon = loadShape("imgs/moon.svg");
    currShape = moon;
    
    test = createGraphics(width,height);
    }
    
    void draw(){
    test.beginDraw();
    test.shapeMode(CENTER);
    test.shape(currShape, (width/2) + (width/4), (height/2) - (height/6));
    test.endDraw();
    
    tint(255,100);
    imageMode(CORNER);
    image(test, 0,0);
    }
    
Sign In or Register to comment.