Disabling color blending of a shape in P3D

I got a simple TRIANGLE_STRIP shape with 3 vertexs. I need to have each triangle with only one solid color, but Processing is blending the colors when I’m on P3d (it works fine on P2D).

How can I disable it?

void setup() {
  size(400, 400, P3D);

  fill(255, 0, 0);
  beginShape(TRIANGLE_STRIP);
  vertex(120, 300);
  vertex(160, 80);
  vertex(200, 300);

  fill(0, 0, 255);

  vertex(240, 80);

  endShape();
}

image

Hi! Welcome to the forum :slight_smile:

I haven’t tried this, but maybe using the flat keywoard in front of the varying used to pass the color from the vertex shader to the fragment shader?

If that works, you could tweak the vert and frag shaders, and write flat in front of the line that starts with varying in each file, then load and enable the shader, and draw your shapes.

But it’s possible that this only works in other versions of opengl.

But why do that? :slight_smile:

Hey, thank you for answering me, but I really didn’t understand how to do that in Processing.

Yeah, it’s a bit advanced.

You will have 3 files:

yourSketch.pde
data/ColorFrag.glsl
data/ColorVert.glsl

yourSketch.pde

PShader flatRender;

void setup() {
  size(400, 400, P3D);
  
  flatRender = loadShader("ColorFrag.glsl", "ColorVert.glsl");
  shader(flatRender);

  fill(255, 0, 0);
  beginShape(TRIANGLE_STRIP);
  vertex(120, 300);
  vertex(160, 80);
  vertex(200, 300);
  fill(0, 0, 255);
  vertex(240, 80);
  endShape();
}

ColorFrag.glsl

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

flat varying vec4 vertColor;

void main() {
  gl_FragColor = vertColor;
}

ColorVert.glsl

uniform mat4 transformMatrix;

attribute vec4 position;
attribute vec4 color;

flat varying vec4 vertColor;

void main() {
  gl_Position = transformMatrix * position;
    
  vertColor = color;
}

a

But maybe if you explain why you need this simpler approaches can be found :slight_smile:

1 Like