How can I get blendMode to blend everything?

edited October 2014 in Questions about Code

I've been using blendMode for and I realised then when you combine more than 2 color areas parts stop getting blended.

I was trying to do this in a more complicated sketch so I put a really simple one together to demonstrate the problem.

In this image (http://i.imgur.com/2i2ZRzg.png) you can see that there is a section that is not getting blended at all.

Why is this? Can it be fixed?

Demonstration Code:

void setup()
{
  size(600,600);
  background(0);
  blendMode(ADD);
  noStroke();
  fill(random(0,255),random(0,255),random(0,255));
  rect(100,100,100,100);
  fill(random(0,255),random(0,255),random(0,255));
  rect(150,150,100,100);
  fill(random(0,255),random(0,255),random(0,255));
  rect(175,175,100,100);
}

More complicated code where I discovered the problem:

ArrayList sq;

float momentum;
boolean pressed;

void setup()
{
  size(600,600);
  sq = new ArrayList();
  for(int i = 0; i < 3; i++)
  {
    float size = random(75.0f,150.0f);
    sq.add(new squareRect(random(100.0f,400.0f),random(100.0f,400.0f),size,size));
  }
}

void draw()
{
  blendMode(ADD);
  background(0);
  rectMode(CENTER);
  noStroke();
  for(int i = 0; i < sq.size(); i++)
  {
    squareRect s = (squareRect) sq.get(i);
    s.update();

    if(!s.justAdded)
    {
      if(s.removal)
      {
        sq.remove(i);
      }
      else
      {
        fill(s.c);
        rect(s.x,s.y,s.w,s.h);
      }
    }
    else
    {
      s.justAdded=false;
    }
  }
  if(pressed)
  {
    momentum= momentum*1.05f;
  }
  else
  {
    momentum=0;
  }

}

void mousePressed()
{
  pressed=true;
  momentum=1.0f;
}

void mouseReleased()
{

 boolean stop = false;
 pressed=false; 
 for(int i = 0; i < sq.size(); i++)
  {
    squareRect s = (squareRect) sq.get(i);
    s.clickCheck(i, sq, stop, momentum);
    if(s.removal)
    {
      break;
    }
  }
}

class squareRect
{
  float tarX, tarY;
  float x, y, w, h;
  boolean removal;
  boolean justAdded;
  int removalTimer = 0;
  color c;

  public squareRect(float nx, float ny, float nw, float nh)
  {
     x=nx;
     y=ny;
     w=nw;
     h=nh;
     tarX=nx;
     tarY=ny;
     justAdded=true;
     c = color(random(0,255),random(0,255),random(0,255));
  }

  public squareRect(float nx, float ny, float nw, float nh, float nTarX, float nTarY, color nc)
  {
     x=nx;
     y=ny;
     w=nw;
     h=nh;
     tarX=nTarX;
     tarY=nTarY;
     justAdded=true;
     c=nc;
  }

  public void split(int dex, ArrayList list, float mom)
  {
    float cx = x+(w/2);
    float cy = y+(h/2);

    color newColor = color(random(0,255),random(0,255),random(0,255));

    list.add(new squareRect(x+(w/4),y+(h/4),(w/2),(h/2),x+(w/3)+mom,y+(h/3)+mom, newColor));
    list.add(new squareRect(x-(w/4),y+(h/4),(w/2),(h/2),x-(w/3)-mom,y+(h/3)+mom, newColor));
    list.add(new squareRect(x-(w/4),y-(h/4),(w/2),(h/2),x-(w/3)-mom,y-(h/3)-mom, newColor));
    list.add(new squareRect(x+(w/4),y-(h/4),(w/2),(h/2),x+(w/3)+mom,y-(h/3)-mom, newColor));
  }

  public void update()
  {
    x = lerp(x,tarX,.1f);
    y = lerp(y,tarY,.1f);
  }

  public void clickCheck(int dex, ArrayList list, boolean stopper, float mom)
  {
    if(mouseX >= x-(w/2) && mouseX < x+(w/2))
    {
      if(mouseY >= y-(h/2) && mouseY < y+(h/2))
      {
        split(dex,list, mom);
        removal=true;
      }
    }
  }
}

Answers

  • Answer ✓

    This has already been fixed. When I run your demonstration code on 2.2.1. is shows the problem, however when I run it against the build from the current source code, the problem does not show. You can search issues / changes on GitHub to see when it was fixed it or just wait for / download a newer version, once it is released.

Sign In or Register to comment.