I already knew this thread, but the code was not working for me. I’ve adapted the shader now, so it runs under MacOS too. It seems that if I change the shape mode to POINTS
and adapting the shader breaks it. The vert_color
attribute is always just black vec4(0.0, 0.0, 0.0, 0.0)
.
PShape shape;
PShader shader;
public void setup() {
size(500, 500, P3D);
shape = createShape();
shape.beginShape(POINTS);
// Visual
shape.noFill();
shape.stroke(255, 0, 0);
// Custom Attribute
shape.attrib("vert_color", 1.0, 0.5, 0.0, 1.0); // Supposed to be Orange
shape.vertex(-100, -100);
shape.vertex(100, -100);
shape.vertex(100, 100);
shape.vertex(-100, 100);
shape.endShape();
// Exemple shader
shader = createShader();
}
public void draw() {
background(255);
shader(shader, POINTS);
translate(width/2, height/2);
shape(shape);
}
public PShader createShader() {
String[] vert = new String[] {
"#version 150",
"",
"uniform mat4 projection;",
"uniform mat4 modelview;",
"",
"in vec4 position;",
"in vec4 color;",
"in vec2 offset;",
"in vec4 vert_color;",
"",
"out vec4 vertColor;",
"",
"void main() {",
"vec4 pt = position;",
"pt /= 10.0;",
"vec4 pos = modelview * pt;",
"vec4 clip = projection * pos;",
"gl_Position = clip + projection * vec4(offset, 0, 0);",
"",
// setting the vert color
"vertColor = vert_color;",
"}"
};
String[] frag = new String[] {
"#version 150",
"in vec4 vertColor;",
"out vec4 fragColor;",
"",
"void main() {",
" fragColor = vertColor;",
"}"
};
return new PShader(this, vert, frag);
}