我花了几个小时来隔离一些我不理解的WebGL行为。
让我们假设一个具有整数属性的顶点着色器:
letters: Array<string> = [a, b, c, d, e];
currentLetter: string= c;
使用以下着色器属性绑定:
<div *ngFor="let letter of letters" (click)="currentLetter = letter">{{letter}}</div>
最后是以下AttribPointer配置:
precision highp int;
in vec4 vtx_pos;
in vec3 vtx_nrm;
in ivec4 vtx_int; // <- integer attribute
void main() {
// blah blah...
}
使用此配置(禁用VertexAttribArray(2)),浏览器会为属性2抛出类型错误:
Chrome:var p = gl.createProgram();
gl.bindAttribLocation(p, 0, "vtx_pos");
gl.bindAttribLocation(p, 1, "vtx_nrm");
gl.bindAttribLocation(p, 2, "vtx_int"); // <- ivec4 on array #2
Firefox:gl.vertexAttribPointer(0, 4, gl.FLOAT, false, 28, 0);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 28, 16);
gl.enableVertexAttribArray(1);
gl.disableVertexAttribArray(2); // <- here is the devil
我所理解的是,当VertexAttribArray没有使用正确的vertexAttrib I 指针显式启用时,WebGL认为它是以FLOAT&#34;提供的。默认情况下,抛出类型错误。
我不明白的是:为什么要检查提供的禁用 VertexAttribArray的类型,其中,logicaly, nothing 是否已提供?
除了将VertexAttribArray设置为虚拟外,是否有一些魔法可以避免此错误?
答案 0 :(得分:2)
引用GL ES3.0§2.8:
如果槽索引处的着色器属性的基本类型不是浮点(例如,有符号或无符号整数),则结果属性值是未定义的。
WebGL 2.0 eliminates undefined behaviour here and mandates that an error should be produced。
因此,默认情况下,非数组属性的值确实是浮点向量。如果着色器中的实际属性是无符号整数值的整数,则应通过vertexAttribI4*
family的调用手动指定其值,即:
gl.vertexAttribI4i(2, 0, 0, 0, 0);