我的顶点由具有以下结构的数组组成:
[ Position ][ colour ]
[float][float][float][byte][byte][byte][byte]
传递顶点位置没有问题:
gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo);
gl.vertexAttribPointer(this.material.aVertexPosition, 3, gl.FLOAT, false, 4, 0);
但我不知道如何将颜色传递给着色器。不幸的是,不可能在 glsl 着色器中使用整数,所以我必须使用浮点数。
如何将无符号字节颜色值转换为 glsl 浮点颜色值?我分别对 r、g 和 b 进行了这样的尝试,但颜色都搞乱了:
gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo);
gl.vertexAttribPointer(this.material.aR, 1, gl.BYTE, false, 15, 12);
顶点着色器 (colouredPoint.vs)
precision highp float;
attribute vec3 aVertexPosition;
attribute float aR;
attribute float aG;
attribute float aB;
uniform mat4 world;
uniform mat4 view;
uniform mat4 proj;
varying vec3 vVertexColour;
void main(void){
gl_PointSize = 4.0;
gl_Position = proj * view * world * vec4(aVertexPosition, 1.0);
vVertexColour = vec3(aR, aG, aB);
}
像素着色器 (colouredPoint.fs)
precision highp float;
varying vec3 vVertexColour;
void main(void){
gl_FragColor = vec4(vVertexColour, 1);
}