How to implement a shader with color interpolation other than linear?

edited April 15 in GLSL / Shaders

In P2D if you write a code like this

beginShape();
fill(255, 0, 0);
vertex(x1, y1);
fill(0, 255, 0);
vertex(x2, y2);
fill(0, 0, 255);
vertex(x3, y3);
endShape();

the output is a triangle which is filled with color that varies across its surface, interpolating between R/G/B linearly.
Is there a simple way to write a shader that would interpolate the three colors differently? If yes, can you please guide me how to do that? Would this be a vertex or a fragment shader?

Answers

  • edited April 16

    If you really need a shader I can help you with the blend the different colours part though. Btw might not need a shader, have you tried different blendmodes if they make a difference? I did not even know you could colour each vertex in a shape.

    float x1,y1,x2,y2,x3,y3;
    int mode=1;
    void setup(){
      size(400,400,P2D);
      x1=y1=y2=0;
      x2=x3=width;
      y3=height;
    }
    void draw(){
    background(180);
    blendMode(mode);
    if(frameCount%10==0){  mode++; mode%=10; }
    beginShape();
    fill(255, 0, 0);
    vertex(x1, y1);
    fill(0, 255, 0);
    vertex(x2, y2);
    fill(0, 0, 255);
    vertex(x3, y3);
    endShape();
    }
    
  • edited April 16

    Sorry for my previous post, I think it's not what you wan't.
    What you want is the regular linear blendmode, but along an S curve rather than a line right?
    In other words change the ratios of color blended rather than how they're blended.

Sign In or Register to comment.